Skip to content

Commit

Permalink
调整
Browse files Browse the repository at this point in the history
  • Loading branch information
Gu-ZT committed Apr 5, 2024
1 parent 6894627 commit 0a8c06c
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,6 @@ private boolean record = false;
private final NonNullList<Boolean> disabled = this.getNewDisabled();
@Getter
private final NonNullList<@Unmodifiable ItemStack> filter = this.getNewFilter();
private static int levelHash = 1;
private static NonNullList<ItemStack> remainingItemsCache;
private final Deque<AutoCrafterCache> cache = new ArrayDeque<>();

public AutoCrafterBlockEntity(BlockPos pos, BlockState blockState) {
Expand Down Expand Up @@ -82,21 +80,19 @@ private static void craft(@NotNull Level level, @NotNull AutoCrafterBlockEntity
if (!entity.canCraft()) return;
Optional<AutoCrafterCache> cacheOptional = entity.cache.stream().filter(recipe -> recipe.test(entity)).findFirst();
Optional<CraftingRecipe> optional;
NonNullList<ItemStack> 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);
Expand All @@ -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());
}
Expand Down Expand Up @@ -222,15 +217,18 @@ public static class AutoCrafterCache implements Predicate<Container> {
private final Container container;
@Getter
private final Optional<CraftingRecipe> recipe;
@Getter
private final NonNullList<ItemStack> remaining;

public AutoCrafterCache(@NotNull Container container, Optional<CraftingRecipe> recipe) {
public AutoCrafterCache(@NotNull Container container, Optional<CraftingRecipe> recipe, NonNullList<ItemStack> remaining) {
this.container = new SimpleContainer(container.getContainerSize());
for (int i = 0; i < container.getContainerSize(); i++) {
ItemStack item = container.getItem(i).copy();
item.setCount(1);
this.container.setItem(i, item);
}
this.recipe = recipe;
this.remaining = remaining;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 0a8c06c

Please sign in to comment.