Skip to content

Commit d46f62a

Browse files
committed
fix: spawners unstackable for 1.20_R1 and 1.21_R1
1 parent 16720d4 commit d46f62a

File tree

2 files changed

+31
-21
lines changed
  • modules
    • v1_20_R4/src/main/java/de/dustplanet/silkspawners/compat/v1_20_R4
    • v1_21_R1/src/main/java/de/dustplanet/silkspawners/compat/v1_21_R1

2 files changed

+31
-21
lines changed

modules/v1_20_R4/src/main/java/de/dustplanet/silkspawners/compat/v1_20_R4/NMSHandler.java

+19-14
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242

4343
import de.dustplanet.silkspawners.compat.api.NMSProvider;
4444
import net.minecraft.core.Registry;
45+
import net.minecraft.core.component.DataComponentMap;
4546
import net.minecraft.core.component.DataComponents;
4647
import net.minecraft.core.registries.BuiltInRegistries;
4748
import net.minecraft.nbt.CompoundTag;
@@ -174,17 +175,21 @@ public void setSpawnersUnstackable() {
174175
final ResourceLocation resourceLocation = new ResourceLocation(NAMESPACED_SPAWNER_ID);
175176
final Registry<Item> itemRegistry = BuiltInRegistries.ITEM;
176177
final Item spawner = itemRegistry.get(resourceLocation);
178+
final DataComponentMap currentComponents = spawner.components();
179+
final DataComponentMap updatedComponents = DataComponentMap.composite(currentComponents,
180+
DataComponentMap.builder().set(DataComponents.MAX_STACK_SIZE, 1).build());
177181
try {
178-
final Field maxStackSize = Item.class.getDeclaredField("maxStackSize");
179-
maxStackSize.setAccessible(true);
180-
maxStackSize.set(spawner, 1);
182+
183+
final Field components = Item.class.getDeclaredField("components");
184+
components.setAccessible(true);
185+
components.set(spawner, updatedComponents);
181186
} catch (@SuppressWarnings("unused") NoSuchFieldException | SecurityException | IllegalArgumentException
182187
| IllegalAccessException e) {
183188
try {
184-
// int maxStackSize -> d
185-
final Field maxStackSize = Item.class.getDeclaredField("d");
186-
maxStackSize.setAccessible(true);
187-
maxStackSize.set(spawner, 1);
189+
// DataComponentMap components -> c
190+
final Field components = Item.class.getDeclaredField("c");
191+
components.setAccessible(true);
192+
components.set(spawner, updatedComponents);
188193
} catch (IllegalArgumentException | IllegalAccessException | NoSuchFieldException | SecurityException e1) {
189194
e1.printStackTrace();
190195
}
@@ -232,8 +237,8 @@ public ItemStack setNBTEntityID(final ItemStack item, final String entity) {
232237
itemStack = CraftItemStack.asNMSCopy(craftStack);
233238
final CustomData blockData = itemStack.getOrDefault(DataComponents.BLOCK_ENTITY_DATA, CustomData.EMPTY);
234239
final CustomData customData = itemStack.getOrDefault(DataComponents.CUSTOM_DATA, CustomData.EMPTY);
235-
CompoundTag tag = blockData.copyTag();
236-
CompoundTag customTag = customData.copyTag();
240+
final CompoundTag tag = blockData.copyTag();
241+
final CompoundTag customTag = customData.copyTag();
237242

238243
// Check for SilkSpawners key
239244
if (!customTag.contains("SilkSpawners")) {
@@ -294,7 +299,7 @@ public String getVanillaNBTEntityID(final ItemStack item) {
294299
final CraftItemStack craftStack = CraftItemStack.asCraftCopy(item);
295300
itemStack = CraftItemStack.asNMSCopy(craftStack);
296301
final CustomData blockEntityData = itemStack.getOrDefault(DataComponents.BLOCK_ENTITY_DATA, CustomData.EMPTY);
297-
CompoundTag tag = blockEntityData.copyTag();
302+
final CompoundTag tag = blockEntityData.copyTag();
298303

299304
if (tag.contains("EntityId")) {
300305
return tag.getString("EntityId");
@@ -313,7 +318,7 @@ public String getVanillaNBTEntityID(final ItemStack item) {
313318
/**
314319
* Return the spawner block the player is looking at, or null if isn't.
315320
*
316-
* @param player the player
321+
* @param player the player
317322
* @param distance the reach distance
318323
* @return the found block or null
319324
*/
@@ -345,8 +350,8 @@ public ItemStack newEggItem(final String entityID, final int amount, final Strin
345350
itemStack = CraftItemStack.asNMSCopy(craftStack);
346351
final CustomData blockData = itemStack.getOrDefault(DataComponents.ENTITY_DATA, CustomData.EMPTY);
347352
final CustomData customData = itemStack.getOrDefault(DataComponents.CUSTOM_DATA, CustomData.EMPTY);
348-
CompoundTag tag = blockData.copyTag();
349-
CompoundTag customTag = customData.copyTag();
353+
final CompoundTag tag = blockData.copyTag();
354+
final CompoundTag customTag = customData.copyTag();
350355

351356
if (!customTag.contains("SilkSpawners")) {
352357
customTag.put("SilkSpawners", new CompoundTag());
@@ -373,7 +378,7 @@ public String getVanillaEggNBTEntityID(final ItemStack item) {
373378
final CraftItemStack craftStack = CraftItemStack.asCraftCopy(item);
374379
itemStack = CraftItemStack.asNMSCopy(craftStack);
375380
final CustomData blockEntityData = itemStack.getOrDefault(DataComponents.ENTITY_DATA, CustomData.EMPTY);
376-
CompoundTag tag = blockEntityData.copyTag();
381+
final CompoundTag tag = blockEntityData.copyTag();
377382

378383
if (tag.contains("id")) {
379384
return tag.getString("id").replace("minecraft:", "");

modules/v1_21_R1/src/main/java/de/dustplanet/silkspawners/compat/v1_21_R1/NMSHandler.java

+12-7
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242

4343
import de.dustplanet.silkspawners.compat.api.NMSProvider;
4444
import net.minecraft.core.Registry;
45+
import net.minecraft.core.component.DataComponentMap;
4546
import net.minecraft.core.component.DataComponents;
4647
import net.minecraft.core.registries.BuiltInRegistries;
4748
import net.minecraft.nbt.CompoundTag;
@@ -174,17 +175,21 @@ public void setSpawnersUnstackable() {
174175
final ResourceLocation resourceLocation = ResourceLocation.fromNamespaceAndPath("minecraft", NAMESPACED_SPAWNER_ID);
175176
final Registry<Item> itemRegistry = BuiltInRegistries.ITEM;
176177
final Item spawner = itemRegistry.get(resourceLocation);
178+
final DataComponentMap currentComponents = spawner.components();
179+
final DataComponentMap updatedComponents = DataComponentMap.composite(currentComponents,
180+
DataComponentMap.builder().set(DataComponents.MAX_STACK_SIZE, 1).build());
177181
try {
178-
final Field maxStackSize = Item.class.getDeclaredField("maxStackSize");
179-
maxStackSize.setAccessible(true);
180-
maxStackSize.set(spawner, 1);
182+
183+
final Field components = Item.class.getDeclaredField("components");
184+
components.setAccessible(true);
185+
components.set(spawner, updatedComponents);
181186
} catch (@SuppressWarnings("unused") NoSuchFieldException | SecurityException | IllegalArgumentException
182187
| IllegalAccessException e) {
183188
try {
184-
// int maxStackSize -> d
185-
final Field maxStackSize = Item.class.getDeclaredField("d");
186-
maxStackSize.setAccessible(true);
187-
maxStackSize.set(spawner, 1);
189+
// DataComponentMap components -> c
190+
final Field components = Item.class.getDeclaredField("c");
191+
components.setAccessible(true);
192+
components.set(spawner, updatedComponents);
188193
} catch (IllegalArgumentException | IllegalAccessException | NoSuchFieldException | SecurityException e1) {
189194
e1.printStackTrace();
190195
}

0 commit comments

Comments
 (0)