Solved ItemStack to Item

Discussion in 'Plugin Development' started by winspeednl, Mar 2, 2015.

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

    winspeednl

    Hi,

    i have an onCommand and i want to get the item in the hand of the player
    .getItemInHand() is a ItemStack but i need it to be Item
    Item item = ((Player)sender).getInventory().getItemInHand();
    Item item = (Item)((Player)sender).getInventory().getItemInHand();

    But both of them dont work
     
  2. Offline

    timtower Administrator Administrator Moderator

    @winspeednl An Item is an entity in the world. You can't get that from the inventory.
     
  3. Offline

    winspeednl

    okay a another idea i do item.drop() in on command but then i want it to fire 'PlayerDropItemEvent'
    Or is there a another type of dropEvent?
     
  4. Offline

    timtower Administrator Administrator Moderator

    @winspeednl You can throw that event just fine. Just make sure to do that before you drop the item and check if it is canceled.
     
  5. Offline

    winspeednl

    Code:
    
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String args[]){
          Player player = (Player) sender;
          if(cmd.getName().equalsIgnoreCase("tcr")){
            
    World world = player.getWorld();
              float X = Float.parseFloat(args[0]);
              float Y = Float.parseFloat(args[1]);
              float Z = Float.parseFloat(args[2]);
    
              ItemStack item = player.getInventory().getItemInHand();
              ItemMeta im = item.getItemMeta();
              //Item it = (Item) player.getInventory().getItemInHand();
              //sender.sendMessage(X + " - " + Y + " - " + Z);
              im.setLore(Arrays.asList("nopickup"));
              item.setItemMeta(im);
              world.dropItem(new Location(world, X, Y, Z), item); // does not fire PlayerDropItemEvent?
          }
          return false;
         }
        @EventHandler
        public void onDrop(PlayerDropItemEvent event) {
          event.getPlayer().sendMessage("Drop");
          World world = event.getItemDrop().getWorld();
          WitherSkull skull = (WitherSkull) world.spawn(event.getItemDrop().getLocation(), WitherSkull.class);
          skull.setDirection(new Vector(0, 0, 0));
          skull.setVelocity(new Vector(0, 0, 0));
        
          Item item = event.getItemDrop();
          ItemStack is = item.getItemStack();
          ItemMeta im = is.getItemMeta();
            im.setLore(Arrays.asList("nopickup"));
            is.setItemMeta(im);
            skull.setPassenger(item);
        }
    
    What is the problem here?
     
  6. Offline

    timtower Administrator Administrator Moderator

  7. Offline

    winspeednl

    Any examples on that?
     
  8. Offline

    timtower Administrator Administrator Moderator

    @winspeednl Create a new instance of the PlayerDropItemEvent, then throw that event, getServer().getPluginManager().callEvent
     
  9. Offline

    winspeednl

    Got it using
    Code:
    ItemStack item = player.getInventory().getItemInHand();
    Item it = world.dropItem(new Location(world, X, Y, Z), item);
    skull.setPassenger(it);
    
    Thanks for your help timtower!
     
  10. Offline

    1Rogue

    Why?!

    You shouldn't call internal bukkit events like that, there are API methods that handle the actions you want and additionally fire the events. Firing the events alone isn't guaranteed to do anything at all.

    http://docs.codelanx.com/Bukkit/1.8...kkit.Location-org.bukkit.inventory.ItemStack-

    (Set the inventory slot to null after getting the reference and then drop it)
     
  11. Offline

    timtower Administrator Administrator Moderator

    @1Rogue I never said that it would do the action on its own. That is why I said this:
     
  12. Offline

    1Rogue

    But you would be throwing a false event because now you have two event objects tied to the same real-world event (the single item dropping).
     
  13. Offline

    guitargun

    @timtower aren't you creating a fake item upon calling this event? I remember that when I called an event it did nothing to the player only to my background running code.
     
  14. Offline

    1Rogue

    You don't create anything. You simply call all the methods that handle the event, but nothing will actually occur.
     
  15. Offline

    timtower Administrator Administrator Moderator

    Does that method you posted throw an event then?
     
  16. Offline

    1Rogue

    Yes.
     
  17. Offline

    timtower Administrator Administrator Moderator

    Never knew that :confused:
     
  18. Offline

    mine-care

    Player player = (Player) sender
    Oh no.... Please no... Tell me that I'm dreaming and this is just a nightmare comming to an end!
    XD read my signature.
     
  19. Offline

    winspeednl

    xD thanks mine-care if i execute the command in a console the console gives me a stacktrace on that line
     
Thread Status:
Not open for further replies.

Share This Page