注册雷达menu

This commit is contained in:
17146 2024-12-25 23:23:37 +08:00
parent 779e71d17e
commit ee8909061d
5 changed files with 163 additions and 4 deletions

View file

@ -3,6 +3,9 @@ package com.atsuishio.superbwarfare.block;
import com.atsuishio.superbwarfare.block.entity.FuMO25BlockEntity; import com.atsuishio.superbwarfare.block.entity.FuMO25BlockEntity;
import com.atsuishio.superbwarfare.init.ModBlockEntities; import com.atsuishio.superbwarfare.init.ModBlockEntities;
import net.minecraft.core.BlockPos; 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.BlockGetter;
import net.minecraft.world.level.Level; import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.Block; 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.entity.BlockEntityType;
import net.minecraft.world.level.block.state.BlockBehaviour; import net.minecraft.world.level.block.state.BlockBehaviour;
import net.minecraft.world.level.block.state.BlockState; 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.CollisionContext;
import net.minecraft.world.phys.shapes.Shapes; import net.minecraft.world.phys.shapes.Shapes;
import net.minecraft.world.phys.shapes.VoxelShape; 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()); 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 @Override
public VoxelShape getShape(BlockState pState, BlockGetter pLevel, BlockPos pPos, CollisionContext pContext) { 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)); 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 <E extends BlockEntity, A extends BlockEntity> BlockEntityTicker<A> createTickerHelper(BlockEntityType<A> pServerType, BlockEntityType<E> pClientType, BlockEntityTicker<? super E> pTicker) { protected static <E extends BlockEntity, A extends BlockEntity> BlockEntityTicker<A> createTickerHelper(BlockEntityType<A> pServerType, BlockEntityType<E> pClientType, BlockEntityTicker<? super E> pTicker) {
return pClientType == pServerType ? (BlockEntityTicker<A>) pTicker : null; return pClientType == pServerType ? (BlockEntityTicker<A>) 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);
}
} }

View file

