Solved Don't get an event?

Discussion in 'Spigot Plugin Development' started by darkshadow22mc, Jun 6, 2016.

Thread Status:
Not open for further replies.
  1. Hi
    I've just noticed that something is wrong with my event:
    Code (Text):
    package perks;

    import net.md_5.bungee.api.ChatColor;

    import org.bukkit.Bukkit;
    import org.bukkit.Material;
    import org.bukkit.craftbukkit.v1_8_R1.entity.CraftPlayer;
    import org.bukkit.entity.Entity;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.EventPriority;
    import org.bukkit.event.Listener;
    import org.bukkit.event.entity.EntityDamageEvent;
    import org.bukkit.event.player.PlayerInteractEntityEvent;
    import org.bukkit.event.player.PlayerJoinEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;

    import vault.Setup;

    public class ModeratorEvent implements Listener{
        @EventHandler(priority = EventPriority.HIGHEST)
        public void rightClickStaffWand(PlayerInteractEntityEvent e) {

            System.out.println("Interacteed");
            if (e.getRightClicked() instanceof Player) {
                System.out.println("Right click a player");
                ItemStack staffwand = new ItemStack(Material.BLAZE_ROD);
                System.out.println("Blaze rod created");
                ItemMeta staffwandMeta = staffwand.getItemMeta();
                System.out.println("Item meta got");
                staffwandMeta.setDisplayName(ChatColor.translateAlternateColorCodes('&', "&c&lStaff Wand"));
                System.out.println("Display name set");
                if (e.getPlayer().getItemInHand().getType() == Material.BLAZE_ROD
                        && e.getPlayer().getItemInHand().getItemMeta() == staffwandMeta) {
                    System.out.println("Successfully checked that item in hand is the correct");

                    Player target = (Player) e.getRightClicked();
                    int ping = ((CraftPlayer) e.getPlayer()).getHandle().ping;

                    e.getPlayer()
                            .sendMessage(
                                    ChatColor
                                            .translateAlternateColorCodes('&',
                                                    "&7------------------------------------------------"));
                    e.getPlayer().sendMessage(
                            ChatColor.translateAlternateColorCodes(
                                    '&',
                                    "&3&lRank: &3"
                                            + Setup.chat.getPlayerPrefix(target)));
                    if (target.getCustomName() == null) {
                        e.getPlayer().sendMessage(
                                ChatColor.translateAlternateColorCodes('&',
                                        "&3&lName: &3" + target.getName()));
                    } else {
                        e.getPlayer().sendMessage(
                                ChatColor.translateAlternateColorCodes('&',
                                        "&3&lReal Name: &3" + target.getName()));
                        e.getPlayer().sendMessage(
                                ChatColor.translateAlternateColorCodes(
                                        '&',
                                        "&3&lDisguise Name: &3"
                                                + target.getCustomName()));
                    }
                    e.getPlayer().sendMessage(
                            ChatColor.translateAlternateColorCodes('&',
                                    "&3&lPing: &3" + ping + " ms"));
                    e.getPlayer()
                            .sendMessage(
                                    ChatColor
                                            .translateAlternateColorCodes('&',
                                                    "&7------------------------------------------------"));

                }

            }else{}

        }
        @EventHandler(priority = EventPriority.HIGH)
        public void playerJoinHide(PlayerJoinEvent e) {

            for (Player p : Bukkit.getOnlinePlayers()) {
                for (Player p2 : ModeratorCommand.moderatormode) {
                    if(p.hasPermission("odin.m.showplayer")){
                       
                    }else{
                        p.hidePlayer(p2);
                    }
                }
            }

        }
        @EventHandler(priority = EventPriority.HIGH)
        public void playerHitModOn(EntityDamageEvent e){
           
            Entity player = e.getEntity();
            if(player instanceof Player){
                Player p = Bukkit.getPlayer(player.getName());
                if(ModeratorCommand.moderatormode.contains(p)){
                   
                    e.setCancelled(true);
                    p.setFireTicks(0);
                   
                }
               
            }
           
        }
    }
     
    I'm not sure about why is happens. Here's the debugging log:
    Code (Text):
    [02:27:17 INFO]: Interacteed
    [02:27:17 INFO]: Right click a player
    [02:27:17 INFO]: Blaze rod created
    [02:27:17 INFO]: Item meta got
    [02:27:17 INFO]: Display name set
    As you can see,the code stops on the if(...) how can I solve it?​
     
  2. Instead of comparing it's ItemMeta to another ItemMeta, try comparing stuff such as display name.
     
    • Useful Useful x 1
  3. Use equals instead of ==

    == will only return true if both object point to the same memory location.
    While equals checks as both objects are equal to each other.

    Easy to understand example:
    Me == I true
    Me == my twin brother false
    Me equals my twin brother true
     
    • Agree Agree x 1
  4. Thanks! Now my problem is this one:

    Code (Text):
    [02:36:58 ERROR]: Could not pass event PlayerInteractEntityEvent to OdiNServer v
    1.0
    org.bukkit.event.EventException
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.ja
    va:305) ~[spigot-1.8.3.jar:git-Spigot-15fa8e1-17a3db7]
            at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.jav
    a:62) ~[spigot-1.8.3.jar:git-Spigot-15fa8e1-17a3db7]
            at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.j
    ava:502) [spigot-1.8.3.jar:git-Spigot-15fa8e1-17a3db7]
            at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.j
    ava:487) [spigot-1.8.3.jar:git-Spigot-15fa8e1-17a3db7]
            at net.minecraft.server.v1_8_R1.PlayerConnection.a(PlayerConnection.java
    :1268) [spigot-1.8.3.jar:git-Spigot-15fa8e1-17a3db7]
            at net.minecraft.server.v1_8_R1.PacketPlayInUseEntity.a(SourceFile:52) [
    spigot-1.8.3.jar:git-Spigot-15fa8e1-17a3db7]
            at net.minecraft.server.v1_8_R1.PacketPlayInUseEntity.a(SourceFile:11) [
    spigot-1.8.3.jar:git-Spigot-15fa8e1-17a3db7]
            at net.minecraft.server.v1_8_R1.PacketHandleTask.run(SourceFile:13) [spi
    got-1.8.3.jar:git-Spigot-15fa8e1-17a3db7]
            at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [
    ?:1.8.0_91]
            at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_91]
            at net.minecraft.server.v1_8_R1.MinecraftServer.z(MinecraftServer.java:6
    96) [spigot-1.8.3.jar:git-Spigot-15fa8e1-17a3db7]
            at net.minecraft.server.v1_8_R1.DedicatedServer.z(DedicatedServer.java:3
    16) [spigot-1.8.3.jar:git-Spigot-15fa8e1-17a3db7]
            at net.minecraft.server.v1_8_R1.MinecraftServer.y(MinecraftServer.java:6
    34) [spigot-1.8.3.jar:git-Spigot-15fa8e1-17a3db7]
            at net.minecraft.server.v1_8_R1.MinecraftServer.run(MinecraftServer.java
    :537) [spigot-1.8.3.jar:git-Spigot-15fa8e1-17a3db7]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_91]
    Caused by: java.lang.NullPointerException
            at perks.ModeratorEvent.rightClickStaffWand(ModeratorEvent.java:50) ~[?:
    ?]
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0
    _91]
            at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0
    _91]
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1
    .8.0_91]
            at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_91]
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.ja
    va:301) ~[spigot-1.8.3.jar:git-Spigot-15fa8e1-17a3db7]
            ... 14 more
    The line of the error is:
    Code (Text):
    e.getPlayer().sendMessage(ChatColor.translateAlternateColorCodes('&',"&3&lRank: "+ Setup.chat.getPlayerPrefix(target)));
    And the method Setup.chat... is a Vault one (yes,I have Vault working)

    Code (Text):
    public boolean setupChat()
        {
            RegisteredServiceProvider<Chat> chatProvider = Bukkit.getServer().getServicesManager().getRegistration(net.milkbowl.vault.chat.Chat.class);
            if (chatProvider != null) {
                chat = chatProvider.getProvider();
            }

            return (chat != null);
        }
    Help?
     
  5. Okay,forget it. I forgot to start the method that change chat == null to chat != null... my bad ;-;
    Thanks @andrew28 and @GPSforLEGENDS
     
Thread Status:
Not open for further replies.