Skip to content

Commit

Permalink
Merge pull request Anvil-Dev#186 from Gu-ZT/Depository
Browse files Browse the repository at this point in the history
提取合成器和溜槽的重复代码
  • Loading branch information
Gugle2308 authored Apr 5, 2024
2 parents 6dee4b5 + 2d64fc6 commit 8f2e834
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -134,23 +134,7 @@ protected void saveAdditional(@NotNull CompoundTag tag) {
this.saveTag(tag);
}

@Override
public boolean canPlaceItem(int index, ItemStack insertingStack) {
if (this.getDisabled().get(index)) return false;
ItemStack storedStack = this.items.get(index);
ItemStack filterStack = this.getFilter().get(index);
if (isRecord() && filterStack.isEmpty()) return insertingStack.isEmpty();
int count = storedStack.getCount();
if (count >= storedStack.getMaxStackSize()) {
return false;
}
if (storedStack.isEmpty()) {
return filterStack.isEmpty() || ItemStack.isSameItemSameTags(insertingStack, filterStack);
}
return !this.smallerStackExist(count, storedStack, index);
}

private boolean smallerStackExist(int count, ItemStack itemStack, int index) {
public boolean smallerStackExist(int count, ItemStack itemStack, int index) {
for (int index2 = index + 1; index2 < 9; ++index2) {
ItemStack itemStack1;
if (this.getDisabled().get(index2) || isRecord() && getFilter().get(index2).isEmpty() || !(itemStack1 = this.getItem(index2)).isEmpty() && (itemStack1.getCount() >= count || !ItemStack.isSameItemSameTags(itemStack1, itemStack)))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,23 +49,8 @@ public ChuteBlockEntity(BlockPos pos, BlockState blockState) {
public ChuteBlockEntity(BlockEntityType<? extends BlockEntity> type, BlockPos pos, BlockState blockState) {
super(type, pos, blockState, 9);
}
@Override
public boolean canPlaceItem(int index, @NotNull ItemStack insertingStack) {
if (this.getDisabled().get(index)) return false;
ItemStack storedStack = this.items.get(index);
ItemStack filterStack = this.getFilter().get(index);
if (isRecord() && filterStack.isEmpty()) return insertingStack.isEmpty();
int count = storedStack.getCount();
if (count >= storedStack.getMaxStackSize()) {
return false;
}
if (storedStack.isEmpty()) {
return filterStack.isEmpty() || ItemStack.isSameItemSameTags(insertingStack, filterStack);
}
return !this.smallerStackExist(count, storedStack, index);
}

private boolean smallerStackExist(int count, ItemStack itemStack, int index) {
public boolean smallerStackExist(int count, ItemStack itemStack, int index) {
for (int index2 = index + 1; index2 < 9; ++index2) {
ItemStack itemStack1;
if (this.getDisabled().get(index2) ||
Expand All @@ -78,6 +63,7 @@ private boolean smallerStackExist(int count, ItemStack itemStack, int index) {
}
return false;
}

@Override
protected @Nonnull Component getDefaultName() {
return Component.translatable("block.anvilcraft.chute");
Expand Down Expand Up @@ -133,8 +119,7 @@ private static boolean tryMoveItems(@Nonnull Level level, BlockPos pos, BlockSta
if (!blockEntity.isOnCooldown() && state.getValue(HopperBlock.ENABLED)) {
boolean bl = false;
if (!blockEntity.isEmpty()) {
// TODO
// bl = HopperBlockEntity.ejectItems(level, pos, state, blockEntity);
bl = ChuteBlockEntity.ejectItems(level, pos, state, blockEntity);
}
if (!blockEntity.inventoryFull()) {
bl |= validator.getAsBoolean();
Expand All @@ -147,6 +132,23 @@ private static boolean tryMoveItems(@Nonnull Level level, BlockPos pos, BlockSta
return false;
}

private static boolean ejectItems(Level level, BlockPos pos, @NotNull BlockState state, Container sourceContainer) {
if (!state.is(ModBlocks.CHUTE.get())) return false;
Direction direction = state.getValue(ChuteBlock.FACING);
ItemDepository depository = ItemDepository.getItemDepository(level, pos.relative(direction), direction.getOpposite());
if (depository == null) return false;
for (int i = 0; i < sourceContainer.getContainerSize(); ++i) {
if (sourceContainer.getItem(i).isEmpty()) continue;
ItemStack itemStack = sourceContainer.getItem(i).copy();
sourceContainer.removeItem(i, itemStack.getCount());
int count = (int) depository.inject(itemStack, itemStack.getCount(), false);
itemStack.setCount(count);
if (count == 0) return true;
sourceContainer.setItem(i, itemStack);
}
return false;
}

public static boolean suckInItems(Level level, Hopper hopper) {
Container container = HopperBlockEntity.getSourceContainer(level, hopper);
if (container != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,13 @@ default void safeSetRecord(boolean record) {
}

NonNullList<Boolean> getDisabled();

NonNullList<@Unmodifiable ItemStack> getFilter();

default NonNullList<Boolean> getNewDisabled() {
return NonNullList.withSize(this.getContainerSize(), false);
}

default NonNullList<@Unmodifiable ItemStack> getNewFilter() {
return NonNullList.withSize(this.getContainerSize(), ItemStack.EMPTY);
}
Expand All @@ -54,4 +56,24 @@ default void saveTag(@NotNull CompoundTag tag) {
ContainerHelper.saveAllItems(filter, this.getFilter());
tag.put("filter", filter);
}

default boolean canPlaceItem(int index, @NotNull ItemStack insertingStack) {
if (this.getDisabled().get(index)) return false;
ItemStack storedStack = this.getItems().get(index);
ItemStack filterStack = this.getFilter().get(index);
if (isRecord() && filterStack.isEmpty()) return insertingStack.isEmpty();
int count = storedStack.getCount();
if (count >= storedStack.getMaxStackSize()) {
return false;
}
if (storedStack.isEmpty()) {
return filterStack.isEmpty() || ItemStack.isSameItemSameTags(insertingStack, filterStack);
}
return !this.smallerStackExist(count, storedStack, index);
}

@SuppressWarnings("BooleanMethodIsAlwaysInverted")
boolean smallerStackExist(int count, ItemStack itemStack, int index);

NonNullList<ItemStack> getItems();
}

0 comments on commit 8f2e834

Please sign in to comment.