Skip to content

Commit

Permalink
fix: spawners unstackable for 1.20_R1 and 1.21_R1
Browse files Browse the repository at this point in the history
  • Loading branch information
timbru31 committed Jun 28, 2024
1 parent 16720d4 commit d46f62a
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -174,17 +175,21 @@ public void setSpawnersUnstackable() {
final ResourceLocation resourceLocation = new ResourceLocation(NAMESPACED_SPAWNER_ID);
final Registry<Item> 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();
}
Expand Down Expand Up @@ -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")) {
Expand Down Expand Up @@ -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");
Expand All @@ -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
*/
Expand Down Expand Up @@ -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());
Expand All @@ -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:", "");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -174,17 +175,21 @@ public void setSpawnersUnstackable() {
final ResourceLocation resourceLocation = ResourceLocation.fromNamespaceAndPath("minecraft", NAMESPACED_SPAWNER_ID);
final Registry<Item> 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();
}
Expand Down

0 comments on commit d46f62a

Please sign in to comment.