Snowball Projectile Not Working!

Discussion in 'Plugin Development' started by Djoy, Aug 27, 2016.

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

    Djoy

    So I recently tried to develop a plugin in which when you right click with a slimeball it will shoot a snowball. It didn't work for some reason when I right clicked a slimeball. I'm fairly new at making plugins so I could be completely wrong with this code. If anyone could please help me figure out what I did wrong, I would gladly appreciate it. And also if you have the time, I was also trying to see how to cause knock back when you're hit with the snowball, but don't take damage. Thanks! :)


    Code:
    package me.djoy.slimesnowballs;
    
    import org.bukkit.Material;
    import org.bukkit.entity.Snowball;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.plugin.java.JavaPlugin;
    
    
    public class Main extends JavaPlugin implements Listener {
       
        @Override
        public void onEnable() {
            getServer().getPluginManager().registerEvents( this,this);
       
        }
       
        @EventHandler
        public void onInteract(PlayerInteractEvent e) {
            if(e.getAction() == Action.RIGHT_CLICK_AIR && e.getAction() == Action.RIGHT_CLICK_BLOCK) {
                if(e.getPlayer().getItemInHand().getType() == Material.SLIME_BALL) {
                    e.getPlayer().launchProjectile(Snowball.class);
              }
          }
        }
       
        @Override
        public void onDisable() {
           
        }
    }
    
     
  2. Offline

    NixBuildz

    1) The code isn't supposed to be between the onEnable/onDisable. Put it outside.

    Here is what the code should look like:
    Code:
    package me.djoy.slimesnowballs;
    
    import org.bukkit.Material;
    import org.bukkit.entity.Snowball;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.plugin.java.JavaPlugin;
    
    
    public class Main extends JavaPlugin implements Listener {
    
        @Override
        public void onEnable() {
            getServer().getPluginManager().registerEvents( this,this);
    
        }
    
    
        @Override
        public void onDisable() {
        
        }
        @EventHandler
        public void onInteract(PlayerInteractEvent e) {
            if(e.getAction() == Action.RIGHT_CLICK_AIR && e.getAction() == Action.RIGHT_CLICK_BLOCK) {
                if(e.getPlayer().getItemInHand().getType() == Material.SLIME_BALL) {
                    e.getPlayer().launchProjectile(Snowball.class);
              }
          }
        }
    
    }
    Do you have any other classes?

    Also, I cant quite help you with the damage...

    I also recommend switching the item from slimeball to snowball as it might confuse the player. But this is all a suggestion.
     
    Last edited: Aug 27, 2016
    Mindlessmink likes this.
  3. Offline

    Djoy

    Okay I did this, but it's still not working. And no, I don't have any more classes.

    Code:
    package me.djoy.slimesnowballs;
    
    import org.bukkit.Material;
    import org.bukkit.entity.Snowball;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class Main extends JavaPlugin implements Listener {
    
        @Override
        public void onEnable() {
            getServer().getPluginManager().registerEvents(this, this);
    
        }
    
        @Override
        public void onDisable() {
    
        }
    
        @EventHandler
        public void onInteract(PlayerInteractEvent e) {
            if (e.getAction() == Action.RIGHT_CLICK_AIR && e.getAction() == Action.RIGHT_CLICK_BLOCK) {
                if (e.getPlayer().getItemInHand().getType() == Material.SLIME_BALL) {
                    e.getPlayer().launchProjectile(Snowball.class);
                }
            }
        }
    }
    
     
  4. @Djoy An action cannot be right clicking the air AND a block. Go into MC and try to right click the air and a block at the same time. It is impossible. Make it an or.
     
  5. Lmao, I just noticed that @bwfcwalshy >.< Change it to || not && :p
     
  6. Offline

    Djoy

    Hmmm it's still not working for me. @bwfcwalshy @Mindlessmink I changed it to an or and it's still not shooting the snowballs.

    Code:
    package me.djoy.slimesnowballs;
    
    import org.bukkit.Material;
    import org.bukkit.entity.Snowball;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.plugin.java.JavaPlugin;
    
    import net.md_5.bungee.api.ChatColor;
    
    public class Main extends JavaPlugin implements Listener {
    
        @Override
        public void onEnable() {
            getServer().getPluginManager().registerEvents(this, this);
    
        }
    
        @Override
        public void onDisable() {
    
        }
        @EventHandler
        public void onInteract(PlayerInteractEvent e) {
            if (e.getAction() == Action.RIGHT_CLICK_AIR || e.getAction() == Action.RIGHT_CLICK_BLOCK) {
                if (e.getPlayer().getItemInHand().getType() == Material.SLIME_BALL) {
                    if(e.getPlayer().getItemInHand().getItemMeta().getDisplayName()== ChatColor.GREEN + "Slime Sling") {
                    e.getPlayer().launchProjectile(Snowball.class);
                }
            }
        }
    }}
     
  7. Offline

    Zombie_Striker

    You should null check the item before getting it's type. If a player is holding air, ItemInHand will return a null object, meaning this line could throw an NPE.
    1. Same thing, make sure the item has item meta, and make sure it has a display name.
    2. You can't comparing strings using "==". That compares Memory space, not the actual value of the string., Change this to either "equals" or "equalsignorecase" if you don't care about capitalization.
     
  8. Offline

    Djoy

    @Zombie_Striker I apologize for asking, but what do you mean by null checking an item before getting its type? Like I said I'm new to making plugins, but I really do appreciate the help! :)
     
  9. @Djoy
    Null checking is basically just checking if the object is null. Easiest way is like this:
    Code:java
    1. if (myObject != null) {
    2. myObject.aMethod();
    3. }
     
  10. Offline

    Djoy

    Ok. So would I just put that next to the Event Handler? @AlvinB
     
Thread Status:
Not open for further replies.

Share This Page