@ -34,6 +34,7 @@ import javax.annotation.Nullable;
@SuppressWarnings("deprecation") @SuppressWarnings("deprecation")
public class ReforgingTableBlock extends Block { public class ReforgingTableBlock extends Block {
public static final BooleanProperty WATERLOGGED = BlockStateProperties.WATERLOGGED; public static final BooleanProperty WATERLOGGED = BlockStateProperties.WATERLOGGED;
public static final DirectionProperty FACING = HorizontalDirectionalBlock.FACING; public static final DirectionProperty FACING = HorizontalDirectionalBlock.FACING;
private static final Component CONTAINER_TITLE = Component.translatable("container.superbwarfare.reforging_table"); 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); return super.updateShape(state, facing, facingState, world, currentPos, facingPos);
} }
@Override @Override
@Nullable @Nullable
public MenuProvider getMenuProvider(BlockState pState, Level pLevel, BlockPos pPos) { public MenuProvider getMenuProvider(BlockState pState, Level pLevel, BlockPos pPos) {

View file

@ -1,6 +1,7 @@
package com.atsuishio.superbwarfare.block.entity; package com.atsuishio.superbwarfare.block.entity;
import com.atsuishio.superbwarfare.init.ModBlockEntities; import com.atsuishio.superbwarfare.init.ModBlockEntities;
import com.atsuishio.superbwarfare.menu.FuMO25Menu;
import net.minecraft.core.BlockPos; import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction; import net.minecraft.core.Direction;
import net.minecraft.nbt.CompoundTag; 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.Inventory;
import net.minecraft.world.entity.player.Player; import net.minecraft.world.entity.player.Player;
import net.minecraft.world.inventory.AbstractContainerMenu; import net.minecraft.world.inventory.AbstractContainerMenu;
import net.minecraft.world.inventory.ContainerLevelAccess;
import net.minecraft.world.level.Level; import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.entity.BlockEntity; import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.state.BlockState; 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; public static final int MAX_ENERGY = 1000000;
// 固定距离以后有人改动这个需要自行解决GUI渲染问题
public static final int DEFAULT_RANGE = 96;
public static final int MAX_RANGE = 128;
private LazyOptional<EnergyStorage> energyHandler; private LazyOptional<EnergyStorage> energyHandler;
public FuncType type = FuncType.NORMAL; public FuncType type = FuncType.NORMAL;
public int time = 0;
public FuMO25BlockEntity(BlockPos pPos, BlockState pBlockState) { public FuMO25BlockEntity(BlockPos pPos, BlockState pBlockState) {
super(ModBlockEntities.FUMO_25.get(), pPos, pBlockState); super(ModBlockEntities.FUMO_25.get(), pPos, pBlockState);
this.energyHandler = LazyOptional.of(() -> new EnergyStorage(MAX_ENERGY)); 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"))); getCapability(ForgeCapabilities.ENERGY).ifPresent(handler -> ((EnergyStorage) handler).deserializeNBT(pTag.get("Energy")));
} }
this.type = FuncType.values()[Mth.clamp(pTag.getInt("Type"), 0, 3)]; this.type = FuncType.values()[Mth.clamp(pTag.getInt("Type"), 0, 3)];
this.time = pTag.getInt("Time");
} }
@Override @Override
@ -58,17 +65,19 @@ public class FuMO25BlockEntity extends BlockEntity implements MenuProvider {
getCapability(ForgeCapabilities.ENERGY).ifPresent(handler -> pTag.put("Energy", ((EnergyStorage) handler).serializeNBT())); getCapability(ForgeCapabilities.ENERGY).ifPresent(handler -> pTag.put("Energy", ((EnergyStorage) handler).serializeNBT()));
pTag.putInt("Type", this.type.ordinal()); pTag.putInt("Type", this.type.ordinal());
pTag.putInt("Time", this.time);
} }
@Override @Override
public Component getDisplayName() { public Component getDisplayName() {
return null; return Component.translatable("container.superbwarfare.fumo_25");
} }
@Nullable @Nullable
@Override @Override
public AbstractContainerMenu createMenu(int pContainerId, Inventory pPlayerInventory, Player pPlayer) { 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 @Override

View file

@ -2,6 +2,7 @@ package com.atsuishio.superbwarfare.init;
import com.atsuishio.superbwarfare.ModUtils; import com.atsuishio.superbwarfare.ModUtils;
import com.atsuishio.superbwarfare.menu.ChargingStationMenu; import com.atsuishio.superbwarfare.menu.ChargingStationMenu;
import com.atsuishio.superbwarfare.menu.FuMO25Menu;
import com.atsuishio.superbwarfare.menu.ReforgingTableMenu; import com.atsuishio.superbwarfare.menu.ReforgingTableMenu;
import com.atsuishio.superbwarfare.menu.VehicleMenu; import com.atsuishio.superbwarfare.menu.VehicleMenu;
import net.minecraft.world.inventory.MenuType; import net.minecraft.world.inventory.MenuType;
@ -23,4 +24,7 @@ public class ModMenuTypes {
public static final RegistryObject<MenuType<VehicleMenu>> VEHICLE_MENU = public static final RegistryObject<MenuType<VehicleMenu>> VEHICLE_MENU =
REGISTRY.register("vehicle_menu", REGISTRY.register("vehicle_menu",
() -> IForgeMenuType.create((windowId, inv, data) -> new VehicleMenu(windowId, inv))); () -> IForgeMenuType.create((windowId, inv, data) -> new VehicleMenu(windowId, inv)));
public static final RegistryObject<MenuType<FuMO25Menu>> FUMO_25_MENU =
REGISTRY.register("fumo_25_menu",
() -> IForgeMenuType.create((windowId, inv, data) -> new FuMO25Menu(windowId, inv)));
} }

View file

@ -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());
}
}
}