Solved setHeldItemSlot() Problem

Discussion in 'Plugin Development' started by etaxi341, Jul 3, 2013.

Thread Status:
Not open for further replies.
  1. Offline

    etaxi341

    Hey Programmers out there! I am working on a new Plugin. I want to change the Players Selected ItemBar Slot to 1 if the Slot 0 contains a sword which is not a Golden_Sword. So far it works. But when I want the Player to switch his Selected ItemBar Slot the Item in this slot automatically is the same Item as in Slot 0 ... Please Help me Here is my Code:

    Code:java
    1. @EventHandler
    2. public void MineBlock(PlayerInteractEvent ev){
    3. Player p = ev.getPlayer();
    4. if((ev.getAction() == Action.RIGHT_CLICK_BLOCK ||
    5. ev.getAction() == Action.RIGHT_CLICK_AIR ) && (
    6. ev.getPlayer().getItemInHand().getType() == Material.WOOD_SWORD ||
    7. ev.getPlayer().getItemInHand().getType() == Material.STONE_SWORD ||
    8. ev.getPlayer().getItemInHand().getType() == Material.IRON_SWORD ||
    9. ev.getPlayer().getItemInHand().getType() == Material.DIAMOND_SWORD)){
    10. if (p.getInventory().getHeldItemSlot() == 0){
    11. p.sendMessage("TEST 1");
    12. if (p.getInventory().getItem(1).getType() == Material.GOLD_SWORD){
    13. p.sendMessage("TEST 2");
    14. ItemStack is = p.getInventory().getItem(1);
    15. p.getInventory().setHeldItemSlot(1);
    16. p.getInventory().setItemInHand(is);
    17. }
    18. }
    19. }
    20. }


    By the way both messages appear so in the Chat will be TEST 1 and a line under this is TEST 2 so the Plugin definitely loads this part of the Plugin.
     
  2. Offline

    nisovin

    There's no need to use setItemInHand, that item will already be in their hand when you switch their hotbar slot.
     
  3. Offline

    etaxi341

    nisovin The Problem is that the Item 1 gets written over from item 0 so i have two times item 0 instead of one time Item 0 and one time Item 1
     
  4. Offline

    nisovin

    Setting the slot from the interact event may be causing a problem. Try using the scheduler to add a 1 tick delay.
     
    etaxi341 likes this.
  5. Offline

    etaxi341

    Thank you :) It worked!
     
Thread Status:
Not open for further replies.

Share This Page