Java Code Examples for net.minecraft.entity.player.InventoryPlayer#getHotbarSize()

The following examples show how to use net.minecraft.entity.player.InventoryPlayer#getHotbarSize() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: WrapperInventoryPlayer.java    From ClientBase with MIT License 4 votes vote down vote up
public static int getHotbarSize() {
    return InventoryPlayer.getHotbarSize();
}
 
Example 2
Source File: CraftInventoryPlayer.java    From Kettle with GNU General Public License v3.0 4 votes vote down vote up
@Override
public void setItem(int index, ItemStack item) {
    super.setItem(index, item);
    if (this.getHolder() == null) return;
    EntityPlayerMP player = ((CraftPlayer) this.getHolder()).getHandle();
    if (player.connection == null) return;
    // PacketPlayOutSetSlot places the items differently than setItem()
    //
    // Between, and including, index 9 (the first index outside of the hotbar) and index 35 (the last index before
    // armor slots) both PacketPlayOutSetSlot and setItem() places the items in the player's inventory the same way.
    // Index 9 starts at the upper left corner of the inventory and moves to the right as it increases. When it
    // reaches the end of the line it goes back to the left side of the new line in the inventory. Basically, it
    // follows the path your eyes would follow as you read a book.
    //
    // The player's hotbar is indexed 0-8 in setItem(). The order goes: 0-8 hotbar, 9-35 normal inventory, 36 boots,
    // 37 leggings, 38 chestplate, and 39 helmet. For indexes > 39 an ArrayIndexOutOfBoundsException will be thrown.
    //
    // PacketPlayOutSetSlot works very differently. Slots 0-8 are as follows: 0 crafting output, 1-4 crafting input,
    // 5 helmet, 6 chestplate, 7 leggings, and 8 boots. Then, 9-35 work exactly the same as setItem(). The hotbar
    // for PacketPlayOutSetSlot starts at index 36, and continues to index 44. Items placed where index is < 0 or
    // > 44 have no action. Basically, the upper part of the player's inventory (crafting area and armor slots) is
    // the first "row" of 9 slots for PacketPlayOutSetSlot. From there the rows work as normal, from left to right
    // all the way down, including the hotbar.
    //
    // With this in mind, we have to modify the index we give PacketPlayOutSetSlot to match the index we intended
    // with setItem(). First, if the index is 0-8, we need to add 36, or 4 rows worth of slots, to the index. This
    // will push the item down to the correct spot in the hotbar.
    //
    // Now when index is > 35 (if index > 39 an ArrayIndexOutOfBoundsException will be thrown, so we need not worry
    // about it) then we need to reset the index, and then count backwards  from the "top" of the inventory. That is
    // to say, we first find (index - 36), which will give us the index required for the armor slots. Now, we need
    // to reverse the order of the index from 8. That means we need 0 to correspond to 8, 1 to correspond to 7,
    // 2 to correspond to 6, and 3 to correspond to 5. We do this simply by taking the result of (index - 36) and
    // subtracting that value from 8.
    if (index < InventoryPlayer.getHotbarSize()) {
        index += 36;
    } else if (index > 39) {
        index += 5; // Off hand
    } else if (index > 35) {
        index = 8 - (index - 36);
    }
    player.connection.sendPacket(new SPacketSetSlot(player.inventoryContainer.windowId, index, CraftItemStack.asNMSCopy(item)));
}
 
