Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.cclloyd.ccmodpack;
- import net.minecraft.entity.player.EntityPlayer;
- import net.minecraft.entity.player.InventoryPlayer;
- import net.minecraft.inventory.Container;
- import net.minecraft.inventory.Slot;
- import net.minecraft.item.ItemStack;
- public class WirelessChestContainer extends Container {
- /**
- * The container is used to link the client side gui to the server side inventory and it is where
- * you add the slots to your gui. It can also be used to sync server side data with the client but
- * that will be covered in a later tutorial
- */
- // Stores a reference to the tile entity instance for later use
- private WirelessChestEntity wirelessChestEntity;
- public WirelessChestEntity getEntity() {
- return wirelessChestEntity;
- }
- // 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 final int HOTBAR_COUNT = 9;
- private final int PLAYER_ROWS = 3;
- private final int PLAYER_COLUMNS = 9;
- private final int PLAYER_SLOTS = PLAYER_COLUMNS * PLAYER_ROWS;
- private final int VANILLA_SLOTS = HOTBAR_COUNT + PLAYER_SLOTS;
- private final int VANILLA_START = 0;
- private final int TE_START = VANILLA_START + VANILLA_SLOTS;
- private final int TE_ROWS = 3;
- private final int TE_COLUMNS = 9;
- private final int TE_SLOTS = TE_ROWS * TE_COLUMNS;
- public WirelessChestContainer(InventoryPlayer invPlayer, WirelessChestEntity wirelessChestEntity) {
- this.wirelessChestEntity = wirelessChestEntity;
- // Adds all the slots for items
- final int SLOT_X_SPACING = 18;
- final int SLOT_Y_SPACING = 18;
- final int HOTBAR_XPOS = 8;
- final int HOTBAR_YPOS = 164;
- final int PLAYER_XPOS = 8;
- final int PLAYER_YPOS = 106;
- final int TE_XPOS = 8;
- final int TE_YPOS = 48;
- // Add the players hotbar to the gui - the [xpos, ypos] location of each item
- for (int x = 0; x < HOTBAR_COUNT; x++) {
- int slotNumber = x;
- addSlotToContainer(new Slot(invPlayer, slotNumber, HOTBAR_XPOS + SLOT_X_SPACING * x, HOTBAR_YPOS));
- }
- // Add the rest of the players inventory to the gui
- for (int y = 0; y < PLAYER_ROWS; y++) {
- for (int x = 0; x < PLAYER_COLUMNS; x++) {
- int slotNumber = HOTBAR_COUNT + y * PLAYER_COLUMNS + x;
- int xpos = PLAYER_XPOS + x * SLOT_X_SPACING;
- int ypos = PLAYER_YPOS + y * SLOT_Y_SPACING;
- addSlotToContainer(new Slot(invPlayer, slotNumber, xpos, ypos));
- }
- }
- if (TE_SLOTS != wirelessChestEntity.getSizeInventory()) {
- System.err.println("Mismatched slot count in ContainerBasic(" + TE_SLOTS
- + ") and TileInventory (" + wirelessChestEntity.getSizeInventory()+")");
- }
- // Add the tile inventory container to the gui
- for (int y = 0; y < TE_ROWS; y++) {
- for (int x = 0; x < TE_COLUMNS; x++) {
- //int slotNumber = HOTBAR_COUNT + y * PLAYER_COLUMNS + x;
- //int slotNumber = TE_START + (y * TE_COLUMNS) + x;
- int slotNumber = (y * HOTBAR_COUNT) + x;
- int xpos = TE_XPOS + (x * SLOT_X_SPACING);
- int ypos = TE_YPOS + (y * SLOT_Y_SPACING);
- addSlotToContainer(new Slot(wirelessChestEntity, slotNumber, xpos, ypos));
- }
- }
- }
- // Vanilla calls this method every tick to make sure the player is still able to access the inventory, and if not closes the gui
- @Override
- public boolean canInteractWith(EntityPlayer player)
- {
- return wirelessChestEntity.isUseableByPlayer(player);
- }
- // This is where you specify what happens when a player shift clicks a slot in the gui
- // (when you shift click a slot in the TileEntity Inventory, it moves it to the first available position in the hotbar and/or
- // player inventory. When you you shift-click a hotbar or player inventory item, it moves it to the first available
- // position in the TileEntity inventory)
- // At the very least you must override this and return null or the game will crash when the player shift clicks a slot
- // returns null if the source slot is empty, or if none of the the source slot items could be moved
- // otherwise, returns a copy of the source stack
- @Override
- public ItemStack transferStackInSlot(EntityPlayer player, int sourceSlotIndex)
- {
- Slot sourceSlot = (Slot)inventorySlots.get(sourceSlotIndex);
- if (sourceSlot == null || !sourceSlot.getHasStack()) return null;
- ItemStack sourceStack = sourceSlot.getStack();
- ItemStack copyOfSourceStack = sourceStack.copy();
- // Check if the slot clicked is one of the vanilla container slots
- if (sourceSlotIndex >= VANILLA_START && sourceSlotIndex < VANILLA_START + VANILLA_SLOTS) {
- // This is a vanilla container slot so merge the stack into the tile inventory
- if (!mergeItemStack(sourceStack, TE_START, TE_START + TE_SLOTS, false)){
- return null;
- }
- } else if (sourceSlotIndex >= TE_START && sourceSlotIndex < TE_START + TE_SLOTS) {
- // This is a TE slot so merge the stack into the players inventory
- if (!mergeItemStack(sourceStack, VANILLA_START, VANILLA_START + VANILLA_SLOTS, false)) {
- return null;
- }
- } else {
- System.err.print("Invalid slotIndex:" + sourceSlotIndex);
- return null;
- }
- // If stack size == 0 (the entire stack was moved) set slot contents to null
- if (sourceStack.stackSize == 0) {
- sourceSlot.putStack(null);
- } else {
- sourceSlot.onSlotChanged();
- }
- sourceSlot.onPickupFromSlot(player, sourceStack);
- return copyOfSourceStack;
- }
- // pass the close container message to the tileEntityInventory (not strictly needed for this example)
- // see ContainerChest and TileEntityChest
- @Override
- public void onContainerClosed(EntityPlayer playerIn)
- {
- super.onContainerClosed(playerIn);
- this.wirelessChestEntity.closeInventory(playerIn);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment