Package net.minecraft.inventory

Examples of net.minecraft.inventory.Slot


  // to paint the items:
  // func_85044_b(ItemStack stack, int x, int y)        // copy this method, as it's private.
  // tooltip: func_74184_a(ItemStack stack, int x, int y)   // this one is protected, so don't copy.

  private Slot findSlotAt(int x, int y) {
    Slot slot = getSlotAt( x, y );
    if( slot != null && slot.getHasStack() ) {
      if( CraftManager.isEncoded( slot.getStack() ) ) {
        return slot;
      }
    }
    return null;
  }
View Full Code Here


    // circuits
    for( int i = 0; i < 4; i++ ) {
      int x = 20 + (i % 2) * 120;
      int y = 40 + (i / 2) * 44;

      addSlotToContainer( new Slot( crafter.circuits, i, x, y ) {
        @Override
        public boolean isItemValid(ItemStack stack) {
          return CraftManager.isValid( stack );
        }

        @Override
        public int getSlotStackLimit() {
          return 1;
        }
      } );
    }

    // crafting grid (62,17) 3x3 (18x18)
    gridFirstSlot = this.inventorySlots.size();
    for( int i = 0; i < 3; i++ ) {
      for( int e = 0; e < 3; e++ ) {
        int x = 18 * e + 62, y = 18 * i + 17, index = e + i * 3;
        addSlotToContainer( new Slot( crafter.craftGrid, index, x, y ) );
      }
    }

    // grid's output (80,78)
    addSlotToContainer( new SlotCraft( crafter, crafter.results, player, 4, 80, 78 ) );


    // resources (8,107) 3x9 (18x18)
    for( int i = 0; i < 3; i++ ) {
      for( int e = 0; e < 9; e++ ) {
        int x = 18 * e + 8, y = 18 * i + 107;
        addSlotToContainer( new Slot( crafter.resources, e + i * 9, x, y ) );
      }
    }

    // player's inventory (8,174) 3x9 (18x18)
    for( int i = 0; i < 3; i++ ) {
      for( int e = 0; e < 9; e++ ) {
        int x = 18 * e + 8, y = 18 * i + 174;
        addSlotToContainer( new Slot( player.inventory, e + i * 9 + 9, x, y ) );
      }
    }
    // player's hot bar (8,232) 1x9 (18x18)
    for( int i = 0; i < 9; i++ ) {
      addSlotToContainer( new Slot( player.inventory, i, 18 * i + 8, 232 ) );
    }

    this.onCraftMatrixChanged( crafter.craftGrid );
  }
View Full Code Here

    return true;
  }

  @Override
  public ItemStack transferStackInSlot(EntityPlayer player, int slotID) {
    Slot slot = (Slot) inventorySlots.get( slotID );

    if( slot == null || !slot.getHasStack() )
      return null;
    ItemStack stackInSlot = slot.getStack();
    ItemStack stack = stackInSlot.copy();

    if( slot instanceof SlotCraft ) {
      if( player.worldObj.isRemote ) // the server should handle this.
        return null;

      if( !slot.canTakeStack( player ) )
        return null;
      // add to the resources buffer.
      stackInSlot = ((SlotCraft) slot).getCraftedStack();
      ItemStack copy = stackInSlot == null ? null : stackInSlot.copy();

      if( mergeCraftedStack( stackInSlot, 8 + 10, 8 + 10 + 27 ) ) {
        slot.onPickupFromSlot( player, copy );
        slot.onSlotChanged();
        return copy;
      }
      return null;
    }

    // From the crafter to the resources buffer.
    if( slotID < 8 ) {
      if( !mergeItemStack( stackInSlot, 8 + 10, 8 + 10 + 27, false ) )
        return null;

    } else if( slotID < 8 + 10 ) { // from the crafting grid.
      if( !mergeItemStack( stackInSlot, 8 + 10, inventorySlots.size(), false ) )
        return null;

    } else if( slotID < 8 + 10 + 27 ) { // from the resources buffer
      // chips first try to go to the chip slots.
      if( stackInSlot.getItem() instanceof ItemChip ) {
        if( !mergeItemStack( stackInSlot, 4, 8, false ) ) // try add to the chip slots.
          if( !mergeItemStack( stackInSlot, 8 + 10 + 27, inventorySlots.size(), false ) ) // add to the player's inv.
            return null;

        // prevent retrying by returning null.
        stack = null;

      } else { // any other item goes to the player's inventory.
        if( !mergeItemStack( stackInSlot, 8 + 10 + 27, inventorySlots.size(), false ) )
          return null;
      }

    } else { // From the player's inventory to the resources buffer.
      if( !mergeItemStack( stackInSlot, 8 + 10, 8 + 10 + 27, false ) )
        return null;
    }

    if( stackInSlot.stackSize == 0 ) {
      slot.putStack( null );
    }
    slot.onSlotChanged();

    return stack;
  }
