Solved Shoot a fireball with a stick if inventory contains coal

Discussion in 'Plugin Development' started by woutboy, May 3, 2014.

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

    woutboy

    Hello developers,
    I'm quite new to developing in java and bukkit, but I've created some easy plugins before.

    As my title says I want to shoot a fireball with a stick if my inventory contains coal. So I've written this code:

    Code:java
    1.  
    2. @SuppressWarnings("deprecation")
    3. @EventHandler
    4. public void onInteraction(PlayerInteractEvent event){
    5. ItemStack i = event.getItem();
    6. Player player = event.getPlayer();
    7. PlayerInventory inventory = player.getInventory();
    8. ItemStack coal = new ItemStack(Material.COAL, 1);
    9.  
    10. if (event.getAction() == Action.RIGHT_CLICK_AIR){
    11. if (i.getType().equals(Material.STICK)){
    12. player.getLocation().getDirection().normalize().multiply(1);
    13. player.launchProjectile(SmallFireball.class);
    14. if (inventory.contains(coal)){
    15. inventory.removeItem(coal);
    16. player.updateInventory();
    17. }
    18. }
    19.  


    But this won't work. When A player right clicks with a stick in the air a fireball wil be launched, but nothing else.

    Can somebody explain to me what I'm doing wrong?
     
  2. Offline

    tommycake50

    Check for the coal before you launch the fireball so it launches only if they have coal (put the launching code inside the if(inventory.contains(coal))).
     
  3. Offline

    woutboy

    Can you please show me an example? Thx!
     
  4. Offline

    tommycake50

    Code:java
    1. if(i.getType().equals(Material.STICK)){
    2. if (inventory.contains(coal)){
    3. inventory.removeItem(coal);
    4. player.updateInventory();
    5. player.getLocation().getDirection().normalize().multiply(1);
    6. player.launchProjectile(SmallFireball.class);
    7. }
    8. }
     
  5. Offline

    woutboy

    Thank you! This is exactly what I wanted.

    But now I've found another bug in my code: If you have a stack of coal and you're trying to fire a fireball, you cant fire them. The only way to fire those fireballs is to separate one of the coal items.

    Do you know the solution for this?
     
  6. Offline

    tommycake50

    Can you do if(inventory.contains(Material.COAL))
     
  7. Offline

    woutboy

    tommycake50
    This works, but if a player has Charcoal in his inventory he will be able to shoot unlimited..
     
  8. Offline

    kennethbgoodin

    Do if inventory.containsAtLeast(Material.COAL, 1) rather than using contains().

    I would add a check to make sure it's not charcoal.
     
  9. Offline

    woutboy

    kennethbgoodin That worked! But I've done it with
    Code:java
    1. if (inventory.containsAtLeast(coal, 1)){
    2.  


    Because Material.Coal will display an underline.

    Thank you in advance! This solved my problem!

    After creating a custom crafting recipe and a custom item(with lore etc.) I can shoot the fireballs and it all worked fine. But when I click with a stick in the air my console gives me an error: Could not pass playerInteract event to ...

    here is my src code:
    Code:java
    1. @SuppressWarnings("deprecation")
    2. @EventHandler
    3. public void onInteractionFlameThrower(PlayerInteractEvent event){
    4. Player player = event.getPlayer();
    5. PlayerInventory inventory = player.getInventory();
    6. Location loc = player.getLocation();
    7. ItemStack coal = new ItemStack(Material.COAL, 1);
    8.  
    9. if (event.getAction() == Action.RIGHT_CLICK_AIR){
    10. boolean FlameThrower = player.getItemInHand().getItemMeta().getDisplayName().equals("FlameThrower");
    11. if(FlameThrower){
    12. if (inventory.containsAtLeast(coal, 1)){
    13. inventory.removeItem(coal);
    14. player.updateInventory();
    15. player.getLocation().getDirection().normalize().multiply(1);
    16. player.launchProjectile(SmallFireball.class);
    17. player.launchProjectile(SmallFireball.class);
    18. player.launchProjectile(SmallFireball.class);
    19. player.launchProjectile(SmallFireball.class);
    20. player.launchProjectile(SmallFireball.class);
    21. player.playSound(loc, Sound.WITHER_SHOOT, 1, 1);
    22. } else {
    23. player.sendMessage(ChatColor.BLUE + "[Pluginname] " + ChatColor.WHITE + "Je hebt steenkool nodig om te kunnen schieten!");
    24. }
    25. }
    26. }
    27. }
    28. @EventHandler(priority = EventPriority.HIGH)
    29. public void onInteractionCompass(PlayerInteractEvent event){
    30. ItemStack i = event.getItem();
    31. Player player = event.getPlayer();
    32.  
    33. if(event.getAction() == Action.RIGHT_CLICK_AIR){
    34. if (i.getType().equals(Material.COMPASS)){
    35. player.sendMessage(ChatColor.BLUE + "[pluginName] " + ChatColor.WHITE + "Om de locatie van een speler te vinden gebruik: /search (spelernaam)");
    36. }
    37. }
    38. }



    Does somebody know how to fix this? My console is giving me errors and I don't know the solution.


    I'm Dutch and because of that the .sendMessages are also written in Dutch :p


    EDIT: Also if I'm Clicking with my compass the console is giving me the same error..

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 7, 2016
  10. Offline

    XgXXSnipz

    In your payer.getItenInHand() you need to make it so it's like, p.getItemInHand().equals(new ItemStack(Material.Stick, 1)
     
  11. Offline

    woutboy

    What do you mean? Do i have to put it after the boolean FlameThrower =...? If I do that, I think I don't need my custom item to throw those fireballs..? As I've said before, java isn't my best language. Sorry for that:oops:
     
  12. Offline

    kennethbgoodin

    Paste the full stacktrace. Just knowing that it can't pass the event doesn't help.

    But it sounds like there's a null pointer, and you need to first make sure the item in the player's hand has a name, after checking if the action is RIGHT_CLICK_AIR; if you check the name before checking if it has a name or not, there's a likely chance it won't have a name, and then you'll get a null pointer exception.

    Code:
    if (event.getAction() == Action.RIGHT_CLICK_AIR){
         if (player.getItemInHand().getItemMeta().hasDisplayName() {
              //the rest of your code
         }
    }
    
     
  13. Offline

    woutboy

    I've changed my code, now it checks if the item has a name before the other code is executed. But if I rename a stick, it will execute my code. Is there a way to do something like: player.getItemInhand().getItemMeta(//The item lore here?//).hasDisplayName()

    If I can check for an item lore my problem is solved. :)

    EDIT: Solved! If I check for a lore on the line under the if statement for display name using: .hasLore it'll work! Thx everyone!
     
Thread Status:
Not open for further replies.

Share This Page