From 529309b4f4421bd52f940ff0ed3bec918f22971b Mon Sep 17 00:00:00 2001 From: Patbox <39821509+Patbox@users.noreply.github.com> Date: Fri, 25 Jun 2021 12:55:44 +0200 Subject: [PATCH] Fix issues on client and block id desync --- gradle.properties | 4 +-- .../mixin/block/BlockEventS2CPacketMixin.java | 8 +++-- .../EntityTrackerUpdateS2CPacketMixin.java | 20 +++++++------ .../EntityEquipmentUpdateS2CPacketMixin.java | 14 +++++---- .../mixin/item/InventoryS2CPacketMixin.java | 14 +++++---- .../mixin/item/PacketByteBufMixin.java | 12 ++++++++ ...ScreenHandlerSlotUpdateS2CPacketMixin.java | 6 ++-- .../mixin/other/SimpleRegistryMixin.java | 29 +++++++++++++++++++ src/main/java/eu/pb4/polymer/other/Event.java | 23 +++++++++++++++ .../resourcepack/ResourcePackUtils.java | 19 +++--------- src/main/resources/polymer.mixins.json | 1 + 11 files changed, 107 insertions(+), 43 deletions(-) create mode 100644 src/main/java/eu/pb4/polymer/mixin/other/SimpleRegistryMixin.java create mode 100644 src/main/java/eu/pb4/polymer/other/Event.java diff --git a/gradle.properties b/gradle.properties index cba2fd01..bf21d7ad 100644 --- a/gradle.properties +++ b/gradle.properties @@ -8,10 +8,10 @@ org.gradle.jvmargs=-Xmx1G loader_version=0.11.3 # Mod Properties - mod_version = 0.1.0-pre4-1.17 + mod_version = 0.1.0-pre5-1.17 maven_group = eu.pb4 archives_base_name = polymer # Dependencies # currently not on the main fabric site, check on the maven: https://maven.fabricmc.net/net/fabricmc/fabric-api/fabric-api - fabric_version=0.34.8+1.17 + fabric_version=0.36.0+1.17 diff --git a/src/main/java/eu/pb4/polymer/mixin/block/BlockEventS2CPacketMixin.java b/src/main/java/eu/pb4/polymer/mixin/block/BlockEventS2CPacketMixin.java index 16def575..d9fe906b 100644 --- a/src/main/java/eu/pb4/polymer/mixin/block/BlockEventS2CPacketMixin.java +++ b/src/main/java/eu/pb4/polymer/mixin/block/BlockEventS2CPacketMixin.java @@ -28,9 +28,11 @@ public class BlockEventS2CPacketMixin { @Environment(EnvType.CLIENT) @Inject(method = "getBlock", at = @At("TAIL"), cancellable = true) private void replaceBlockClient(CallbackInfoReturnable cir) { - ServerPlayerEntity player = MinecraftClient.getInstance().getServer().getPlayerManager().getPlayer(MinecraftClient.getInstance().player.getUuid()); - if (this.oldBlock instanceof VirtualBlock virtualBlock) { - cir.setReturnValue(virtualBlock.getVirtualBlock(this.pos, player.getServerWorld())); + if (MinecraftClient.getInstance().getServer() != null) { + ServerPlayerEntity player = MinecraftClient.getInstance().getServer().getPlayerManager().getPlayer(MinecraftClient.getInstance().player.getUuid()); + if (this.oldBlock instanceof VirtualBlock virtualBlock) { + cir.setReturnValue(virtualBlock.getVirtualBlock(this.pos, player.getServerWorld())); + } } } diff --git a/src/main/java/eu/pb4/polymer/mixin/entity/EntityTrackerUpdateS2CPacketMixin.java b/src/main/java/eu/pb4/polymer/mixin/entity/EntityTrackerUpdateS2CPacketMixin.java index 8d08b197..ff8cdb49 100644 --- a/src/main/java/eu/pb4/polymer/mixin/entity/EntityTrackerUpdateS2CPacketMixin.java +++ b/src/main/java/eu/pb4/polymer/mixin/entity/EntityTrackerUpdateS2CPacketMixin.java @@ -64,16 +64,18 @@ private void removeInvalidEntries(int id, DataTracker tracker, boolean forceUpda @Environment(EnvType.CLIENT) @Inject(method = "getTrackedValues", at = @At("RETURN"), cancellable = true) private void replaceItemsWithVirtualOnes(CallbackInfoReturnable>> cir) { - List> list = new ArrayList<>(); - ServerPlayerEntity player = MinecraftClient.getInstance().getServer().getPlayerManager().getPlayer(MinecraftClient.getInstance().player.getUuid()); + if (MinecraftClient.getInstance().getServer() != null) { + List> list = new ArrayList<>(); + ServerPlayerEntity player = MinecraftClient.getInstance().getServer().getPlayerManager().getPlayer(MinecraftClient.getInstance().player.getUuid()); - for (DataTracker.Entry entry : cir.getReturnValue()) { - if (entry.get() instanceof ItemStack stack) { - list.add(new DataTracker.Entry(entry.getData(), ItemHelper.getVirtualItemStack(stack, player))); - } else { - list.add(entry); + for (DataTracker.Entry entry : cir.getReturnValue()) { + if (entry.get() instanceof ItemStack stack) { + list.add(new DataTracker.Entry(entry.getData(), ItemHelper.getVirtualItemStack(stack, player))); + } else { + list.add(entry); + } } - } - cir.setReturnValue(list); + cir.setReturnValue(list); + } }} diff --git a/src/main/java/eu/pb4/polymer/mixin/item/EntityEquipmentUpdateS2CPacketMixin.java b/src/main/java/eu/pb4/polymer/mixin/item/EntityEquipmentUpdateS2CPacketMixin.java index 3d8d025a..46191f6a 100644 --- a/src/main/java/eu/pb4/polymer/mixin/item/EntityEquipmentUpdateS2CPacketMixin.java +++ b/src/main/java/eu/pb4/polymer/mixin/item/EntityEquipmentUpdateS2CPacketMixin.java @@ -23,14 +23,16 @@ public class EntityEquipmentUpdateS2CPacketMixin { @Environment(EnvType.CLIENT) @Inject(method = "getEquipmentList", at = @At("RETURN"), cancellable = true) private void replaceItemsWithVirtualOnes(CallbackInfoReturnable>> cir) { - List> list = new ArrayList<>(); - ServerPlayerEntity player = MinecraftClient.getInstance().getServer().getPlayerManager().getPlayer(MinecraftClient.getInstance().player.getUuid()); + if (MinecraftClient.getInstance().getServer() != null) { + List> list = new ArrayList<>(); + ServerPlayerEntity player = MinecraftClient.getInstance().getServer().getPlayerManager().getPlayer(MinecraftClient.getInstance().player.getUuid()); - for (Pair pair : cir.getReturnValue()) { - list.add(new Pair<>(pair.getFirst(), ItemHelper.getVirtualItemStack(pair.getSecond(), player))); - } + for (Pair pair : cir.getReturnValue()) { + list.add(new Pair<>(pair.getFirst(), ItemHelper.getVirtualItemStack(pair.getSecond(), player))); + } - cir.setReturnValue(list); + cir.setReturnValue(list); + } } } diff --git a/src/main/java/eu/pb4/polymer/mixin/item/InventoryS2CPacketMixin.java b/src/main/java/eu/pb4/polymer/mixin/item/InventoryS2CPacketMixin.java index 8df375be..2b5c6088 100644 --- a/src/main/java/eu/pb4/polymer/mixin/item/InventoryS2CPacketMixin.java +++ b/src/main/java/eu/pb4/polymer/mixin/item/InventoryS2CPacketMixin.java @@ -21,13 +21,15 @@ public class InventoryS2CPacketMixin { @Environment(EnvType.CLIENT) @Inject(method = "getContents", at = @At("RETURN"), cancellable = true) private void replaceItemsWithVirtualOnes(CallbackInfoReturnable> cir) { - List list = new ArrayList<>(); - ServerPlayerEntity player = MinecraftClient.getInstance().getServer().getPlayerManager().getPlayer(MinecraftClient.getInstance().player.getUuid()); + if (MinecraftClient.getInstance().getServer() != null) { + List list = new ArrayList<>(); + ServerPlayerEntity player = MinecraftClient.getInstance().getServer().getPlayerManager().getPlayer(MinecraftClient.getInstance().player.getUuid()); - for (ItemStack stack : cir.getReturnValue()) { - list.add(ItemHelper.getVirtualItemStack(stack, player)); - } + for (ItemStack stack : cir.getReturnValue()) { + list.add(ItemHelper.getVirtualItemStack(stack, player)); + } - cir.setReturnValue(list); + cir.setReturnValue(list); + } } } diff --git a/src/main/java/eu/pb4/polymer/mixin/item/PacketByteBufMixin.java b/src/main/java/eu/pb4/polymer/mixin/item/PacketByteBufMixin.java index 52d3a473..8fd45ea1 100644 --- a/src/main/java/eu/pb4/polymer/mixin/item/PacketByteBufMixin.java +++ b/src/main/java/eu/pb4/polymer/mixin/item/PacketByteBufMixin.java @@ -1,6 +1,9 @@ package eu.pb4.polymer.mixin.item; import eu.pb4.polymer.item.ItemHelper; +import net.fabricmc.api.EnvType; +import net.fabricmc.api.Environment; +import net.minecraft.client.MinecraftClient; import net.minecraft.item.ItemStack; import net.minecraft.network.PacketByteBuf; import org.spongepowered.asm.mixin.Mixin; @@ -17,8 +20,17 @@ private ItemStack replaceWithVanillaItem(ItemStack itemStack) { return ItemHelper.getVirtualItemStack(itemStack, PacketContext.get().getTarget()); } + @Environment(EnvType.SERVER) @Inject(method = "readItemStack", at = @At("RETURN"), cancellable = true) private void replaceWithRealItem(CallbackInfoReturnable cir) { cir.setReturnValue(ItemHelper.getRealItemStack(cir.getReturnValue())); } + + @Environment(EnvType.CLIENT) + @Inject(method = "readItemStack", at = @At("RETURN"), cancellable = true) + private void replaceWithRealItemClient(CallbackInfoReturnable cir) { + if (MinecraftClient.getInstance().getServer() != null) { + cir.setReturnValue(ItemHelper.getRealItemStack(cir.getReturnValue())); + } + } } \ No newline at end of file diff --git a/src/main/java/eu/pb4/polymer/mixin/item/ScreenHandlerSlotUpdateS2CPacketMixin.java b/src/main/java/eu/pb4/polymer/mixin/item/ScreenHandlerSlotUpdateS2CPacketMixin.java index 1dbdefa1..baf99acb 100644 --- a/src/main/java/eu/pb4/polymer/mixin/item/ScreenHandlerSlotUpdateS2CPacketMixin.java +++ b/src/main/java/eu/pb4/polymer/mixin/item/ScreenHandlerSlotUpdateS2CPacketMixin.java @@ -19,7 +19,9 @@ public class ScreenHandlerSlotUpdateS2CPacketMixin { @Environment(EnvType.CLIENT) @Inject(method = "getItemStack", at = @At("RETURN"), cancellable = true) private void replaceItemsWithVirtualOnes(CallbackInfoReturnable cir) { - ServerPlayerEntity player = MinecraftClient.getInstance().getServer().getPlayerManager().getPlayer(MinecraftClient.getInstance().player.getUuid()); - cir.setReturnValue(ItemHelper.getVirtualItemStack(cir.getReturnValue(), player)); + if (MinecraftClient.getInstance().getServer() != null) { + ServerPlayerEntity player = MinecraftClient.getInstance().getServer().getPlayerManager().getPlayer(MinecraftClient.getInstance().player.getUuid()); + cir.setReturnValue(ItemHelper.getVirtualItemStack(cir.getReturnValue(), player)); + } } } diff --git a/src/main/java/eu/pb4/polymer/mixin/other/SimpleRegistryMixin.java b/src/main/java/eu/pb4/polymer/mixin/other/SimpleRegistryMixin.java new file mode 100644 index 00000000..8543b263 --- /dev/null +++ b/src/main/java/eu/pb4/polymer/mixin/other/SimpleRegistryMixin.java @@ -0,0 +1,29 @@ +package eu.pb4.polymer.mixin.other; + +import com.mojang.serialization.Lifecycle; +import eu.pb4.polymer.interfaces.VirtualObject; +import net.minecraft.util.registry.RegistryKey; +import net.minecraft.util.registry.SimpleRegistry; +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.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; + + +@Mixin(SimpleRegistry.class) +public abstract class SimpleRegistryMixin { + @Shadow private int nextId; + + @Shadow public abstract V set(int rawId, RegistryKey key, V entry, Lifecycle lifecycle); + + @Inject(method = "add", at = @At("HEAD"), cancellable = true) + private void moveVirtualObjectToTheEnd(RegistryKey key, V entry, Lifecycle lifecycle, CallbackInfoReturnable cir) { + if (entry instanceof VirtualObject) { + int current = this.nextId; + this.set(current + 1000000, key, entry, lifecycle); + this.nextId = current + 1; + cir.setReturnValue(entry); + } + } +} diff --git a/src/main/java/eu/pb4/polymer/other/Event.java b/src/main/java/eu/pb4/polymer/other/Event.java new file mode 100644 index 00000000..4009861a --- /dev/null +++ b/src/main/java/eu/pb4/polymer/other/Event.java @@ -0,0 +1,23 @@ +package eu.pb4.polymer.other; + +import java.util.ArrayList; +import java.util.List; + +public class Event { + private List> handlers = new ArrayList<>(); + + public void register(EventHandler event) { + this.handlers.add(event); + } + + public void invoke(T obj) { + for (EventHandler consumer : this.handlers) { + consumer.call(obj); + } + } + + + public interface EventHandler { + void call(T obj); + } +} diff --git a/src/main/java/eu/pb4/polymer/resourcepack/ResourcePackUtils.java b/src/main/java/eu/pb4/polymer/resourcepack/ResourcePackUtils.java index adb6c185..dee82533 100644 --- a/src/main/java/eu/pb4/polymer/resourcepack/ResourcePackUtils.java +++ b/src/main/java/eu/pb4/polymer/resourcepack/ResourcePackUtils.java @@ -2,10 +2,9 @@ import com.google.gson.JsonParser; import eu.pb4.polymer.PolymerMod; +import eu.pb4.polymer.other.Event; import it.unimi.dsi.fastutil.objects.Object2ObjectArrayMap; import it.unimi.dsi.fastutil.objects.Object2ObjectMap; -import net.fabricmc.fabric.api.event.Event; -import net.fabricmc.fabric.api.event.EventFactory; import net.fabricmc.loader.api.FabricLoader; import net.minecraft.item.Item; import net.minecraft.util.Identifier; @@ -20,7 +19,7 @@ public class ResourcePackUtils { private static final Object2ObjectMap> ITEMS = new Object2ObjectArrayMap<>(); private static final Set MOD_IDS = new HashSet<>(); - private static int CMD_OFFSET = PolymerMod.POLYMC_COMPAT ? 1000 : 1; + private static int CMD_OFFSET = PolymerMod.POLYMC_COMPAT ? 100000 : 1; /** * This method can be used to register custom model data for items @@ -63,7 +62,7 @@ public static boolean build(Path path) { boolean successful = true; RPBuilder builder = new DefaultRPBuilder(path); - RESOURCE_PACK_CREATION_EVENT.invoker().call(builder); + RESOURCE_PACK_CREATION_EVENT.invoke(builder); for (String modId : MOD_IDS) { successful = builder.copyModAssets(modId) && successful; @@ -91,15 +90,5 @@ public static boolean build(Path path) { } } - - public static final Event RESOURCE_PACK_CREATION_EVENT = EventFactory.createArrayBacked(ResourcePackEvent.class, (callbacks) -> (builder) -> { - for(ResourcePackEvent callback : callbacks) { - callback.call(builder); - } - }); - - @FunctionalInterface - public interface ResourcePackEvent { - void call(RPBuilder builder); - } + public static final Event RESOURCE_PACK_CREATION_EVENT = new Event<>(); } diff --git a/src/main/resources/polymer.mixins.json b/src/main/resources/polymer.mixins.json index 4e9c5649..79c44664 100644 --- a/src/main/resources/polymer.mixins.json +++ b/src/main/resources/polymer.mixins.json @@ -36,6 +36,7 @@ "other.CommandManagerMixin", "other.DimensionTypeAccessor", "other.RegistrySyncManagerMixin", + "other.SimpleRegistryMixin", "other.SynchronizeRecipesS2CPacketMixin", "polymc.BlockPolyGeneratorMixin" ],