View Full Code Here

    // First, check if the stack can fit.
    int missingSpace = itemStack.stackSize;
    int emptySlots = 0;

    for( int i = indexMin; i < indexMax && missingSpace > 0; i++ ) {
      Slot tempSlot = (Slot) this.inventorySlots.get( i );
      ItemStack stackInSlot = tempSlot.getStack();

      if( stackInSlot == null ) {
        emptySlots++;
        continue;
      }

      if( stackInSlot.itemID == itemStack.itemID
          && itemStack.getItemDamage() == stackInSlot.getItemDamage()
          && ItemStack.areItemStackTagsEqual( itemStack, stackInSlot ) ) {

        missingSpace -= Math.min( stackInSlot.getMaxStackSize(), tempSlot.getSlotStackLimit() ) - stackInSlot.stackSize;
      }
    }

    // prevent crafting if there is no space for the crafted item.
    if( missingSpace > 0 )
      if( emptySlots == 0 )
        return false;

    // Try to merge with existing stacks.
    if( itemStack.isStackable() ) {

      for( int i = indexMin; i < indexMax; i++ ) {
        if( itemStack.stackSize <= 0 )
          break;

        Slot targetSlot = (Slot) this.inventorySlots.get( i );
        ItemStack stackInSlot = targetSlot.getStack();

        if( stackInSlot == null )
          continue;

        if( stackInSlot.itemID == itemStack.itemID
            && (!itemStack.getHasSubtypes() || itemStack.getItemDamage() == stackInSlot.getItemDamage())
            && ItemStack.areItemStackTagsEqual( itemStack, stackInSlot ) ) {

          int sum = itemStack.stackSize + stackInSlot.stackSize;
          int maxStackSize = Math.min( stackInSlot.getMaxStackSize(), targetSlot.getSlotStackLimit() );

          if( sum <= maxStackSize ) {
            stackInSlot.stackSize = sum;
            targetSlot.onSlotChanged();
            return true;
          } else if( stackInSlot.stackSize < maxStackSize ) {
            itemStack.stackSize -= maxStackSize - stackInSlot.stackSize;
            stackInSlot.stackSize = maxStackSize;
            targetSlot.onSlotChanged();
          }
        }
      }
    }

    // Add to an empty slot.
    if( itemStack.stackSize > 0 ) {

      for( int i = indexMin; i < indexMax; i++ ) {

        Slot targetSlot = (Slot) this.inventorySlots.get( i );
        ItemStack stackInSlot = targetSlot.getStack();

        if( stackInSlot != null )
          continue;

        targetSlot.putStack( itemStack );
        targetSlot.onSlotChanged();
        return true;
      }
    }

    return true;
View Full Code Here

    if( reverse ) {
      index = indexMax - 1;
    }

    Slot slot;
    ItemStack stackInSlot;

    if( itemStack.isStackable() ) {
      while( itemStack.stackSize > 0 && (!reverse && index < indexMax || reverse && index >= indexMin) ) {
        slot = (Slot) this.inventorySlots.get( index );
        stackInSlot = slot.getStack();

        int maxStackSize = Math.min( itemStack.getMaxStackSize(), slot.getSlotStackLimit() );

        if( stackInSlot != null && stackInSlot.itemID == itemStack.itemID
            && (!itemStack.getHasSubtypes() || itemStack.getItemDamage() == stackInSlot.getItemDamage())
            && ItemStack.areItemStackTagsEqual( itemStack, stackInSlot ) ) {

          int sum = stackInSlot.stackSize + itemStack.stackSize;

          if( sum <= maxStackSize ) {
            itemStack.stackSize = 0;
            stackInSlot.stackSize = sum;
            slot.onSlotChanged();
            retValue = true;
          } else if( stackInSlot.stackSize < maxStackSize ) {
            itemStack.stackSize -= maxStackSize - stackInSlot.stackSize;
            stackInSlot.stackSize = maxStackSize;
            slot.onSlotChanged();
            retValue = true;
          }
        }

        if( reverse ) {
          --index;
        } else {
          ++index;
        }
      }
    }

    if( itemStack.stackSize > 0 ) {
      if( reverse ) {
        index = indexMax - 1;
      } else {
        index = indexMin;
      }

      while( !reverse && index < indexMax || reverse && index >= indexMin ) {
        slot = (Slot) this.inventorySlots.get( index );
        stackInSlot = slot.getStack();
        int maxStackSize = Math.min( itemStack.getMaxStackSize(), slot.getSlotStackLimit() );

        if( stackInSlot == null ) {
          int remaining = 0;
          ItemStack tempStack = itemStack;

          if( itemStack.stackSize > maxStackSize ) {
            remaining = itemStack.stackSize - maxStackSize;
            tempStack = itemStack.splitStack( maxStackSize );
          }

          slot.putStack( tempStack.copy() );
          slot.onSlotChanged();
          itemStack.stackSize = remaining;
          retValue = true;
          break;
        }
View Full Code Here

    if( slotID == -1 ) { // Clear the grid
      clearCraftingGrid();
      return;
    }

    Slot slot = getSlot( slotID );
    if( slot != null ) {
      slot.putStack( stack );
    }
  }
View Full Code Here

  }

  @Override
  protected void clearCraftingGrid() {
    for( int i = 0; i < 9; i++ ) {
      Slot gridSlot = getSlot( i + gridFirstSlot );
      gridSlot.inventory.setInventorySlotContents( i, null );
    }
    crafter.craftGrid.onInventoryChanged();
  }
