Skip to content

Commit

Permalink
修复合成器在过滤模式下,未检查是否塞满
Browse files Browse the repository at this point in the history
  • Loading branch information
DancingSnow0517 committed Apr 10, 2024
1 parent b999ea3 commit 7dd59b0
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package dev.dubhe.anvilcraft.block.entity;

import dev.architectury.injectables.annotations.ExpectPlatform;
import dev.dubhe.anvilcraft.AnvilCraft;
import dev.dubhe.anvilcraft.api.depository.FilteredItemDepository;
import dev.dubhe.anvilcraft.api.depository.IItemDepository;
import dev.dubhe.anvilcraft.api.depository.ItemDepositoryHelper;
Expand Down Expand Up @@ -51,6 +52,7 @@ public void onContentsChanged(int slot) {
setChanged();
}
};
private int cooldown = AnvilCraft.config.autoCrafterCooldown;
private final CraftingContainer craftingContainer = new CraftingContainer() {
@Override
public int getWidth() {
Expand Down Expand Up @@ -125,6 +127,7 @@ public void fillStackedContents(StackedContents contents) {
}
};


public AutoCrafterBlockEntity(BlockEntityType<? extends BlockEntity> type, BlockPos pos, BlockState blockState) {
super(type, pos, blockState);
}
Expand All @@ -144,10 +147,25 @@ public void tick(@NotNull Level level, BlockPos pos) {
if (state.getValue(AutoCrafterBlock.LIT)) craft(level);
}

private boolean canCraft() {
if (cooldown > 0) return false;
if (!depository.isFilterEnabled()) return true;
for (int i = 0; i < depository.getSlots(); i++) {
if (depository.getStack(i).isEmpty()) return false;
if (!depository.isSlotDisabled(i) && depository.getStack(i).isEmpty()) return false;
}
return true;
}

@SuppressWarnings("UnreachableCode")
private void craft(@NotNull Level level) {
ItemStack result;
if (craftingContainer.isEmpty()) return;
if (cooldown <= 0) {
cooldown = AnvilCraft.config.autoCrafterCooldown;
}
cooldown--;
if (!canCraft()) return;
ItemStack result;
Optional<AutoCrafterCache> cacheOptional = cache.stream().filter(recipe -> recipe.test(craftingContainer)).findFirst();
Optional<CraftingRecipe> optional;
NonNullList<ItemStack> remaining;
Expand Down Expand Up @@ -211,12 +229,14 @@ private void craft(@NotNull Level level) {
public void load(@NotNull CompoundTag tag) {
super.load(tag);
depository.deserializeNBT(tag.getCompound("Inventory"));
cooldown = tag.getInt("Cooldown");
}

@Override
protected void saveAdditional(@NotNull CompoundTag tag) {
super.saveAdditional(tag);
tag.put("Inventory", this.depository.serializeNBT());
tag.putInt("Cooldown", cooldown);
}

@Override
Expand Down Expand Up @@ -296,7 +316,7 @@ private void spawnItemEntity(ItemStack stack) {
Vec3 center = getBlockPos().relative(getDirection()).getCenter();
Vector3f step = getDirection().step();
ItemEntity itemEntity = new ItemEntity(
getLevel(), center.x - step.x / 2, center.y - step.y / 2, center.z - step.z / 2,
getLevel(), center.x, center.y, center.z,
stack,
0.25 * step.x, 0.25 * step.y, 0.25 * step.z
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,9 @@ public class AnvilCraftConfig implements ConfigData {
@ConfigEntry.Gui.Tooltip
@ConfigEntry.BoundedDiscrete(max = 64, min = 1)
public int chuteMaxCooldown = 4;

@Comment("Maximum cooldown time of auto crafter (in ticks)")
@ConfigEntry.Gui.Tooltip
@ConfigEntry.BoundedDiscrete(max = 64, min = 1)
public int autoCrafterCooldown = 20;
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,8 @@ public static void init(RegistrateLangProvider provider) {

provider.add(OPTION_STRING.formatted(AnvilCraft.MOD_ID, "chuteMaxCooldown"), "Chute Max Cooldown");
provider.add(OPTION_TOOLTIP_STRING.formatted(AnvilCraft.MOD_ID, "chuteMaxCooldown"), "Maximum distance of chute");

provider.add(OPTION_STRING.formatted(AnvilCraft.MOD_ID, "autoCrafterCooldown"), "Auto Crafter Cooldown");
provider.add(OPTION_TOOLTIP_STRING.formatted(AnvilCraft.MOD_ID, "autoCrafterCooldown"), "Maximum cooldown time of auto crafter (in ticks)");
}
}

0 comments on commit 7dd59b0

Please sign in to comment.