From 7ca2d3e2683930073365dbe144fcb5bc8ca59fc6 Mon Sep 17 00:00:00 2001 From: 17146 <1714673995@qq.com> Date: Sun, 15 Jun 2025 00:43:58 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0todo?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../SuperbItemInterfaceBlockEntity.java | 76 ++++++++++++++++++- 1 file changed, 75 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/atsuishio/superbwarfare/block/entity/SuperbItemInterfaceBlockEntity.java b/src/main/java/com/atsuishio/superbwarfare/block/entity/SuperbItemInterfaceBlockEntity.java index 142ffcc2e..9a2df6376 100644 --- a/src/main/java/com/atsuishio/superbwarfare/block/entity/SuperbItemInterfaceBlockEntity.java +++ b/src/main/java/com/atsuishio/superbwarfare/block/entity/SuperbItemInterfaceBlockEntity.java @@ -2,22 +2,27 @@ package com.atsuishio.superbwarfare.block.entity; import com.atsuishio.superbwarfare.init.ModBlockEntities; import net.minecraft.core.BlockPos; +import net.minecraft.core.Direction; import net.minecraft.core.HolderLookup; import net.minecraft.core.NonNullList; import net.minecraft.nbt.CompoundTag; import net.minecraft.network.chat.Component; import net.minecraft.world.Container; import net.minecraft.world.ContainerHelper; +import net.minecraft.world.WorldlyContainer; import net.minecraft.world.entity.player.Inventory; import net.minecraft.world.entity.player.Player; import net.minecraft.world.inventory.AbstractContainerMenu; import net.minecraft.world.item.ItemStack; import net.minecraft.world.level.Level; +import net.minecraft.world.level.block.HopperBlock; import net.minecraft.world.level.block.entity.BaseContainerBlockEntity; import net.minecraft.world.level.block.state.BlockState; import org.jetbrains.annotations.NotNull; import javax.annotation.ParametersAreNonnullByDefault; +import java.util.function.BooleanSupplier; +import java.util.stream.IntStream; public class SuperbItemInterfaceBlockEntity extends BaseContainerBlockEntity { @@ -37,10 +42,79 @@ public class SuperbItemInterfaceBlockEntity extends BaseContainerBlockEntity { blockEntity.tickedGameTime = level.getGameTime(); if (!blockEntity.isOnCooldown()) { blockEntity.setCooldown(0); -// tryMoveItems(pLevel, pPos, pState, pBlockEntity, () -> suckInItems(pLevel, pBlockEntity)); +// tryMoveItems(level, pos, state, blockEntity, () -> suckInItems(level, blockEntity)); } } + private static boolean tryMoveItems(Level pLevel, BlockPos pPos, BlockState pState, SuperbItemInterfaceBlockEntity pBlockEntity, BooleanSupplier pValidator) { + if (!pLevel.isClientSide) { + if (!pBlockEntity.isOnCooldown() && pState.getValue(HopperBlock.ENABLED)) { + boolean flag = false; + if (!pBlockEntity.isEmpty()) { + flag = ejectItems(pLevel, pPos, pState, pBlockEntity); + } + + if (!pBlockEntity.inventoryFull()) { + flag |= pValidator.getAsBoolean(); + } + + if (flag) { + pBlockEntity.setCooldown(8); + setChanged(pLevel, pPos, pState); + return true; + } + } + + } + return false; + } + + private static boolean ejectItems(Level pLevel, BlockPos pPos, BlockState pState, SuperbItemInterfaceBlockEntity pSourceContainer) { +// if (net.minecraftforge.items.VanillaInventoryCodeHooks.insertHook(pSourceContainer)) return true; +// Container container = getAttachedContainer(pLevel, pPos, pState); +// if (container == null) { +// return false; +// } else { + // TODO 替换成开启吸物品的directions +// Direction direction = pState.getValue(HopperBlock.FACING).getOpposite(); +// if (!isFullContainer(container, direction)) { +// for (int i = 0; i < pSourceContainer.getContainerSize(); ++i) { +// if (!pSourceContainer.getItem(i).isEmpty()) { +// ItemStack itemstack = pSourceContainer.getItem(i).copy(); +// ItemStack itemstack1 = addItem(pSourceContainer, container, pSourceContainer.removeItem(i, 1), direction); +// if (itemstack1.isEmpty()) { +// container.setChanged(); +// return true; +// } +// +// pSourceContainer.setItem(i, itemstack); +// } +// } +// } + return false; +// } + } + + private static IntStream getSlots(Container pContainer, Direction pDirection) { + return pContainer instanceof WorldlyContainer ? IntStream.of(((WorldlyContainer) pContainer).getSlotsForFace(pDirection)) : IntStream.range(0, pContainer.getContainerSize()); + } + + private static boolean isFullContainer(Container pContainer, Direction pDirection) { + return getSlots(pContainer, pDirection).allMatch((p_59379_) -> { + ItemStack itemstack = pContainer.getItem(p_59379_); + return itemstack.getCount() >= itemstack.getMaxStackSize(); + }); + } + + private boolean inventoryFull() { + for (ItemStack itemstack : this.items) { + if (itemstack.isEmpty() || itemstack.getCount() != itemstack.getMaxStackSize()) { + return false; + } + } + return true; + } + @Override @ParametersAreNonnullByDefault protected void loadAdditional(CompoundTag tag, HolderLookup.Provider registries) {