View Full Code Here

    // grid
    gridFirstSlot = inventorySlots.size();
    for( int i = 0; i < 3; i++ ) {
      for( int e = 0; e < 3; e++ ) {
        this.addSlotToContainer( new Slot( craftPad.gridInv, i * 3 + e, e * 18 + 24, i * 18 + 24 ) {
          @Override
          public boolean canTakeStack(EntityPlayer player) {
            return false;
          }
        } );
      }
    }

    // chip
    this.addSlotToContainer( new Slot( craftPad.chipInv, 0, 137, 40 ) {
      @Override
      public boolean isItemValid(ItemStack stack) {
        return stack != null && stack.getItem() instanceof ItemChip;
      }

      @Override
      public void onSlotChanged() {
        onChipChanged( this );
        super.onSlotChanged();
      }

      @Override
      public int getSlotStackLimit() {
        return 1;
      }

    } );

    // main player inv
    for( int i = 0; i < 3; i++ ) {
      for( int e = 0; e < 9; e++ ) {
        this.addSlotToContainer( new Slot( player.inventory, (i + 1) * 9 + e, e * 18 + 8, i * 18 + 98 ) );
      }
    }

    // hot-bar
    for( int i = 0; i < 9; ++i ) {
      this.addSlotToContainer( new Slot( player.inventory, i, i * 18 + 8, 156 ) );
    }
  }
View Full Code Here

    if( slotID == -1 ) { // Clear the grid
      clearCraftingGrid();
      return;
    }

    Slot slot = (Slot) this.inventorySlots.get( slotID );
    if( slot != null ) {
      slot.putStack( stack );
    }
  }
View Full Code Here

  @Override
  public ItemStack transferStackInSlot(EntityPlayer player, int slotID) {
    // only the output slot and any slot with a chip on it will respond to shift-clicking

    Slot slot = (Slot) inventorySlots.get( slotID );
    if( slot == null || !slot.getHasStack() )
      return null;

    ItemStack stackInSlot = slot.getStack();
    ItemStack retValue = stackInSlot.copy();

    // output's slot
    if( slot instanceof SlotCraft ) {
      stackInSlot = ((SlotCraft) slot).getCraftedStack();
      ItemStack copy = stackInSlot == null ? null : stackInSlot.copy();

      if( mergeCraftedStack( stackInSlot, 11, inventorySlots.size() ) ) {
        slot.onPickupFromSlot( player, stackInSlot );
        slot.onSlotChanged();
        return copy;
      }
      return null;
    }

    // Special treatment for chips.
    if( stackInSlot.getItem() instanceof ItemChip ) {
      if( slotID == 10 ) { // chip slot
        // try add to player's inventory
        if( !mergeItemStack( stackInSlot, 11, inventorySlots.size(), false ) )
          return null;
      } else if( slotID >= 11 ) { // slot on player's inv
        ItemStack currentChip = craftPad.chipInv.getStackInSlot( 0 );

        if( currentChip == null ) { // empty chip slot
          // add to the chip's slot
          if( !mergeItemStack( stackInSlot.splitStack( 1 ), 10, 11, false ) )
            return null;

        } else if( CraftManager.isEncoded( currentChip ) && CraftManager.isEncoded( stackInSlot ) ) {
          // swap the chips.
          slot.putStack( currentChip );

          Slot chipSlot = (Slot) inventorySlots.get( 10 );
          chipSlot.putStack( stackInSlot );
          chipSlot.onSlotChanged();
        }
      }
    }

    if( stackInSlot.stackSize == 0 ) {
View Full Code Here

TOP

Related Classes of net.minecraft.inventory.Slot

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.