From d46f62a1e6847e8e2e674aaed2a7eded078f88d1 Mon Sep 17 00:00:00 2001 From: Tim Brust Date: Fri, 28 Jun 2024 13:25:36 +0200 Subject: [PATCH] fix: spawners unstackable for 1.20_R1 and 1.21_R1 --- .../compat/v1_20_R4/NMSHandler.java | 33 +++++++++++-------- .../compat/v1_21_R1/NMSHandler.java | 19 +++++++---- 2 files changed, 31 insertions(+), 21 deletions(-) diff --git a/modules/v1_20_R4/src/main/java/de/dustplanet/silkspawners/compat/v1_20_R4/NMSHandler.java b/modules/v1_20_R4/src/main/java/de/dustplanet/silkspawners/compat/v1_20_R4/NMSHandler.java index e8c19dcd..f74fd002 100644 --- a/modules/v1_20_R4/src/main/java/de/dustplanet/silkspawners/compat/v1_20_R4/NMSHandler.java +++ b/modules/v1_20_R4/src/main/java/de/dustplanet/silkspawners/compat/v1_20_R4/NMSHandler.java @@ -42,6 +42,7 @@ import de.dustplanet.silkspawners.compat.api.NMSProvider; import net.minecraft.core.Registry; +import net.minecraft.core.component.DataComponentMap; import net.minecraft.core.component.DataComponents; import net.minecraft.core.registries.BuiltInRegistries; import net.minecraft.nbt.CompoundTag; @@ -174,17 +175,21 @@ public void setSpawnersUnstackable() { final ResourceLocation resourceLocation = new ResourceLocation(NAMESPACED_SPAWNER_ID); final Registry itemRegistry = BuiltInRegistries.ITEM; final Item spawner = itemRegistry.get(resourceLocation); + final DataComponentMap currentComponents = spawner.components(); + final DataComponentMap updatedComponents = DataComponentMap.composite(currentComponents, + DataComponentMap.builder().set(DataComponents.MAX_STACK_SIZE, 1).build()); try { - final Field maxStackSize = Item.class.getDeclaredField("maxStackSize"); - maxStackSize.setAccessible(true); - maxStackSize.set(spawner, 1); + + final Field components = Item.class.getDeclaredField("components"); + components.setAccessible(true); + components.set(spawner, updatedComponents); } catch (@SuppressWarnings("unused") NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException e) { try { - // int maxStackSize -> d - final Field maxStackSize = Item.class.getDeclaredField("d"); - maxStackSize.setAccessible(true); - maxStackSize.set(spawner, 1); + // DataComponentMap components -> c + final Field components = Item.class.getDeclaredField("c"); + components.setAccessible(true); + components.set(spawner, updatedComponents); } catch (IllegalArgumentException | IllegalAccessException | NoSuchFieldException | SecurityException e1) { e1.printStackTrace(); } @@ -232,8 +237,8 @@ public ItemStack setNBTEntityID(final ItemStack item, final String entity) { itemStack = CraftItemStack.asNMSCopy(craftStack); final CustomData blockData = itemStack.getOrDefault(DataComponents.BLOCK_ENTITY_DATA, CustomData.EMPTY); final CustomData customData = itemStack.getOrDefault(DataComponents.CUSTOM_DATA, CustomData.EMPTY); - CompoundTag tag = blockData.copyTag(); - CompoundTag customTag = customData.copyTag(); + final CompoundTag tag = blockData.copyTag(); + final CompoundTag customTag = customData.copyTag(); // Check for SilkSpawners key if (!customTag.contains("SilkSpawners")) { @@ -294,7 +299,7 @@ public String getVanillaNBTEntityID(final ItemStack item) { final CraftItemStack craftStack = CraftItemStack.asCraftCopy(item); itemStack = CraftItemStack.asNMSCopy(craftStack); final CustomData blockEntityData = itemStack.getOrDefault(DataComponents.BLOCK_ENTITY_DATA, CustomData.EMPTY); - CompoundTag tag = blockEntityData.copyTag(); + final CompoundTag tag = blockEntityData.copyTag(); if (tag.contains("EntityId")) { return tag.getString("EntityId"); @@ -313,7 +318,7 @@ public String getVanillaNBTEntityID(final ItemStack item) { /** * Return the spawner block the player is looking at, or null if isn't. * - * @param player the player + * @param player the player * @param distance the reach distance * @return the found block or null */ @@ -345,8 +350,8 @@ public ItemStack newEggItem(final String entityID, final int amount, final Strin itemStack = CraftItemStack.asNMSCopy(craftStack); final CustomData blockData = itemStack.getOrDefault(DataComponents.ENTITY_DATA, CustomData.EMPTY); final CustomData customData = itemStack.getOrDefault(DataComponents.CUSTOM_DATA, CustomData.EMPTY); - CompoundTag tag = blockData.copyTag(); - CompoundTag customTag = customData.copyTag(); + final CompoundTag tag = blockData.copyTag(); + final CompoundTag customTag = customData.copyTag(); if (!customTag.contains("SilkSpawners")) { customTag.put("SilkSpawners", new CompoundTag()); @@ -373,7 +378,7 @@ public String getVanillaEggNBTEntityID(final ItemStack item) { final CraftItemStack craftStack = CraftItemStack.asCraftCopy(item); itemStack = CraftItemStack.asNMSCopy(craftStack); final CustomData blockEntityData = itemStack.getOrDefault(DataComponents.ENTITY_DATA, CustomData.EMPTY); - CompoundTag tag = blockEntityData.copyTag(); + final CompoundTag tag = blockEntityData.copyTag(); if (tag.contains("id")) { return tag.getString("id").replace("minecraft:", ""); diff --git a/modules/v1_21_R1/src/main/java/de/dustplanet/silkspawners/compat/v1_21_R1/NMSHandler.java b/modules/v1_21_R1/src/main/java/de/dustplanet/silkspawners/compat/v1_21_R1/NMSHandler.java index d6bab79b..b51c91ab 100644 --- a/modules/v1_21_R1/src/main/java/de/dustplanet/silkspawners/compat/v1_21_R1/NMSHandler.java +++ b/modules/v1_21_R1/src/main/java/de/dustplanet/silkspawners/compat/v1_21_R1/NMSHandler.java @@ -42,6 +42,7 @@ import de.dustplanet.silkspawners.compat.api.NMSProvider; import net.minecraft.core.Registry; +import net.minecraft.core.component.DataComponentMap; import net.minecraft.core.component.DataComponents; import net.minecraft.core.registries.BuiltInRegistries; import net.minecraft.nbt.CompoundTag; @@ -174,17 +175,21 @@ public void setSpawnersUnstackable() { final ResourceLocation resourceLocation = ResourceLocation.fromNamespaceAndPath("minecraft", NAMESPACED_SPAWNER_ID); final Registry itemRegistry = BuiltInRegistries.ITEM; final Item spawner = itemRegistry.get(resourceLocation); + final DataComponentMap currentComponents = spawner.components(); + final DataComponentMap updatedComponents = DataComponentMap.composite(currentComponents, + DataComponentMap.builder().set(DataComponents.MAX_STACK_SIZE, 1).build()); try { - final Field maxStackSize = Item.class.getDeclaredField("maxStackSize"); - maxStackSize.setAccessible(true); - maxStackSize.set(spawner, 1); + + final Field components = Item.class.getDeclaredField("components"); + components.setAccessible(true); + components.set(spawner, updatedComponents); } catch (@SuppressWarnings("unused") NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException e) { try { - // int maxStackSize -> d - final Field maxStackSize = Item.class.getDeclaredField("d"); - maxStackSize.setAccessible(true); - maxStackSize.set(spawner, 1); + // DataComponentMap components -> c + final Field components = Item.class.getDeclaredField("c"); + components.setAccessible(true); + components.set(spawner, updatedComponents); } catch (IllegalArgumentException | IllegalAccessException | NoSuchFieldException | SecurityException e1) { e1.printStackTrace(); }