Not shooting snowball

Discussion in 'Plugin Development' started by grasshopperMatt123, Mar 10, 2014.

Thread Status:
Not open for further replies.
  1. Well, basically I am working on a plugin that shoots a snowball if a variable is true and the slot is equal to something, is it possible someone could find my error and why it's not shooting a snowball.


    package me.grasshoppermatt.mainpackage;

    import org.bukkit.Effect;
    import org.bukkit.entity.Player;
    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;

    public class MainEvents implements Listener {

    boolean snowball1 = true;

    @EventHandler
    public void PlayerInteractEvent(PlayerInteractEvent e) {
    Player p = e.getPlayer();
    if(snowball1 == true) {
    if(!(e.getAction() == Action.RIGHT_CLICK_BLOCK)) {
    if(p.getInventory().getHeldItemSlot() == 1) {
    Snowball snowball = e.getPlayer().launchProjectile(Snowball.class);
    snowball.getWorld().playEffect(e.getPlayer().getLocation(), Effect.SMOKE, 55);
    }
    }
    }
    }
     
  2. Offline

    MooshViolet

    Your code is saying if they did not right click a block, and if they are on slot 1, launch a snowball? I believe you need to return true after
    Code:java
    1. if(!(e.getAction() == Action.RIGHT_CLICK_BLOCK)) {

    So your code would look like this:
    Code:java
    1. package me.grasshoppermatt.mainpackage;
    2.  
    3. import org.bukkit.Effect;
    4. import org.bukkit.entity.Player;
    5. import org.bukkit.entity.Snowball;
    6. import org.bukkit.event.EventHandler;
    7. import org.bukkit.event.Listener;
    8. import org.bukkit.event.block.Action;
    9. import org.bukkit.event.player.PlayerInteractEvent;
    10.  
    11. public class MainEvents implements Listener {
    12.  
    13. boolean snowball1 = true;
    14.  
    15. @EventHandler
    16. public void PlayerInteractEvent(PlayerInteractEvent e) {
    17. Player p = e.getPlayer();
    18. if(snowball1 == true) {
    19. if(!(e.getAction() == Action.RIGHT_CLICK_BLOCK)) {
    20. return true;
    21. }
    22. if(p.getInventory().getHeldItemSlot() == 1) {
    23. Snowball snowball = e.getPlayer().launchProjectile(Snowball.class);
    24. snowball.getWorld().playEffect(e.getPlayer().getLocation(), Effect.SMOKE, 55);
    25. }
    26. }
    27. }

    Also. If this is your main class, you need to have an onEnable and register events:
    Code:java
    1. public void onEnable(){
    2. getServer().getPluginManager().registerEvents(this, this);
    3. }

    Hope this helped.
     
    grasshopperMatt123 likes this.
  3. Thanks for that Catch :) I also had the Main class do that :p have a nice day man.

    Also, you can't return a void true.

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

    Onlineids

    Cant return void at all
     
  5. Offline

    MooshViolet

    grasshopperMatt123
    You are right, you have to change the ! Statement to just a normal statement.
     
  6. Onlineids MooshViolet You can't? Whatever happened to

    PHP:
    if(e.getAction() != Action.RIGHT_CLICK_BLOCK) {
        return;
    }
     
  7. Offline

    GameplayJDK

    Onlineids
    You can return a void. But because the type void means nothing, you just end the method with that. So if you don't want the code behind a certain line to be executed, use return;
     
  8. Exactly what I was hinting at, you can return a void just not with a true false ect.
     
Thread Status:
Not open for further replies.

Share This Page