Interact Event Help

Discussion in 'Plugin Development' started by thepaperboy99, Aug 21, 2013.

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

    thepaperboy99

    Hello Bukkit, I need a little help with my code and why it is not working in the way that I want it to.
    Heres my code so far:

    Code:java
    1. @EventHandler
    2. public void onThrow(PlayerInteractEvent event){
    3. Player player = event.getPlayer();
    4.  
    5. if(event.getAction() == Action.RIGHT_CLICK_AIR){
    6. ItemStack itemm = new ItemStack(Material.SLIME_BALL);
    7. ItemMeta itemeta = itemm.getItemMeta();
    8. itemeta.setDisplayName(ChatColor.RED +"Slime Grenade");
    9. itemm.setItemMeta(itemeta);
    10. if (player.getItemInHand().equals(itemm)) {
    11. player.getInventory().remove(itemm);
    12. final Item item = player.getWorld().dropItem(player.getEyeLocation(), itemm);
    13. item.setMetadata("Grenade", (MetadataValue) new FixedMetadataValue(plugin, true));
    14. item.setVelocity(player.getEyeLocation().getDirection().multiply(2));
    15. Bukkit.getScheduler().scheduleSyncDelayedTask(plugin, new Runnable(){
    16.  
    17. @Override
    18. public void run() {
    19. Location loc = item.getLocation();
    20. item.remove();
    21. loc.getWorld().createExplosion(item.getLocation().getBlockX(), item.getLocation().getBlockY(), item.getLocation().getBlockZ(), 5F, false, true);
    22.  
    23. }
    24.  
    25. }, 100L);
    26.  
    27. }
    28.  
    29.  
    30. }
    31. }


    Whenever I right click it with one "grenade" it works, but if its stacked with 2 or more, it doesn't. Anyone have anyways I can fix this? Thanks.
     
  2. Instead of creating an itemstack and compairing it to the item they have inhand, try doing this
    Code:java
    1. if(event.getPlayer().getItemInHand().getType().equals(Material.SLIME_BALL)){
    2.  
    3. //Do stuff.
    4.  
    5. }
     
  3. Offline

    thepaperboy99



    But then how would I use the code above? (The item meta with setting the name). Because then they could use normal slime balls, but I only want them to be able to throw the slimeballs with the item meta above in my code. Any suggestions? Thanks.
     
  4. Offline

    hatstand

    Try using your original code, but use .isSimilar() instead of .equals() - It does the same thing as .equals(), but ignores stack size.
     
  5. Offline

    thepaperboy99



    Thanks! But know do you know how to remove the item? Because the .remove(item) method is not working for it
     
  6. Offline

    hatstand

    That would be because it's using .equals() for checking which ItemStack to remove, which as we know, doesn't work if you don't have the exact same amounts. That said, you aren't trying to remove the entire stack of slime balls, are you? From what I've gathered, you only want to remove one. Try using .removeItem(), with the ItemStack you created, not the item in the player's hand. If that doesn't work, try just adjusting the amount of items in the player's hand to be one less.
     
  7. Offline

    thepaperboy99

    Thanks! everything works now. Solved.
     
Thread Status:
Not open for further replies.

Share This Page