Package net.minecraft.inventory

Examples of net.minecraft.inventory.Slot


    // 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.getHasSubtypes() || 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


  }

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

      } else {
        this.func_94533_d();
      }

    } else if( this.dragState == 1 ) { // add slot to the list.
      Slot slot = (Slot) this.inventorySlots.get( slotID );
      if( slot == null )
        return;

      if( func_94527_a( slot, playerInventory.getItemStack(), true ) && slot.isItemValid( playerInventory.getItemStack() )
          && playerInventory.getItemStack().stackSize > this.draggedSlots.size() && this.canDragIntoSlot( slot ) ) {

        this.draggedSlots.add( slot );
      }

    } else if( this.dragState == 2 ) {
      if( !this.draggedSlots.isEmpty() ) {
        ItemStack playerStack = playerInventory.getItemStack().copy();
        int stackSize = playerInventory.getItemStack().stackSize;
        int trimmedSize = getTrimmedStackSize( playerStack );

        for( Slot slot : draggedSlots ) {
          if( slot == null )
            continue;

          if( func_94527_a( slot, playerInventory.getItemStack(), true ) && slot.isItemValid( playerStack )
              && playerStack.stackSize >= this.draggedSlots.size() && this.canDragIntoSlot( slot ) ) {

            ItemStack itemStack = playerStack.copy();
            if( isGhostSlot( slot ) ) {
              itemStack.stackSize = 1;
              slot.putStack( itemStack );
              continue;
            }

            int slotStackSize = slot.getHasStack() ? slot.getStack().stackSize : 0;
            itemStack.stackSize = trimmedSize + slotStackSize;

            int max = Math.min( itemStack.getMaxStackSize(), slot.getSlotStackLimit() );
            if( itemStack.stackSize > max ) {
              itemStack.stackSize = max;
            }

            stackSize -= itemStack.stackSize - slotStackSize;
            slot.putStack( itemStack );
          }
        }

        playerStack.stackSize = stackSize;
View Full Code Here

        return super.slotClick( slotID, buttonPressed, flag, player );

      if( isUpdateRequired() )
        onContentsChanged();

      Slot slot = slotID < 0 ? null : getSlot( slotID );

      ItemStack stackInSlot;
      ItemStack playerStack;

      InventoryPlayer inventoryPlayer = player.inventory;

      switch( specialCase ) {
        case 1: // clicking on the crafting grid slots:
          if( flag == 1 ) { // clear on shift-clicking.
            slot.putStack( null );
            return null;
          }

          playerStack = inventoryPlayer.getItemStack();

          if( buttonPressed == 0 || playerStack == null ) {
            slot.putStack( null );

          } else if( buttonPressed == 1 ) {
            ItemStack copy = playerStack.copy();
            copy.stackSize = 1;
            slot.putStack( copy );
          }
          return null;

        case 2: // interacting with the hotkeys:
          ItemStack invStack = inventoryPlayer.getStackInSlot( buttonPressed );
          if( invStack != null ) {
            ItemStack copy = invStack.copy();
            copy.stackSize = 1;
            slot.putStack( copy );
          }
          return invStack;

        case 4: // pressing the DROP key.
          stackInSlot = slot.getStack();
          slot.putStack( null );
          return stackInSlot;

        case 5: // placing the dragged stuff.
          handleDragging( slotID, buttonPressed, player.inventory );

          this.detectAndSendChanges();
          break;

        case 6: // double click (clears the crafting grid).
          clearCraftingGrid();
          this.detectAndSendChanges();
          return null;

        case 10: // redirect the right click into a left click.
          return slotClick( slotID, 0, flag, player );

        case 11: // regular clicking on an output slot.

          if( !slot.getHasStack() || !slot.canTakeStack( player ) )
            return null;

          stackInSlot = ((SlotCraft) slot).getCraftedStack();
          playerStack = inventoryPlayer.getItemStack();

          if( playerStack == null ) { // Full extraction from slot.
            inventoryPlayer.setItemStack( stackInSlot );
            slot.onPickupFromSlot( player, inventoryPlayer.getItemStack() );
          } else {
            int sum = stackInSlot.stackSize + playerStack.stackSize;

            // Merge into player's hand.
            if( Utils.equalsStacks( stackInSlot, playerStack ) && sum <= stackInSlot.getMaxStackSize() ) {
              playerStack.stackSize = sum;
              slot.onPickupFromSlot( player, inventoryPlayer.getItemStack() );
            }
          }
          slot.onSlotChanged();
          return stackInSlot;

        case 12: // interacting with the hotkeys
          if( !slot.canTakeStack( player ) )
            return null;
          stackInSlot = ((SlotCraft) slot).getCraftedStack();

          if( stackInSlot == null )
          return null;

          playerStack = inventoryPlayer.getStackInSlot( buttonPressed );
          if( playerStack == null ) {
            inventoryPlayer.setInventorySlotContents( buttonPressed, stackInSlot );
            slot.onPickupFromSlot( player, stackInSlot );
          } else {
            int indx = inventoryPlayer.getFirstEmptyStack();
            if( indx > -1 ) {
              inventoryPlayer.setInventorySlotContents( buttonPressed, stackInSlot );
              inventoryPlayer.addItemStackToInventory( playerStack );
              slot.onPickupFromSlot( player, stackInSlot );
            }
          }
          return stackInSlot;

        case 14: // dropping from an output slot.
          if( slot.getHasStack() && slot.canTakeStack( player ) ) {
            ItemStack itemStack = ((SlotCraft) slot).getCraftedStack();
            slot.onPickupFromSlot( player, itemStack );
            player.dropPlayerItem( ((SlotCraft) slot).getCraftedStack() );
            return itemStack;
          }
          return null;
      }
View Full Code Here

  private int counterMax = 128;
  private ItemStack lastItemStack = null;

  @Override
  protected void retrySlotClick(int slotID, int mouseClick, boolean flag, EntityPlayer player) {
    Slot slot = (Slot) this.inventorySlots.get( slotID );
    if( slot instanceof SlotCraft ) {
      if( mouseClick == 1 )
        return;
    }
View Full Code Here

    return null;
  }

  protected boolean checkSlot(int slotID, int buttonPressed, int flag, EntityPlayer player) {
    if( slotID >= 0 && slotID < inventorySlots.size() ) {
      Slot slot = getSlot( slotID );
      if( flag == 0 || flag == 1 || flag == 4 ) {
        if( slot != null && slotContainsHeldItem( slot, player ) ) {
          onPickupPrevented( player, slot.getStack(), slot );
          return false;
        }
      }
      if( flag == 2 && buttonPressed == getHeldItemSlotIndex() ) {
        onPickupPrevented( player, getParentItem(), getSlotWithHeldItem( player ) );
View Full Code Here

    // inv: 8, 98
    // hot-bar: 8, 156


    // output slot
    this.addSlotToContainer( new Slot( internalInventory, 9, 110, 35 ) {
      @Override
      public boolean isItemValid(ItemStack item) {
        return false;
      }

      @Override
      public boolean canTakeStack(EntityPlayer player) {
        return false;
      }
    } );

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

          @Override
          public void onSlotChanged() {
            updateOutputSlot();
          }
        } );
      }
    }

    // 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

  @Override
  public ItemStack slotClick(int slotID, int buttomPressed, int flag, EntityPlayer player) {

    // Special handle for the grid slots.
    if( 1 == slotID && slotID < 10 ) {
      Slot slot = ((Slot) this.inventorySlots.get( slotID ));

      if( flag == 0 ) { // regular clicking.
        ItemStack playerStack = player.inventory.getItemStack();
        if( buttomPressed == 0 || playerStack == null ) {
          slot.putStack( null );
        } else if( buttomPressed == 1 ) {
          ItemStack copy = playerStack.copy();
          copy.stackSize = 1;
          slot.putStack( copy );
        }
        slot.onSlotChanged();
        return null;
      }

      if( flag == 1 )
        return null; // do nothing on shift click.

      if( flag == 2 ) { // interact with the hot-bar
        ItemStack invStack = player.inventory.getStackInSlot( buttomPressed );
        if( invStack != null ) {
          ItemStack copy = invStack.copy();
          copy.stackSize = 1;

          slot.putStack( copy );
          slot.onSlotChanged();

          return invStack;
        }

      }
View Full Code Here

  private void updateOutputSlot() {
    ItemStack[] gridContents = Arrays.copyOf( internalInventory.getContents(), 9 );
    CraftRecipe recipe = RecipeUtils.getRecipe( gridContents, player.worldObj );

    Slot outputSlot = (Slot) inventorySlots.get( 0 );
    ItemStack item = null;
    if( recipe != null ) {
      InventoryCrafting grid = InventoryUtils.simulateCraftingInventory( gridContents );
      item = recipe.getRecipePointer().getOutputFrom( grid );
    }
    outputSlot.putStack( item );

    int notify = item == null ? 1 : 0;
    // todo: notify the GuiRecipe that the recipe has changed. 0 means has recipe, 1 means no recipe. (packet 0x05)
  }
View Full Code Here

  @Override
  public void setStack(int slotID, ItemStack stack) {
    if( slotID == -1 ) { // Clear the grid
      for( int i = 0; i < 9; i++ ) {
        Slot slot = (Slot) this.inventorySlots.get( i + 1 );
        slot.putStack( null );
      }
      return;
    }

    Slot slot = (Slot) this.inventorySlots.get( slotID );
    if( slot != null ) {
      slot.putStack( stack );
    }
  }
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.