diff --git a/src/main/java/io/ix0rai/rainglow/Rainglow.java b/src/main/java/io/ix0rai/rainglow/Rainglow.java index d8474fa..ffcdf98 100644 --- a/src/main/java/io/ix0rai/rainglow/Rainglow.java +++ b/src/main/java/io/ix0rai/rainglow/Rainglow.java @@ -10,9 +10,10 @@ import net.fabricmc.api.ModInitializer; import net.fabricmc.fabric.api.networking.v1.PayloadTypeRegistry; import net.fabricmc.fabric.api.networking.v1.ServerPlayConnectionEvents; +import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking; import net.fabricmc.fabric.api.resource.ResourceManagerHelper; import net.fabricmc.loader.api.FabricLoader; -import net.minecraft.entity.data.DataTracker; +import net.minecraft.entity.Entity; import net.minecraft.resource.ResourceType; import net.minecraft.text.Text; import net.minecraft.util.Identifier; @@ -22,7 +23,10 @@ import org.slf4j.LoggerFactory; import java.util.ArrayList; +import java.util.HashMap; import java.util.List; +import java.util.Map; +import java.util.UUID; public class Rainglow implements ModInitializer { public static final String MOD_ID = "rainglow"; @@ -37,12 +41,16 @@ public class Rainglow implements ModInitializer { public static final Identifier SERVER_MODE_DATA_ID = id("server_mode_data"); public static final List RAINGLOW_DATAPACKS = new ArrayList<>(); + private static final Map colours = new HashMap<>(); + @Override public void onInitialize() { ResourceManagerHelper.get(ResourceType.SERVER_DATA).registerReloadListener((RainglowResourceReloader) () -> SERVER_MODE_DATA_ID); PayloadTypeRegistry.playS2C().register(RainglowNetworking.ConfigSyncPayload.PACKET_ID, RainglowNetworking.ConfigSyncPayload.PACKET_CODEC); PayloadTypeRegistry.playS2C().register(RainglowNetworking.ModeSyncPayload.PACKET_ID, RainglowNetworking.ModeSyncPayload.PACKET_CODEC); + PayloadTypeRegistry.playS2C().register(RainglowNetworking.ColourPayload.PACKET_ID, RainglowNetworking.ColourPayload.PACKET_CODEC); + PayloadTypeRegistry.playC2S().register(RainglowNetworking.ColourPayload.PACKET_ID, RainglowNetworking.ColourPayload.PACKET_CODEC); ServerPlayConnectionEvents.JOIN.register((handler, sender, server) -> { // send modes to client @@ -50,6 +58,16 @@ public void onInitialize() { // send config to client RainglowNetworking.syncConfig(handler.player); + + // send all colours to client + RainglowNetworking.sendColoursTo(handler.player); + }); + + ServerPlayNetworking.registerGlobalReceiver(RainglowNetworking.ColourPayload.PACKET_ID, (payload, context) -> { + for (var entry : payload.colours().entrySet()) { + RainglowColour colour = entry.getValue(); + Rainglow.setColour(entry.getKey(), colour); + } }); } @@ -57,14 +75,14 @@ public static Identifier id(String id) { return Identifier.of(MOD_ID, id); } - public static String generateRandomColourId(World world, RandomGenerator random) { + public static RainglowColour generateRandomColour(World world, RandomGenerator random) { var colours = MODE_CONFIG.getMode(world).getColours(); - return colours.get(random.nextInt(colours.size())).getId(); + return colours.get(random.nextInt(colours.size())); } - public static boolean colourUnloaded(World world, RainglowEntity entityType, String colour) { + public static boolean colourUnloaded(World world, RainglowEntity entityType, RainglowColour colour) { var colours = MODE_CONFIG.getMode(world).getColours(); - return !colours.contains(RainglowColour.get(colour)) && !colour.equals(entityType.getDefaultColour().getId()); + return !colours.contains(colour) && colour != entityType.getDefaultColour(); } public static String translatableTextKey(String key) { @@ -80,15 +98,37 @@ public static Text translatableText(String key) { return Text.translatable(translatableTextKey(key)); } - public static RainglowColour getColour(World world, RainglowEntity entityType, DataTracker tracker, RandomGenerator random) { + public static RainglowColour getColour(Entity entity) { + RainglowColour colour = colours.get(entity.getUuid()); + RainglowEntity entityType = RainglowEntity.get(entity); + // generate random colour if the squid's colour isn't currently loaded - String colour = tracker.get(entityType.getTrackedData()); - if (colourUnloaded(world, entityType, colour)) { + if (colourUnloaded(entity.getWorld(), entityType, colour)) { // Use last generated colour if not null else generate a new colour - tracker.set(entityType.getTrackedData(), generateRandomColourId(world, random)); - colour = tracker.get(entityType.getTrackedData()); + colour = generateRandomColour(entity.getWorld(), entity.getRandom()); + colours.put(entity.getUuid(), colour); } - return RainglowColour.get(colour); + return colour; + } + + public static void setColour(Entity entity, RainglowColour colour) { + setColour(entity.getUuid(), colour); + + if (entity.getWorld().isClient()) { + // sync to server; will then be synced to all clients + RainglowNetworking.sendColourChangeToServer(entity, colour); + } else if (entity.getWorld().getServer().isDedicated()) { + // sync to all clients + RainglowNetworking.sendColourChangeToClients(entity, colour); + } + } + + public static void setColour(UUID uuid, RainglowColour colour) { + colours.put(uuid, colour); + } + + public static Map getColours() { + return colours; } } diff --git a/src/main/java/io/ix0rai/rainglow/client/RainglowClient.java b/src/main/java/io/ix0rai/rainglow/client/RainglowClient.java index 3d320ea..c540a2f 100644 --- a/src/main/java/io/ix0rai/rainglow/client/RainglowClient.java +++ b/src/main/java/io/ix0rai/rainglow/client/RainglowClient.java @@ -1,9 +1,11 @@ package io.ix0rai.rainglow.client; +import com.mojang.datafixers.util.Either; import folk.sisby.kaleido.lib.quiltconfig.api.values.TrackedValue; import folk.sisby.kaleido.lib.quiltconfig.api.values.ValueList; import folk.sisby.kaleido.lib.quiltconfig.api.values.ValueMap; import io.ix0rai.rainglow.Rainglow; +import io.ix0rai.rainglow.config.PerWorldConfig; import io.ix0rai.rainglow.data.RainglowColour; import io.ix0rai.rainglow.data.RainglowMode; import io.ix0rai.rainglow.data.RainglowResourceReloader; @@ -18,6 +20,7 @@ import net.minecraft.resource.ResourceType; import net.minecraft.util.Identifier; +import java.nio.file.Path; import java.util.ArrayList; import java.util.List; @@ -74,6 +77,16 @@ public void onInitializeClient() { }); }); + ClientPlayNetworking.registerGlobalReceiver(RainglowNetworking.ColourPayload.PACKET_ID, (payload, context) -> { + MinecraftClient client = context.client(); + client.execute(() -> { + for (var entry : payload.colours().entrySet()) { + RainglowColour colour = entry.getValue(); + Rainglow.setColour(entry.getKey(), colour); + } + }); + }); + ClientPlayConnectionEvents.DISCONNECT.register((handler, client) -> client.execute(() -> { // reset values to those configured in file @@ -95,4 +108,12 @@ public void log() { } }); } + + public static Either getSaveNameClient() { + if (MinecraftClient.getInstance().isInSingleplayer()) { + return Either.left(PerWorldConfig.getWorldPath(MinecraftClient.getInstance().getServer())); + } else { + return Either.right(MinecraftClient.getInstance().getCurrentServerEntry().address); + } + } } diff --git a/src/main/java/io/ix0rai/rainglow/config/CustomModeScreen.java b/src/main/java/io/ix0rai/rainglow/config/CustomModeScreen.java index a2045cf..2b593b6 100644 --- a/src/main/java/io/ix0rai/rainglow/config/CustomModeScreen.java +++ b/src/main/java/io/ix0rai/rainglow/config/CustomModeScreen.java @@ -100,7 +100,9 @@ public void init() { } @Override - protected void initOptionButtons() {} + protected void initOptionButtons() { + // no-op + } @Override protected void repositionElements() { diff --git a/src/main/java/io/ix0rai/rainglow/config/PerWorldConfig.java b/src/main/java/io/ix0rai/rainglow/config/PerWorldConfig.java index 47ca576..611fb33 100644 --- a/src/main/java/io/ix0rai/rainglow/config/PerWorldConfig.java +++ b/src/main/java/io/ix0rai/rainglow/config/PerWorldConfig.java @@ -8,9 +8,9 @@ import folk.sisby.kaleido.lib.quiltconfig.api.values.TrackedValue; import folk.sisby.kaleido.lib.quiltconfig.api.values.ValueMap; import io.ix0rai.rainglow.Rainglow; +import io.ix0rai.rainglow.client.RainglowClient; import io.ix0rai.rainglow.data.RainglowMode; import io.ix0rai.rainglow.mixin.MinecraftServerAccessor; -import net.minecraft.client.MinecraftClient; import net.minecraft.server.MinecraftServer; import net.minecraft.server.dedicated.DedicatedServer; import net.minecraft.world.World; @@ -95,16 +95,12 @@ private static Either getSaveName(World world) { return Either.left(getWorldPath(world.getServer())); } } else { - if (MinecraftClient.getInstance().isInSingleplayer()) { - return Either.left(getWorldPath(MinecraftClient.getInstance().getServer())); - } else { - return Either.right(MinecraftClient.getInstance().getCurrentServerEntry().address); - } + return RainglowClient.getSaveNameClient(); } } @SuppressWarnings("ConstantConditions") - private static Path getWorldPath(MinecraftServer server) { + public static Path getWorldPath(MinecraftServer server) { return ((MinecraftServerAccessor) server).getSession().method_54543().path(); } diff --git a/src/main/java/io/ix0rai/rainglow/data/RainglowEntity.java b/src/main/java/io/ix0rai/rainglow/data/RainglowEntity.java index d29a220..618ed4e 100644 --- a/src/main/java/io/ix0rai/rainglow/data/RainglowEntity.java +++ b/src/main/java/io/ix0rai/rainglow/data/RainglowEntity.java @@ -3,9 +3,6 @@ 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; import net.minecraft.entity.mob.SlimeEntity; import net.minecraft.entity.passive.AllayEntity; import net.minecraft.entity.passive.GlowSquidEntity; @@ -16,7 +13,6 @@ import net.minecraft.util.random.RandomGenerator; import net.minecraft.world.World; import org.jetbrains.annotations.Nullable; -import org.spongepowered.asm.mixin.Unique; import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; import java.util.Arrays; @@ -24,9 +20,9 @@ import java.util.function.Function; public enum RainglowEntity { - 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); + GLOW_SQUID("glow_squid", RainglowColour.BLUE, GlowSquidEntityData::new), + ALLAY("allay", RainglowColour.BLUE, AllayEntityData::new), + SLIME("slime", RainglowColour.LIME, SlimeEntityData::new); private static final HashMap BY_ID = new HashMap<>(); static { @@ -35,13 +31,11 @@ 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, Function entityDataFactory) { + RainglowEntity(String id, RainglowColour defaultColour, Function entityDataFactory) { this.id = id; this.defaultColour = defaultColour; - this.trackedData = trackedData; this.entityDataFactory = entityDataFactory; } @@ -53,10 +47,6 @@ public RainglowColour getDefaultColour() { return this.defaultColour; } - public TrackedData getTrackedData() { - return this.trackedData; - } - public Identifier getDefaultTexture() { return this.defaultColour.getTexture(this); } @@ -74,13 +64,13 @@ public Item getItem(int index) { } public RainglowColour readNbt(World world, NbtCompound nbt, RandomGenerator random) { - String colour = nbt.getString(Rainglow.CUSTOM_NBT_KEY); + RainglowColour colour = RainglowColour.get(nbt.getString(Rainglow.CUSTOM_NBT_KEY)); if (Rainglow.colourUnloaded(world, this, colour)) { - colour = Rainglow.generateRandomColourId(world, random); + colour = Rainglow.generateRandomColour(world, random); } - return RainglowColour.get(colour); + return colour; } public static RainglowEntity read(PacketByteBuf buf) { @@ -96,7 +86,6 @@ public static RainglowEntity get(String id) { return BY_ID.get(id); } - @Unique @SuppressWarnings("all") public static RainglowEntity get(Entity entity) { if (entity instanceof GlowSquidEntity) { @@ -111,7 +100,7 @@ public static RainglowEntity get(Entity entity) { } public void overrideTexture(Entity entity, CallbackInfoReturnable cir) { - RainglowColour colour = Rainglow.getColour(entity.getWorld(), this, entity.getDataTracker(), entity.getWorld().getRandom()); + RainglowColour colour = Rainglow.getColour(entity); // if the colour is default we don't need to override the method // this optimises a tiny bit diff --git a/src/main/java/io/ix0rai/rainglow/data/RainglowNetworking.java b/src/main/java/io/ix0rai/rainglow/data/RainglowNetworking.java index 7435975..8eadb7a 100644 --- a/src/main/java/io/ix0rai/rainglow/data/RainglowNetworking.java +++ b/src/main/java/io/ix0rai/rainglow/data/RainglowNetworking.java @@ -1,16 +1,20 @@ package io.ix0rai.rainglow.data; import io.ix0rai.rainglow.Rainglow; +import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking; import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking; +import net.minecraft.entity.Entity; import net.minecraft.network.PacketByteBuf; import net.minecraft.network.RegistryByteBuf; import net.minecraft.network.codec.PacketCodec; import net.minecraft.network.packet.payload.CustomPayload; import net.minecraft.server.network.ServerPlayerEntity; +import net.minecraft.server.world.ServerWorld; import java.util.Collection; import java.util.List; import java.util.Map; +import java.util.UUID; public class RainglowNetworking { public static void syncConfig(ServerPlayerEntity player) { @@ -68,4 +72,38 @@ public Id getId() { return PACKET_ID; } } + + public static void sendColoursTo(ServerPlayerEntity player) { + ServerPlayNetworking.send(player, new ColourPayload(Rainglow.getColours())); + } + + public static void sendColourChangeToServer(Entity entity, RainglowColour colour) { + ClientPlayNetworking.send(new ColourPayload(Map.of(entity.getUuid(), colour))); + } + + public static void sendColourChangeToClients(Entity entity, RainglowColour colour) { + if (entity.getWorld() instanceof ServerWorld serverWorld) { + serverWorld.getPlayers().forEach(player -> ServerPlayNetworking.send(player, new ColourPayload(Map.of(entity.getUuid(), colour)))); + } else { + throw new RuntimeException("Cannot send colour change to clients from client"); + } + } + + public record ColourPayload(Map colours) implements CustomPayload { + public static final CustomPayload.Id PACKET_ID = new CustomPayload.Id<>(Rainglow.id("colour_change")); + public static final PacketCodec PACKET_CODEC = PacketCodec.create(ColourPayload::write, ColourPayload::read); + + public void write(RegistryByteBuf buf) { + buf.writeMap(this.colours, (b, uuid) -> b.writeUuid(uuid), RainglowColour::write); + } + + public static ColourPayload read(RegistryByteBuf buf) { + return new ColourPayload(buf.readMap(b -> b.readUuid(), RainglowColour::read)); + } + + @Override + public Id getId() { + return PACKET_ID; + } + } } diff --git a/src/main/java/io/ix0rai/rainglow/mixin/AllayEntityMixin.java b/src/main/java/io/ix0rai/rainglow/mixin/AllayEntityMixin.java index f387c4c..a1ff810 100644 --- a/src/main/java/io/ix0rai/rainglow/mixin/AllayEntityMixin.java +++ b/src/main/java/io/ix0rai/rainglow/mixin/AllayEntityMixin.java @@ -6,11 +6,11 @@ import io.ix0rai.rainglow.data.RainglowEntity; import net.minecraft.entity.Entity; import net.minecraft.entity.EntityType; -import net.minecraft.entity.data.DataTracker.Builder; import net.minecraft.entity.passive.AllayEntity; import net.minecraft.nbt.NbtCompound; import net.minecraft.world.World; import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Shadow; import org.spongepowered.asm.mixin.injection.At; import org.spongepowered.asm.mixin.injection.Inject; import org.spongepowered.asm.mixin.injection.Redirect; @@ -18,19 +18,16 @@ @Mixin(AllayEntity.class) public abstract class AllayEntityMixin extends Entity implements AllayVariantProvider { + @Shadow public abstract void writeCustomDataToNbt(NbtCompound nbt); + protected AllayEntityMixin(EntityType entityType, World world) { super(entityType, world); throw new UnsupportedOperationException(); } - @Inject(method = "initDataTracker", at = @At("TAIL")) - protected void initDataTracker(Builder builder, CallbackInfo ci) { - builder.add(RainglowEntity.ALLAY.getTrackedData(), RainglowEntity.ALLAY.getDefaultColour().getId()); - } - @Inject(method = "writeCustomDataToNbt", at = @At("TAIL")) public void writeCustomDataToNbt(NbtCompound nbt, CallbackInfo ci) { - RainglowColour colour = Rainglow.getColour(this.getWorld(), RainglowEntity.ALLAY, this.getDataTracker(), this.random); + RainglowColour colour = Rainglow.getColour(this); nbt.putString(Rainglow.CUSTOM_NBT_KEY, colour.getId()); } @@ -42,18 +39,18 @@ public void readCustomDataFromNbt(NbtCompound nbt, CallbackInfo ci) { // triggered when an allay duplicates, to apply the same colour as parent @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.getWorld(), RainglowEntity.ALLAY, this.getDataTracker(), this.random); - entity.getDataTracker().set(RainglowEntity.ALLAY.getTrackedData(), colour.getId()); + RainglowColour colour = Rainglow.getColour(this); + ((AllayVariantProvider) entity).setVariant(colour); return this.getWorld().spawnEntity(entity); } @Override public RainglowColour getVariant() { - return Rainglow.getColour(this.getWorld(), RainglowEntity.ALLAY, this.getDataTracker(), this.random); + return Rainglow.getColour(this); } @Override public void setVariant(RainglowColour colour) { - this.getDataTracker().set(RainglowEntity.ALLAY.getTrackedData(), colour.getId()); + Rainglow.setColour(this, colour); } } diff --git a/src/main/java/io/ix0rai/rainglow/mixin/DyeItemMixin.java b/src/main/java/io/ix0rai/rainglow/mixin/DyeItemMixin.java index eb0f556..40a4a07 100644 --- a/src/main/java/io/ix0rai/rainglow/mixin/DyeItemMixin.java +++ b/src/main/java/io/ix0rai/rainglow/mixin/DyeItemMixin.java @@ -1,9 +1,9 @@ package io.ix0rai.rainglow.mixin; import io.ix0rai.rainglow.Rainglow; +import io.ix0rai.rainglow.data.RainglowColour; import io.ix0rai.rainglow.data.RainglowEntity; import net.minecraft.entity.LivingEntity; -import net.minecraft.entity.data.DataTracker; import net.minecraft.entity.player.PlayerEntity; import net.minecraft.item.DyeItem; import net.minecraft.item.ItemStack; @@ -24,20 +24,19 @@ public class DyeItemMixin { @Inject(method = "useOnEntity", at = @At("TAIL"), cancellable = true) private void useOnEntity(ItemStack stack, PlayerEntity user, LivingEntity entity, Hand hand, CallbackInfoReturnable cir) { if (Rainglow.CONFIG.allowDyeing.value()) { - String colour = getDye(stack); + RainglowColour colour = RainglowColour.get(getDye(stack)); RainglowEntity entityType = RainglowEntity.get(entity); - if (entityType != null && !Rainglow.colourUnloaded(user.getWorld(), entityType, colour) + if (entityType != null + && !Rainglow.colourUnloaded(user.getWorld(), entityType, colour) && Rainglow.CONFIG.isEntityEnabled(entityType) - && !Rainglow.getColour(user.getWorld(), entityType, entity.getDataTracker(), entity.getWorld().getRandom()).getId().equals(colour)) { + && Rainglow.getColour(entity) != 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 = entity.getDataTracker(); - tracker.set(entityType.getTrackedData(), colour); - + Rainglow.setColour(entity, colour); cir.setReturnValue(ActionResult.success(user.getWorld().isClient())); } } diff --git a/src/main/java/io/ix0rai/rainglow/mixin/GlowSquidEntityMixin.java b/src/main/java/io/ix0rai/rainglow/mixin/GlowSquidEntityMixin.java index fad2f04..cf06b45 100644 --- a/src/main/java/io/ix0rai/rainglow/mixin/GlowSquidEntityMixin.java +++ b/src/main/java/io/ix0rai/rainglow/mixin/GlowSquidEntityMixin.java @@ -8,7 +8,6 @@ import net.minecraft.entity.mob.WaterCreatureEntity; import net.minecraft.entity.passive.GlowSquidEntity; import net.minecraft.entity.passive.SquidEntity; -import net.minecraft.entity.data.DataTracker.Builder; import net.minecraft.nbt.NbtCompound; import net.minecraft.particle.ParticleEffect; import net.minecraft.particle.ParticleTypes; @@ -27,14 +26,9 @@ protected GlowSquidEntityMixin(EntityType entityType, Wor throw new UnsupportedOperationException(); } - @Inject(method = "initDataTracker", at = @At("TAIL")) - protected void initDataTracker(Builder builder, CallbackInfo ci) { - builder.add(RainglowEntity.GLOW_SQUID.getTrackedData(), RainglowEntity.GLOW_SQUID.getDefaultColour().getId()); - } - @Inject(method = "writeCustomDataToNbt", at = @At("TAIL")) public void writeCustomDataToNbt(NbtCompound nbt, CallbackInfo ci) { - RainglowColour colour = Rainglow.getColour(this.getWorld(), RainglowEntity.GLOW_SQUID, this.getDataTracker(), this.random); + RainglowColour colour = Rainglow.getColour(this); nbt.putString(Rainglow.CUSTOM_NBT_KEY, colour.getId()); } @@ -49,7 +43,7 @@ public void readCustomDataFromNbt(NbtCompound nbt, CallbackInfo ci) { */ @Inject(method = "tickMovement", at = @At(value = "INVOKE", target = "Lnet/minecraft/world/World;addParticle(Lnet/minecraft/particle/ParticleEffect;DDDDDD)V"), cancellable = true) public void tickMovement(CallbackInfo ci) { - RainglowColour colour = Rainglow.getColour(this.getWorld(), RainglowEntity.GLOW_SQUID, this.getDataTracker(), this.random); + RainglowColour colour = Rainglow.getColour(this); if (colour != RainglowColour.BLUE) { // we add 100 to g to let the mixin know that we want to override the method @@ -60,12 +54,12 @@ public void tickMovement(CallbackInfo ci) { @Override public RainglowColour getVariant() { - return Rainglow.getColour(this.getWorld(), RainglowEntity.GLOW_SQUID, this.getDataTracker(), this.random); + return Rainglow.getColour(this); } @Override public void setVariant(RainglowColour colour) { - this.getDataTracker().set(RainglowEntity.GLOW_SQUID.getTrackedData(), colour.getId()); + Rainglow.setColour(this, colour); } @Mixin(SquidEntity.class) @@ -85,7 +79,7 @@ protected SquidEntityMixin(EntityType entityType, private int spawnParticles(ServerWorld instance, ParticleEffect particle, double x, double y, double z, int count, double deltaX, double deltaY, double deltaZ, double speed) { if (((Object) this) instanceof GlowSquidEntity) { // send in custom colour data - RainglowColour colour = Rainglow.getColour(this.getWorld(), RainglowEntity.GLOW_SQUID, this.getDataTracker(), this.random); + RainglowColour colour = Rainglow.getColour(this); int index = colour.ordinal(); // round x to 1 decimal place and append index data to the next two return ((ServerWorld) this.getWorld()).spawnParticles(particle, (Math.round(x * 10)) / 10D + index / 1000D, y + 0.5, z, 0, deltaX, deltaY, deltaZ, speed); diff --git a/src/main/java/io/ix0rai/rainglow/mixin/MobEntityMixin.java b/src/main/java/io/ix0rai/rainglow/mixin/MobEntityMixin.java index de59dc9..0359f48 100644 --- a/src/main/java/io/ix0rai/rainglow/mixin/MobEntityMixin.java +++ b/src/main/java/io/ix0rai/rainglow/mixin/MobEntityMixin.java @@ -41,6 +41,6 @@ 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.getWorld(), this.random)); + return i >= rarity ? entity.getDefaultColour() : Rainglow.generateRandomColour(this.getWorld(), this.random); } } diff --git a/src/main/java/io/ix0rai/rainglow/mixin/SlimeEntityMixin.java b/src/main/java/io/ix0rai/rainglow/mixin/SlimeEntityMixin.java index b63d9d2..ea090f9 100644 --- a/src/main/java/io/ix0rai/rainglow/mixin/SlimeEntityMixin.java +++ b/src/main/java/io/ix0rai/rainglow/mixin/SlimeEntityMixin.java @@ -6,7 +6,6 @@ import io.ix0rai.rainglow.data.SlimeVariantProvider; import net.minecraft.entity.Entity; import net.minecraft.entity.EntityType; -import net.minecraft.entity.data.DataTracker; import net.minecraft.entity.mob.SlimeEntity; import net.minecraft.nbt.NbtCompound; import net.minecraft.particle.ParticleEffect; @@ -30,14 +29,9 @@ protected SlimeEntityMixin(EntityType entityType, World w throw new UnsupportedOperationException(); } - @Inject(method = "initDataTracker", at = @At("TAIL")) - protected void initDataTracker(DataTracker.Builder builder, CallbackInfo ci) { - builder.add(RainglowEntity.SLIME.getTrackedData(), RainglowEntity.SLIME.getDefaultColour().getId()); - } - @Inject(method = "writeCustomDataToNbt", at = @At("TAIL")) public void writeCustomDataToNbt(NbtCompound nbt, CallbackInfo ci) { - RainglowColour colour = Rainglow.getColour(this.getWorld(), RainglowEntity.SLIME, this.getDataTracker(), this.random); + RainglowColour colour = Rainglow.getColour(this); nbt.putString(Rainglow.CUSTOM_NBT_KEY, colour.getId()); } @@ -51,8 +45,8 @@ public void readCustomDataFromNbt(NbtCompound nbt, CallbackInfo ci) { */ @Redirect(method = "remove", at = @At(value = "INVOKE", target = "Lnet/minecraft/world/World;spawnEntity(Lnet/minecraft/entity/Entity;)Z")) public boolean spawnWithParentColour(World instance, Entity entity) { - RainglowColour colour = Rainglow.getColour(this.getWorld(), RainglowEntity.SLIME, this.getDataTracker(), this.random); - entity.getDataTracker().set(RainglowEntity.SLIME.getTrackedData(), colour.getId()); + RainglowColour colour = Rainglow.getColour(this); + ((SlimeVariantProvider) entity).setVariant(colour); return this.getWorld().spawnEntity(entity); } @@ -68,7 +62,7 @@ public boolean spawnWithParentColour(World instance, Entity entity) { ) public void tick(CallbackInfo ci) { float size = this.getDimensions(this.getPose()).width(); - RainglowColour colour = Rainglow.getColour(this.getWorld(), RainglowEntity.SLIME, this.getDataTracker(), this.random); + RainglowColour colour = Rainglow.getColour(this); int index = colour.ordinal(); for (int j = 0; j < size / 2; j ++) { @@ -83,11 +77,11 @@ public void tick(CallbackInfo ci) { @Override public RainglowColour getVariant() { - return Rainglow.getColour(this.getWorld(), RainglowEntity.SLIME, this.getDataTracker(), this.random); + return Rainglow.getColour(this); } @Override public void setVariant(RainglowColour colour) { - this.getDataTracker().set(RainglowEntity.SLIME.getTrackedData(), colour.getId()); + Rainglow.setColour(this, colour); } }