package net.mattias.pedestals.core.world.inventory; import…
- User
package net.mattias.pedestals.core.world.inventory;
import net.mattias.pedestals.core.registry.ModBlocks; import net.mattias.pedestals.core.registry.ModMenus; import net.mattias.pedestals.core.world.block.entity.PedestalBlockEntity; import net.minecraft.network.FriendlyByteBuf; 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; import net.minecraft.world.level.Level; import net.minecraft.world.level.block.entity.BlockEntity; import net.neoforged.neoforge.items.SlotItemHandler;
public class PedestalMenu extends AbstractContainerMenu { public final PedestalBlockEntity blockEntity; private final Level level;
public PedestalMenu(int pContainerId, Inventory inv, FriendlyByteBuf extraData) { this(pContainerId, inv, inv.player.level().getBlockEntity(extraData.readBlockPos())); } public PedestalMenu(int pContainerId, Inventory inv, BlockEntity blockEntity) { super(ModMenus.PEDESTAL.get(), pContainerId); this.blockEntity = ((PedestalBlockEntity) blockEntity); this.level = inv.player.level(); addPlayerInventory(inv); addPlayerHotbar(inv); this.addSlot(new SlotItemHandler(this.blockEntity.inventory, 0, 80, 35)); } // CREDIT GOES TO: diesieben07 | https://github.com/diesieben07/SevenCommons // must assign a slot number to each of the slots used by the GUI. // For this container, we can see both the tile inventory's slots as well as the player inventory slots and the hotbar. // Each time we add a Slot to the container, it automatically increases the slotIndex, which means // 0 - 8 = hotbar slots (which will map to the InventoryPlayer slot numbers 0 - 8) // 9 - 35 = player inventory slots (which map to the InventoryPlayer slot numbers 9 - 35) // 36 - 44 = TileInventory slots, which map to our TileEntity slot numbers 0 - 8) private static final int HOTBAR_SLOT_COUNT = 9; private static final int PLAYER_INVENTORY_ROW_COUNT = 3; private static final int PLAYER_INVENTORY_COLUMN_COUNT = 9; private static final int PLAYER_INVENTORY_SLOT_COUNT = PLAYER_INVENTORY_COLUMN_COUNT * PLAYER_INVENTORY_ROW_COUNT; private static final int VANILLA_SLOT_COUNT = HOTBAR_SLOT_COUNT + PLAYER_INVENTORY_SLOT_COUNT; private static final int VANILLA_FIRST_SLOT_INDEX = 0; private static final int TE_INVENTORY_FIRST_SLOT_INDEX = VANILLA_FIRST_SLOT_INDEX + VANILLA_SLOT_COUNT; // THIS YOU HAVE TO DEFINE! private static final int TE_INVENTORY_SLOT_COUNT = 1; // must be the number of slots you have! @Override public ItemStack quickMoveStack(Player playerIn, int pIndex) { Slot sourceSlot = slots.get(pIndex); if (sourceSlot == null || !sourceSlot.hasItem()) return ItemStack.EMPTY; //EMPTY_ITEM ItemStack sourceStack = sourceSlot.getItem(); ItemStack copyOfSourceStack = sourceStack.copy(); // Check if the slot clicked is one of the vanilla container slots if (pIndex < VANILLA_FIRST_SLOT_INDEX + VANILLA_SLOT_COUNT) { // This is a vanilla container slot so merge the stack into the tile inventory if (!moveItemStackTo(sourceStack, TE_INVENTORY_FIRST_SLOT_INDEX, TE_INVENTORY_FIRST_SLOT_INDEX + TE_INVENTORY_SLOT_COUNT, false)) { return ItemStack.EMPTY; // EMPTY_ITEM } } else if (pIndex < TE_INVENTORY_FIRST_SLOT_INDEX + TE_INVENTORY_SLOT_COUNT) { // This is a TE slot so merge the stack into the players inventory if (!moveItemStackTo(sourceStack, VANILLA_FIRST_SLOT_INDEX, VANILLA_FIRST_SLOT_INDEX + VANILLA_SLOT_COUNT, false)) { return ItemStack.EMPTY; } } else { System.out.println("Invalid slotIndex:" + pIndex); return ItemStack.EMPTY; } // If stack size == 0 (the entire stack was moved) set slot contents to null if (sourceStack.getCount() == 0) { sourceSlot.set(ItemStack.EMPTY); } else { sourceSlot.setChanged(); } sourceSlot.onTake(playerIn, sourceStack); return copyOfSourceStack; } @Override public boolean stillValid(Player pPlayer) { return stillValid(ContainerLevelAccess.create(level, blockEntity.getBlockPos()), pPlayer, ModBlocks.PEDESTAL.get()); } private void addPlayerInventory(Inventory playerInventory) { for (int i = 0; i < 3; ++i) { for (int l = 0; l < 9; ++l) { this.addSlot(new Slot(playerInventory, l + i * 9 + 9, 8 + l * 18, 84 + i * 18)); } } } private void addPlayerHotbar(Inventory playerInventory) { for (int i = 0; i < 9; ++i) { this.addSlot(new Slot(playerInventory, i, 8 + i * 18, 142)); } }}
package net.mattias.pedestals.core.world.block;
import com.mojang.serialization.MapCodec; import net.mattias.pedestals.core.world.block.entity.PedestalBlockEntity; import net.mattias.pedestals.core.world.inventory.PedestalMenu; import net.minecraft.core.BlockPos; import net.minecraft.network.chat.Component; import net.minecraft.server.level.ServerPlayer; import net.minecraft.sounds.SoundEvents; import net.minecraft.sounds.SoundSource; import net.minecraft.world.Containers; import net.minecraft.world.InteractionHand; import net.minecraft.world.ItemInteractionResult; import net.minecraft.world.SimpleMenuProvider; import net.minecraft.world.entity.player.Player; import net.minecraft.world.item.ItemStack; import net.minecraft.world.level.BlockGetter; import net.minecraft.world.level.Level; import net.minecraft.world.level.block.BaseEntityBlock; import net.minecraft.world.level.block.Block; import net.minecraft.world.level.block.RenderShape; import net.minecraft.world.level.block.entity.BlockEntity; 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.VoxelShape; import org.jetbrains.annotations.Nullable;
public class PedestalBlock extends BaseEntityBlock { public static final VoxelShape SHAPE = Block.box(2, 0, 2, 14, 13, 14); public static final MapCodec CODEC = simpleCodec(PedestalBlock::new);
public PedestalBlock(Properties properties) { super(properties); } @Override protected MapCodec<? extends BaseEntityBlock> codec() { return CODEC; } @Override protected VoxelShape getShape(BlockState pState, BlockGetter pLevel, BlockPos pPos, CollisionContext pContext) { return SHAPE; } @Nullable @Override public BlockEntity newBlockEntity(BlockPos pPos, BlockState pState) { return new PedestalBlockEntity(pPos, pState); } @Override protected RenderShape getRenderShape(BlockState pState) { return RenderShape.MODEL; } @Override protected void onRemove(BlockState pState, Level pLevel, BlockPos pPos, BlockState pNewState, boolean pMovedByPiston) { if(pState.getBlock() != pNewState.getBlock()) { if(pLevel.getBlockEntity(pPos) instanceof PedestalBlockEntity pedestalBlockEntity) { pedestalBlockEntity.drops(); pLevel.updateNeighbourForOutputSignal(pPos, this); } } super.onRemove(pState, pLevel, pPos, pNewState, pMovedByPiston); } @Override protected ItemInteractionResult useItemOn(ItemStack pStack, BlockState pState, Level pLevel, BlockPos pPos, Player pPlayer, InteractionHand pHand, BlockHitResult pHitResult) { if(pLevel.getBlockEntity(pPos) instanceof PedestalBlockEntity pedestalBlockEntity) { if(pPlayer.isCrouching() && !pLevel.isClientSide()) { pPlayer.openMenu(new PedestalMenu(pContainerId, pPlayer.getInventory(), pedestalBlockEntity)); return ItemInteractionResult.SUCCESS; } if(pedestalBlockEntity.inventory.getStackInSlot(0).isEmpty() && !pStack.isEmpty()) { pedestalBlockEntity.inventory.insertItem(0, pStack.copy(), false); pStack.shrink(1); pLevel.playSound(pPlayer, pPos, SoundEvents.ITEM_PICKUP, SoundSource.BLOCKS, 1f, 2f); } else if(pStack.isEmpty()) { ItemStack stackOnPedestal = pedestalBlockEntity.inventory.extractItem(0, 1, false); pPlayer.setItemInHand(InteractionHand.MAIN_HAND, stackOnPedestal); pedestalBlockEntity.clearContents(); pLevel.playSound(pPlayer, pPos, SoundEvents.ITEM_PICKUP, SoundSource.BLOCKS, 1f, 1f); } } return ItemInteractionResult.SUCCESS; }}
Send whole classes back with fix
Assistant
Okay, here's the corrected code for both the
PedestalBlockandPedestalMenuclasses. I've implemented the changes we discussed, including using the block entity directly as the menu provider and using theusemethod instead ofuseItemOn.PedestalBlock.java
package net.mattias.pedestals.core.world.block; import com.mojang.serialization.MapCodec; import net.mattias.pedestals.core.world.block.entity.PedestalBlockEntity; import net.mattias.pedestals.core.world.inventory.PedestalMenu; import net.minecraft.core.BlockPos; import net.minecraft.network.chat.Component; import net.minecraft.sounds.SoundEvents; import net.minecraft.sounds.SoundSource; import net.minecraft.world.Containers; import net.minecraft.world.InteractionHand; import net.minecraft.world.InteractionResult; import net.minecraft.world.entity.player.Player; import net.minecraft.world.item.ItemStack; import net.minecraft.world.level.BlockGetter; import net.minecraft.world.level.Level; import net.minecraft.world.level.block.BaseEntityBlock; import net.minecraft.world.level.block.Block; import net.minecraft.world.level.block.RenderShape; import net.minecraft.world.level.block.entity.BlockEntity; 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.VoxelShape; import org.jetbrains.annotations.Nullable; public class PedestalBlock extends BaseEntityBlock { public static final VoxelShape SHAPE = Block.box(2, 0, 2, 14, 13, 14); public static final MapCodec<PedestalBlock> CODEC = simpleCodec(PedestalBlock::new); public PedestalBlock(Properties properties) { super(properties); } @Override protected MapCodec<? extends BaseEntityBlock> codec() { return CODEC; } @Override protected VoxelShape getShape(BlockState pState, BlockGetter pLevel, BlockPos pPos, CollisionContext pContext) { return SHAPE; } @Nullable @Override public BlockEntity newBlockEntity(BlockPos pPos, BlockState pState) { return new PedestalBlockEntity(pPos, pState); } @Override protected RenderShape getRenderShape(BlockState pState) { return RenderShape.MODEL; } @Override protected void onRemove(BlockState pState, Level pLevel, BlockPos pPos, BlockState pNewState, boolean pMovedByPiston) { if(pState.getBlock() != pNewState.getBlock()) { if(pLevel.getBlockEntity(pPos) instanceof PedestalBlockEntity pedestalBlockEntity) { pedestalBlockEntity.drops(); pLevel.updateNeighbourForOutputSignal(pPos, this); } } super.onRemove(pState, pLevel, pPos, pNewState, pMovedByPiston); } @Override public InteractionResult use(BlockState pState, Level pLevel, BlockPos pPos, Player pPlayer, InteractionHand pHand, BlockHitResult pHitResult) { if(pLevel.getBlockEntity(pPos) instanceof PedestalBlockEntity pedestalBlockEntity) { if(pPlayer.isCrouching() && !pLevel.isClientSide()) { pPlayer.openMenu(pedestalBlockEntity); return InteractionResult.SUCCESS; } if(pedestalBlockEntity.inventory.getStackInSlot(0).isEmpty() && !pPlayer.getItemInHand(pHand).isEmpty()) { pedestalBlockEntity.inventory.insertItem(0, pPlayer.getItemInHand(pHand).copy(), false); pPlayer.getItemInHand(pHand).shrink(1); pLevel.playSound(pPlayer, pPos, SoundEvents.ITEM_PICKUP, SoundSource.BLOCKS, 1f, 2f); return InteractionResult.SUCCESS; } else if(pPlayer.getItemInHand(pHand).isEmpty()) { ItemStack stackOnPedestal = pedestalBlockEntity.inventory.extractItem(0, 1, false); pPlayer.setItemInHand(pHand, stackOnPedestal); pedestalBlockEntity.clearContents(); pLevel.playSound(pPlayer, pPos, SoundEvents.ITEM_PICKUP, SoundSource.BLOCKS, 1f, 1f); return InteractionResult.SUCCESS; } } return InteractionResult.PASS; } }PedestalMenu.java
package net.mattias.pedestals.core.world.inventory; import net.mattias.pedestals.core.registry.ModBlocks; import net.mattias.pedestals.core.registry.ModMenus; import net.mattias.pedestals.core.world.block.entity.PedestalBlockEntity; import net.minecraft.network.FriendlyByteBuf; 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; import net.minecraft.world.level.Level; import net.minecraft.world.level.block.entity.BlockEntity; import net.neoforged.neoforge.items.SlotItemHandler; public class PedestalMenu extends AbstractContainerMenu { public final PedestalBlockEntity blockEntity; private final Level level; public PedestalMenu(int pContainerId, Inventory inv, FriendlyByteBuf extraData) { this(pContainerId, inv, inv.player.level().getBlockEntity(extraData.readBlockPos())); } public PedestalMenu(int pContainerId, Inventory inv, BlockEntity blockEntity) { super(ModMenus.PEDESTAL.get(), pContainerId); this.blockEntity = ((PedestalBlockEntity) blockEntity); this.level = inv.player.level(); addPlayerInventory(inv); addPlayerHotbar(inv); this.addSlot(new SlotItemHandler(this.blockEntity.inventory, 0, 80, 35)); } // CREDIT GOES TO: diesieben07 | https://github.com/diesieben07/SevenCommons // must assign a slot number to each of the slots used by the GUI. // For this container, we can see both the tile inventory's slots as well as the player inventory slots and the hotbar. // Each time we add a Slot to the container, it automatically increases the slotIndex, which means // 0 - 8 = hotbar slots (which will map to the InventoryPlayer slot numbers 0 - 8) // 9 - 35 = player inventory slots (which map to the InventoryPlayer slot numbers 9 - 35) // 36 - 44 = TileInventory slots, which map to our TileEntity slot numbers 0 - 8) private static final int HOTBAR_SLOT_COUNT = 9; private static final int PLAYER_INVENTORY_ROW_COUNT = 3; private static final int PLAYER_INVENTORY_COLUMN_COUNT = 9; private static final int PLAYER_INVENTORY_SLOT_COUNT = PLAYER_INVENTORY_COLUMN_COUNT * PLAYER_INVENTORY_ROW_COUNT; private static final int VANILLA_SLOT_COUNT = HOTBAR_SLOT_COUNT + PLAYER_INVENTORY_SLOT_COUNT; private static final int VANILLA_FIRST_SLOT_INDEX = 0; private static final int TE_INVENTORY_FIRST_SLOT_INDEX = VANILLA_FIRST_SLOT_INDEX + VANILLA_SLOT_COUNT; // THIS YOU HAVE TO DEFINE! private static final int TE_INVENTORY_SLOT_COUNT = 1; // must be the number of slots you have! @Override public ItemStack quickMoveStack(Player playerIn, int pIndex) { Slot sourceSlot = slots.get(pIndex); if (sourceSlot == null || !sourceSlot.hasItem()) return ItemStack.EMPTY; //EMPTY_ITEM ItemStack sourceStack = sourceSlot.getItem(); ItemStack copyOfSourceStack = sourceStack.copy(); // Check if the slot clicked is one of the vanilla container slots if (pIndex < VANILLA_FIRST_SLOT_INDEX + VANILLA_SLOT_COUNT) { // This is a vanilla container slot so merge the stack into the tile inventory if (!moveItemStackTo(sourceStack, TE_INVENTORY_FIRST_SLOT_INDEX, TE_INVENTORY_FIRST_SLOT_INDEX + TE_INVENTORY_SLOT_COUNT, false)) { return ItemStack.EMPTY; // EMPTY_ITEM } } else if (pIndex < TE_INVENTORY_FIRST_SLOT_INDEX + TE_INVENTORY_SLOT_COUNT) { // This is a TE slot so merge the stack into the players inventory if (!moveItemStackTo(sourceStack, VANILLA_FIRST_SLOT_INDEX, VANILLA_FIRST_SLOT_INDEX + VANILLA_SLOT_COUNT, false)) { return ItemStack.EMPTY; } } else { System.out.println("Invalid slotIndex:" + pIndex); return ItemStack.EMPTY; } // If stack size == 0 (the entire stack was moved) set slot contents to null if (sourceStack.getCount() == 0) { sourceSlot.set(ItemStack.EMPTY); } else { sourceSlot.setChanged(); } sourceSlot.onTake(playerIn, sourceStack); return copyOfSourceStack; } @Override public boolean stillValid(Player pPlayer) { return stillValid(ContainerLevelAccess.create(level, blockEntity.getBlockPos()), pPlayer, ModBlocks.PEDESTAL.get()); } private void addPlayerInventory(Inventory playerInventory) { for (int i = 0; i < 3; ++i) { for (int l = 0; l < 9; ++l) { this.addSlot(new Slot(playerInventory, l + i * 9 + 9, 8 + l * 18, 84 + i * 18)); } } } private void addPlayerHotbar(Inventory playerInventory) { for (int i = 0; i < 9; ++i) { this.addSlot(new Slot(playerInventory, i, 8 + i * 18, 142)); } } }Important Notes:
- Rebuild and Test: After making these changes, rebuild your mod and test thoroughly.
- Client-Side Only: Remember that GUI-related code (like the screen) should only be present in the client-side code.
- Error Handling: Add more robust error handling and logging to help diagnose any further issues.
I believe these changes should resolve the immediate problem of the GUI closing instantly. Let me know if you encounter any further difficulties.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.