diff --git a/src/main/java/io/ix0rai/rainglow/data/RainglowEntity.java b/src/main/java/io/ix0rai/rainglow/data/RainglowEntity.java index f0d041b..b8eba45 100644 --- a/src/main/java/io/ix0rai/rainglow/data/RainglowEntity.java +++ b/src/main/java/io/ix0rai/rainglow/data/RainglowEntity.java @@ -2,6 +2,7 @@ import io.ix0rai.rainglow.Rainglow; import net.minecraft.entity.Entity; +import net.minecraft.entity.EntityData; import net.minecraft.entity.data.DataTracker; import net.minecraft.entity.data.TrackedData; import net.minecraft.entity.data.TrackedDataHandlerRegistry; @@ -14,15 +15,17 @@ import net.minecraft.util.Identifier; import net.minecraft.util.random.RandomGenerator; import org.jetbrains.annotations.Nullable; +import org.spongepowered.asm.mixin.Unique; import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; import java.util.Arrays; import java.util.HashMap; +import java.util.function.Function; public enum RainglowEntity { - GLOW_SQUID("glow_squid", RainglowColour.BLUE, DataTracker.registerData(GlowSquidEntity.class, TrackedDataHandlerRegistry.STRING)), - ALLAY("allay", RainglowColour.BLUE, DataTracker.registerData(AllayEntity.class, TrackedDataHandlerRegistry.STRING)), - SLIME("slime", RainglowColour.LIME, DataTracker.registerData(SlimeEntity.class, TrackedDataHandlerRegistry.STRING)); + GLOW_SQUID("glow_squid", RainglowColour.BLUE, DataTracker.registerData(GlowSquidEntity.class, TrackedDataHandlerRegistry.STRING), GlowSquidEntityData::new), + ALLAY("allay", RainglowColour.BLUE, DataTracker.registerData(AllayEntity.class, TrackedDataHandlerRegistry.STRING), AllayEntityData::new), + SLIME("slime", RainglowColour.LIME, DataTracker.registerData(SlimeEntity.class, TrackedDataHandlerRegistry.STRING), SlimeEntityData::new); private static final HashMap BY_ID = new HashMap<>(); static { @@ -32,11 +35,13 @@ public enum RainglowEntity { private final String id; private final RainglowColour defaultColour; private final TrackedData trackedData; + private final Function entityDataFactory; - RainglowEntity(String id, RainglowColour defaultColour, TrackedData trackedData) { + RainglowEntity(String id, RainglowColour defaultColour, TrackedData trackedData, Function entityDataFactory) { this.id = id; this.defaultColour = defaultColour; this.trackedData = trackedData; + this.entityDataFactory = entityDataFactory; } public String getId() { @@ -55,6 +60,10 @@ public Identifier getDefaultTexture() { return this.defaultColour.getTexture(this); } + public EntityData createEntityData(RainglowColour colour) { + return this.entityDataFactory.apply(colour); + } + public Item getItem(int index) { if (index == -1) { return this.getDefaultColour().getItem(); @@ -86,6 +95,20 @@ public static RainglowEntity get(String id) { return BY_ID.get(id); } + @Unique + @SuppressWarnings("all") + public static RainglowEntity get(Entity entity) { + if (entity instanceof GlowSquidEntity) { + return GLOW_SQUID; + } else if (entity instanceof AllayEntity) { + return ALLAY; + } else if (entity instanceof SlimeEntity) { + return SLIME; + } + + return null; + } + public void overrideTexture(Entity entity, CallbackInfoReturnable cir) { RainglowColour colour = Rainglow.getColour(this, entity.getDataTracker(), entity.getWorld().getRandom()); diff --git a/src/main/java/io/ix0rai/rainglow/mixin/AllayEntityMixin.java b/src/main/java/io/ix0rai/rainglow/mixin/AllayEntityMixin.java index 1a6a57d..0890197 100644 --- a/src/main/java/io/ix0rai/rainglow/mixin/AllayEntityMixin.java +++ b/src/main/java/io/ix0rai/rainglow/mixin/AllayEntityMixin.java @@ -48,18 +48,18 @@ public void readCustomDataFromNbt(NbtCompound nbt, CallbackInfo ci) { @Redirect(method = "duplicate", at = @At(value = "INVOKE", target = "Lnet/minecraft/world/World;spawnEntity(Lnet/minecraft/entity/Entity;)Z")) public boolean spawnWithColour(World instance, Entity entity) { RainglowColour colour = Rainglow.getColour(THIS, this.getDataTracker(), this.getRandom()); - entity.getDataTracker().set(RainglowEntity.ALLAY.getTrackedData(), colour.getId()); + entity.getDataTracker().set(THIS.getTrackedData(), colour.getId()); return this.getWorld().spawnEntity(entity); } @Override public RainglowColour getVariant() { - return Rainglow.getColour(RainglowEntity.ALLAY, this.getDataTracker(), this.getRandom()); + return Rainglow.getColour(THIS, this.getDataTracker(), this.getRandom()); } @Override public void setVariant(RainglowColour colour) { - this.getDataTracker().set(RainglowEntity.ALLAY.getTrackedData(), colour.getId()); + this.getDataTracker().set(THIS.getTrackedData(), colour.getId()); } @Unique diff --git a/src/main/java/io/ix0rai/rainglow/mixin/DyeItemMixin.java b/src/main/java/io/ix0rai/rainglow/mixin/DyeItemMixin.java index f462935..36be191 100644 --- a/src/main/java/io/ix0rai/rainglow/mixin/DyeItemMixin.java +++ b/src/main/java/io/ix0rai/rainglow/mixin/DyeItemMixin.java @@ -4,9 +4,6 @@ import io.ix0rai.rainglow.data.RainglowEntity; import net.minecraft.entity.LivingEntity; import net.minecraft.entity.data.DataTracker; -import net.minecraft.entity.mob.SlimeEntity; -import net.minecraft.entity.passive.AllayEntity; -import net.minecraft.entity.passive.GlowSquidEntity; import net.minecraft.entity.player.PlayerEntity; import net.minecraft.item.DyeItem; import net.minecraft.item.ItemStack; @@ -27,40 +24,20 @@ public class DyeItemMixin { @Inject(method = "useOnEntity", at = @At("TAIL"), cancellable = true) private void useOnEntity(ItemStack stack, PlayerEntity user, LivingEntity entity, Hand hand, CallbackInfoReturnable cir) { String colour = getDye(stack); + RainglowEntity entityType = RainglowEntity.get(entity); - if (entity instanceof GlowSquidEntity glowSquid && glowSquid.isAlive() && !Rainglow.getColour(RainglowEntity.GLOW_SQUID, glowSquid.getDataTracker(), glowSquid.getRandom()).equals(colour)) { - glowSquid.getWorld().playSoundFromEntity(user, glowSquid, SoundEvents.BLOCK_AMETHYST_BLOCK_CHIME, SoundCategory.PLAYERS, 1.0f, 1.0f); + if (entityType != null && !Rainglow.colourUnloaded(entityType, colour) + && !Rainglow.getColour(entityType, entity.getDataTracker(), entity.getWorld().getRandom()).getId().equals(colour)) { + entity.getWorld().playSoundFromEntity(user, entity, SoundEvents.BLOCK_AMETHYST_CLUSTER_BREAK, SoundCategory.PLAYERS, 5.0f, 1.0f); if (!user.getWorld().isClient()) { stack.decrement(1); } - DataTracker tracker = glowSquid.getDataTracker(); - tracker.set(RainglowEntity.GLOW_SQUID.getTrackedData(), colour); - - cir.setReturnValue(ActionResult.success(user.getWorld().isClient())); - } else if (entity instanceof AllayEntity allayEntity && allayEntity.isAlive() && !Rainglow.getColour(RainglowEntity.ALLAY, allayEntity.getDataTracker(), allayEntity.getRandom()).equals(colour)) { - allayEntity.getWorld().playSoundFromEntity(user, allayEntity, SoundEvents.BLOCK_AMETHYST_BLOCK_CHIME, SoundCategory.PLAYERS, 1.0f, 1.0f); - if (!user.getWorld().isClient()) { - stack.decrement(1); - } - - DataTracker tracker = allayEntity.getDataTracker(); - tracker.set(RainglowEntity.ALLAY.getTrackedData(), colour); - - cir.setReturnValue(ActionResult.success(user.getWorld().isClient())); - } else if (entity instanceof SlimeEntity slimeEntity && slimeEntity.isAlive() && !Rainglow.getColour(RainglowEntity.SLIME, slimeEntity.getDataTracker(), slimeEntity.getRandom()).equals(colour)) { - slimeEntity.getWorld().playSoundFromEntity(user, slimeEntity, SoundEvents.BLOCK_AMETHYST_BLOCK_CHIME, SoundCategory.PLAYERS, 1.0f, 1.0f); - if (!user.getWorld().isClient()) { - stack.decrement(1); - } - - DataTracker tracker = slimeEntity.getDataTracker(); - tracker.set(RainglowEntity.SLIME.getTrackedData(), colour); + DataTracker tracker = entity.getDataTracker(); + tracker.set(entityType.getTrackedData(), colour); cir.setReturnValue(ActionResult.success(user.getWorld().isClient())); } - - cir.setReturnValue(ActionResult.PASS); } @Unique diff --git a/src/main/java/io/ix0rai/rainglow/mixin/MobEntityMixin.java b/src/main/java/io/ix0rai/rainglow/mixin/MobEntityMixin.java index d41e488..495bf2d 100644 --- a/src/main/java/io/ix0rai/rainglow/mixin/MobEntityMixin.java +++ b/src/main/java/io/ix0rai/rainglow/mixin/MobEntityMixin.java @@ -1,22 +1,14 @@ package io.ix0rai.rainglow.mixin; import io.ix0rai.rainglow.Rainglow; -import io.ix0rai.rainglow.data.AllayEntityData; -import io.ix0rai.rainglow.data.AllayVariantProvider; import io.ix0rai.rainglow.data.RainglowColour; -import io.ix0rai.rainglow.data.GlowSquidEntityData; -import io.ix0rai.rainglow.data.GlowSquidVariantProvider; import io.ix0rai.rainglow.data.RainglowEntity; -import io.ix0rai.rainglow.data.SlimeEntityData; -import io.ix0rai.rainglow.data.SlimeVariantProvider; import net.minecraft.entity.EntityData; import net.minecraft.entity.EntityType; import net.minecraft.entity.LivingEntity; import net.minecraft.entity.SpawnReason; +import net.minecraft.entity.VariantProvider; import net.minecraft.entity.mob.MobEntity; -import net.minecraft.entity.mob.SlimeEntity; -import net.minecraft.entity.passive.AllayEntity; -import net.minecraft.entity.passive.GlowSquidEntity; import net.minecraft.world.LocalDifficulty; import net.minecraft.world.ServerWorldAccess; import net.minecraft.world.World; @@ -36,41 +28,19 @@ protected MobEntityMixin(EntityType entityType, World world @SuppressWarnings("all") @Inject(method = "initialize", at = @At("RETURN"), cancellable = true) public void initialize(ServerWorldAccess world, LocalDifficulty difficulty, SpawnReason spawnReason, @Nullable EntityData entityData, CallbackInfoReturnable cir) { - if ((Object) this instanceof GlowSquidEntity glowSquid) { - RainglowColour colour = generateColour(); - ((GlowSquidVariantProvider) glowSquid).setVariant(colour); - cir.setReturnValue(new GlowSquidEntityData(colour)); - } else if ((Object) this instanceof AllayEntity allay) { - RainglowColour colour = generateColour(); - ((AllayVariantProvider) allay).setVariant(colour); - cir.setReturnValue(new AllayEntityData(colour)); - } else if ((Object) this instanceof SlimeEntity slime) { - RainglowColour colour = generateColour(); - ((SlimeVariantProvider) slime).setVariant(colour); - cir.setReturnValue(new SlimeEntityData(colour)); + RainglowEntity entity = RainglowEntity.get(this); + if (entity != null) { + RainglowColour colour = generateColour(entity); + ((VariantProvider) this).setVariant(colour); + cir.setReturnValue(entity.createEntityData(colour)); } } @Unique - private RainglowColour generateColour() { - RainglowEntity entity = getCurrentEntity(); + private RainglowColour generateColour(RainglowEntity entity) { int i = random.nextInt(100); int rarity = Rainglow.CONFIG.getRarity(entity); return i >= rarity ? entity.getDefaultColour() : RainglowColour.get(Rainglow.generateRandomColourId(this.random)); } - - @Unique - @SuppressWarnings("all") - private RainglowEntity getCurrentEntity() { - if ((Object) this instanceof GlowSquidEntity) { - return RainglowEntity.GLOW_SQUID; - } else if ((Object) this instanceof AllayEntity) { - return RainglowEntity.ALLAY; - } else if ((Object) this instanceof SlimeEntity) { - return RainglowEntity.SLIME; - } else { - throw new RuntimeException("unsupported entity"); - } - } }