Skip to content

Commit

Permalink
Fix Empty ShulkerBox stack (#257)
Browse files Browse the repository at this point in the history
  • Loading branch information
s-yh-china committed Jul 22, 2024
1 parent 2ac9175 commit fd5e8be
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 79 deletions.
82 changes: 9 additions & 73 deletions patches/server/0042-Stackable-ShulkerBoxes.patch
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ index d04bf7d06855022c973073fb84c5d3d65f2553e1..9bedd76330e71ca7f3feecf0b90ec960

if (j > 0) {
diff --git a/src/main/java/net/minecraft/world/entity/item/ItemEntity.java b/src/main/java/net/minecraft/world/entity/item/ItemEntity.java
index ea0d9335446b20073b9aafb9de453097355db79c..38c2e27343d68d016621cf953222b915b56a0df2 100644
index ea0d9335446b20073b9aafb9de453097355db79c..9f86b2c5af580f27f7216b4184acb72b54fee550 100644
--- a/src/main/java/net/minecraft/world/entity/item/ItemEntity.java
+++ b/src/main/java/net/minecraft/world/entity/item/ItemEntity.java
@@ -22,10 +22,12 @@ import net.minecraft.world.entity.EntityType;
Expand All @@ -69,21 +69,7 @@ index ea0d9335446b20073b9aafb9de453097355db79c..38c2e27343d68d016621cf953222b915
import org.bukkit.event.entity.EntityPickupItemEvent;
import org.bukkit.event.entity.EntityRemoveEvent;
import org.bukkit.event.player.PlayerPickupItemEvent;
@@ -79,6 +82,13 @@ public class ItemEntity extends Entity implements TraceableEntity {
this.setDeltaMovement(this.random.nextDouble() * 0.2D - 0.1D, 0.2D, this.random.nextDouble() * 0.2D - 0.1D);
this.setItem(stack);
// Paper end - Don't use level random in entity constructors
+ // Leaves start - stackable shulker boxes
+ if (org.leavesmc.leaves.LeavesConfig.shulkerBoxStackSize > 1) {
+ if (stack.getItem() instanceof BlockItem bi && bi.getBlock() instanceof ShulkerBoxBlock) {
+ org.leavesmc.leaves.util.ShulkerBoxUtils.cleanUpShulkerBoxTag(stack);
+ }
+ }
+ // Leaves end - stackable shulker boxes
}

public ItemEntity(Level world, double x, double y, double z, ItemStack stack, double velocityX, double velocityY, double velocityZ) {
@@ -315,10 +325,49 @@ public class ItemEntity extends Entity implements TraceableEntity {
@@ -315,10 +318,49 @@ public class ItemEntity extends Entity implements TraceableEntity {
private boolean isMergable() {
ItemStack itemstack = this.getItem();

Expand Down Expand Up @@ -279,7 +265,7 @@ index c39c773112fb8b534b926f2f2b47fe6fbb69fcb2..ae9338b8a5598e7a257b469adae72858

@Nullable
diff --git a/src/main/java/net/minecraft/world/item/ItemStack.java b/src/main/java/net/minecraft/world/item/ItemStack.java
index 312b57b4ef340935f4335989ce1d6a4b8b61532c..fa3dbb93a049962fe10cb0391d437acdb45c82af 100644
index 2c312c0b741fb96a008881e9e01fa660a1fb63ab..9e89743328d8b05d69d5637ffc2e4831ada160b3 100644
--- a/src/main/java/net/minecraft/world/item/ItemStack.java
+++ b/src/main/java/net/minecraft/world/item/ItemStack.java
@@ -225,7 +225,7 @@ public final class ItemStack implements DataComponentHolder {
Expand Down Expand Up @@ -377,79 +363,29 @@ index 5b7eedaa4f52ac72da417358034f3f457d6a2b45..b6a419ab08a8ee1ddffa234ddc07027c
ignoreTileUpdates = true; // Paper - Perf: Optimize Hoppers
diff --git a/src/main/java/org/leavesmc/leaves/util/ShulkerBoxUtils.java b/src/main/java/org/leavesmc/leaves/util/ShulkerBoxUtils.java
new file mode 100644
index 0000000000000000000000000000000000000000..401e4384007ffd8ef21b9a3dc6c07d34d257843a
index 0000000000000000000000000000000000000000..cde26081e8324742ab648bcefc64723bf95023c5
--- /dev/null
+++ b/src/main/java/org/leavesmc/leaves/util/ShulkerBoxUtils.java
@@ -0,0 +1,78 @@
@@ -0,0 +1,28 @@
+package org.leavesmc.leaves.util;
+
+import net.minecraft.core.component.DataComponentMap;
+import net.minecraft.core.component.DataComponentPatch;
+import net.minecraft.core.component.PatchedDataComponentMap;
+import net.minecraft.nbt.CompoundTag;
+import net.minecraft.world.inventory.Slot;
+import net.minecraft.core.component.DataComponents;
+import net.minecraft.world.item.BlockItem;
+import net.minecraft.world.item.ItemStack;
+import net.minecraft.world.item.component.CustomData;
+import net.minecraft.world.item.component.ItemContainerContents;
+import net.minecraft.world.level.block.ShulkerBoxBlock;
+import org.jetbrains.annotations.NotNull;
+import org.leavesmc.leaves.LeavesConfig;
+
+import static net.minecraft.core.component.DataComponents.BLOCK_ENTITY_DATA;
+
+// Powered by fabric-carpet/src/main/java/carpet/helpers/InventoryHelper.java
+public class ShulkerBoxUtils {
+ // From nbt/NbtElement.java createTag()
+ public static final int TAG_END = 0;
+ public static final int TAG_BYTE = 1;
+ public static final int TAG_SHORT = 2;
+ public static final int TAG_INT = 3;
+ public static final int TAG_LONG = 4;
+ public static final int TAG_FLOAT = 5;
+ public static final int TAG_DOUBLE = 6;
+ public static final int TAG_BYTEARRAY = 7;
+ public static final int TAG_STRING = 8;
+ public static final int TAG_LIST = 9;
+ public static final int TAG_COMPOUND = 10;
+ public static final int TAG_INTARRAY = 11;
+ public static final int TAG_LONGARRAY = 12;
+
+ public static boolean cleanUpShulkerBoxTag(@NotNull ItemStack stack) {
+ boolean changed = false;
+ DataComponentMap components = stack.getComponents();
+
+ if (!components.has(BLOCK_ENTITY_DATA)) return false;
+
+ CompoundTag bet = components.get(BLOCK_ENTITY_DATA).copyTag();
+ if (bet.contains("Items", TAG_LIST) && bet.getList("Items", TAG_COMPOUND).isEmpty()) {
+ bet.remove("Items");
+ stack.applyComponents(DataComponentPatch.builder().set(BLOCK_ENTITY_DATA, CustomData.of(bet)).build());
+ changed = true;
+ }
+
+ if (bet.isEmpty() || (bet.size() == 1 && bet.getString("id").equals("minecraft:shulker_box"))) {
+ stack.applyComponents(DataComponentPatch.builder().remove(BLOCK_ENTITY_DATA).build());
+ changed = true;
+ }
+ if (components.isEmpty()) {
+ stack.components = (PatchedDataComponentMap.fromPatch(DataComponentMap.EMPTY, DataComponentPatch.EMPTY));
+ changed = true;
+ }
+ return changed;
+ }
+
+ public static boolean shulkerBoxHasItems(@NotNull ItemStack stack) {
+ DataComponentMap components = stack.getComponents();
+
+ if (!components.has(BLOCK_ENTITY_DATA)) return false;
+
+ CompoundTag bet = components.get(BLOCK_ENTITY_DATA).copyTag();
+ return bet.contains("Items", TAG_LIST) && !bet.getList("Items", TAG_COMPOUND).isEmpty();
+ return stack.getComponents().getOrDefault(DataComponents.CONTAINER, ItemContainerContents.EMPTY).stream().findAny().isEmpty();
+ }
+
+ public static int getItemStackMaxCount(ItemStack stack) {
+ if (LeavesConfig.shulkerBoxStackSize > 1 && stack.getItem() instanceof BlockItem bi &&
+ bi.getBlock() instanceof ShulkerBoxBlock && !org.leavesmc.leaves.util.ShulkerBoxUtils.shulkerBoxHasItems(stack)) {
+ bi.getBlock() instanceof ShulkerBoxBlock && !org.leavesmc.leaves.util.ShulkerBoxUtils.shulkerBoxHasItems(stack)) {
+ return org.leavesmc.leaves.LeavesConfig.shulkerBoxStackSize;
+ }
+ return stack.getMaxStackSize();
Expand Down
6 changes: 3 additions & 3 deletions patches/server/0049-Use-vanilla-random-config.patch
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Subject: [PATCH] Use vanilla random config


diff --git a/src/main/java/net/minecraft/world/entity/Entity.java b/src/main/java/net/minecraft/world/entity/Entity.java
index d3453751012d0ebfda1f4e93187820603bb27f08..2a37944ffb6a253428c0474d24b258e791a8cce0 100644
index decc1d6be70eb501ebf75dd5907fabfbc2584928..993efcba5c43e2ba6d8fd2eb61215ed67c9c6a6b 100644
--- a/src/main/java/net/minecraft/world/entity/Entity.java
+++ b/src/main/java/net/minecraft/world/entity/Entity.java
@@ -559,7 +559,7 @@ public abstract class Entity implements SyncedDataHolder, Nameable, EntityAccess
Expand Down Expand Up @@ -44,7 +44,7 @@ index 42f4e544fe7fbc342f15eacb5e38d40849e3c419..0bd9ee3d0b546564a069f4aaf7a33e35
}

diff --git a/src/main/java/net/minecraft/world/entity/item/ItemEntity.java b/src/main/java/net/minecraft/world/entity/item/ItemEntity.java
index 38c2e27343d68d016621cf953222b915b56a0df2..a1d4d8b6fe488a7056c6613acbd35d3be71ae039 100644
index 9f86b2c5af580f27f7216b4184acb72b54fee550..d5ea322202b6f9cdc193d4cf2f43c547009e20ff 100644
--- a/src/main/java/net/minecraft/world/entity/item/ItemEntity.java
+++ b/src/main/java/net/minecraft/world/entity/item/ItemEntity.java
@@ -79,7 +79,13 @@ public class ItemEntity extends Entity implements TraceableEntity {
Expand All @@ -61,7 +61,7 @@ index 38c2e27343d68d016621cf953222b915b56a0df2..a1d4d8b6fe488a7056c6613acbd35d3b
+ // Leaves end - vanilla yes, paper no
this.setItem(stack);
// Paper end - Don't use level random in entity constructors
// Leaves start - stackable shulker boxes
}
diff --git a/src/main/java/net/minecraft/world/entity/item/PrimedTnt.java b/src/main/java/net/minecraft/world/entity/item/PrimedTnt.java
index 8d494b549abb452bb554220060cc9c5beec5edf6..ac05fe9a17f67a4cb12cdb9c381896d7bb0b3f6a 100644
--- a/src/main/java/net/minecraft/world/entity/item/PrimedTnt.java
Expand Down
6 changes: 3 additions & 3 deletions patches/server/0110-Make-Item-tick-vanilla.patch
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ Subject: [PATCH] Make Item tick vanilla


diff --git a/src/main/java/net/minecraft/world/entity/item/ItemEntity.java b/src/main/java/net/minecraft/world/entity/item/ItemEntity.java
index a1d4d8b6fe488a7056c6613acbd35d3be71ae039..ab277eee0ab86b6c5896d4e5179fcfc4f1a34375 100644
index d5ea322202b6f9cdc193d4cf2f43c547009e20ff..26cdc6d7e8026c5290681b87b6b54bb3b7731f1c 100644
--- a/src/main/java/net/minecraft/world/entity/item/ItemEntity.java
+++ b/src/main/java/net/minecraft/world/entity/item/ItemEntity.java
@@ -259,6 +259,9 @@ public class ItemEntity extends Entity implements TraceableEntity {
@@ -252,6 +252,9 @@ public class ItemEntity extends Entity implements TraceableEntity {
// Spigot start - copied from above
@Override
public void inactiveTick() {
Expand All @@ -18,7 +18,7 @@ index a1d4d8b6fe488a7056c6613acbd35d3be71ae039..ab277eee0ab86b6c5896d4e5179fcfc4
// CraftBukkit start - Use wall time for pickup and despawn timers
int elapsedTicks = MinecraftServer.currentTick - this.lastTick;
if (this.pickupDelay != 32767) this.pickupDelay -= elapsedTicks;
@@ -276,6 +279,8 @@ public class ItemEntity extends Entity implements TraceableEntity {
@@ -269,6 +272,8 @@ public class ItemEntity extends Entity implements TraceableEntity {
// CraftBukkit end
this.discard(EntityRemoveEvent.Cause.DESPAWN); // CraftBukkit - add Bukkit remove cause
}
Expand Down

0 comments on commit fd5e8be

Please sign in to comment.