Skip to content

Commit

Permalink
deduplicate a bit
Browse files Browse the repository at this point in the history
  • Loading branch information
ix0rai committed May 24, 2024
1 parent 070d71e commit 3afe57f
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 42 deletions.
35 changes: 22 additions & 13 deletions src/main/java/io/ix0rai/rainglow/data/RainglowColour.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

public enum RainglowColour {
BLACK("black", new RGB(0.0F, 0.0F, 0.0F), new RGB(0.0F, 0.0F, 0.0F), new RGB(0, 0, 0), Items.BLACK_DYE),
Expand All @@ -35,35 +36,43 @@ public enum RainglowColour {
}

private final String id;
private Identifier texture;
private final Map<RainglowEntity, Identifier> textures;
private final RGB passiveParticleRgb;
private final RGB altPassiveParticleRgb;
private final RGB inkRgb;
private final Item item;

RainglowColour(String id, RGB passiveParticleRgb, RGB altPassiveParticleRgb, RGB inkRgb, Item item) {
this.id = id;
this.texture = new Identifier("textures/entity/squid/" + this.getId() + ".png");
this.textures = new HashMap<>();
this.passiveParticleRgb = passiveParticleRgb;
this.altPassiveParticleRgb = altPassiveParticleRgb;
this.inkRgb = inkRgb;
this.item = item;
}

public Identifier getTexture(RainglowEntity entityType) {
// use minecraft's textures when possible, so we can ship fewer textures
if (entityType == RainglowEntity.GLOW_SQUID) {
String textureName = this.getId().equals("blue") ? "glow_squid" : this.getId();
this.texture = new Identifier("textures/entity/squid/" + textureName + ".png");
} else if (entityType == RainglowEntity.ALLAY) {
String textureName = this.getId().equals("blue") ? "allay" : this.getId();
this.texture = new Identifier("textures/entity/allay/" + textureName + ".png");
} else {
String textureName = this.getId().equals("lime") ? "slime" : this.getId();
this.texture = new Identifier("textures/entity/slime/" + textureName + ".png");
if (this.textures.isEmpty()) {
for (RainglowEntity entity : RainglowEntity.values()) {
// use minecraft's textures when possible, so we can ship fewer textures
switch (entity) {
case GLOW_SQUID -> {
String textureName = RainglowEntity.GLOW_SQUID.getDefaultColour() == this ? "glow_squid" : this.getId();
this.textures.put(entity, new Identifier("textures/entity/squid/" + textureName + ".png"));
}
case ALLAY -> {
String textureName = RainglowEntity.ALLAY.getDefaultColour() == this ? "allay" : this.getId();
this.textures.put(entity, new Identifier("textures/entity/allay/" + textureName + ".png"));
}
case SLIME -> {
String textureName = RainglowEntity.SLIME.getDefaultColour() == this ? "slime" : this.getId();
this.textures.put(entity, new Identifier("textures/entity/slime/" + textureName + ".png"));
}
}
}
}

return this.texture;
return this.textures.get(entityType);
}

public String getId() {
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/io/ix0rai/rainglow/data/RainglowEntity.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.ix0rai.rainglow.data;

import io.ix0rai.rainglow.Rainglow;
import net.minecraft.entity.Entity;
import net.minecraft.entity.data.DataTracker;
import net.minecraft.entity.data.TrackedData;
import net.minecraft.entity.data.TrackedDataHandlerRegistry;
Expand All @@ -13,6 +14,7 @@
import net.minecraft.util.Identifier;
import net.minecraft.util.random.RandomGenerator;
import org.jetbrains.annotations.Nullable;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

import java.util.Arrays;
import java.util.HashMap;
Expand Down Expand Up @@ -83,4 +85,15 @@ public static void write(PacketByteBuf buf, RainglowEntity entity) {
public static RainglowEntity get(String id) {
return BY_ID.get(id);
}

public void overrideTexture(Entity entity, CallbackInfoReturnable<Identifier> cir) {
RainglowColour colour = Rainglow.getColour(this, entity.getDataTracker(), entity.getWorld().getRandom());

// if the colour is default we don't need to override the method
// this optimises a tiny bit
if (Rainglow.CONFIG.isEntityEnabled(this) && colour != this.getDefaultColour()) {
Identifier texture = Rainglow.getTexture(this, colour.getId());
cir.setReturnValue(texture != null ? texture : this.getDefaultTexture());
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package io.ix0rai.rainglow.mixin.client;

import io.ix0rai.rainglow.Rainglow;
import io.ix0rai.rainglow.data.RainglowColour;
import io.ix0rai.rainglow.data.RainglowEntity;
import net.minecraft.client.render.entity.AllayEntityRenderer;
import net.minecraft.entity.passive.AllayEntity;
Expand All @@ -15,13 +13,6 @@
public class AllayEntityRendererMixin {
@Inject(method = "getTexture*", at = @At("HEAD"), cancellable = true)
public void getTexture(AllayEntity allayEntity, CallbackInfoReturnable<Identifier> cir) {
RainglowColour colour = Rainglow.getColour(RainglowEntity.ALLAY, allayEntity.getDataTracker(), allayEntity.getRandom());

// if the colour is blue we don't need to override the method
// this optimises a tiny bit
if (Rainglow.CONFIG.isEntityEnabled(RainglowEntity.ALLAY) && colour != RainglowEntity.ALLAY.getDefaultColour()) {
Identifier texture = Rainglow.getTexture(RainglowEntity.ALLAY, colour.getId());
cir.setReturnValue(texture != null ? texture : RainglowEntity.ALLAY.getDefaultTexture());
}
RainglowEntity.ALLAY.overrideTexture(allayEntity, cir);
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package io.ix0rai.rainglow.mixin.client;

import io.ix0rai.rainglow.Rainglow;
import io.ix0rai.rainglow.data.RainglowColour;
import io.ix0rai.rainglow.data.RainglowEntity;
import net.minecraft.client.render.entity.GlowSquidEntityRenderer;
import net.minecraft.entity.passive.GlowSquidEntity;
Expand All @@ -19,13 +17,6 @@ public class GlowSquidEntityRendererMixin {
*/
@Inject(method = "getTexture*", at = @At("HEAD"), cancellable = true)
public void getTexture(GlowSquidEntity glowSquidEntity, CallbackInfoReturnable<Identifier> cir) {
RainglowColour colour = Rainglow.getColour(RainglowEntity.GLOW_SQUID, glowSquidEntity.getDataTracker(), glowSquidEntity.getRandom());

// if the colour is blue we don't need to override the method
// this optimises a tiny bit
if (Rainglow.CONFIG.isEntityEnabled(RainglowEntity.GLOW_SQUID) && colour != RainglowEntity.GLOW_SQUID.getDefaultColour()) {
Identifier texture = Rainglow.getTexture(RainglowEntity.GLOW_SQUID, colour.getId());
cir.setReturnValue(texture != null ? texture : RainglowEntity.GLOW_SQUID.getDefaultTexture());
}
RainglowEntity.GLOW_SQUID.overrideTexture(glowSquidEntity, cir);
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package io.ix0rai.rainglow.mixin.client;

import io.ix0rai.rainglow.Rainglow;
import io.ix0rai.rainglow.data.RainglowColour;
import io.ix0rai.rainglow.data.RainglowEntity;
import net.minecraft.client.render.entity.SlimeEntityRenderer;
import net.minecraft.entity.mob.SlimeEntity;
Expand All @@ -15,12 +13,6 @@
public class SlimeEntityRendererMixin {
@Inject(method = "getTexture*", at = @At("HEAD"), cancellable = true)
public void getTexture(SlimeEntity entity, CallbackInfoReturnable<Identifier> cir) {
RainglowColour colour = Rainglow.getColour(RainglowEntity.SLIME, entity.getDataTracker(), entity.getRandom());

// don't override if the colour is lime, use the default texture
if (Rainglow.CONFIG.isEntityEnabled(RainglowEntity.SLIME) && colour != RainglowEntity.SLIME.getDefaultColour()) {
Identifier texture = Rainglow.getTexture(RainglowEntity.SLIME, colour.getId());
cir.setReturnValue(texture != null ? texture : RainglowEntity.SLIME.getDefaultTexture());
}
RainglowEntity.SLIME.overrideTexture(entity, cir);
}
}

0 comments on commit 3afe57f

Please sign in to comment.