Skip to content

Commit af8b57d

Browse files
committed
start working on fixing vanilla compat
1 parent 1f125f4 commit af8b57d

File tree

10 files changed

+118
-73
lines changed

10 files changed

+118
-73
lines changed

gradle.properties

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ org.gradle.jvmargs=-Xmx1G
33

44
# minecraft, mappings and loader dependencies
55
# check these on https://modmuss50.me/fabric.html
6-
minecraft_version=1.21-rc1
7-
quilt_mappings=2
6+
minecraft_version=1.21
7+
quilt_mappings=17
88
loader_version=0.15.11
99

1010
# mod properties

src/main/java/io/ix0rai/rainglow/Rainglow.java

Lines changed: 39 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
import net.fabricmc.fabric.api.networking.v1.ServerPlayConnectionEvents;
1313
import net.fabricmc.fabric.api.resource.ResourceManagerHelper;
1414
import net.fabricmc.loader.api.FabricLoader;
15-
import net.minecraft.entity.data.DataTracker;
15+
import net.minecraft.entity.Entity;
1616
import net.minecraft.resource.ResourceType;
1717
import net.minecraft.text.Text;
1818
import net.minecraft.util.Identifier;
@@ -22,7 +22,10 @@
2222
import org.slf4j.LoggerFactory;
2323

2424
import java.util.ArrayList;
25+
import java.util.HashMap;
2526
import java.util.List;
27+
import java.util.Map;
28+
import java.util.UUID;
2629

