From 253ff329c76704f06b274bdbea96327988ced1b1 Mon Sep 17 00:00:00 2001 From: Sollace Date: Thu, 31 Oct 2024 21:09:39 +0000 Subject: [PATCH] Fixed compatibility with synatra connector --- .../com/minelittlepony/unicopia/Race.java | 4 +- .../unicopia/ability/Abilities.java | 2 +- .../ability/magic/MultiSpellSlot.java | 6 +-- .../ability/magic/spell/effect/SpellType.java | 5 ++- .../diet/affliction/AfflictionType.java | 2 +- .../mixin/forgified/MixinIForgeBoat.java | 2 +- .../MixinPacketAndPayloadAcceptor.java | 15 +++++++ .../mixin/server/MixinEntityTrackerEntry.java | 12 +++-- .../mixin/server/MixinServerPlayerEntity.java | 8 +++- .../unicopia/network/track/DataTracker.java | 17 +++---- .../network/track/DataTrackerManager.java | 17 +++---- .../network/track/MsgTrackedValues.java | 45 ++++--------------- .../unicopia/network/track/ObjectTracker.java | 17 ++++--- .../network/track/TrackableObject.java | 3 +- .../unicopia/util/RegistryUtils.java | 13 +++++- .../util/serialization/PacketCodecUtils.java | 13 ------ src/main/resources/unicopia.mixin.json | 3 +- 17 files changed, 88 insertions(+), 96 deletions(-) create mode 100644 src/main/java/com/minelittlepony/unicopia/mixin/forgified/MixinPacketAndPayloadAcceptor.java diff --git a/src/main/java/com/minelittlepony/unicopia/Race.java b/src/main/java/com/minelittlepony/unicopia/Race.java index 37d21ab0..27e9f022 100644 --- a/src/main/java/com/minelittlepony/unicopia/Race.java +++ b/src/main/java/com/minelittlepony/unicopia/Race.java @@ -42,8 +42,8 @@ public record Race ( boolean canInteractWithClouds ) implements Affine { public static final String DEFAULT_ID = "unicopia:unset"; - public static final Registry REGISTRY = RegistryUtils.createDefaulted(Unicopia.id("race"), DEFAULT_ID); - public static final Registry COMMAND_REGISTRY = RegistryUtils.createDefaulted(Unicopia.id("race/grantable"), DEFAULT_ID); + public static final Registry REGISTRY = RegistryUtils.createSynced(Unicopia.id("race"), DEFAULT_ID); + public static final Registry COMMAND_REGISTRY = RegistryUtils.createSynced(Unicopia.id("race/grantable"), DEFAULT_ID); public static final RegistryKey> REGISTRY_KEY = REGISTRY.getKey(); private static final DynamicCommandExceptionType UNKNOWN_RACE_EXCEPTION = new DynamicCommandExceptionType(id -> Text.translatable("commands.race.fail", id)); private static final Function COMPOSITES = Util.memoize(race -> new Composite(race, null, null)); diff --git a/src/main/java/com/minelittlepony/unicopia/ability/Abilities.java b/src/main/java/com/minelittlepony/unicopia/ability/Abilities.java index 43fb6ab7..415ff31b 100644 --- a/src/main/java/com/minelittlepony/unicopia/ability/Abilities.java +++ b/src/main/java/com/minelittlepony/unicopia/ability/Abilities.java @@ -18,7 +18,7 @@ import net.minecraft.network.codec.PacketCodecs; import net.minecraft.registry.Registry; public interface Abilities { - Registry> REGISTRY = RegistryUtils.createSimple(Unicopia.id("abilities")); + Registry> REGISTRY = RegistryUtils.createSynced(Unicopia.id("abilities")); PacketCodec> PACKET_CODEC = PacketCodecs.registryValue(REGISTRY.getKey()); Map>> BY_SLOT = new EnumMap<>(AbilitySlot.class); BiFunction>> BY_SLOT_AND_COMPOSITE_RACE = Util.memoize((slot, race) -> { diff --git a/src/main/java/com/minelittlepony/unicopia/ability/magic/MultiSpellSlot.java b/src/main/java/com/minelittlepony/unicopia/ability/magic/MultiSpellSlot.java index 470a6a51..ff38f8ec 100644 --- a/src/main/java/com/minelittlepony/unicopia/ability/magic/MultiSpellSlot.java +++ b/src/main/java/com/minelittlepony/unicopia/ability/magic/MultiSpellSlot.java @@ -149,16 +149,14 @@ class MultiSpellSlot implements SpellSlots, NbtSerialisable { } @Override - public void read(ByteBuf buffer, WrapperLookup lookup) { + public void read(RegistryByteBuf buffer, WrapperLookup lookup) { byte contentType = buffer.readByte(); if (contentType == 1) { readTrackedNbt(PacketCodecs.NBT_COMPOUND.decode(buffer), lookup); } else { T spell = this.spell.get(); if (spell != null) { - spell.getDataTracker().load(MsgTrackedValues.TrackerEntries.PACKET_CODEC.decode( - buffer instanceof RegistryByteBuf r ? r : new RegistryByteBuf(buffer, owner.asEntity().getRegistryManager()) - ), lookup); + spell.getDataTracker().load(MsgTrackedValues.TrackerEntries.PACKET_CODEC.decode(buffer), buffer.getRegistryManager()); } } } diff --git a/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/effect/SpellType.java b/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/effect/SpellType.java index ff0974af..876f3424 100644 --- a/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/effect/SpellType.java +++ b/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/effect/SpellType.java @@ -44,13 +44,14 @@ import net.minecraft.server.command.ServerCommandSource; public final class SpellType implements Affine, SpellPredicate { public static final Identifier EMPTY_ID = Unicopia.id("none"); - public static final SpellType EMPTY_KEY = builder(t -> null).affinity(Affinity.NEUTRAL).color(0xFFFFFF).unobtainable().build(EMPTY_ID); - public static final Registry> REGISTRY = RegistryUtils.createSimple(Unicopia.id("spells")); + public static final Registry> REGISTRY = RegistryUtils.createSynced(Unicopia.id("spells"), EMPTY_ID.toString()); public static final RegistryKey>> REGISTRY_KEY = REGISTRY.getKey(); public static final Codec> CODEC = REGISTRY.getCodec(); public static final PacketCodec> PACKET_CODEC = PacketCodecs.registryValue(REGISTRY_KEY); + public static final SpellType EMPTY_KEY = register(EMPTY_ID.getPath(), builder(t -> null).affinity(Affinity.NEUTRAL).color(0xFFFFFF).unobtainable()); + private static final DynamicCommandExceptionType UNKNOWN_SPELL_TYPE_EXCEPTION = new DynamicCommandExceptionType(id -> Text.translatable("spell_type.unknown", id)); public static final SpellType PLACE_CONTROL_SPELL = register("place_controller", SpellType.builder(PlacementControlSpell::new).affinity(Affinity.NEUTRAL).unobtainable().stackable().shape(GemstoneItem.Shape.DONUT)); diff --git a/src/main/java/com/minelittlepony/unicopia/diet/affliction/AfflictionType.java b/src/main/java/com/minelittlepony/unicopia/diet/affliction/AfflictionType.java index 3d77d484..8de9fdda 100644 --- a/src/main/java/com/minelittlepony/unicopia/diet/affliction/AfflictionType.java +++ b/src/main/java/com/minelittlepony/unicopia/diet/affliction/AfflictionType.java @@ -15,7 +15,7 @@ import net.minecraft.util.Util; public record AfflictionType(Identifier id, MapCodec codec, PacketCodec packetCodec) { public static final String DEFAULT_ID = "unicopia:apply_status_effect"; - public static final Registry> REGISTRY = RegistryUtils.createDefaulted(Unicopia.id("affliction_type"), DEFAULT_ID); + public static final Registry> REGISTRY = RegistryUtils.createSynced(Unicopia.id("affliction_type"), DEFAULT_ID); public static final Codec CODEC = CodecUtils.apply(REGISTRY.getCodec() .dispatch("type", Affliction::getType, AfflictionType::codec), elementCodec -> Codec .withAlternative(elementCodec, Codec.list(elementCodec), afflictions -> { diff --git a/src/main/java/com/minelittlepony/unicopia/mixin/forgified/MixinIForgeBoat.java b/src/main/java/com/minelittlepony/unicopia/mixin/forgified/MixinIForgeBoat.java index dd7abfe8..a6fd499e 100644 --- a/src/main/java/com/minelittlepony/unicopia/mixin/forgified/MixinIForgeBoat.java +++ b/src/main/java/com/minelittlepony/unicopia/mixin/forgified/MixinIForgeBoat.java @@ -10,7 +10,7 @@ import net.minecraft.fluid.Fluids; import net.minecraft.registry.tag.FluidTags; @Pseudo -@Mixin(targets = "net.minecraftforge.common.extensions.IForgeBoat") +@Mixin(targets = "net.neoforged.neoforge.common.extensions.IAbstractBoatExtension") interface MixinIForgeBoat { @ModifyVariable( method = "canBoatInFluid(Lnet/minecraft/fluid/FluidState;)Z", diff --git a/src/main/java/com/minelittlepony/unicopia/mixin/forgified/MixinPacketAndPayloadAcceptor.java b/src/main/java/com/minelittlepony/unicopia/mixin/forgified/MixinPacketAndPayloadAcceptor.java new file mode 100644 index 00000000..f223d098 --- /dev/null +++ b/src/main/java/com/minelittlepony/unicopia/mixin/forgified/MixinPacketAndPayloadAcceptor.java @@ -0,0 +1,15 @@ +package com.minelittlepony.unicopia.mixin.forgified; + +import java.util.function.Consumer; + +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Pseudo; + +import net.minecraft.network.listener.ClientCommonPacketListener; +import net.minecraft.network.packet.Packet; + +@Pseudo +@Mixin(targets = "net.neoforged.neoforge.network.bundle.PacketAndPayloadAcceptor") +interface MixinPacketAndPayloadAcceptor extends Consumer> { + +} diff --git a/src/main/java/com/minelittlepony/unicopia/mixin/server/MixinEntityTrackerEntry.java b/src/main/java/com/minelittlepony/unicopia/mixin/server/MixinEntityTrackerEntry.java index d25a3e57..c26e82cf 100644 --- a/src/main/java/com/minelittlepony/unicopia/mixin/server/MixinEntityTrackerEntry.java +++ b/src/main/java/com/minelittlepony/unicopia/mixin/server/MixinEntityTrackerEntry.java @@ -6,13 +6,13 @@ import org.spongepowered.asm.mixin.Final; 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.Coerce; import org.spongepowered.asm.mixin.injection.Inject; import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; import com.minelittlepony.unicopia.network.track.Trackable; import net.minecraft.entity.Entity; -import net.minecraft.network.listener.ClientPlayPacketListener; import net.minecraft.network.packet.Packet; import net.minecraft.server.network.EntityTrackerEntry; import net.minecraft.server.network.ServerPlayerEntity; @@ -29,8 +29,12 @@ abstract class MixinEntityTrackerEntry { Trackable.of(entity).getDataTrackers().tick(this::sendSyncPacket); } - @Inject(method = "sendPackets", at = @At("RETURN")) - private void unicopia_onSendPackets(ServerPlayerEntity player, Consumer> sender, CallbackInfo info) { - Trackable.of(entity).getDataTrackers().sendInitial(player, sender); + @SuppressWarnings("unchecked") + @Inject(method = "sendPackets", at = @At("RETURN"), require = 0) + private void unicopia_onSendPackets(ServerPlayerEntity player, @Coerce Object sender, CallbackInfo info) { + // NEO: Consumer> replaced with PacketAndPayloadAcceptor + if (sender instanceof Consumer s) { + Trackable.of(entity).getDataTrackers().sendInitial(player, s); + } } } diff --git a/src/main/java/com/minelittlepony/unicopia/mixin/server/MixinServerPlayerEntity.java b/src/main/java/com/minelittlepony/unicopia/mixin/server/MixinServerPlayerEntity.java index b662df7b..4e4885a2 100644 --- a/src/main/java/com/minelittlepony/unicopia/mixin/server/MixinServerPlayerEntity.java +++ b/src/main/java/com/minelittlepony/unicopia/mixin/server/MixinServerPlayerEntity.java @@ -42,10 +42,14 @@ abstract class MixinServerPlayerEntity extends PlayerEntity implements ScreenHan get().copyFrom(((Equine.Container)oldPlayer).get(), alive); } - @Inject(method = "trySleep(Lnet/minecraft/util/math/BlockPos;)Lcom/mojang/datafixers/util/Either;", at = @At( + @Inject(method = { + "trySleep(Lnet/minecraft/util/math/BlockPos;)Lcom/mojang/datafixers/util/Either;", + // NEO: https://github.com/neoforged/NeoForge/blob/62eb61a8e0c726faa30eaa3f4998ba1101368ee4/patches/net/minecraft/server/level/ServerPlayer.java.patch#L77 + "lambda$0()Lcom/mojang/datafixers/util/Either;" + }, at = @At( value = "FIELD", target = "net/minecraft/entity/player/PlayerEntity$SleepFailureReason.NOT_POSSIBLE_NOW:Lnet/minecraft/entity/player/PlayerEntity$SleepFailureReason;" - ), cancellable = true) + ), cancellable = true, require = 0) private void onTrySleep(BlockPos pos, CallbackInfoReturnable> info) { if (get().getSpecies().isNocturnal() && get().asWorld().getGameRules().getBoolean(UGameRules.DO_NOCTURNAL_BAT_PONIES)) { ((PlayerEntity)this).sendMessage(Text.translatable("block.unicopia.bed.no_sleep.nocturnal"), true); diff --git a/src/main/java/com/minelittlepony/unicopia/network/track/DataTracker.java b/src/main/java/com/minelittlepony/unicopia/network/track/DataTracker.java index bacbcbf2..72ea46b3 100644 --- a/src/main/java/com/minelittlepony/unicopia/network/track/DataTracker.java +++ b/src/main/java/com/minelittlepony/unicopia/network/track/DataTracker.java @@ -9,13 +9,14 @@ import java.util.Optional; import com.minelittlepony.unicopia.util.Untyped; -import io.netty.buffer.ByteBuf; +import io.netty.buffer.Unpooled; import it.unimi.dsi.fastutil.ints.IntOpenHashSet; import it.unimi.dsi.fastutil.ints.IntSet; import it.unimi.dsi.fastutil.objects.ObjectArrayList; import net.minecraft.network.RegistryByteBuf; import net.minecraft.network.codec.PacketCodec; import net.minecraft.network.codec.PacketCodecs; +import net.minecraft.registry.DynamicRegistryManager; import net.minecraft.registry.RegistryWrapper.WrapperLookup; public class DataTracker { @@ -86,7 +87,7 @@ public class DataTracker { return getInitialPairs(lookup); } - Map updates = writePersistentObjects(lookup, false); + Map updates = writePersistentObjects(lookup, false); if (dirtyIndices.isEmpty() && updates.isEmpty()) { return Optional.empty(); @@ -98,21 +99,21 @@ public class DataTracker { for (int i : toSend) { pairs.add(codecs.get(i)); } - return Optional.of(new MsgTrackedValues.TrackerEntries(id, false, pairs, Untyped.cast(updates))); + return Optional.of(new MsgTrackedValues.TrackerEntries(id, false, pairs, updates)); } - private Map writePersistentObjects(WrapperLookup lookup, boolean initial) { - Map updates = new HashMap<>(); + private Map writePersistentObjects(WrapperLookup lookup, boolean initial) { + Map updates = new HashMap<>(); for (int i = 0; i < persistentObjects.size(); i++) { TrackableObject o = persistentObjects.get(i); TrackableObject.Status status = initial ? TrackableObject.Status.NEW : o.getStatus(); int id = i; - o.write(status, lookup).ifPresent(data -> updates.put(id, data)); + o.write(status, lookup).ifPresent(data -> updates.put(id, data.copy().array())); } return updates; } - public synchronized void load(MsgTrackedValues.TrackerEntries values, WrapperLookup lookup) { + public synchronized void load(MsgTrackedValues.TrackerEntries values, DynamicRegistryManager lookup) { if (values.wipe()) { codecs.clear(); codecs.addAll(values.values()); @@ -129,7 +130,7 @@ public class DataTracker { for (var entry : values.objects().entrySet()) { TrackableObject o = persistentObjects.get(entry.getKey()); if (o != null) { - o.read(entry.getValue(), lookup); + o.read(new RegistryByteBuf(Unpooled.wrappedBuffer(entry.getValue()), lookup), lookup); } } } diff --git a/src/main/java/com/minelittlepony/unicopia/network/track/DataTrackerManager.java b/src/main/java/com/minelittlepony/unicopia/network/track/DataTrackerManager.java index cba80adc..00380789 100644 --- a/src/main/java/com/minelittlepony/unicopia/network/track/DataTrackerManager.java +++ b/src/main/java/com/minelittlepony/unicopia/network/track/DataTrackerManager.java @@ -6,16 +6,17 @@ import java.util.function.Consumer; import java.util.function.Supplier; import com.minelittlepony.unicopia.network.Channel; + import it.unimi.dsi.fastutil.objects.ObjectArrayList; import net.minecraft.entity.Entity; import net.minecraft.network.listener.ClientPlayPacketListener; import net.minecraft.network.packet.Packet; -import net.minecraft.registry.RegistryWrapper.WrapperLookup; +import net.minecraft.registry.DynamicRegistryManager; import net.minecraft.server.network.ServerPlayerEntity; public class DataTrackerManager { private final Entity entity; - private final WrapperLookup lookup; + private final DynamicRegistryManager lookup; final boolean isClient; private final List trackers = new ObjectArrayList<>(); private final List> objectTrackers = new ObjectArrayList<>(); @@ -44,13 +45,11 @@ public class DataTrackerManager { packetEmitters.add((sender, initial) -> { var update = initial ? tracker.getInitialPairs(lookup) : tracker.getDirtyPairs(lookup); if (update.isPresent()) { - try (var payload = new MsgTrackedValues( + sender.accept(Channel.SERVER_TRACKED_ENTITY_DATA.toPacket(new MsgTrackedValues( entity.getId(), Optional.empty(), update - )) { - sender.accept(Channel.SERVER_TRACKED_ENTITY_DATA.toPacket(payload)); - } + ))); } }); return tracker; @@ -62,13 +61,11 @@ public class DataTrackerManager { packetEmitters.add((sender, initial) -> { var update = initial ? tracker.getInitialPairs(lookup) : tracker.getDirtyPairs(lookup); if (update.isPresent()) { - try (var payload = new MsgTrackedValues( + sender.accept(Channel.SERVER_TRACKED_ENTITY_DATA.toPacket(new MsgTrackedValues( entity.getId(), update, Optional.empty() - )) { - sender.accept(Channel.SERVER_TRACKED_ENTITY_DATA.toPacket(payload)); - } + ))); } }); return tracker; diff --git a/src/main/java/com/minelittlepony/unicopia/network/track/MsgTrackedValues.java b/src/main/java/com/minelittlepony/unicopia/network/track/MsgTrackedValues.java index e5dabcee..1f085608 100644 --- a/src/main/java/com/minelittlepony/unicopia/network/track/MsgTrackedValues.java +++ b/src/main/java/com/minelittlepony/unicopia/network/track/MsgTrackedValues.java @@ -1,6 +1,5 @@ package com.minelittlepony.unicopia.network.track; -import java.io.Closeable; import java.util.ArrayList; import java.util.HashMap; import java.util.HashSet; @@ -10,10 +9,8 @@ import java.util.Optional; import java.util.Set; import java.util.UUID; -import com.minelittlepony.unicopia.util.serialization.PacketCodecUtils; import com.sollace.fabwork.api.packets.Handled; -import io.netty.buffer.ByteBuf; import net.minecraft.entity.Entity; import net.minecraft.entity.player.PlayerEntity; import net.minecraft.network.RegistryByteBuf; @@ -25,7 +22,7 @@ public record MsgTrackedValues( int owner, Optional updatedObjects, Optional updatedTrackers -) implements Handled, AutoCloseable { +) implements Handled { public static final PacketCodec PACKET_CODEC = PacketCodec.tuple( PacketCodecs.INTEGER, MsgTrackedValues::owner, PacketCodecs.optional(TrackerObjects.PACKET_CODEC), MsgTrackedValues::updatedObjects, @@ -35,54 +32,28 @@ public record MsgTrackedValues( @Override public void handle(PlayerEntity sender) { - try (this) { - Entity entity = sender.getWorld().getEntityById(owner); - if (entity instanceof Trackable trackable) { - trackable.getDataTrackers().load(this); - } + Entity entity = sender.getWorld().getEntityById(owner); + if (entity instanceof Trackable trackable) { + trackable.getDataTrackers().load(this); } } - public record TrackerObjects(int id, Set removedValues, Map values) implements Closeable { + public record TrackerObjects(int id, Set removedValues, Map values) { public static final PacketCodec PACKET_CODEC = PacketCodec.tuple( PacketCodecs.INTEGER, TrackerObjects::id, Uuids.PACKET_CODEC.collect(PacketCodecs.toCollection(HashSet::new)), TrackerObjects::removedValues, - PacketCodecs.map(HashMap::new, Uuids.PACKET_CODEC, PacketCodecUtils.REGISTRY_BUFFER), TrackerObjects::values, + PacketCodecs.map(HashMap::new, Uuids.PACKET_CODEC, PacketCodecs.BYTE_ARRAY), TrackerObjects::values, TrackerObjects::new ); - - @Override - public void close() { - values.values().forEach(buf -> { - if (buf.refCnt() > 0) { - buf.release(); - } - }); - } } - public record TrackerEntries(int id, boolean wipe, List> values, Map objects) implements Closeable { + public record TrackerEntries(int id, boolean wipe, List> values, Map objects) { public static final PacketCodec PACKET_CODEC = PacketCodec.tuple( PacketCodecs.INTEGER, TrackerEntries::id, PacketCodecs.BOOL, TrackerEntries::wipe, DataTracker.Pair.PACKET_CODEC.collect(PacketCodecs.toCollection(ArrayList::new)), TrackerEntries::values, - PacketCodecs.map(HashMap::new, PacketCodecs.INTEGER, PacketCodecUtils.REGISTRY_BUFFER), TrackerEntries::objects, + PacketCodecs.map(HashMap::new, PacketCodecs.INTEGER, PacketCodecs.BYTE_ARRAY), TrackerEntries::objects, TrackerEntries::new ); - - @Override - public void close() { - objects.values().forEach(buf -> { - if (buf.refCnt() > 0) { - buf.release(); - } - }); - } - } - - @Override - public void close() { - updatedObjects.ifPresent(TrackerObjects::close); - updatedObjects.ifPresent(TrackerObjects::close); } } diff --git a/src/main/java/com/minelittlepony/unicopia/network/track/ObjectTracker.java b/src/main/java/com/minelittlepony/unicopia/network/track/ObjectTracker.java index 3d35347a..d8595898 100644 --- a/src/main/java/com/minelittlepony/unicopia/network/track/ObjectTracker.java +++ b/src/main/java/com/minelittlepony/unicopia/network/track/ObjectTracker.java @@ -12,8 +12,10 @@ import java.util.function.Supplier; import org.jetbrains.annotations.Nullable; import com.minelittlepony.unicopia.network.track.TrackableObject.Status; -import io.netty.buffer.ByteBuf; +import io.netty.buffer.Unpooled; import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap; +import net.minecraft.network.RegistryByteBuf; +import net.minecraft.registry.DynamicRegistryManager; import net.minecraft.registry.RegistryWrapper.WrapperLookup; public class ObjectTracker> { @@ -89,10 +91,10 @@ public class ObjectTracker> { return Optional.empty(); } - Map updates = new HashMap<>(); + Map updates = new HashMap<>(); quickAccess.entrySet().forEach(object -> { object.getValue().write(Status.NEW, lookup).ifPresent(data -> { - updates.put(object.getKey(), data); + updates.put(object.getKey(), data.copy().array()); }); }); @@ -101,7 +103,7 @@ public class ObjectTracker> { synchronized Optional getDirtyPairs(WrapperLookup lookup) { if (!trackedObjects.isEmpty()) { - Map updates = new HashMap<>(); + Map updates = new HashMap<>(); Set removedTrackableObjects = new HashSet<>(); trackedObjects.entrySet().removeIf(object -> { TrackableObject.Status status = object.getValue().getStatus(); @@ -110,7 +112,7 @@ public class ObjectTracker> { return true; } object.getValue().write(status, lookup).ifPresent(data -> { - updates.put(object.getKey(), data); + updates.put(object.getKey(), data.copy().array()); }); return false; }); @@ -124,7 +126,7 @@ public class ObjectTracker> { return Optional.empty(); } - synchronized void load(MsgTrackedValues.TrackerObjects objects, WrapperLookup lookup) { + synchronized void load(MsgTrackedValues.TrackerObjects objects, DynamicRegistryManager lookup) { objects.removedValues().forEach(removedId -> { T o = trackedObjects.remove(removedId); if (o != null) { @@ -137,7 +139,8 @@ public class ObjectTracker> { o = constructor.get(); trackedObjects.put(id, o); } - o.read(data, lookup); + + o.read(new RegistryByteBuf(Unpooled.wrappedBuffer(data), lookup), lookup); }); quickAccess = Map.copyOf(trackedObjects); } diff --git a/src/main/java/com/minelittlepony/unicopia/network/track/TrackableObject.java b/src/main/java/com/minelittlepony/unicopia/network/track/TrackableObject.java index ee76189d..b50b9e79 100644 --- a/src/main/java/com/minelittlepony/unicopia/network/track/TrackableObject.java +++ b/src/main/java/com/minelittlepony/unicopia/network/track/TrackableObject.java @@ -5,13 +5,14 @@ import java.util.Optional; import io.netty.buffer.ByteBuf; import io.netty.buffer.Unpooled; import net.minecraft.nbt.NbtCompound; +import net.minecraft.network.RegistryByteBuf; import net.minecraft.network.codec.PacketCodecs; import net.minecraft.registry.RegistryWrapper.WrapperLookup; public interface TrackableObject> { Status getStatus(); - default void read(ByteBuf buffer, WrapperLookup lookup) { + default void read(RegistryByteBuf buffer, WrapperLookup lookup) { readTrackedNbt(PacketCodecs.NBT_COMPOUND.decode(buffer), lookup); } diff --git a/src/main/java/com/minelittlepony/unicopia/util/RegistryUtils.java b/src/main/java/com/minelittlepony/unicopia/util/RegistryUtils.java index 4c7b003b..7d7dce90 100644 --- a/src/main/java/com/minelittlepony/unicopia/util/RegistryUtils.java +++ b/src/main/java/com/minelittlepony/unicopia/util/RegistryUtils.java @@ -8,6 +8,7 @@ import com.google.common.base.Predicates; import com.mojang.serialization.Lifecycle; import net.fabricmc.fabric.api.event.registry.FabricRegistryBuilder; +import net.fabricmc.fabric.api.event.registry.RegistryAttribute; import net.minecraft.registry.tag.TagKey; import net.minecraft.util.Identifier; import net.minecraft.util.Util; @@ -22,8 +23,16 @@ public interface RegistryUtils { return FabricRegistryBuilder.from(new SimpleRegistry(RegistryKey.ofRegistry(id), Lifecycle.stable())).buildAndRegister(); } - static Registry createDefaulted(Identifier id, String def) { - return FabricRegistryBuilder.from(new SimpleDefaultedRegistry(def, RegistryKey.ofRegistry(id), Lifecycle.stable(), false)).buildAndRegister(); + static Registry createSynced(Identifier id) { + return FabricRegistryBuilder.from(new SimpleRegistry(RegistryKey.ofRegistry(id), Lifecycle.stable())) + .attribute(RegistryAttribute.SYNCED) + .buildAndRegister(); + } + + static Registry createSynced(Identifier id, String def) { + return FabricRegistryBuilder.from(new SimpleDefaultedRegistry(def, RegistryKey.ofRegistry(id), Lifecycle.stable(), false)) + .attribute(RegistryAttribute.SYNCED) + .buildAndRegister(); } static RegistryEntryList entriesForTag(World world, TagKey key) { diff --git a/src/main/java/com/minelittlepony/unicopia/util/serialization/PacketCodecUtils.java b/src/main/java/com/minelittlepony/unicopia/util/serialization/PacketCodecUtils.java index ed88e7f4..9eddb1a5 100644 --- a/src/main/java/com/minelittlepony/unicopia/util/serialization/PacketCodecUtils.java +++ b/src/main/java/com/minelittlepony/unicopia/util/serialization/PacketCodecUtils.java @@ -9,9 +9,7 @@ import java.util.function.Function; import java.util.function.IntFunction; import io.netty.buffer.ByteBuf; -import io.netty.buffer.Unpooled; import net.minecraft.network.PacketByteBuf; -import net.minecraft.network.RegistryByteBuf; import net.minecraft.network.codec.PacketCodec; import net.minecraft.network.codec.PacketCodecs; import net.minecraft.registry.RegistryKey; @@ -21,17 +19,6 @@ import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.Vec3d; public interface PacketCodecUtils { - PacketCodec REGISTRY_BUFFER = PacketCodec.of((bytes, buffer) -> { - int length = bytes.readableBytes(); - buffer.writeInt(length); - if (length > 0) { - buffer.writeBytes(bytes, 0, length); - } - }, buffer -> { - int length = buffer.readInt(); - return new RegistryByteBuf(length > 0 ? buffer.readBytes(length) : Unpooled.EMPTY_BUFFER, buffer.getRegistryManager()); - }); - PacketCodec> REGISTRY_KEY = PacketCodec.tuple( Identifier.PACKET_CODEC, RegistryKey::getRegistry, Identifier.PACKET_CODEC, RegistryKey::getValue, diff --git a/src/main/resources/unicopia.mixin.json b/src/main/resources/unicopia.mixin.json index 3dfd23a1..20215020 100644 --- a/src/main/resources/unicopia.mixin.json +++ b/src/main/resources/unicopia.mixin.json @@ -85,7 +85,8 @@ "seasons.MixinFertilizableUtil", "ad_astra.MixinApiHelper", "ad_astra.MixinOxygenApi", - "forgified.MixinIForgeBoat" + "forgified.MixinIForgeBoat", + "forgified.MixinPacketAndPayloadAcceptor" ], "client": [ "client.MixinAnimalModel",