Skip to content

Commit

Permalink
finish up
Browse files Browse the repository at this point in the history
  • Loading branch information
ix0rai committed Aug 8, 2024
1 parent af8b57d commit 33f1581
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 16 deletions.
18 changes: 15 additions & 3 deletions src/main/java/io/ix0rai/rainglow/Rainglow.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand All @@ -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) {
Expand Down Expand Up @@ -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<UUID, RainglowColour> getColours() {
return colours;
}
Expand Down
21 changes: 21 additions & 0 deletions src/main/java/io/ix0rai/rainglow/client/RainglowClient.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;

Expand Down Expand Up @@ -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
Expand All @@ -95,4 +108,12 @@ public void log() {
}
});
}

public static Either<Path, String> getSaveNameClient() {
if (MinecraftClient.getInstance().isInSingleplayer()) {
return Either.left(PerWorldConfig.getWorldPath(MinecraftClient.getInstance().getServer()));
} else {
return Either.right(MinecraftClient.getInstance().getCurrentServerEntry().address);
}
}
}
10 changes: 3 additions & 7 deletions src/main/java/io/ix0rai/rainglow/config/PerWorldConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -95,16 +95,12 @@ private static Either<Path, String> 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();
}

Expand Down
6 changes: 2 additions & 4 deletions src/main/java/io/ix0rai/rainglow/data/RainglowNetworking.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<UUID, RainglowColour> colours) implements CustomPayload {
public static final CustomPayload.Id<ColourPayload> PACKET_ID = new CustomPayload.Id<>(Rainglow.id("colour_change"));
public static final PacketCodec<RegistryByteBuf, ColourPayload> PACKET_CODEC = PacketCodec.create(ColourPayload::write, ColourPayload::read);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 0 additions & 1 deletion src/main/java/io/ix0rai/rainglow/mixin/DyeItemMixin.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 33f1581

Please sign in to comment.