diff --git a/polymer-core/src/main/java/eu/pb4/polymer/core/api/other/PolymerSoundEvent.java b/polymer-core/src/main/java/eu/pb4/polymer/core/api/other/PolymerSoundEvent.java index 77a7a2c6..a0505d6f 100644 --- a/polymer-core/src/main/java/eu/pb4/polymer/core/api/other/PolymerSoundEvent.java +++ b/polymer-core/src/main/java/eu/pb4/polymer/core/api/other/PolymerSoundEvent.java @@ -15,7 +15,7 @@ * This class allows for creation of custom sound effects * It can be used to play custom sounds for players with resourcepack while keeping fallback for vanilla clients */ -/*public class PolymerSoundEvent extends SoundEvent implements PolymerSyncedObject { +public class PolymerSoundEvent implements PolymerSyncedObject { @Nullable protected final SoundEvent polymerSound; @@ -39,7 +39,6 @@ static PolymerSoundEvent of(UUID uuid, Identifier identifier, float distanceToTr } public PolymerSoundEvent(@Nullable UUID uuid, Identifier id, float distanceToTravel, boolean useStaticDistance, @Nullable SoundEvent vanillaEvent) { - super(id, distanceToTravel, useStaticDistance); this.source = uuid; this.polymerSound = vanillaEvent; } @@ -48,4 +47,4 @@ public PolymerSoundEvent(@Nullable UUID uuid, Identifier id, float distanceToTra public SoundEvent getPolymerReplacement(PacketContext context) { return this.source == null || this.polymerSound == null || PolymerUtils.hasResourcePack(context.getPlayer(), this.source) ? this : (this.polymerSound instanceof PolymerSoundEvent pe ? pe.getPolymerReplacement(context) : this.polymerSound); } -}*/ +} diff --git a/polymer-core/src/main/java/eu/pb4/polymer/core/api/utils/PolymerSyncedObject.java b/polymer-core/src/main/java/eu/pb4/polymer/core/api/utils/PolymerSyncedObject.java index aede94f5..6ceb2f38 100644 --- a/polymer-core/src/main/java/eu/pb4/polymer/core/api/utils/PolymerSyncedObject.java +++ b/polymer-core/src/main/java/eu/pb4/polymer/core/api/utils/PolymerSyncedObject.java @@ -1,5 +1,6 @@ package eu.pb4.polymer.core.api.utils; +import eu.pb4.polymer.core.impl.interfaces.RegistryExtension; import net.minecraft.registry.Registry; import net.minecraft.server.network.ServerPlayerEntity; import org.jetbrains.annotations.Nullable; @@ -10,7 +11,6 @@ */ public interface PolymerSyncedObject extends PolymerObject { - /** * Generic method to get polymer replacement sent to player * @@ -35,6 +35,21 @@ default boolean canSyncRawToClient(PacketContext context) { } static boolean canSyncRawToClient(Registry registry, T obj, PacketContext context) { - return obj instanceof PolymerSyncedObject pol ? pol.canSyncRawToClient(context) : !PolymerUtils.isServerOnly(registry, obj); + var pol = getSyncedObjectDefinition(registry, obj); + return pol != null ? pol.canSyncRawToClient(context) : !PolymerUtils.isServerOnly(registry, obj); + } + + @Nullable + static PolymerSyncedObject getSyncedObjectDefinition(@Nullable Registry registry, T obj) { + if (obj instanceof PolymerSyncedObject instance) { + //noinspection unchecked + return (PolymerSyncedObject) instance; + } + return registry instanceof RegistryExtension extension ? ((RegistryExtension) extension).polymer$getOverlay(obj) : null; + } + + static boolean canSynchronizeToPolymerClient(Registry registry, T entry, PacketContext.NotNullWithPlayer ctx) { + var obj = getSyncedObjectDefinition(registry, entry); + return obj == null || obj.canSynchronizeToPolymerClient(ctx); } } diff --git a/polymer-core/src/main/java/eu/pb4/polymer/core/impl/interfaces/RegistryExtension.java b/polymer-core/src/main/java/eu/pb4/polymer/core/impl/interfaces/RegistryExtension.java index fa3bc80e..ba1b92ea 100644 --- a/polymer-core/src/main/java/eu/pb4/polymer/core/impl/interfaces/RegistryExtension.java +++ b/polymer-core/src/main/java/eu/pb4/polymer/core/impl/interfaces/RegistryExtension.java @@ -1,5 +1,6 @@ package eu.pb4.polymer.core.impl.interfaces; +import eu.pb4.polymer.core.api.utils.PolymerSyncedObject; import net.minecraft.registry.Registry; import net.minecraft.registry.entry.RegistryEntryList; import net.minecraft.registry.tag.TagKey; @@ -12,7 +13,9 @@ static List getPolymerEntries(Registry registry) { return ((RegistryExtension) registry).polymer$getEntries(); } - Map, RegistryEntryList.Named> polymer$getTagsInternal(); List polymer$getEntries(); + + void polymer$setOverlay(T value, PolymerSyncedObject syncedObject); + PolymerSyncedObject polymer$getOverlay(T value); } diff --git a/polymer-core/src/main/java/eu/pb4/polymer/core/impl/networking/PolymerServerProtocol.java b/polymer-core/src/main/java/eu/pb4/polymer/core/impl/networking/PolymerServerProtocol.java index 51284c1f..2568f074 100644 --- a/polymer-core/src/main/java/eu/pb4/polymer/core/impl/networking/PolymerServerProtocol.java +++ b/polymer-core/src/main/java/eu/pb4/polymer/core/impl/networking/PolymerServerProtocol.java @@ -269,15 +269,18 @@ private static void sendSync(ServerPlayNetworkHandler handler, CustomPayl private static void sendSync(ServerPlayNetworkHandler handler, CustomPayload.Id> packetId, Iterable iterable, boolean bypassPolymerCheck, BufferWritableCreator writableFunction) { var version = PolymerServerNetworking.getSupportedVersion(handler, packetId.id()); + Registry registry = null; + if (iterable instanceof RegistryExtension && !bypassPolymerCheck) { iterable = ((RegistryExtension) iterable).polymer$getEntries(); + registry = (Registry) iterable; } if (version != -1) { var entries = new ArrayList(); var ctx = PacketContext.create(handler); for (var entry : iterable) { - if (!bypassPolymerCheck || (entry instanceof PolymerSyncedObject obj && obj.canSynchronizeToPolymerClient(ctx))) { + if (!bypassPolymerCheck || PolymerSyncedObject.canSynchronizeToPolymerClient(registry, entry, ctx)) { var val = writableFunction.serialize(entry, handler, version); if (val != null) { entries.add(val); diff --git a/polymer-core/src/main/java/eu/pb4/polymer/core/mixin/other/PacketCodecsEntriesMixin.java b/polymer-core/src/main/java/eu/pb4/polymer/core/mixin/other/PacketCodecsEntriesMixin.java index 5ff3019b..a89c6f00 100644 --- a/polymer-core/src/main/java/eu/pb4/polymer/core/mixin/other/PacketCodecsEntriesMixin.java +++ b/polymer-core/src/main/java/eu/pb4/polymer/core/mixin/other/PacketCodecsEntriesMixin.java @@ -22,8 +22,10 @@ public abstract class PacketCodecsEntriesMixin { @ModifyVariable(method = "encode(Lio/netty/buffer/ByteBuf;Ljava/lang/Object;)V", at = @At("HEAD"), argsOnly = true) private Object polymer$changeData(Object val, ByteBuf buf) { var player = PacketContext.get(); - - if (val instanceof PolymerSyncedObject polymerSyncedObject) { + + + var polymerSyncedObject = PolymerSyncedObject.getSyncedObjectDefinition(null, val); + if (polymerSyncedObject != null) { var obj = polymerSyncedObject.getPolymerReplacement(player); if (obj != null) { diff --git a/polymer-core/src/main/java/eu/pb4/polymer/core/mixin/other/PacketCodecsRegistryEntryListMixin.java b/polymer-core/src/main/java/eu/pb4/polymer/core/mixin/other/PacketCodecsRegistryEntryListMixin.java index 05ed5ae3..1d026d17 100644 --- a/polymer-core/src/main/java/eu/pb4/polymer/core/mixin/other/PacketCodecsRegistryEntryListMixin.java +++ b/polymer-core/src/main/java/eu/pb4/polymer/core/mixin/other/PacketCodecsRegistryEntryListMixin.java @@ -1,48 +1,56 @@ package eu.pb4.polymer.core.mixin.other; +import eu.pb4.polymer.core.api.other.PolymerSoundEvent; import eu.pb4.polymer.core.api.utils.PolymerObject; import eu.pb4.polymer.core.api.utils.PolymerSyncedObject; import eu.pb4.polymer.core.api.utils.PolymerUtils; import eu.pb4.polymer.rsm.api.RegistrySyncUtils; import net.minecraft.network.RegistryByteBuf; import net.minecraft.registry.Registries; +import net.minecraft.registry.RegistryKey; import net.minecraft.registry.entry.RegistryEntry; import net.minecraft.registry.entry.RegistryEntryList; import net.minecraft.sound.SoundEvent; +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.ModifyVariable; import xyz.nucleoid.packettweaker.PacketContext; import java.util.ArrayList; +import java.util.List; +@SuppressWarnings({"rawtypes", "unchecked"}) @Mixin(targets = "net/minecraft/network/codec/PacketCodecs$20", priority = 500) public abstract class PacketCodecsRegistryEntryListMixin { + @Shadow @Final private RegistryKey field_54511; + @ModifyVariable(method = "encode(Lnet/minecraft/network/RegistryByteBuf;Lnet/minecraft/registry/entry/RegistryEntryList;)V", at = @At("HEAD"), argsOnly = true) private RegistryEntryList polymer$changeData(RegistryEntryList registryEntryList, RegistryByteBuf registryByteBuf) { if (registryEntryList.getTagKey().isEmpty()) { var player = PacketContext.get(); var arr = new ArrayList(); + var reg = registryByteBuf.getRegistryManager().getOrThrow(this.field_54511); for (var i = 0; i < registryEntryList.size(); i++) { var val = registryEntryList.get(i); - /*if (val.value() instanceof PolymerSoundEvent syncedObject) { - var replacement = syncedObject.getPolymerReplacement(player); - if (replacement instanceof PolymerSoundEvent) { - arr.add(RegistryEntry.of(replacement)); + var obj = PolymerSyncedObject.getSyncedObjectDefinition(reg, val); + if (obj instanceof PolymerSoundEvent syncedObject) { + var replacement = syncedObject.getPolymerReplacement(player); + if (replacement != null) { + arr.add(reg.getEntry(replacement)); } - - arr.add(Registries.SOUND_EVENT.getEntry(replacement)); - } */ - - if (val.value() instanceof SoundEvent soundEvent && RegistrySyncUtils.isServerEntry(Registries.SOUND_EVENT, soundEvent)) { + } else if (val.value() instanceof SoundEvent soundEvent && RegistrySyncUtils.isServerEntry(Registries.SOUND_EVENT, soundEvent)) { arr.add(RegistryEntry.of(val.value())); - } else if ((val.value() instanceof PolymerSyncedObject s && s.canSyncRawToClient(player)) || !(val.value() instanceof PolymerObject)) { + } else if ((obj != null && obj.canSyncRawToClient(player)) || !(val.value() instanceof PolymerObject)) { arr.add(val); } } + + return RegistryEntryList.of((List) arr); } return registryEntryList; diff --git a/polymer-core/src/main/java/eu/pb4/polymer/core/mixin/other/PacketCodecsRegistryMixin.java b/polymer-core/src/main/java/eu/pb4/polymer/core/mixin/other/PacketCodecsRegistryMixin.java index 19af6331..ff7d4e1c 100644 --- a/polymer-core/src/main/java/eu/pb4/polymer/core/mixin/other/PacketCodecsRegistryMixin.java +++ b/polymer-core/src/main/java/eu/pb4/polymer/core/mixin/other/PacketCodecsRegistryMixin.java @@ -22,25 +22,32 @@ public abstract class PacketCodecsRegistryMixin { @ModifyVariable(method = "encode(Lnet/minecraft/network/RegistryByteBuf;Ljava/lang/Object;)V", at = @At("HEAD"), argsOnly = true) private Object polymer$changeData(Object val, RegistryByteBuf buf) { var player = PacketContext.get(); + //noinspection unchecked + var reg = buf.getRegistryManager().getOrThrow(this.field_53746); - if (val instanceof PolymerSyncedObject polymerSyncedObject) { - var obj = polymerSyncedObject.getPolymerReplacement(player); - if (obj != null) { - return obj; - } - } else if (val instanceof RegistryEntry registryEntry) { + if (val instanceof RegistryEntry registryEntry) { var value = registryEntry.value(); - if (value instanceof PolymerSyncedObject polymerSyncedObject) { - var obj = polymerSyncedObject.getPolymerReplacement(player); + var obj = PolymerSyncedObject.getSyncedObjectDefinition(reg, value); - if (obj != null) { + if (obj != null) { + var replacement = obj.getPolymerReplacement(player); + + if (replacement != null) { //noinspection unchecked - return buf.getRegistryManager().getOrThrow(this.field_53746).getEntry(obj); + return reg.getEntry(replacement); } } - } + } else { + var obj = PolymerSyncedObject.getSyncedObjectDefinition(reg, val); + if (obj != null) { + var replacement = obj.getPolymerReplacement(player); + if (replacement != null) { + return replacement; + } + } + } return val; } diff --git a/polymer-core/src/main/java/eu/pb4/polymer/core/mixin/other/RegistryFixedCodecMixin.java b/polymer-core/src/main/java/eu/pb4/polymer/core/mixin/other/RegistryFixedCodecMixin.java index 27a9b842..7444be20 100644 --- a/polymer-core/src/main/java/eu/pb4/polymer/core/mixin/other/RegistryFixedCodecMixin.java +++ b/polymer-core/src/main/java/eu/pb4/polymer/core/mixin/other/RegistryFixedCodecMixin.java @@ -1,5 +1,7 @@ package eu.pb4.polymer.core.mixin.other; +import com.llamalad7.mixinextras.sugar.Local; +import com.mojang.serialization.DynamicOps; import eu.pb4.polymer.common.api.PolymerCommonUtils; import eu.pb4.polymer.core.api.entity.PolymerEntityUtils; import eu.pb4.polymer.core.api.utils.PolymerSyncedObject; @@ -10,6 +12,7 @@ import net.minecraft.registry.Registries; import net.minecraft.registry.Registry; import net.minecraft.registry.RegistryKey; +import net.minecraft.registry.RegistryOps; import net.minecraft.registry.entry.RegistryEntry; import net.minecraft.registry.entry.RegistryFixedCodec; import org.spongepowered.asm.mixin.Final; @@ -29,8 +32,9 @@ public class RegistryFixedCodecMixin { method = "encode(Lnet/minecraft/registry/entry/RegistryEntry;Lcom/mojang/serialization/DynamicOps;Ljava/lang/Object;)Lcom/mojang/serialization/DataResult;", at = @At("HEAD") ) - private RegistryEntry polymerCore$swapEntry(RegistryEntry entry) { + private RegistryEntry polymerCore$swapEntry(RegistryEntry entry, @Local(argsOnly = true) DynamicOps ops) { if (PolymerCommonUtils.isServerNetworkingThread()) { + // Todo var player = PacketContext.get(); try { if (entry.value() instanceof PolymerSyncedObject polymerSyncedObject) { diff --git a/polymer-core/src/main/java/eu/pb4/polymer/core/mixin/other/SimpleRegistryMixin.java b/polymer-core/src/main/java/eu/pb4/polymer/core/mixin/other/SimpleRegistryMixin.java index 41dd17af..c963ee59 100644 --- a/polymer-core/src/main/java/eu/pb4/polymer/core/mixin/other/SimpleRegistryMixin.java +++ b/polymer-core/src/main/java/eu/pb4/polymer/core/mixin/other/SimpleRegistryMixin.java @@ -2,10 +2,12 @@ import com.mojang.serialization.Lifecycle; import eu.pb4.polymer.core.api.utils.PolymerObject; +import eu.pb4.polymer.core.api.utils.PolymerSyncedObject; import eu.pb4.polymer.core.impl.ImplPolymerRegistryEvent; import eu.pb4.polymer.core.impl.PolymerImplUtils; import eu.pb4.polymer.core.impl.interfaces.RegistryExtension; import eu.pb4.polymer.rsm.api.RegistrySyncUtils; +import joptsimple.internal.AbbreviationMap; import net.minecraft.recipe.RecipeSerializer; import net.minecraft.registry.Registry; import net.minecraft.registry.RegistryKey; @@ -23,8 +25,8 @@ import org.spongepowered.asm.mixin.injection.Inject; import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; -import java.lang.reflect.ParameterizedType; import java.util.ArrayList; +import java.util.IdentityHashMap; import java.util.List; import java.util.Map; @@ -34,6 +36,8 @@ public abstract class SimpleRegistryMixin implements RegistryExtension, Re @Nullable @Unique private List polymer$objects = null; + @Unique + private final IdentityHashMap> overlays = new IdentityHashMap<>(); @Inject(method = "add", at = @At("TAIL")) private void polymer$storeStatus(RegistryKey key, T value, RegistryEntryInfo info, CallbackInfoReturnable> cir) { @@ -63,4 +67,14 @@ public abstract class SimpleRegistryMixin implements RegistryExtension, Re public Map, RegistryEntryList.Named> polymer$getTagsInternal() { return this.tags; } + + @Override + public void polymer$setOverlay(T value, PolymerSyncedObject syncedObject) { + this.overlays.put(value, syncedObject); + } + + @Override + public PolymerSyncedObject polymer$getOverlay(T value) { + return this.overlays.get(value); + } }