From ee8909061d7314af23278ed326b549ab3fcb0ae2 Mon Sep 17 00:00:00 2001 From: 17146 <1714673995@qq.com> Date: Wed, 25 Dec 2024 23:23:37 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B3=A8=E5=86=8C=E9=9B=B7=E8=BE=BEmenu?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../superbwarfare/block/FuMO25Block.java | 29 +++++ .../block/ReforgingTableBlock.java | 2 +- .../block/entity/FuMO25BlockEntity.java | 15 ++- .../superbwarfare/init/ModMenuTypes.java | 4 + .../superbwarfare/menu/FuMO25Menu.java | 117 ++++++++++++++++++ 5 files changed, 163 insertions(+), 4 deletions(-) create mode 100644 src/main/java/com/atsuishio/superbwarfare/menu/FuMO25Menu.java diff --git a/src/main/java/com/atsuishio/superbwarfare/block/FuMO25Block.java b/src/main/java/com/atsuishio/superbwarfare/block/FuMO25Block.java index 09ecd1c24..eadba9d7c 100644 --- a/src/main/java/com/atsuishio/superbwarfare/block/FuMO25Block.java +++ b/src/main/java/com/atsuishio/superbwarfare/block/FuMO25Block.java @@ -3,6 +3,9 @@ package com.atsuishio.superbwarfare.block; import com.atsuishio.superbwarfare.block.entity.FuMO25BlockEntity; import com.atsuishio.superbwarfare.init.ModBlockEntities; import net.minecraft.core.BlockPos; +import net.minecraft.world.InteractionHand; +import net.minecraft.world.InteractionResult; +import net.minecraft.world.entity.player.Player; import net.minecraft.world.level.BlockGetter; import net.minecraft.world.level.Level; import net.minecraft.world.level.block.Block; @@ -13,6 +16,7 @@ import net.minecraft.world.level.block.entity.BlockEntityTicker; import net.minecraft.world.level.block.entity.BlockEntityType; import net.minecraft.world.level.block.state.BlockBehaviour; import net.minecraft.world.level.block.state.BlockState; +import net.minecraft.world.phys.BlockHitResult; import net.minecraft.world.phys.shapes.CollisionContext; import net.minecraft.world.phys.shapes.Shapes; import net.minecraft.world.phys.shapes.VoxelShape; @@ -25,6 +29,19 @@ public class FuMO25Block extends Block implements EntityBlock { super(BlockBehaviour.Properties.of().sound(SoundType.METAL).strength(3.0f).requiresCorrectToolForDrops()); } + @Override + public InteractionResult use(BlockState pState, Level pLevel, BlockPos pPos, Player pPlayer, InteractionHand pHand, BlockHitResult pHit) { + if (pLevel.isClientSide) { + return InteractionResult.SUCCESS; + } else { + BlockEntity blockentity = pLevel.getBlockEntity(pPos); + if (blockentity instanceof FuMO25BlockEntity blockEntity) { + pPlayer.openMenu(blockEntity); + } + return InteractionResult.CONSUME; + } + } + @Override public VoxelShape getShape(BlockState pState, BlockGetter pLevel, BlockPos pPos, CollisionContext pContext) { return Shapes.or(box(1, 0, 1, 15, 1, 15), box(7, 1, 7, 9, 16, 9)); @@ -50,4 +67,16 @@ public class FuMO25Block extends Block implements EntityBlock { protected static BlockEntityTicker createTickerHelper(BlockEntityType pServerType, BlockEntityType pClientType, BlockEntityTicker pTicker) { return pClientType == pServerType ? (BlockEntityTicker) pTicker : null; } + + @Override + public void onRemove(BlockState pState, Level pLevel, BlockPos pPos, BlockState pNewState, boolean pMovedByPiston) { + if (!pState.is(pNewState.getBlock())) { + BlockEntity blockentity = pLevel.getBlockEntity(pPos); + if (blockentity instanceof FuMO25BlockEntity) { + pLevel.updateNeighbourForOutputSignal(pPos, this); + } + } + + super.onRemove(pState, pLevel, pPos, pNewState, pMovedByPiston); + } } diff --git a/src/main/java/com/atsuishio/superbwarfare/block/ReforgingTableBlock.java b/src/main/java/com/atsuishio/superbwarfare/block/ReforgingTableBlock.java index 67e729eb2..d1fa13894 100644 --- a/src/main/java/com/atsuishio/superbwarfare/block/ReforgingTableBlock.java +++ b/src/main/java/com/atsuishio/superbwarfare/block/ReforgingTableBlock.java @@ -34,6 +34,7 @@ import javax.annotation.Nullable; @SuppressWarnings("deprecation") public class ReforgingTableBlock extends Block { + public static final BooleanProperty WATERLOGGED = BlockStateProperties.WATERLOGGED; public static final DirectionProperty FACING = HorizontalDirectionalBlock.FACING; private static final Component CONTAINER_TITLE = Component.translatable("container.superbwarfare.reforging_table"); @@ -115,7 +116,6 @@ public class ReforgingTableBlock extends Block { return super.updateShape(state, facing, facingState, world, currentPos, facingPos); } - @Override @Nullable public MenuProvider getMenuProvider(BlockState pState, Level pLevel, BlockPos pPos) { diff --git a/src/main/java/com/atsuishio/superbwarfare/block/entity/FuMO25BlockEntity.java b/src/main/java/com/atsuishio/superbwarfare/block/entity/FuMO25BlockEntity.java index 2d98cc848..f41bcf7bc 100644 --- a/src/main/java/com/atsuishio/superbwarfare/block/entity/FuMO25BlockEntity.java +++ b/src/main/java/com/atsuishio/superbwarfare/block/entity/FuMO25BlockEntity.java @@ -1,6 +1,7 @@ package com.atsuishio.superbwarfare.block.entity; import com.atsuishio.superbwarfare.init.ModBlockEntities; +import com.atsuishio.superbwarfare.menu.FuMO25Menu; import net.minecraft.core.BlockPos; import net.minecraft.core.Direction; import net.minecraft.nbt.CompoundTag; @@ -11,6 +12,7 @@ import net.minecraft.world.MenuProvider; import net.minecraft.world.entity.player.Inventory; import net.minecraft.world.entity.player.Player; import net.minecraft.world.inventory.AbstractContainerMenu; +import net.minecraft.world.inventory.ContainerLevelAccess; import net.minecraft.world.level.Level; import net.minecraft.world.level.block.entity.BlockEntity; import net.minecraft.world.level.block.state.BlockState; @@ -24,13 +26,17 @@ public class FuMO25BlockEntity extends BlockEntity implements MenuProvider { public static final int MAX_ENERGY = 1000000; + // 固定距离,以后有人改动这个需要自行解决GUI渲染问题 + public static final int DEFAULT_RANGE = 96; + public static final int MAX_RANGE = 128; + private LazyOptional energyHandler; public FuncType type = FuncType.NORMAL; + public int time = 0; public FuMO25BlockEntity(BlockPos pPos, BlockState pBlockState) { super(ModBlockEntities.FUMO_25.get(), pPos, pBlockState); - this.energyHandler = LazyOptional.of(() -> new EnergyStorage(MAX_ENERGY)); } @@ -50,6 +56,7 @@ public class FuMO25BlockEntity extends BlockEntity implements MenuProvider { getCapability(ForgeCapabilities.ENERGY).ifPresent(handler -> ((EnergyStorage) handler).deserializeNBT(pTag.get("Energy"))); } this.type = FuncType.values()[Mth.clamp(pTag.getInt("Type"), 0, 3)]; + this.time = pTag.getInt("Time"); } @Override @@ -58,17 +65,19 @@ public class FuMO25BlockEntity extends BlockEntity implements MenuProvider { getCapability(ForgeCapabilities.ENERGY).ifPresent(handler -> pTag.put("Energy", ((EnergyStorage) handler).serializeNBT())); pTag.putInt("Type", this.type.ordinal()); + pTag.putInt("Time", this.time); } @Override public Component getDisplayName() { - return null; + return Component.translatable("container.superbwarfare.fumo_25"); } @Nullable @Override public AbstractContainerMenu createMenu(int pContainerId, Inventory pPlayerInventory, Player pPlayer) { - return null; + if (this.level == null) return null; + return new FuMO25Menu(pContainerId, pPlayerInventory, ContainerLevelAccess.create(this.level, this.getBlockPos())); } @Override diff --git a/src/main/java/com/atsuishio/superbwarfare/init/ModMenuTypes.java b/src/main/java/com/atsuishio/superbwarfare/init/ModMenuTypes.java index 55a660a9c..a96123d40 100644 --- a/src/main/java/com/atsuishio/superbwarfare/init/ModMenuTypes.java +++ b/src/main/java/com/atsuishio/superbwarfare/init/ModMenuTypes.java @@ -2,6 +2,7 @@ package com.atsuishio.superbwarfare.init; import com.atsuishio.superbwarfare.ModUtils; import com.atsuishio.superbwarfare.menu.ChargingStationMenu; +import com.atsuishio.superbwarfare.menu.FuMO25Menu; import com.atsuishio.superbwarfare.menu.ReforgingTableMenu; import com.atsuishio.superbwarfare.menu.VehicleMenu; import net.minecraft.world.inventory.MenuType; @@ -23,4 +24,7 @@ public class ModMenuTypes { public static final RegistryObject> VEHICLE_MENU = REGISTRY.register("vehicle_menu", () -> IForgeMenuType.create((windowId, inv, data) -> new VehicleMenu(windowId, inv))); + public static final RegistryObject> FUMO_25_MENU = + REGISTRY.register("fumo_25_menu", + () -> IForgeMenuType.create((windowId, inv, data) -> new FuMO25Menu(windowId, inv))); } diff --git a/src/main/java/com/atsuishio/superbwarfare/menu/FuMO25Menu.java b/src/main/java/com/atsuishio/superbwarfare/menu/FuMO25Menu.java new file mode 100644 index 000000000..ed3daee73 --- /dev/null +++ b/src/main/java/com/atsuishio/superbwarfare/menu/FuMO25Menu.java @@ -0,0 +1,117 @@ +package com.atsuishio.superbwarfare.menu; + +import com.atsuishio.superbwarfare.init.ModBlocks; +import com.atsuishio.superbwarfare.init.ModItems; +import com.atsuishio.superbwarfare.init.ModMenuTypes; +import net.minecraft.world.Container; +import net.minecraft.world.SimpleContainer; +import net.minecraft.world.entity.player.Inventory; +import net.minecraft.world.entity.player.Player; +import net.minecraft.world.inventory.AbstractContainerMenu; +import net.minecraft.world.inventory.ContainerLevelAccess; +import net.minecraft.world.inventory.Slot; +import net.minecraft.world.item.ItemStack; + +public class FuMO25Menu extends AbstractContainerMenu { + + protected final Container container; + protected final ContainerLevelAccess access; + + public static final int X_OFFSET = 0; + public static final int Y_OFFSET = 11; + + public FuMO25Menu(int pContainerId, Inventory pPlayerInventory) { + this(pContainerId, pPlayerInventory, new SimpleContainer(1), ContainerLevelAccess.NULL); + } + + public FuMO25Menu(int pContainerId, Inventory pPlayerInventory, ContainerLevelAccess access) { + this(pContainerId, pPlayerInventory, new SimpleContainer(1), access); + } + + public FuMO25Menu(int pContainerId, Inventory inventory, Container container, ContainerLevelAccess access) { + super(ModMenuTypes.FUMO_25_MENU.get(), pContainerId); + + checkContainerSize(container, 1); + + this.container = container; + this.access = access; + + this.addSlot(new ParaSlot(container, 0, 8 + X_OFFSET, 11 + Y_OFFSET)); + + for (int i = 0; i < 3; ++i) { + for (int j = 0; j < 9; ++j) { + this.addSlot(new Slot(inventory, j + i * 9 + 9, 8 + j * 18 + X_OFFSET, 84 + i * 18 + Y_OFFSET)); + } + } + + for (int k = 0; k < 9; ++k) { + this.addSlot(new Slot(inventory, k, 8 + k * 18 + X_OFFSET, 142 + Y_OFFSET)); + } + } + + @Override + public ItemStack quickMoveStack(Player pPlayer, int pIndex) { + ItemStack itemstack = ItemStack.EMPTY; + Slot slot = this.slots.get(pIndex); + if (slot.hasItem()) { + ItemStack itemstack1 = slot.getItem(); + itemstack = itemstack1.copy(); + if (pIndex != 0) { + if (!this.moveItemStackTo(itemstack1, 0, 1, false)) { + return ItemStack.EMPTY; + } else if (pIndex >= 1 && pIndex < 28) { + if (!this.moveItemStackTo(itemstack1, 28, 37, false)) { + return ItemStack.EMPTY; + } + } else if (pIndex >= 28 && pIndex < 37 && !this.moveItemStackTo(itemstack1, 1, 28, false)) { + return ItemStack.EMPTY; + } + } else if (!this.moveItemStackTo(itemstack1, 1, 37, false)) { + return ItemStack.EMPTY; + } + + if (itemstack1.isEmpty()) { + slot.setByPlayer(ItemStack.EMPTY); + } else { + slot.setChanged(); + } + + if (itemstack1.getCount() == itemstack.getCount()) { + return ItemStack.EMPTY; + } + + slot.onTake(pPlayer, itemstack1); + } + + return itemstack; + } + + @Override + public boolean stillValid(Player pPlayer) { + return this.access.evaluate((level, pos) -> level.getBlockState(pos).is(ModBlocks.FUMO_25.get()) + && pPlayer.distanceToSqr((double) pos.getX() + 0.5D, (double) pos.getY() + 0.5D, (double) pos.getZ() + 0.5D) <= 64.0D, true); + } + + @Override + public void removed(Player pPlayer) { + this.access.execute((level, pos) -> { + ItemStack para = this.container.getItem(0); + if (!para.isEmpty()) { + pPlayer.getInventory().placeItemBackInInventory(para); + } + this.container.removeItemNoUpdate(0); + }); + } + + static class ParaSlot extends Slot { + + public ParaSlot(Container pContainer, int pSlot, int pX, int pY) { + super(pContainer, pSlot, pX, pY); + } + + @Override + public boolean mayPlace(ItemStack pStack) { + return pStack.is(ModItems.FIRING_PARAMETERS.get()); + } + } +}