Skip to content

Instantly share code, notes, and snippets.

@Chailotl
Created March 25, 2021 22:13
Show Gist options
  • Select an option

  • Save Chailotl/fb0d7797a04247ad2e13ecfed0a8b07f to your computer and use it in GitHub Desktop.

Select an option

Save Chailotl/fb0d7797a04247ad2e13ecfed0a8b07f to your computer and use it in GitHub Desktop.
Camo Creepers
public interface ICreeper
{
void setColor(float red, float green, float blue);
int getColor();
boolean getCave();
}
@Mixin(CreeperEntityModel.class)
public abstract class InjectCreeperEntityModel<T extends Entity> extends CompositeEntityModel<T> implements ICreeper
{
private float red = 1f;
private float blue = 1f;
private float green = 1f;
public void setColor(float red, float green, float blue)
{
this.red = red;
this.green = green;
this.blue = blue;
}
public void render(MatrixStack matrices, VertexConsumer vertices, int light,
int overlay, float red, float green, float blue, float alpha)
{
super.render(matrices, vertices, light, overlay, this.red, this.green, this.blue, alpha);
}
}
@Mixin(CreeperEntityRenderer.class)
public abstract class InjectCreeperEntityRenderer extends MobEntityRenderer<CreeperEntity, CreeperEntityModel<CreeperEntity>>
{
private static final Identifier NEW_TEXTURE = new Identifier(Main.MOD_ID, "textures/entity/creeper/creeper.png");
public InjectCreeperEntityRenderer(EntityRenderDispatcher entityRenderDispatcher,
CreeperEntityModel<CreeperEntity> entityModel, float f)
{
super(entityRenderDispatcher, entityModel, f);
}
public void render(CreeperEntity creeperEntity, float f, float g,
MatrixStack matrices, VertexConsumerProvider provider, int i)
{
int color = ((ICreeper) creeperEntity).getColor();
((ICreeper) model).setColor((color >> 16 & 255) / 255f, (color >> 8 & 255) / 255f, (color & 255) / 255f);
super.render(creeperEntity, f, g, matrices, provider, i);
}
@Inject(
method = "getTexture",
at = @At("RETURN"),
cancellable = true)
private void changeTexture(CreeperEntity creeperEntity,
CallbackInfoReturnable<Identifier> info)
{
if (!((ICreeper) creeperEntity).getCave())
{
info.setReturnValue(NEW_TEXTURE);
}
}
}
@Mixin(CreeperEntity.class)
public abstract class MixinCreeperEntity extends HostileEntity implements ICreeper
{
private boolean gotColor = false;
private static final TrackedData<Integer> COLOR;
private static final TrackedData<Boolean> CAVE;
protected MixinCreeperEntity(EntityType<? extends HostileEntity> entityType, World world)
{
super(entityType, world);
}
public int getColor()
{
return dataTracker.get(COLOR);
}
public boolean getCave()
{
return dataTracker.get(CAVE);
}
@Inject(
method = "initDataTracker",
at = @At("TAIL"))
private void trackColor(CallbackInfo info)
{
dataTracker.startTracking(COLOR, 0xffffff);
dataTracker.startTracking(CAVE, false);
}
@Override
public void setPos(double x, double y, double z)
{
super.setPos(x, y, z);
if (!world.isClient && !gotColor && isChunkPosUpdateRequested())
{
gotColor = true;
if (getBlockPos().getY() < 60)
{
dataTracker.set(CAVE, true);
}
else
{
dataTracker.set(COLOR, BiomeColors.getGrassColor(world, getBlockPos()));
}
}
}
@Inject(
method = "writeCustomDataToTag",
at = @At("TAIL"))
private void writeColor(CompoundTag tag, CallbackInfo info)
{
tag.putInt("Color", dataTracker.get(COLOR));
tag.putBoolean("Cave", dataTracker.get(CAVE));
}
@Inject(
method = "readCustomDataFromTag",
at = @At("TAIL"))
private void readColor(CompoundTag tag, CallbackInfo info)
{
if (tag.contains("Color", 99))
{
gotColor = true;
dataTracker.set(COLOR, tag.getInt("Color"));
dataTracker.set(CAVE, tag.getBoolean("Cave"));
}
}
static {
COLOR = DataTracker.registerData(CreeperEntity.class, TrackedDataHandlerRegistry.INTEGER);
CAVE = DataTracker.registerData(CreeperEntity.class, TrackedDataHandlerRegistry.BOOLEAN);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment