Solved PlayerInteractEvent help

Discussion in 'Spigot Plugin Development' started by PiggyPiglet, Apr 30, 2017.

Thread Status:
Not open for further replies.
  1. Hi, I've got a class that should listen for people clicking on a stick called §cWand, and if so, summon a fireball. However, absolutely nothing happens, no errors, nothing.
    Code (Text):
    package me.piggypiglet.warrior;

    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.player.PlayerInteractEvent;

    import static org.bukkit.Material.*;
    import static org.bukkit.entity.EntityType.*;
    import static org.bukkit.event.block.Action.*;

    public class MageEvent implements Listener {
        @EventHandler
        public void onPlayerInteractEvent(PlayerInteractEvent e) {
            Player p = e.getPlayer();
            Action a = e.getAction();
            if (a == RIGHT_CLICK_AIR || a == RIGHT_CLICK_BLOCK || a == LEFT_CLICK_AIR || a == LEFT_CLICK_BLOCK) {
                if (e.getItem().getType() == STICK) {
                    if (e.getItem().getItemMeta().getDisplayName().equals("§cWand")) {
                        return;
                    }
                }
            }
            p.getWorld().spawnEntity(p.getLocation(), FIREBALL);
        }
    }
    Any help is appreciated. I am also a beginner in plugin development but I will do my best to understand responses.

    EDIT: I have registered it in main, in onenable by
    getServer().getPluginManager().registerEvents(new MageEvent(), this);

    EDIT 2: Just tried this,
    Code (Text):
            if (a == RIGHT_CLICK_AIR || a == RIGHT_CLICK_BLOCK || a == LEFT_CLICK_AIR || a == LEFT_CLICK_BLOCK) {
                if (p.getInventory().getItemInMainHand().getType() == STICK) {
                    if (p.getInventory().getItemInMainHand().getItemMeta().getDisplayName().equals("§cWand")) {
                        return;
                    }
                }
            }
    Didn't work either.
     
    #1 PiggyPiglet, Apr 30, 2017
    Last edited: Apr 30, 2017
    • Like Like x 1
  2. md_5

    Administrator Developer

    Might be a good idea to mention how you fixed it incase anyone has the same issue.
     
    • Agree Agree x 3
    • Like Like x 1
  3. @md_5 Good idea, fairly silly of me to not put the fix in here since I've suggest many people to do the same.
    Fixed by changing
    Code (Text):
            if (a == RIGHT_CLICK_AIR || a == RIGHT_CLICK_BLOCK || a == LEFT_CLICK_AIR || a == LEFT_CLICK_BLOCK) {
                if (e.getItem().getType() == STICK) {
                    if (e.getItem().getItemMeta().getDisplayName().equals("§cWand")) {
                        return;
                    }
                }
            }
            p.getWorld().spawnEntity(p.getLocation(), FIREBALL);
    to
    Code (Text):
            if (a == RIGHT_CLICK_AIR || a == RIGHT_CLICK_BLOCK || a == LEFT_CLICK_AIR || a == LEFT_CLICK_BLOCK) {
                if (e.getItem().getType() == STICK) {
                    if (e.getItem().getItemMeta().getDisplayName().equals("§cWand")) {
                        p.getWorld().spawnEntity(p.getLocation(), FIREBALL);
                    }
                }
            }
    https://github.com/PiggyPiglet/Chal...Warrior/me/piggypiglet/warrior/MageEvent.java
     
    #4 PiggyPiglet, Apr 30, 2017
    Last edited: Apr 30, 2017
Thread Status:
Not open for further replies.