2730
public class Rainglow implements ModInitializer {
2831
public static final String MOD_ID = "rainglow";
@@ -37,34 +40,41 @@ public class Rainglow implements ModInitializer {
3740
public static final Identifier SERVER_MODE_DATA_ID = id("server_mode_data");
3841
public static final List<String> RAINGLOW_DATAPACKS = new ArrayList<>();
3942

43+
private static final Map<UUID, RainglowColour> colours = new HashMap<>();
44+
4045
@Override
4146
public void onInitialize() {
4247
ResourceManagerHelper.get(ResourceType.SERVER_DATA).registerReloadListener((RainglowResourceReloader) () -> SERVER_MODE_DATA_ID);
4348

4449
PayloadTypeRegistry.playS2C().register(RainglowNetworking.ConfigSyncPayload.PACKET_ID, RainglowNetworking.ConfigSyncPayload.PACKET_CODEC);
4550
PayloadTypeRegistry.playS2C().register(RainglowNetworking.ModeSyncPayload.PACKET_ID, RainglowNetworking.ModeSyncPayload.PACKET_CODEC);
51+
PayloadTypeRegistry.playS2C().register(RainglowNetworking.ColourPayload.PACKET_ID, RainglowNetworking.ColourPayload.PACKET_CODEC);
52+
PayloadTypeRegistry.playC2S().register(RainglowNetworking.ColourPayload.PACKET_ID, RainglowNetworking.ColourPayload.PACKET_CODEC);
4653

4754
ServerPlayConnectionEvents.JOIN.register((handler, sender, server) -> {
4855
// send modes to client
4956
RainglowNetworking.syncModes(handler.player);
5057

5158
// send config to client
5259
RainglowNetworking.syncConfig(handler.player);
60+
61+
// send all colours to client
62+
RainglowNetworking.sendColoursTo(handler.player);
5363
});
5464
}
5565

5666
public static Identifier id(String id) {
5767
return Identifier.of(MOD_ID, id);
5868
}
5969

60-
public static String generateRandomColourId(World world, RandomGenerator random) {
70+
public static RainglowColour generateRandomColour(World world, RandomGenerator random) {
6171
var colours = MODE_CONFIG.getMode(world).getColours();
62-
return colours.get(random.nextInt(colours.size())).getId();
72+
return colours.get(random.nextInt(colours.size()));
6373
}
6474

65-
public static boolean colourUnloaded(World world, RainglowEntity entityType, String colour) {
75+
public static boolean colourUnloaded(World world, RainglowEntity entityType, RainglowColour colour) {
6676
var colours = MODE_CONFIG.getMode(world).getColours();
67-
return !colours.contains(RainglowColour.get(colour)) && !colour.equals(entityType.getDefaultColour().getId());
77+
return !colours.contains(colour) && !colour.equals(entityType.getDefaultColour());
6878
}
6979

7080
public static String translatableTextKey(String key) {
@@ -80,15 +90,33 @@ public static Text translatableText(String key) {
8090
return Text.translatable(translatableTextKey(key));
8191
}
8292

83-
public static RainglowColour getColour(World world, RainglowEntity entityType, DataTracker tracker, RandomGenerator random) {
93+
public static RainglowColour getColour(Entity entity) {
94+
RainglowColour colour = colours.get(entity.getUuid());
95+
RainglowEntity entityType = RainglowEntity.get(entity);
96+
8497
// generate random colour if the squid's colour isn't currently loaded
85-
String colour = tracker.get(entityType.getTrackedData());
86-
if (colourUnloaded(world, entityType, colour)) {
98+
if (colourUnloaded(entity.getWorld(), entityType, colour)) {
8799
// Use last generated colour if not null else generate a new colour
88-
tracker.set(entityType.getTrackedData(), generateRandomColourId(world, random));
89-
colour = tracker.get(entityType.getTrackedData());
100+
colour = generateRandomColour(entity.getWorld(), entity.getRandom());
101+
colours.put(entity.getUuid(), colour);
90102
}
91103

92-
return RainglowColour.get(colour);
104+
return colour;
105+
}
106+
107+
public static void setColour(Entity entity, RainglowColour colour) {
108+
colours.put(entity.getUuid(), colour);
109+
110+
if (entity.getWorld().isClient()) {
111+
// sync to server; will then be synced to all clients
112+
RainglowNetworking.sendColourChangeToServer(entity, colour);
113+
} else {
114+
// sync to all clients
115+
RainglowNetworking.sendColourChangeToClients(entity, colour);
116+
}
117+
}
118+
119+
public static Map<UUID, RainglowColour> getColours() {
120+
return colours;
93121
}
94122
}

src/main/java/io/ix0rai/rainglow/config/CustomModeScreen.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,9 @@ public void init() {
100100
}
101101

102102
@Override
103-
protected void method_60325() {}
103+
protected void initOptionButtons() {
104+
// no-op
105+
}
104106

105107
@Override
106108
protected void repositionElements() {

src/main/java/io/ix0rai/rainglow/data/RainglowEntity.java

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,6 @@
33
import io.ix0rai.rainglow.Rainglow;
44
import net.minecraft.entity.Entity;
55
import net.minecraft.entity.EntityData;
6-
import net.minecraft.entity.data.DataTracker;
7-
import net.minecraft.entity.data.TrackedData;
8-
import net.minecraft.entity.data.TrackedDataHandlerRegistry;
96
import net.minecraft.entity.mob.SlimeEntity;
107
import net.minecraft.entity.passive.AllayEntity;
118
import net.minecraft.entity.passive.GlowSquidEntity;
@@ -16,17 +13,16 @@
1613
import net.minecraft.util.random.RandomGenerator;
1714
import net.minecraft.world.World;
1815
import org.jetbrains.annotations.Nullable;
19-
import org.spongepowered.asm.mixin.Unique;
2016
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
2117

2218
import java.util.Arrays;
2319
import java.util.HashMap;
2420
import java.util.function.Function;
2521

2622
public enum RainglowEntity {
27-
GLOW_SQUID("glow_squid", RainglowColour.BLUE, DataTracker.registerData(GlowSquidEntity.class, TrackedDataHandlerRegistry.STRING), GlowSquidEntityData::new),
28-
ALLAY("allay", RainglowColour.BLUE, DataTracker.registerData(AllayEntity.class, TrackedDataHandlerRegistry.STRING), AllayEntityData::new),
29-
SLIME("slime", RainglowColour.LIME, DataTracker.registerData(SlimeEntity.class, TrackedDataHandlerRegistry.STRING), SlimeEntityData::new);
23+
GLOW_SQUID("glow_squid", RainglowColour.BLUE, GlowSquidEntityData::new),
24+
ALLAY("allay", RainglowColour.BLUE, AllayEntityData::new),
25+
SLIME("slime", RainglowColour.LIME, SlimeEntityData::new);
3026

3127
private static final HashMap<String, RainglowEntity> BY_ID = new HashMap<>();
3228
static {
@@ -35,13 +31,11 @@ public enum RainglowEntity {
3531

3632
private final String id;
3733
private final RainglowColour defaultColour;
38-
private final TrackedData<String> trackedData;
3934
private final Function<RainglowColour, EntityData> entityDataFactory;
4035

41-
RainglowEntity(String id, RainglowColour defaultColour, TrackedData<String> trackedData, Function<RainglowColour, EntityData> entityDataFactory) {
36+
RainglowEntity(String id, RainglowColour defaultColour, Function<RainglowColour, EntityData> entityDataFactory) {
4237
this.id = id;
4338
this.defaultColour = defaultColour;
44-
this.trackedData = trackedData;
4539
this.entityDataFactory = entityDataFactory;
4640
}
4741

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

56-
public TrackedData<String> getTrackedData() {
57-
return this.trackedData;
58-
}
59-
6050
public Identifier getDefaultTexture() {
6151
return this.defaultColour.getTexture(this);
6252
}
@@ -74,13 +64,13 @@ public Item getItem(int index) {
7464
}
7565

7666
public RainglowColour readNbt(World world, NbtCompound nbt, RandomGenerator random) {
77-
String colour = nbt.getString(Rainglow.CUSTOM_NBT_KEY);
67+
RainglowColour colour = RainglowColour.get(nbt.getString(Rainglow.CUSTOM_NBT_KEY));
7868

7969
if (Rainglow.colourUnloaded(world, this, colour)) {
80-
colour = Rainglow.generateRandomColourId(world, random);
70+
colour = Rainglow.generateRandomColour(world, random);
8171
}
8272

83-
return RainglowColour.get(colour);
73+
return colour;
8474
}
8575

8676
public static RainglowEntity read(PacketByteBuf buf) {
@@ -96,7 +86,6 @@ public static RainglowEntity get(String id) {
9686
return BY_ID.get(id);
9787
}
9888

99-
@Unique
10089
@SuppressWarnings("all")
10190
public static RainglowEntity get(Entity entity) {
10291
if (entity instanceof GlowSquidEntity) {
@@ -111,7 +100,7 @@ public static RainglowEntity get(Entity entity) {
111100
}
112101

113102
public void overrideTexture(Entity entity, CallbackInfoReturnable<Identifier> cir) {
114-
RainglowColour colour = Rainglow.getColour(entity.getWorld(), this, entity.getDataTracker(), entity.getWorld().getRandom());
103+
RainglowColour colour = Rainglow.getColour(entity);
115104

116105
// if the colour is default we don't need to override the method
117106
// this optimises a tiny bit

src/main/java/io/ix0rai/rainglow/data/RainglowNetworking.java

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,20 @@
11
package io.ix0rai.rainglow.data;
22

33
import io.ix0rai.rainglow.Rainglow;
4+
import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking;
45
import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking;
6+
import net.minecraft.entity.Entity;
57
import net.minecraft.network.PacketByteBuf;
68
import net.minecraft.network.RegistryByteBuf;
79
import net.minecraft.network.codec.PacketCodec;
810
import net.minecraft.network.packet.payload.CustomPayload;
911
import net.minecraft.server.network.ServerPlayerEntity;
12+
import net.minecraft.server.world.ServerWorld;
1013

1114
import java.util.Collection;
1215
import java.util.List;
1316
import java.util.Map;
17+
import java.util.UUID;
1418

1519
public class RainglowNetworking {
1620
public static void syncConfig(ServerPlayerEntity player) {
@@ -68,4 +72,40 @@ public Id<? extends CustomPayload> getId() {
6872
return PACKET_ID;
6973
}
7074
}
75+
76+
public static void sendColoursTo(ServerPlayerEntity player) {
77+
ServerPlayNetworking.send(player, new ColourPayload(Rainglow.getColours()));
78+
}
79+
80+
public static void sendColourChangeToServer(Entity entity, RainglowColour colour) {
81+
ClientPlayNetworking.send(new ColourPayload(Map.of(entity.getUuid(), colour)));
82+
}
83+
84+
public static void sendColourChangeToClients(Entity entity, RainglowColour colour) {
85+
if (entity.getWorld() instanceof ServerWorld serverWorld) {
86+
serverWorld.getPlayers().forEach(player -> ServerPlayNetworking.send(player, new ColourPayload(Map.of(entity.getUuid(), colour))));
87+
}
88+
89+
throw new RuntimeException("Cannot send colour change to clients from client");
90+
}
91+
92+
// todo: receivers
93+
94+
public record ColourPayload(Map<UUID, RainglowColour> colours) implements CustomPayload {
95+
public static final CustomPayload.Id<ColourPayload> PACKET_ID = new CustomPayload.Id<>(Rainglow.id("colour_change"));
96+
public static final PacketCodec<RegistryByteBuf, ColourPayload> PACKET_CODEC = PacketCodec.create(ColourPayload::write, ColourPayload::read);
97+
98+
public void write(RegistryByteBuf buf) {
99+
buf.writeMap(this.colours, (b, uuid) -> b.writeUuid(uuid), RainglowColour::write);
100+
}
101+
102+
public static ColourPayload read(RegistryByteBuf buf) {
103+
return new ColourPayload(buf.readMap(b -> b.readUuid(), RainglowColour::read));
104+
}
105+
106+
@Override
107+
public Id<? extends CustomPayload> getId() {
108+
return PACKET_ID;
109+
}
110+
}
71111
}

src/main/java/io/ix0rai/rainglow/mixin/AllayEntityMixin.java

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,26 +11,24 @@
1111
import net.minecraft.nbt.NbtCompound;
1212
import net.minecraft.world.World;
1313
import org.spongepowered.asm.mixin.Mixin;
14+
import org.spongepowered.asm.mixin.Shadow;
1415
import org.spongepowered.asm.mixin.injection.At;
1516
import org.spongepowered.asm.mixin.injection.Inject;
1617
import org.spongepowered.asm.mixin.injection.Redirect;
1718
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
1819

1920
@Mixin(AllayEntity.class)
2021
public abstract class AllayEntityMixin extends Entity implements AllayVariantProvider {
22+
@Shadow public abstract void writeCustomDataToNbt(NbtCompound nbt);
23+
2124
protected AllayEntityMixin(EntityType<? extends AllayEntity> entityType, World world) {
2225
super(entityType, world);
2326
throw new UnsupportedOperationException();
2427
}
2528

26-
@Inject(method = "initDataTracker", at = @At("TAIL"))
27-
protected void initDataTracker(Builder builder, CallbackInfo ci) {
28-
builder.add(RainglowEntity.ALLAY.getTrackedData(), RainglowEntity.ALLAY.getDefaultColour().getId());
29-
}
30-
3129
@Inject(method = "writeCustomDataToNbt", at = @At("TAIL"))
3230
public void writeCustomDataToNbt(NbtCompound nbt, CallbackInfo ci) {
33-
RainglowColour colour = Rainglow.getColour(this.getWorld(), RainglowEntity.ALLAY, this.getDataTracker(), this.random);
31+
RainglowColour colour = Rainglow.getColour(this);
3432
nbt.putString(Rainglow.CUSTOM_NBT_KEY, colour.getId());
3533
}
3634

@@ -42,18 +40,18 @@ public void readCustomDataFromNbt(NbtCompound nbt, CallbackInfo ci) {
4240
// triggered when an allay duplicates, to apply the same colour as parent
4341
@Redirect(method = "duplicate", at = @At(value = "INVOKE", target = "Lnet/minecraft/world/World;spawnEntity(Lnet/minecraft/entity/Entity;)Z"))
4442
public boolean spawnWithColour(World instance, Entity entity) {
45-
RainglowColour colour = Rainglow.getColour(this.getWorld(), RainglowEntity.ALLAY, this.getDataTracker(), this.random);
46-
entity.getDataTracker().set(RainglowEntity.ALLAY.getTrackedData(), colour.getId());
43+
RainglowColour colour = Rainglow.getColour(this);
44+
((AllayVariantProvider) entity).setVariant(colour);
4745
return this.getWorld().spawnEntity(entity);
4846
}
4947

5048
@Override
5149
public RainglowColour getVariant() {
52-
return Rainglow.getColour(this.getWorld(), RainglowEntity.ALLAY, this.getDataTracker(), this.random);
50+
return Rainglow.getColour(this);
5351
}
5452

5553
@Override
5654
public void setVariant(RainglowColour colour) {
57-
this.getDataTracker().set(RainglowEntity.ALLAY.getTrackedData(), colour.getId());
55+
Rainglow.setColour(this, colour);
5856
}
5957
}

src/main/java/io/ix0rai/rainglow/mixin/DyeItemMixin.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package io.ix0rai.rainglow.mixin;
22

33
import io.ix0rai.rainglow.Rainglow;
4+
import io.ix0rai.rainglow.data.RainglowColour;
45
import io.ix0rai.rainglow.data.RainglowEntity;
56
import net.minecraft.entity.LivingEntity;
67
import net.minecraft.entity.data.DataTracker;
@@ -24,20 +25,19 @@ public class DyeItemMixin {
2425
@Inject(method = "useOnEntity", at = @At("TAIL"), cancellable = true)
2526
private void useOnEntity(ItemStack stack, PlayerEntity user, LivingEntity entity, Hand hand, CallbackInfoReturnable<ActionResult> cir) {
2627
if (Rainglow.CONFIG.allowDyeing.value()) {
27-
String colour = getDye(stack);
28+
RainglowColour colour = RainglowColour.get(getDye(stack));
2829
RainglowEntity entityType = RainglowEntity.get(entity);
2930

30-
if (entityType != null && !Rainglow.colourUnloaded(user.getWorld(), entityType, colour)
31+
if (entityType != null
32+
&& !Rainglow.colourUnloaded(user.getWorld(), entityType, colour)
3133
&& Rainglow.CONFIG.isEntityEnabled(entityType)
32-
&& !Rainglow.getColour(user.getWorld(), entityType, entity.getDataTracker(), entity.getWorld().getRandom()).getId().equals(colour)) {
34+
&& Rainglow.getColour(entity) != colour) {
3335
entity.getWorld().playSoundFromEntity(user, entity, SoundEvents.BLOCK_AMETHYST_CLUSTER_BREAK, SoundCategory.PLAYERS, 5.0f, 1.0f);
3436
if (!user.getWorld().isClient()) {
3537
stack.decrement(1);
3638
}
3739

38-
DataTracker tracker = entity.getDataTracker();
39-
tracker.set(entityType.getTrackedData(), colour);
40-
40+
Rainglow.setColour(entity, colour);
4141
cir.setReturnValue(ActionResult.success(user.getWorld().isClient()));
4242
}
4343
}

0 commit comments

Comments
 (0)