From 0a8c06c697940fd5fea0d34e7b360c97a0842c21 Mon Sep 17 00:00:00 2001 From: Gugle Date: Sat, 6 Apr 2024 02:19:40 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B0=83=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../block/entity/AutoCrafterBlockEntity.java | 34 +++++++++---------- .../block/entity/ChuteBlockEntity.java | 1 + 2 files changed, 17 insertions(+), 18 deletions(-) diff --git a/common/src/main/java/dev/dubhe/anvilcraft/block/entity/AutoCrafterBlockEntity.java b/common/src/main/java/dev/dubhe/anvilcraft/block/entity/AutoCrafterBlockEntity.java index 1a11b118d..3cf9077b6 100644 --- a/common/src/main/java/dev/dubhe/anvilcraft/block/entity/AutoCrafterBlockEntity.java +++ b/common/src/main/java/dev/dubhe/anvilcraft/block/entity/AutoCrafterBlockEntity.java @@ -44,8 +44,6 @@ private boolean record = false; private final NonNullList disabled = this.getNewDisabled(); @Getter private final NonNullList<@Unmodifiable ItemStack> filter = this.getNewFilter(); - private static int levelHash = 1; - private static NonNullList remainingItemsCache; private final Deque cache = new ArrayDeque<>(); public AutoCrafterBlockEntity(BlockPos pos, BlockState blockState) { @@ -82,21 +80,19 @@ private static void craft(@NotNull Level level, @NotNull AutoCrafterBlockEntity if (!entity.canCraft()) return; Optional cacheOptional = entity.cache.stream().filter(recipe -> recipe.test(entity)).findFirst(); Optional optional; + NonNullList remaining; if (cacheOptional.isPresent()) { - optional = cacheOptional.get().getRecipe(); + AutoCrafterCache crafterCache = cacheOptional.get(); + optional = crafterCache.getRecipe(); + remaining = crafterCache.getRemaining(); } else { optional = level.getRecipeManager().getRecipeFor(RecipeType.CRAFTING, entity, level); - AutoCrafterCache cache = new AutoCrafterCache(entity, optional); + remaining = level.getRecipeManager().getRemainingItemsFor(RecipeType.CRAFTING, entity, level); + AutoCrafterCache cache = new AutoCrafterCache(entity, optional, remaining); entity.cache.push(cache); - while (entity.cache.size() >= 10) { - entity.cache.pop(); - } + while (entity.cache.size() >= 10) entity.cache.pop(); } if (optional.isEmpty()) return; - if (level.hashCode() != levelHash) { - levelHash = level.hashCode(); - remainingItemsCache = level.getRecipeManager().getRemainingItemsFor(RecipeType.CRAFTING, entity, level); - } itemStack = optional.get().assemble(entity, level.registryAccess()); if (!itemStack.isItemEnabled(level.enabledFeatures())) return; Container result = new SimpleContainer(1); @@ -112,12 +108,11 @@ private static void craft(@NotNull Level level, @NotNull AutoCrafterBlockEntity stack.shrink(1); entity.setItem(i, stack); } - Container container1 = new SimpleContainer(remainingItemsCache.size()); - for (int i = 0; i < remainingItemsCache.size(); i++) { - container1.setItem(i, remainingItemsCache.get(i)); - } - for (int i = 0; i < remainingItemsCache.size(); i++) { - entity.outputItem(itemDepository,entity.getDirection(), level, entity.getBlockPos(), container1, i, true, true, true, false); + Container container1 = new SimpleContainer(remaining.size()); + for (int i = 0; i < remaining.size(); i++) container1.setItem(i, remaining.get(i)); + for (int i = 0; i < remaining.size(); i++) { + if (container1.getItem(i).isEmpty()) continue; + entity.outputItem(itemDepository, entity.getDirection(), level, entity.getBlockPos(), container1, i, true, true, true, false); } level.updateNeighborsAt(pos, ModBlocks.AUTO_CRAFTER.get()); } @@ -222,8 +217,10 @@ public static class AutoCrafterCache implements Predicate { private final Container container; @Getter private final Optional recipe; + @Getter + private final NonNullList remaining; - public AutoCrafterCache(@NotNull Container container, Optional recipe) { + public AutoCrafterCache(@NotNull Container container, Optional recipe, NonNullList remaining) { this.container = new SimpleContainer(container.getContainerSize()); for (int i = 0; i < container.getContainerSize(); i++) { ItemStack item = container.getItem(i).copy(); @@ -231,6 +228,7 @@ public AutoCrafterCache(@NotNull Container container, Optional r this.container.setItem(i, item); } this.recipe = recipe; + this.remaining = remaining; } @Override diff --git a/common/src/main/java/dev/dubhe/anvilcraft/block/entity/ChuteBlockEntity.java b/common/src/main/java/dev/dubhe/anvilcraft/block/entity/ChuteBlockEntity.java index 8e8fcc4ef..c5fb8f817 100644 --- a/common/src/main/java/dev/dubhe/anvilcraft/block/entity/ChuteBlockEntity.java +++ b/common/src/main/java/dev/dubhe/anvilcraft/block/entity/ChuteBlockEntity.java @@ -98,6 +98,7 @@ public static void tick(Level level, BlockPos pos, BlockEntity e) { ChuteBlockEntity.tryMoveItems(level, pos, state, entity, () -> ChuteBlockEntity.suckInItems(level, entity)); entity.dropOrInsert(level, pos); } + level.updateNeighborsAt(pos, ModBlocks.AUTO_CRAFTER.get()); } public void dropOrInsert(Level level, @NotNull BlockPos pos) {