Example 3
Source File: BookBot.java    From ForgeHax with MIT License 4 votes vote down vote up
@Override
public void run() {
  try {
    while (!stopped) {
      // check to see if we've finished the book
      if (!parser.hasNext()) {
        this.status = Status.FINISHED;
        break;
      }
      
      sleep();
      
      // wait for screen
      if (MC.currentScreen != null) {
        this.status = Status.AWAITING_GUI_CLOSE;
        continue;
      }
      
      // search for empty book
      int slot = -1;
      ItemStack selected = null;
      for (int i = 0; i < InventoryPlayer.getHotbarSize(); i++) {
        ItemStack stack = getLocalPlayer().inventory.getStackInSlot(i);
        if (stack != null
            && !stack.equals(ItemStack.EMPTY)
            && stack.getItem() instanceof ItemWritableBook) {
          slot = i;
          selected = stack;
          break;
        }
      }
      
      // make sure we found a book
      if (slot == -1) {
        this.status = Status.NEED_EMPTY_BOOKS_IN_HOTBAR;
        continue;
      }
      
      // set selected item to that slot
      while (getLocalPlayer().inventory.currentItem != slot) {
        getLocalPlayer().inventory.currentItem = slot;
        this.status = Status.CHANGING_HELD_ITEM;
        sleep();
      }
      
      final ItemStack item = selected;
      
      // open the book gui screen
      this.status = Status.OPENING_BOOK;
      MC.addScheduledTask(() -> getLocalPlayer().openBook(item, EnumHand.MAIN_HAND));
      
      // wait for gui to open
      while (!(MC.currentScreen instanceof GuiScreenBook)) {
        sleep();
      }
      
      // send book to server
      this.status = Status.WRITING_BOOK;
      MC.addScheduledTask(
          () -> {
            sendBook(item);
            MC.displayGuiScreen(null);
          });
      
      // wait for screen to close
      while (MC.currentScreen != null) {
        sleep();
      }
    }
  } catch (Throwable t) {
    this.status = Status.ERROR;
  } finally {
    if (finalListener != null) {
      finalListener.accept(this);
      finalListener = null;
    }
    
    // set stopped to true
    this.stopped = true;
    
    if (!this.status.equals(Status.FINISHED) && !this.status.equals(Status.ERROR)) {
      this.status = Status.STOPPED;
    }
  }
}
 
Example 4
Source File: LocalPlayerInventory.java    From ForgeHax with MIT License 4 votes vote down vote up
public static int getHotbarSize() {
  return InventoryPlayer.getHotbarSize();
}
 
Example 5
Source File: ItemEnderTool.java    From enderutilities with GNU Lesser General Public License v3.0 4 votes vote down vote up
private EnumActionResult placeBlock(ItemStack stack, EntityPlayer playerIn, EnumHand hand,
        World worldIn, BlockPos pos, EnumFacing side, float hitX, float hitY, float hitZ)
{
    // Items in the off-hand, let vanilla handle that case
    if (playerIn.getHeldItemOffhand().isEmpty() == false)
    {
        return EnumActionResult.PASS;
    }

    int origSlot = playerIn.inventory.currentItem;
    int slot = (origSlot >= InventoryPlayer.getHotbarSize() - 1 ? 0 : origSlot + 1);
    ItemStack targetStack = playerIn.inventory.getStackInSlot(slot);

    // If the tool is in the first slot of the hotbar and there is no ItemBlock in the second slot, we fall back to the last slot
    if (origSlot == 0 && (targetStack.isEmpty() || (targetStack.getItem() instanceof ItemBlock) == false))
    {
        slot = InventoryPlayer.getHotbarSize() - 1;
        targetStack = playerIn.inventory.getStackInSlot(slot);
    }

    // If the target stack is an ItemBlock, we try to place that in the world
    if (targetStack.isEmpty() == false && targetStack.getItem() instanceof ItemBlock)
    {
        // Check if we can place the block
        if (BlockUtils.checkCanPlaceBlockAt(worldIn, pos, side, ((ItemBlock) targetStack.getItem()).getBlock()))
        {
            ItemStack stackTool = playerIn.getHeldItem(hand);
            playerIn.inventory.setInventorySlotContents(slot, ItemStack.EMPTY);
            EntityUtils.setHeldItemWithoutEquipSound(playerIn, hand, targetStack);
            int sizeOrig = targetStack.getCount();
            EnumActionResult result = EnumActionResult.PASS;

            try
            {
                result = targetStack.onItemUse(playerIn, worldIn, pos, hand, side, hitX, hitY, hitZ);
            }
            catch (Exception e) {}

            EntityUtils.setHeldItemWithoutEquipSound(playerIn, hand, stackTool);

            if (playerIn.capabilities.isCreativeMode)
            {
                targetStack.setCount(sizeOrig);
            }

            // Return the items to their original slot
            playerIn.inventory.setInventorySlotContents(slot, targetStack.isEmpty() ? ItemStack.EMPTY : targetStack);
            playerIn.inventoryContainer.detectAndSendChanges();

            return result;
        }
    }

    return EnumActionResult.PASS;
}