9
0
mirror of https://github.com/LeavesMC/Leaves.git synced 2025-12-19 14:59:32 +00:00

fix: fix hopper event (#739)

* fix: fix hopper event

* fix: fix events

* fix: fix comment
This commit is contained in:
MC_XiaoHei
2025-09-18 10:03:56 +08:00
committed by GitHub
parent a82724fc3c
commit 9c8a2fc79b

View File

@@ -1424,10 +1424,10 @@ index 363d85c96bd3fb1a1945595df36e30bd6dd2fa4e..542910b3d5faa85f2b14022932c058bc
+ // Leaves end - Lithium Sleeping Block Entity
}
diff --git a/net/minecraft/world/level/block/entity/HopperBlockEntity.java b/net/minecraft/world/level/block/entity/HopperBlockEntity.java
index f3d2d86b5065064013f37592ba7f53fdf4066629..8b101e41440bf78da1eae507153e118c04615934 100644
index d3e1a968fdbba65a388a67d0d578876188f5dc12..50f25da61740eac611cdcdeebd426fa2fa25653f 100644
--- a/net/minecraft/world/level/block/entity/HopperBlockEntity.java
+++ b/net/minecraft/world/level/block/entity/HopperBlockEntity.java
@@ -27,8 +27,30 @@ import net.minecraft.world.level.block.state.BlockState;
@@ -27,8 +27,29 @@ import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.storage.ValueInput;
import net.minecraft.world.level.storage.ValueOutput;
import net.minecraft.world.phys.AABB;
@@ -1438,7 +1438,6 @@ index f3d2d86b5065064013f37592ba7f53fdf4066629..8b101e41440bf78da1eae507153e118c
+import net.minecraft.world.level.chunk.LevelChunk;
+import net.minecraft.world.CompoundContainer;
+import net.minecraft.server.level.ServerLevel;
+import org.leavesmc.leaves.lithium.api.inventory.LithiumCooldownReceivingInventory;
+import org.leavesmc.leaves.lithium.api.inventory.LithiumInventory;
+import org.leavesmc.leaves.lithium.common.block.entity.SleepingBlockEntity;
+import org.leavesmc.leaves.lithium.common.block.entity.inventory_change_tracking.InventoryChangeListener;
@@ -1460,7 +1459,7 @@ index f3d2d86b5065064013f37592ba7f53fdf4066629..8b101e41440bf78da1eae507153e118c
public static final int MOVE_ITEM_SPEED = 8;
public static final int HOPPER_CONTAINER_SIZE = 5;
private static final int[][] CACHED_SLOTS = new int[54][];
@@ -123,6 +145,7 @@ public class HopperBlockEntity extends RandomizableContainerBlockEntity implemen
@@ -123,6 +144,7 @@ public class HopperBlockEntity extends RandomizableContainerBlockEntity implemen
@Override
public void setBlockState(BlockState blockState) {
@@ -1468,7 +1467,7 @@ index f3d2d86b5065064013f37592ba7f53fdf4066629..8b101e41440bf78da1eae507153e118c
super.setBlockState(blockState);
this.facing = blockState.getValue(HopperBlock.FACING);
}
@@ -151,6 +174,7 @@ public class HopperBlockEntity extends RandomizableContainerBlockEntity implemen
@@ -151,6 +173,7 @@ public class HopperBlockEntity extends RandomizableContainerBlockEntity implemen
boolean result = tryMoveItems(level, pos, state, blockEntity, () -> {
return suckInItems(level, blockEntity);
});
@@ -1476,7 +1475,7 @@ index f3d2d86b5065064013f37592ba7f53fdf4066629..8b101e41440bf78da1eae507153e118c
if (!result && blockEntity.level.spigotConfig.hopperCheck > 1) {
blockEntity.setCooldown(blockEntity.level.spigotConfig.hopperCheck);
}
@@ -240,6 +264,14 @@ public class HopperBlockEntity extends RandomizableContainerBlockEntity implemen
@@ -240,6 +263,14 @@ public class HopperBlockEntity extends RandomizableContainerBlockEntity implemen
org.leavesmc.leaves.protocol.PcaSyncProtocol.syncBlockEntityToClient(blockEntity);
}
// Leaves end - pca
@@ -1491,6 +1490,14 @@ index f3d2d86b5065064013f37592ba7f53fdf4066629..8b101e41440bf78da1eae507153e118c
return true;
}
}
@@ -438,6 +469,7 @@ public class HopperBlockEntity extends RandomizableContainerBlockEntity implemen
private static void applyCooldown(final Hopper hopper) {
if (hopper instanceof HopperBlockEntity blockEntity && blockEntity.getLevel() != null) {
blockEntity.setCooldown(blockEntity.getLevel().spigotConfig.hopperTransfer);
+ blockEntity.skipNextSleepCheckAfterCooldown = true; // Leaves - Lithium Sleeping Block Entity
}
}
@@ -488,11 +520,19 @@ public class HopperBlockEntity extends RandomizableContainerBlockEntity implemen
}
}
@@ -1541,6 +1548,15 @@ index f3d2d86b5065064013f37592ba7f53fdf4066629..8b101e41440bf78da1eae507153e118c
if (addItem(hopper, itemEntity)) {
return true;
}
@@ -807,7 +855,7 @@ public class HopperBlockEntity extends RandomizableContainerBlockEntity implemen
// CraftBukkit start
@Nullable
private static Container runHopperInventorySearchEvent(
- Container container,
+ @Nullable Container container,
org.bukkit.craftbukkit.block.CraftBlock hopper,
org.bukkit.craftbukkit.block.CraftBlock searchLocation,
org.bukkit.event.inventory.HopperInventorySearchEvent.ContainerType containerType
@@ -935,6 +983,19 @@ public class HopperBlockEntity extends RandomizableContainerBlockEntity implemen
}
@@ -1569,15 +1585,16 @@ index f3d2d86b5065064013f37592ba7f53fdf4066629..8b101e41440bf78da1eae507153e118c
}
public static void entityInside(Level level, BlockPos pos, BlockState state, Entity entity, HopperBlockEntity blockEntity) {
@@ -968,4 +1030,742 @@ public class HopperBlockEntity extends RandomizableContainerBlockEntity implemen
@@ -968,4 +1030,758 @@ public class HopperBlockEntity extends RandomizableContainerBlockEntity implemen
protected AbstractContainerMenu createMenu(int id, Inventory player) {
return new HopperMenu(id, player, this);
}
+
+ // Leaves start - Lithium Sleeping Block Entity
+ private LevelChunk.RebindableTickingBlockEntityWrapper tickWrapper = null;
+ private TickingBlockEntity sleepingTicker = null;
+ @Nullable private LevelChunk.RebindableTickingBlockEntityWrapper tickWrapper = null;
+ @Nullable private TickingBlockEntity sleepingTicker = null;
+ private long myModCountAtLastInsert, myModCountAtLastExtract, myModCountAtLastItemCollect;
+ private boolean skipNextSleepCheckAfterCooldown = false;
+
+ private HopperCachingState.BlockInventory insertionMode = HopperCachingState.BlockInventory.UNKNOWN;
+ private HopperCachingState.BlockInventory extractionMode = HopperCachingState.BlockInventory.UNKNOWN;
@@ -1616,7 +1633,7 @@ index f3d2d86b5065064013f37592ba7f53fdf4066629..8b101e41440bf78da1eae507153e118c
+ private boolean shouldCheckSleep;
+
+ private void checkSleepingConditions() {
+ if (this.isOnCooldown() || this.getLevel() == null) {
+ if (this.cooldownTime > 0 || this.getLevel() == null || skipNextSleepCheckAfterCooldown) {
+ return;
+ }
+ if (isSleeping()) {
@@ -1706,12 +1723,12 @@ index f3d2d86b5065064013f37592ba7f53fdf4066629..8b101e41440bf78da1eae507153e118c
+ }
+
+ @Override
+ public void lithium$setSleepingTicker(TickingBlockEntity sleepingTicker) {
+ public void lithium$setSleepingTicker(@Nullable TickingBlockEntity sleepingTicker) {
+ this.sleepingTicker = sleepingTicker;
+ }
+
+ @Override
+ public TickingBlockEntity lithium$getSleepingTicker() {
+ public @Nullable TickingBlockEntity lithium$getSleepingTicker() {
+ return sleepingTicker;
+ }
+
@@ -1722,12 +1739,13 @@ index f3d2d86b5065064013f37592ba7f53fdf4066629..8b101e41440bf78da1eae507153e118c
+ }
+
+ @Override
+ public LevelChunk.RebindableTickingBlockEntityWrapper lithium$getTickWrapper() {
+ public @Nullable LevelChunk.RebindableTickingBlockEntityWrapper lithium$getTickWrapper() {
+ return tickWrapper;
+ }
+
+ @Override
+ public boolean lithium$startSleeping() {
+ System.out.println("Starting to sleep: " + this);
+ if (this.isSleeping()) {
+ return false;
+ }
@@ -1886,19 +1904,24 @@ index f3d2d86b5065064013f37592ba7f53fdf4066629..8b101e41440bf78da1eae507153e118c
+ this.wakeUpNow();
+ }
+
+ private static Container getExtractInventory(Level world, Hopper hopper, BlockPos extractBlockPos, BlockState extractBlockState) {
+ private static @Nullable Container getExtractInventory(Level world, Hopper hopper, BlockPos extractBlockPos, BlockState extractBlockState) {
+ if (!(hopper instanceof HopperBlockEntity hopperBlockEntity)) {
+ return getSourceContainer(world, hopper, extractBlockPos, extractBlockState); //Hopper Minecarts do not cache Inventories
+ }
+
+ Container blockInventory = hopperBlockEntity.lithium$getExtractBlockInventory(world, extractBlockPos, extractBlockState);
+ if (blockInventory != null) {
+ return blockInventory;
+ if (blockInventory == null) {
+ blockInventory = hopperBlockEntity.lithium$getExtractEntityInventory(world);
+ }
+ return hopperBlockEntity.lithium$getExtractEntityInventory(world);
+ return org.bukkit.event.inventory.HopperInventorySearchEvent.getHandlerList().getRegisteredListeners().length == 0 ? blockInventory : runHopperInventorySearchEvent(
+ blockInventory,
+ org.bukkit.craftbukkit.block.CraftBlock.at(world, hopperBlockEntity.getBlockPos()),
+ org.bukkit.craftbukkit.block.CraftBlock.at(world, extractBlockPos),
+ org.bukkit.event.inventory.HopperInventorySearchEvent.ContainerType.SOURCE
+ );
+ }
+
+ public Container lithium$getExtractBlockInventory(Level world, BlockPos extractBlockPos, BlockState extractBlockState) {
+ public @Nullable Container lithium$getExtractBlockInventory(Level world, BlockPos extractBlockPos, BlockState extractBlockState) {
+ Container blockInventory = this.extractBlockInventory;
+ if (this.extractionMode == HopperCachingState.BlockInventory.NO_BLOCK_INVENTORY) {
+ return null;
@@ -1933,7 +1956,7 @@ index f3d2d86b5065064013f37592ba7f53fdf4066629..8b101e41440bf78da1eae507153e118c
+ return blockInventory;
+ }
+
+ public Container lithium$getInsertBlockInventory(Level world) {
+ public @Nullable Container lithium$getInsertBlockInventory(Level world) {
+ Container blockInventory = this.insertBlockInventory;
+ if (this.insertionMode == HopperCachingState.BlockInventory.NO_BLOCK_INVENTORY) {
+ return null;
@@ -1974,8 +1997,17 @@ index f3d2d86b5065064013f37592ba7f53fdf4066629..8b101e41440bf78da1eae507153e118c
+ return blockInventory;
+ }
+
+ public @Nullable Container getInsertInventory(Level world) {
+ Container blockInventory = getInsertInventory0(world);
+ return org.bukkit.event.inventory.HopperInventorySearchEvent.getHandlerList().getRegisteredListeners().length == 0 ? blockInventory : runHopperInventorySearchEvent(
+ blockInventory,
+ org.bukkit.craftbukkit.block.CraftBlock.at(world, this.getBlockPos()),
+ org.bukkit.craftbukkit.block.CraftBlock.at(world, this.getBlockPos().relative(this.facing)),
+ org.bukkit.event.inventory.HopperInventorySearchEvent.ContainerType.DESTINATION
+ );
+ }
+
+ public Container getInsertInventory(Level world) {
+ public @Nullable Container getInsertInventory0(Level world) {
+ Container blockInventory = this.lithium$getInsertBlockInventory(world);
+ if (blockInventory != null) {
+ return blockInventory;
@@ -2080,7 +2112,7 @@ index f3d2d86b5065064013f37592ba7f53fdf4066629..8b101e41440bf78da1eae507153e118c
+ *
+ * @param insertInventory Block inventory / Blockentity inventory to be remembered
+ */
+ private void cacheInsertBlockInventory(Container insertInventory) {
+ private void cacheInsertBlockInventory(@Nullable Container insertInventory) {
+ assert !(insertInventory instanceof Entity);
+ if (insertInventory instanceof LithiumInventory optimizedInventory) {
+ this.cacheInsertLithiumInventory(optimizedInventory);
@@ -2128,7 +2160,7 @@ index f3d2d86b5065064013f37592ba7f53fdf4066629..8b101e41440bf78da1eae507153e118c
+ *
+ * @param extractInventory Block inventory / Blockentity inventory to be remembered
+ */
+ private void cacheExtractBlockInventory(Container extractInventory) {
+ private void cacheExtractBlockInventory(@Nullable Container extractInventory) {
+ assert !(extractInventory instanceof Entity);
+ if (extractInventory instanceof LithiumInventory optimizedInventory) {
+ this.cacheExtractLithiumInventory(optimizedInventory);
@@ -2185,7 +2217,7 @@ index f3d2d86b5065064013f37592ba7f53fdf4066629..8b101e41440bf78da1eae507153e118c
+ return itemEntities;
+ }
+
+ private static Boolean lithiumInsert(Level world, BlockPos pos, HopperBlockEntity hopperBlockEntity, Container insertInventory) {
+ private static @Nullable Boolean lithiumInsert(Level world, BlockPos pos, HopperBlockEntity hopperBlockEntity, @Nullable Container insertInventory) {
+ if (insertInventory == null || hopperBlockEntity instanceof net.minecraft.world.WorldlyContainer) {
+ //call the vanilla code to allow other mods inject features
+ //e.g. carpet mod allows hoppers to insert items into wool blocks
@@ -2250,7 +2282,7 @@ index f3d2d86b5065064013f37592ba7f53fdf4066629..8b101e41440bf78da1eae507153e118c
+ return false;
+ }
+
+ private static Boolean lithiumExtract(Level world, Hopper to, Container from) {
+ private static @Nullable Boolean lithiumExtract(Level world, Hopper to, Container from) {
+ if (!(to instanceof HopperBlockEntity hopperBlockEntity)) {
+ return null; //optimizations not implemented for hopper minecarts
+ }