diff --git a/src/main/java/io/ix0rai/rainglow/mixin/MobEntityMixin.java b/src/main/java/io/ix0rai/rainglow/mixin/MobEntityMixin.java index 5698821..678c5f4 100644 --- a/src/main/java/io/ix0rai/rainglow/mixin/MobEntityMixin.java +++ b/src/main/java/io/ix0rai/rainglow/mixin/MobEntityMixin.java @@ -17,10 +17,12 @@ import net.minecraft.entity.passive.AllayEntity; import net.minecraft.entity.passive.GlowSquidEntity; import net.minecraft.nbt.NbtCompound; +import net.minecraft.util.random.RandomGenerator; import net.minecraft.world.LocalDifficulty; import net.minecraft.world.ServerWorldAccess; import net.minecraft.world.World; import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Unique; import org.spongepowered.asm.mixin.injection.At; import org.spongepowered.asm.mixin.injection.Inject; import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; @@ -36,16 +38,21 @@ protected MobEntityMixin(EntityType entityType, World world public void initialize(ServerWorldAccess world, LocalDifficulty difficulty, SpawnReason spawnReason, EntityData entityData, NbtCompound entityNbt, CallbackInfoReturnable cir) { if ((Object) this instanceof GlowSquidEntity glowSquid) { String colour = Rainglow.generateRandomColourId(this.getRandom()); - ((GlowSquidVariantProvider) glowSquid).setVariant(this.random.nextInt(100) >= Rainglow.CONFIG.getRarity() ? RainglowColour.BLUE : RainglowColour.get(colour)); - cir.setReturnValue(new GlowSquidEntityData(this.random.nextInt(100) >= Rainglow.CONFIG.getRarity() ? RainglowColour.BLUE : RainglowColour.get(colour))); + ((GlowSquidVariantProvider) glowSquid).setVariant(getColourOrDefault(this.random, RainglowColour.BLUE, colour)); + cir.setReturnValue(new GlowSquidEntityData(getColourOrDefault(this.random, RainglowColour.BLUE, colour))); } else if ((Object) this instanceof AllayEntity allay) { String colour = Rainglow.generateRandomColourId(this.getRandom()); - ((AllayVariantProvider) allay).setVariant(this.random.nextInt(100) >= Rainglow.CONFIG.getRarity() ? RainglowColour.BLUE : RainglowColour.get(colour)); - cir.setReturnValue(new AllayEntityData(this.random.nextInt(100) >= Rainglow.CONFIG.getRarity() ? RainglowColour.BLUE : RainglowColour.get(colour))); + ((AllayVariantProvider) allay).setVariant(getColourOrDefault(this.random, RainglowColour.BLUE, colour)); + cir.setReturnValue(new AllayEntityData(getColourOrDefault(this.random, RainglowColour.BLUE, colour))); } else if ((Object) this instanceof SlimeEntity slime) { String colour = Rainglow.generateRandomColourId(this.getRandom()); - ((SlimeVariantProvider) slime).setVariant(this.random.nextInt(100) >= Rainglow.CONFIG.getRarity() ? RainglowColour.LIME : RainglowColour.get(colour)); - cir.setReturnValue(new SlimeEntityData(this.random.nextInt(100) >= Rainglow.CONFIG.getRarity() ? RainglowColour.LIME : RainglowColour.get(colour))); + ((SlimeVariantProvider) slime).setVariant(getColourOrDefault(this.random, RainglowColour.LIME, colour)); + cir.setReturnValue(new SlimeEntityData(getColourOrDefault(this.random, RainglowColour.LIME, colour))); } } + + @Unique + private static RainglowColour getColourOrDefault(RandomGenerator random, RainglowColour defaultColour, String randomColour) { + return random.nextInt(100) >= Rainglow.CONFIG.getRarity() ? defaultColour : RainglowColour.get(randomColour); + } }