Skip to content

Commit

Permalink
Merge pull request #34 from rosebudmods/fix-vanilla-compat
Browse files Browse the repository at this point in the history
fix compat with vanilla
  • Loading branch information
ix0rai authored Aug 8, 2024
2 parents 338f4bd + 6f31496 commit 5a5a44e
Show file tree
Hide file tree
Showing 11 changed files with 150 additions and 80 deletions.
62 changes: 51 additions & 11 deletions src/main/java/io/ix0rai/rainglow/Rainglow.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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";
Expand All @@ -37,34 +41,48 @@ public class Rainglow implements ModInitializer {
public static final Identifier SERVER_MODE_DATA_ID = id("server_mode_data");
public static final List<String> RAINGLOW_DATAPACKS = new ArrayList<>();

private static final Map<UUID, RainglowColour> 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
RainglowNetworking.syncModes(handler.player);

// 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);
}
});
}

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) {
Expand All @@ -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<UUID, RainglowColour> getColours() {
return colours;
}
}
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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,9 @@ public void init() {
}

@Override
protected void initOptionButtons() {}
protected void initOptionButtons() {
// no-op
}

@Override
protected void repositionElements() {
Expand Down
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
27 changes: 8 additions & 19 deletions src/main/java/io/ix0rai/rainglow/data/RainglowEntity.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -16,17 +13,16 @@
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;
import java.util.HashMap;
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<String, RainglowEntity> BY_ID = new HashMap<>();
static {
Expand All @@ -35,13 +31,11 @@ public enum RainglowEntity {

private final String id;
private final RainglowColour defaultColour;
private final TrackedData<String> trackedData;
private final Function<RainglowColour, EntityData> entityDataFactory;

RainglowEntity(String id, RainglowColour defaultColour, TrackedData<String> trackedData, Function<RainglowColour, EntityData> entityDataFactory) {
RainglowEntity(String id, RainglowColour defaultColour, Function<RainglowColour, EntityData> entityDataFactory) {
this.id = id;
this.defaultColour = defaultColour;
this.trackedData = trackedData;
this.entityDataFactory = entityDataFactory;
}

Expand All @@ -53,10 +47,6 @@ public RainglowColour getDefaultColour() {
return this.defaultColour;
}

public TrackedData<String> getTrackedData() {
return this.trackedData;
}

public Identifier getDefaultTexture() {
return this.defaultColour.getTexture(this);
}
Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -111,7 +100,7 @@ public static RainglowEntity get(Entity entity) {
}

public void overrideTexture(Entity entity, CallbackInfoReturnable<Identifier> 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
Expand Down
38 changes: 38 additions & 0 deletions src/main/java/io/ix0rai/rainglow/data/RainglowNetworking.java
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down Expand Up @@ -68,4 +72,38 @@ public Id<? extends CustomPayload> 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<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);

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<? extends CustomPayload> getId() {
return PACKET_ID;
}
}
}
Loading

0 comments on commit 5a5a44e

Please sign in to comment.