From c3584df9eade78116e2222125b6a06801c24750f Mon Sep 17 00:00:00 2001 From: Patbox <39821509+Patbox@users.noreply.github.com> Date: Tue, 27 Aug 2024 22:13:37 +0200 Subject: [PATCH] Fix suspicious sand not showing polymer items, call syncState() instead of constructing packet incorrectly, breaking chest uis --- gradle.properties | 4 +-- .../common/api/events/BooleanEvent.java | 10 +++++-- .../common/api/events/FunctionEvent.java | 7 ++++- .../common/api/events/SimpleEvent.java | 8 ++++- .../core/api/block/PolymerBlockUtils.java | 11 +++++++ .../core/api/utils/PolymerSyncedObject.java | 3 +- .../polymer/core/api/utils/PolymerUtils.java | 30 +++++++++++-------- .../resourcepack/api/ResourcePackCreator.java | 29 +++++++++++++++--- .../impl/PolymerResourcePackMod.java | 15 ++++++++++ 9 files changed, 93 insertions(+), 24 deletions(-) diff --git a/gradle.properties b/gradle.properties index 41e8e92c..52ef46af 100644 --- a/gradle.properties +++ b/gradle.properties @@ -13,9 +13,9 @@ fabric_version=0.102.1+1.21.1 maven_group = eu.pb4 -mod_version = 0.9.11 +mod_version = 0.9.12 -minecraft_version_supported = ">=1.20.5-" +minecraft_version_supported = ">=1.21-" packet_tweaker_version = 0.5.5+1.21 diff --git a/polymer-common/src/main/java/eu/pb4/polymer/common/api/events/BooleanEvent.java b/polymer-common/src/main/java/eu/pb4/polymer/common/api/events/BooleanEvent.java index f51ac83b..5fedfa86 100644 --- a/polymer-common/src/main/java/eu/pb4/polymer/common/api/events/BooleanEvent.java +++ b/polymer-common/src/main/java/eu/pb4/polymer/common/api/events/BooleanEvent.java @@ -2,11 +2,13 @@ import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; import java.util.List; import java.util.function.Predicate; public final class BooleanEvent { - private List handlers = new ArrayList<>(); + private final List handlers = new ArrayList<>(); public void register(T listener) { this.handlers.add(listener); @@ -25,10 +27,14 @@ public boolean invoke(Predicate invoker) { for (var handler : handlers) { var bool = invoker.test(handler); - if (bool == true) { + if (bool) { return true; } } return false; } + + public Collection invokers() { + return Collections.unmodifiableCollection(this.handlers); + } } diff --git a/polymer-common/src/main/java/eu/pb4/polymer/common/api/events/FunctionEvent.java b/polymer-common/src/main/java/eu/pb4/polymer/common/api/events/FunctionEvent.java index d875553c..23b8c66b 100644 --- a/polymer-common/src/main/java/eu/pb4/polymer/common/api/events/FunctionEvent.java +++ b/polymer-common/src/main/java/eu/pb4/polymer/common/api/events/FunctionEvent.java @@ -3,11 +3,12 @@ import java.util.ArrayList; import java.util.Collection; +import java.util.Collections; import java.util.List; import java.util.function.Function; public final class FunctionEvent { - private List handlers = new ArrayList<>(); + private final List handlers = new ArrayList<>(); public void register(T listener) { this.handlers.add(listener); @@ -25,4 +26,8 @@ public void unregister(T listener) { public R invoke(Function, R> invoker) { return invoker.apply(handlers); } + + public Collection invokers() { + return Collections.unmodifiableCollection(this.handlers); + } } diff --git a/polymer-common/src/main/java/eu/pb4/polymer/common/api/events/SimpleEvent.java b/polymer-common/src/main/java/eu/pb4/polymer/common/api/events/SimpleEvent.java index 2991c574..6a4cbb44 100644 --- a/polymer-common/src/main/java/eu/pb4/polymer/common/api/events/SimpleEvent.java +++ b/polymer-common/src/main/java/eu/pb4/polymer/common/api/events/SimpleEvent.java @@ -2,11 +2,13 @@ import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; import java.util.List; import java.util.function.Consumer; public final class SimpleEvent { - private List handlers = new ArrayList<>(); + private final List handlers = new ArrayList<>(); public void register(T listener) { this.handlers.add(listener); @@ -30,4 +32,8 @@ public void invoke(Consumer invoker) { public boolean isEmpty() { return this.handlers.isEmpty(); } + + public Collection invokers() { + return Collections.unmodifiableCollection(this.handlers); + } } diff --git a/polymer-core/src/main/java/eu/pb4/polymer/core/api/block/PolymerBlockUtils.java b/polymer-core/src/main/java/eu/pb4/polymer/core/api/block/PolymerBlockUtils.java index 3aab6fde..ae76478e 100644 --- a/polymer-core/src/main/java/eu/pb4/polymer/core/api/block/PolymerBlockUtils.java +++ b/polymer-core/src/main/java/eu/pb4/polymer/core/api/block/PolymerBlockUtils.java @@ -255,6 +255,17 @@ public static NbtCompound transformBlockEntityNbt(PacketContext context, BlockEn } } + if (original.contains("item", NbtElement.COMPOUND_TYPE)) { + var stack = ItemStack.fromNbtOrEmpty(lookup, original.getCompound("item")); + if (PolymerItemUtils.isPolymerServerItem(stack, context)) { + if (override == null) { + override = original.copy(); + } + stack = PolymerItemUtils.getPolymerItemStack(stack, context); + override.put("item", stack.encodeAllowEmpty(lookup)); + } + } + if (original.contains("components", NbtElement.COMPOUND_TYPE)) { var ops = lookup.getOps(NbtOps.INSTANCE); 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 c4b238e2..5a8edfcf 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 @@ -2,6 +2,7 @@ import net.minecraft.registry.Registry; import net.minecraft.server.network.ServerPlayerEntity; +import org.jetbrains.annotations.Nullable; /** * Used to mark client-synchronized polymer objects like BlockEntities, Enchantments, Recipes, etc @@ -28,7 +29,7 @@ default boolean canSynchronizeToPolymerClient(ServerPlayerEntity player) { * Allows to mark it to still send it to supported clients (for client optional setups) * Currently used for tags */ - default boolean canSyncRawToClient(ServerPlayerEntity player) { + default boolean canSyncRawToClient(@Nullable ServerPlayerEntity player) { return false; } diff --git a/polymer-core/src/main/java/eu/pb4/polymer/core/api/utils/PolymerUtils.java b/polymer-core/src/main/java/eu/pb4/polymer/core/api/utils/PolymerUtils.java index 47e665b8..5994d1a5 100644 --- a/polymer-core/src/main/java/eu/pb4/polymer/core/api/utils/PolymerUtils.java +++ b/polymer-core/src/main/java/eu/pb4/polymer/core/api/utils/PolymerUtils.java @@ -122,24 +122,28 @@ public static void schedulePacket(ServerPlayNetworkHandler handler, Packet pa public static void reloadWorld(ServerPlayerEntity player) { player.server.execute(() -> { PolymerImplUtils.IS_RELOADING_WORLD.set(Unit.INSTANCE); - player.networkHandler.sendPacket(new InventoryS2CPacket(0, 0, player.playerScreenHandler.getStacks(), player.playerScreenHandler.getCursorStack())); + try { + player.currentScreenHandler.syncState(); - var world = player.getWorld(); - var tacsAccess = ((ServerChunkLoadingManagerAccessor) ((ServerChunkManager) player.getWorld().getChunkManager()).chunkLoadingManager); + var world = player.getWorld(); + var tacsAccess = ((ServerChunkLoadingManagerAccessor) ((ServerChunkManager) player.getWorld().getChunkManager()).chunkLoadingManager); - for (var e : ((ServerWorldAccessor) player.getWorld()).polymer_getEntityManager().getLookup().iterate()) { - var tracker = tacsAccess.polymer$getEntityTrackers().get(e.getId()); - if (tracker != null) { - tracker.stopTracking(player); + for (var e : ((ServerWorldAccessor) player.getWorld()).polymer_getEntityManager().getLookup().iterate()) { + var tracker = tacsAccess.polymer$getEntityTrackers().get(e.getId()); + if (tracker != null) { + tracker.stopTracking(player); + } } - } - player.getChunkFilter().forEach((chunkPos) -> { - var chunk = world.getChunk(chunkPos.x, chunkPos.z); - player.networkHandler.chunkDataSender.unload(player, chunk.getPos()); - player.networkHandler.chunkDataSender.add(chunk); - }); + player.getChunkFilter().forEach((chunkPos) -> { + var chunk = world.getChunk(chunkPos.x, chunkPos.z); + player.networkHandler.chunkDataSender.unload(player, chunk.getPos()); + player.networkHandler.chunkDataSender.add(chunk); + }); + } catch (Throwable e) { + PolymerImpl.LOGGER.warn("Failed to reload player's world view!", e); + } PolymerImplUtils.IS_RELOADING_WORLD.remove(); }); diff --git a/polymer-resource-pack/src/main/java/eu/pb4/polymer/resourcepack/api/ResourcePackCreator.java b/polymer-resource-pack/src/main/java/eu/pb4/polymer/resourcepack/api/ResourcePackCreator.java index ba56d6fc..316c6e1d 100644 --- a/polymer-resource-pack/src/main/java/eu/pb4/polymer/resourcepack/api/ResourcePackCreator.java +++ b/polymer-resource-pack/src/main/java/eu/pb4/polymer/resourcepack/api/ResourcePackCreator.java @@ -1,7 +1,5 @@ package eu.pb4.polymer.resourcepack.api; -import com.google.gson.JsonObject; -import com.mojang.serialization.JsonOps; import eu.pb4.polymer.common.api.events.SimpleEvent; import eu.pb4.polymer.common.impl.CommonImpl; import eu.pb4.polymer.common.impl.CommonImplUtils; @@ -41,7 +39,7 @@ public final class ResourcePackCreator { public final SimpleEvent> creationEvent = new SimpleEvent<>(); public final SimpleEvent finishedEvent = new SimpleEvent<>(); public final SimpleEvent> afterInitialCreationEvent = new SimpleEvent<>(); - private final Map> items = new Object2ObjectOpenCustomHashMap<>(CommonImplUtils.IDENTITY_HASH); + private final Map> items = new IdentityHashMap<>(); private final Object2IntMap itemIds = new Object2IntOpenCustomHashMap<>(CommonImplUtils.IDENTITY_HASH); private final Map> itemModels = new Object2ObjectOpenCustomHashMap<>(CommonImplUtils.IDENTITY_HASH); private final Map> itemOverrides = new Object2ObjectOpenCustomHashMap<>(CommonImplUtils.IDENTITY_HASH); @@ -59,8 +57,31 @@ public final class ResourcePackCreator { public static ResourcePackCreator create() { return new ResourcePackCreator(0); } + public static ResourcePackCreator createCopy(ResourcePackCreator source, boolean copyEvents) { + var creator = new ResourcePackCreator(source.cmdOffset); + if (copyEvents) { + source.creationEvent.invokers().forEach(creator.creationEvent::register); + source.afterInitialCreationEvent.invokers().forEach(creator.afterInitialCreationEvent::register); + source.finishedEvent.invokers().forEach(creator.finishedEvent::register); + } + + source.items.forEach((a, b) -> creator.items.put(a, new ArrayList<>(b))); + creator.itemIds.putAll(source.itemIds); + source.itemModels.forEach((a, b) -> creator.itemModels.put(a, new HashMap<>(b))); + source.itemOverrides.forEach((a, b) -> creator.itemOverrides.put(a, new ArrayList<>(b))); + creator.modIds.addAll(source.modIds); + creator.modIdsNoCopy.addAll(source.modIdsNoCopy); + creator.takenArmorColors.addAll(source.takenArmorColors); + creator.armorModelMap.putAll(source.armorModelMap); + creator.armorColor = source.armorColor; + creator.packDescription = source.packDescription; + creator.packIcon = source.packIcon; + creator.sourcePaths.addAll(source.sourcePaths); + + return creator; + } - protected ResourcePackCreator(int cmdOffset) { + ResourcePackCreator(int cmdOffset) { this.cmdOffset = cmdOffset; this.itemIds.defaultReturnValue(1); } diff --git a/polymer-resource-pack/src/main/java/eu/pb4/polymer/resourcepack/impl/PolymerResourcePackMod.java b/polymer-resource-pack/src/main/java/eu/pb4/polymer/resourcepack/impl/PolymerResourcePackMod.java index fb8ea3c1..94a3984b 100644 --- a/polymer-resource-pack/src/main/java/eu/pb4/polymer/resourcepack/impl/PolymerResourcePackMod.java +++ b/polymer-resource-pack/src/main/java/eu/pb4/polymer/resourcepack/impl/PolymerResourcePackMod.java @@ -11,6 +11,8 @@ import net.fabricmc.api.ClientModInitializer; import net.fabricmc.api.ModInitializer; import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents; +import net.fabricmc.loader.api.FabricLoader; +import net.fabricmc.loader.api.metadata.CustomValue; import net.minecraft.item.ArmorMaterial; import net.minecraft.server.command.ServerCommandSource; import net.minecraft.text.HoverEvent; @@ -45,6 +47,19 @@ public void onInitialize() { CommonImplUtils.registerCommands((x) -> x.then(literal("generate-pack") .requires(CommonImplUtils.permission("command.generate", 3)) .executes(PolymerResourcePackMod::generateResources))); + + for (var mod : FabricLoader.getInstance().getAllMods()) { + var include = mod.getMetadata().getCustomValue("polymer:resource_pack_include"); + var require = mod.getMetadata().getCustomValue("polymer:resource_pack_require"); + + if (include != null && include.getType() == CustomValue.CvType.BOOLEAN && include.getAsBoolean()) { + PolymerResourcePackUtils.addModAssets(mod.getMetadata().getId()); + } + + if (require != null && require.getType() == CustomValue.CvType.BOOLEAN && require.getAsBoolean()) { + PolymerResourcePackUtils.markAsRequired(); + } + } } @Override