From 33f1581ffb99c6844a63f169f5062eec577f165e Mon Sep 17 00:00:00 2001 From: ix0rai Date: Wed, 7 Aug 2024 19:56:13 -0500 Subject: [PATCH] finish up --- .../java/io/ix0rai/rainglow/Rainglow.java | 18 +++++++++++++--- .../rainglow/client/RainglowClient.java | 21 +++++++++++++++++++ .../rainglow/config/PerWorldConfig.java | 10 +++------ .../rainglow/data/RainglowNetworking.java | 6 ++---- .../rainglow/mixin/AllayEntityMixin.java | 1 - .../ix0rai/rainglow/mixin/DyeItemMixin.java | 1 - 6 files changed, 41 insertions(+), 16 deletions(-) diff --git a/src/main/java/io/ix0rai/rainglow/Rainglow.java b/src/main/java/io/ix0rai/rainglow/Rainglow.java index 0d89c87..ffcdf98 100644 --- a/src/main/java/io/ix0rai/rainglow/Rainglow.java +++ b/src/main/java/io/ix0rai/rainglow/Rainglow.java @@ -10,6 +10,7 @@ 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.Entity; @@ -61,6 +62,13 @@ public void onInitialize() { // 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); + } + }); } public static Identifier id(String id) { @@ -74,7 +82,7 @@ public static RainglowColour generateRandomColour(World world, RandomGenerator r public static boolean colourUnloaded(World world, RainglowEntity entityType, RainglowColour colour) { var colours = MODE_CONFIG.getMode(world).getColours(); - return !colours.contains(colour) && !colour.equals(entityType.getDefaultColour()); + return !colours.contains(colour) && colour != entityType.getDefaultColour(); } public static String translatableTextKey(String key) { @@ -105,17 +113,21 @@ public static RainglowColour getColour(Entity entity) { } public static void setColour(Entity entity, RainglowColour colour) { - colours.put(entity.getUuid(), colour); + setColour(entity.getUuid(), colour); if (entity.getWorld().isClient()) { // sync to server; will then be synced to all clients RainglowNetworking.sendColourChangeToServer(entity, colour); - } else { + } 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/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/RainglowNetworking.java b/src/main/java/io/ix0rai/rainglow/data/RainglowNetworking.java index c024044..8eadb7a 100644 --- a/src/main/java/io/ix0rai/rainglow/data/RainglowNetworking.java +++ b/src/main/java/io/ix0rai/rainglow/data/RainglowNetworking.java @@ -84,13 +84,11 @@ public static void sendColourChangeToServer(Entity entity, RainglowColour 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"); } - - throw new RuntimeException("Cannot send colour change to clients from client"); } - // todo: receivers - 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); diff --git a/src/main/java/io/ix0rai/rainglow/mixin/AllayEntityMixin.java b/src/main/java/io/ix0rai/rainglow/mixin/AllayEntityMixin.java index 37e11f8..a1ff810 100644 --- a/src/main/java/io/ix0rai/rainglow/mixin/AllayEntityMixin.java +++ b/src/main/java/io/ix0rai/rainglow/mixin/AllayEntityMixin.java @@ -6,7 +6,6 @@ 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; diff --git a/src/main/java/io/ix0rai/rainglow/mixin/DyeItemMixin.java b/src/main/java/io/ix0rai/rainglow/mixin/DyeItemMixin.java index a721c82..40a4a07 100644 --- a/src/main/java/io/ix0rai/rainglow/mixin/DyeItemMixin.java +++ b/src/main/java/io/ix0rai/rainglow/mixin/DyeItemMixin.java @@ -4,7 +4,6 @@ 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;