Solved PlayerJoinEvent not firing

Discussion in 'Plugin Development' started by bling4525, Jul 17, 2016.

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

    bling4525

    It's exactly what it sounds like. I have a plugin I've been writing that's meant to manage some stuff in my server spawn, and the main class of that plugin contains a PlayerJoinEvent that gives players their items and sends them a chat message. All four classes currently written can be found below.

    The first: SpawnUtil (main class)

    Code:
    package me.droppedorphan.spawnutil;
    
    import java.util.Arrays;
    import java.util.List;
    import java.util.logging.Logger;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.EventPriority;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerJoinEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    import org.bukkit.plugin.Plugin;
    import org.bukkit.plugin.PluginDescriptionFile;
    import org.bukkit.plugin.java.JavaPlugin;
    
        public class SpawnUtil extends JavaPlugin implements Listener{
          
            public static SpawnUtil plugin;
          
            InvHandlers inv = new InvHandlers();
            ItemInteractHandlers i = new ItemInteractHandlers();
              
                public final Logger logger = Logger.getLogger("Minecraft");
              
                @Override
                public void onDisable(){
                  
                    PluginDescriptionFile pdfFile = this.getDescription();
                    this.logger.info(pdfFile.getName() + "version " + pdfFile.getVersion() + " has been disabled.");
                    plugin = null;
                  
                }
              
                @Override
                public void onEnable(){
                  
                    plugin = this;
                  
                    inv.loadWarpInventory();
                    inv.loadCollectiblesInventory();
                    inv.loadSettingsInventory();
               
                    registerEvents(this, i, new GadgetHandler(), inv);
                  
                    PluginDescriptionFile pdfFile = this.getDescription();
                    this.logger.info(pdfFile.getName() + " version " + pdfFile.getVersion() + " has been enabled.");      
                }
              
            public static void registerEvents(org.bukkit.plugin.Plugin plugin, Listener... listeners) {
              
                for (Listener listener : listeners) {
                  
                Bukkit.getServer().getPluginManager().registerEvents(listener, plugin);
              
                }
            }
          
            public static Plugin getPlugin() {
              
                return plugin;
              
            }
          
            @EventHandler(priority = EventPriority.HIGH)
            public void giveJoinItems(PlayerJoinEvent e){
              
                Player p = e.getPlayer();
              
                p.sendMessage("That ran.");
              
                ItemStack ServerNav = new ItemStack(Material.COMPASS);
                ItemMeta NavMeta = ServerNav.getItemMeta();
                ItemStack Railgun = new ItemStack(Material.DIAMOND_HOE);
                ItemMeta RailMeta = Railgun.getItemMeta();
                ItemStack ThrowingTnt = new ItemStack(Material.TNT);
                ItemMeta ThrowingMeta = ThrowingTnt.getItemMeta();
                ItemStack LobbySettings = new ItemStack(Material.REDSTONE_COMPARATOR);
                ItemMeta LobbyMeta = LobbySettings.getItemMeta();
                ItemStack Swag = new ItemStack(Material.RAW_FISH, 1, (short) 3);
                ItemMeta SwagMeta = Swag.getItemMeta();
              
                NavMeta.setDisplayName(ChatColor.AQUA + "" + ChatColor.BOLD + "Server Warps");
                RailMeta.setDisplayName(ChatColor.LIGHT_PURPLE + "" + ChatColor.BOLD + "Railgun");
                ThrowingMeta.setDisplayName(ChatColor.RED + "" + ChatColor.BOLD + "Throwing TNT");
                LobbyMeta.setDisplayName(ChatColor.GREEN + "" + ChatColor.BOLD + "Lobby Settings");
                SwagMeta.setDisplayName(ChatColor.GOLD + "" + ChatColor.BOLD + "Dank Collectibles");
              
                List<String> TNTLore = Arrays.asList("Throw TNT at people you dislike!");
                ThrowingMeta.setLore(TNTLore);          
                List<String> RailgunLore = Arrays.asList("Your enemies won't even know what hit them.");
                RailMeta.setLore(RailgunLore);
                List<String> NavLore = Arrays.asList("Navigate to our minigames!");
                NavMeta.setLore(NavLore);
                List<String> SwagLore = Arrays.asList("Outswag all of your friends!");
                SwagMeta.setLore(SwagLore);
                List<String> SettingsLore = Arrays.asList("Change your personal settings.");
                LobbyMeta.setLore(SettingsLore);
              
                Railgun.setItemMeta(RailMeta);
                ThrowingTnt.setItemMeta(ThrowingMeta);
                LobbySettings.setItemMeta(LobbyMeta);
                Swag.setItemMeta(SwagMeta);
                ServerNav.setItemMeta(NavMeta);
              
                p.getInventory().clear();
              
                p.sendMessage("Inventory Cleared");
              
                p.getInventory().setItem(0, ServerNav);
                p.getInventory().setItem(2, Railgun);
                p.getInventory().setItem(4, LobbySettings);
                p.getInventory().setItem(6, ThrowingTnt);
                p.getInventory().setItem(8, Swag);
              
                p.updateInventory();
              
                p.sendMessage(ChatColor.GOLD
                + "Welcome to "
                + ChatColor.AQUA + ""
                + ChatColor.BOLD + "["
                + ChatColor.LIGHT_PURPLE + ""
                + ChatColor.MAGIC + "l"
                + ChatColor.AQUA + ""
                + ChatColor.BOLD + "]"
                + ChatColor.BLACK + "-"
                + ChatColor.GREEN + ""
                + ChatColor.BOLD + "R3KTCraft!"
                + ChatColor.BLACK + "-"
                + ChatColor.AQUA + ""
                + ChatColor.BOLD + "["
                + ChatColor.LIGHT_PURPLE + ""
                + ChatColor.MAGIC + "l"
                + ChatColor.AQUA + ""
                + ChatColor.BOLD + "]");
              
            }
      
        }
    
    The second class: ItemInteractHandlers

    Code:
    package me.droppedorphan.spawnutil;
    
    import org.bukkit.Material;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerInteractEvent;
    
    public class ItemInteractHandlers implements Listener{
    
        @SuppressWarnings("deprecation")
        @EventHandler
        public void handleClicking(PlayerInteractEvent e){
          
            Player p = e.getPlayer();
          
            InvHandlers invHandler = new InvHandlers();
            GadgetHandler gHandler = new GadgetHandler();
          
            if(p.getItemInHand().getType().equals(Material.COMPASS)){
              
                invHandler.openWarpInventory(p);
              
            }
            else if(p.getItemInHand().getType().equals(Material.DIAMOND_HOE)){
              
                gHandler.instaGib(p);
              
            }
            else if(p.getItemInHand().getType().equals(Material.TNT)){
              
                gHandler.throwTNT(p);
              
            }
            else if(p.getItemInHand().getType().equals(Material.SKULL_ITEM)){
              
                invHandler.openSwagInventory(p);
              
            }
            else if(p.getItemInHand().getType().equals(Material.REDSTONE_COMPARATOR)){
              
                invHandler.openLobbySettingsInventory(p);
              
            }
            else{
              
                return;
              
            }
          
        }
      
    }
    
    The third class: InvHandlers
    Code:
    package me.droppedorphan.spawnutil;
    
    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.List;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.inventory.InventoryClickEvent;
    import org.bukkit.event.player.PlayerDropItemEvent;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    
    public class InvHandlers implements Listener{
      
        public static Inventory inv = Bukkit.createInventory(null, 9, ChatColor.GOLD + "Server Warps");
        public static Inventory invTwo = Bukkit.createInventory(null, 9, ChatColor.GOLD + "Your Dank Collectibles");
        public static Inventory invThree = Bukkit.createInventory(null, 36, ChatColor.GOLD + "Lobby Settings");
        public static Inventory invMorph = Bukkit.createInventory(null, 54, ChatColor.GOLD + "Lobby Morphs");
        public static Inventory invPet = Bukkit.createInventory(null, 54, ChatColor.GOLD + "Pets");
      
        public static ArrayList<Player> railable = new ArrayList<Player>();
        public static ArrayList<Player> TNTable = new ArrayList<Player>();
      
        @EventHandler
        public void stopMovement(InventoryClickEvent e){
          
            e.setCancelled(true);
          
        }
      
        @EventHandler
        public void stopDrops(PlayerDropItemEvent e){
          
            e.setCancelled(true);
          
        }
      
        public void openWarpInventory(Player p){
          
            p.openInventory(inv);
          
        }
      
        public void openSwagInventory(Player p){
          
            p.openInventory(invTwo);
          
        }
      
        public void openLobbySettingsInventory(Player p){
          
            p.openInventory(invThree);
          
        }
      
        public void loadWarpInventory(){
          
            ItemStack creativeWarp = new ItemStack(Material.DIRT);
            ItemMeta creativeMeta = creativeWarp.getItemMeta();
            ItemStack factionsWarp = new ItemStack(Material.TNT);
            ItemMeta factionsMeta = factionsWarp.getItemMeta();
            ItemStack minigamesWarp = new ItemStack(Material.CAKE);
            ItemMeta minigamesMeta = minigamesWarp.getItemMeta();
            ItemStack prisonWarp = new ItemStack(Material.IRON_FENCE);
            ItemMeta prisonMeta = prisonWarp.getItemMeta();
            ItemStack kitPVPWarp = new ItemStack(Material.GOLDEN_APPLE);
            ItemMeta kitPVPMeta = kitPVPWarp.getItemMeta();
          
            creativeMeta.setDisplayName("Creative Plots");
            factionsMeta.setDisplayName("Factions V1");
            minigamesMeta.setDisplayName("Minigames");
            prisonMeta.setDisplayName("Prison");
            kitPVPMeta.setDisplayName("Kit PVP");
          
            List<String> creativeLore = Arrays.asList("Build epic structures in this creative gamemode!");
            List<String> factionsLore = Arrays.asList("Become the top faction in this PVP/War based minigame!");
            List<String> minigamesLore = Arrays.asList("Play some of our awesome minigames!");
            List<String> prisonLore = Arrays.asList("Mine, fight, and escape the prison in this crazy gamemode!");
            List<String> kitPVPLore = Arrays.asList("Show your friends who the real MLG Pro crafter is.");
          
            creativeMeta.setLore(creativeLore);
            factionsMeta.setLore(factionsLore);
            minigamesMeta.setLore(minigamesLore);
            prisonMeta.setLore(prisonLore);
            kitPVPMeta.setLore(kitPVPLore);
          
            creativeWarp.setItemMeta(creativeMeta);
            factionsWarp.setItemMeta(factionsMeta);
            minigamesWarp.setItemMeta(minigamesMeta);
            prisonWarp.setItemMeta(prisonMeta);
            kitPVPWarp.setItemMeta(kitPVPMeta);
          
            inv.setItem(0, creativeWarp);
            inv.setItem(2, factionsWarp);
            inv.setItem(4, minigamesWarp);
            inv.setItem(6, prisonWarp);
            inv.setItem(8, kitPVPWarp);
      
        }
      
        public void loadCollectiblesInventory(){
          
            ItemStack disguises = new ItemStack(Material.DRAGON_EGG);
            ItemMeta disguisesMeta = disguises.getItemMeta();
            ItemStack pets = new ItemStack(Material.BONE);
            ItemMeta petsMeta = pets.getItemMeta();
            ItemStack playerInfo = new ItemStack(Material.SKULL);
            ItemMeta playerMeta = playerInfo.getItemMeta();
            ItemStack cosmetics = new ItemStack(Material.BREWING_STAND);
            ItemMeta cosmeticsMeta = cosmetics.getItemMeta();
          
            disguisesMeta.setDisplayName("Morphs");
            playerMeta.setDisplayName("Player Info");
            cosmeticsMeta.setDisplayName("Other Cosmetics");
            petsMeta.setDisplayName("Pets");
          
            List<String> disguisesLore = Arrays.asList("Disguise yourself as your favorite mob!");
            List<String> playerLore = Arrays.asList("View info and stats about yourself!");
            List<String> cosmeticsLore = Arrays.asList("A proper quickscoper knows the value of appearances.");
            List<String> petsLore = Arrays.asList("Get an adorable companion to follow you everywhere!");
          
            disguisesMeta.setLore(disguisesLore);
            playerMeta.setLore(playerLore);
            cosmeticsMeta.setLore(cosmeticsLore);
            petsMeta.setLore(petsLore);
          
            disguises.setItemMeta(disguisesMeta);
            playerInfo.setItemMeta(playerMeta);
            cosmetics.setItemMeta(cosmeticsMeta);
            pets.setItemMeta(petsMeta);
          
            invTwo.setItem(1, disguises);
            invTwo.setItem(3, playerInfo);
            invTwo.setItem(5, cosmetics);
            invTwo.setItem(7, pets);
      
        }
      
        public void loadSettingsInventory(){
          
            ItemStack visibility = new ItemStack(Material.EYE_OF_ENDER);
            ItemMeta visibilityMeta = visibility.getItemMeta();
            ItemStack speed = new ItemStack(Material.FEATHER);
            ItemMeta speedMeta = speed.getItemMeta();
            ItemStack railable = new ItemStack(Material.DIAMOND_HOE);
            ItemMeta railableMeta = railable.getItemMeta();
            ItemStack TNTable = new ItemStack(Material.DIAMOND_HOE);
            ItemMeta TNTMeta = TNTable.getItemMeta();
            ItemStack whiteGlass = new ItemStack(Material.STAINED_GLASS_PANE, 1, (short) 0);
            ItemStack greyGlass = new ItemStack(Material.STAINED_GLASS_PANE, 1, (short) 7);
            ItemStack greenDye = new ItemStack(Material.INK_SACK, 1, (short) 10);
            ItemStack greyDye = new ItemStack(Material.INK_SACK, 1, (short) 8);
          
            visibilityMeta.setDisplayName("Toggle player visibility");
            speedMeta.setDisplayName("Toggle speed III");
            railableMeta.setDisplayName("Toggle ability to be railgunned");
            TNTMeta.setDisplayName("Toggle TNT knockback");
          
            List<String> visibilityLore = Arrays.asList("This toggles the visibility of players around you.");
            List<String> speedLore = Arrays.asList("This toggles speed III for lobby navigation purposes.");
            List<String> railableLore = Arrays.asList("This toggles whether people can hit you with the Railgun.");
            List<String> TNTLore = Arrays.asList("This toggles whether people can hit you with the Throwing TNT.");
          
            visibilityMeta.setLore(visibilityLore);
            speedMeta.setLore(speedLore);
            railableMeta.setLore(railableLore);
            TNTMeta.setLore(TNTLore);
          
            visibility.setItemMeta(visibilityMeta);
            speed.setItemMeta(speedMeta);
            railable.setItemMeta(railableMeta);
            TNTable.setItemMeta(TNTMeta);
          
            invThree.setItem(10, visibility);
            invThree.setItem(12, speed);
            invThree.setItem(14, railable);
            invThree.setItem(16, TNTable);
            invThree.setItem(19, greenDye);
            invThree.setItem(21, greyDye);
            invThree.setItem(23, greenDye);
            invThree.setItem(25, greenDye);
            invThree.setItem(0, greyGlass);
            invThree.setItem(8, greyGlass);
            invThree.setItem(27, greyGlass);
            invThree.setItem(35, greyGlass);
            invThree.setItem(1, whiteGlass);
            invThree.setItem(2, whiteGlass);
            invThree.setItem(3, whiteGlass);
            invThree.setItem(4, whiteGlass);
            invThree.setItem(5, whiteGlass);
            invThree.setItem(6, whiteGlass);
            invThree.setItem(7, whiteGlass);
            invThree.setItem(9, whiteGlass);
            invThree.setItem(11, whiteGlass);
            invThree.setItem(13, whiteGlass);
            invThree.setItem(15, whiteGlass);
            invThree.setItem(17, whiteGlass);
            invThree.setItem(18, whiteGlass);
            invThree.setItem(20, whiteGlass);
            invThree.setItem(22, whiteGlass);
            invThree.setItem(24, whiteGlass);
            invThree.setItem(26, whiteGlass);
            invThree.setItem(28, whiteGlass);
            invThree.setItem(29, whiteGlass);
            invThree.setItem(30, whiteGlass);
            invThree.setItem(31, whiteGlass);
            invThree.setItem(32, whiteGlass);
            invThree.setItem(33, whiteGlass);
            invThree.setItem(34, whiteGlass);
          
        }
      
    }
    
    And finally the last class: GadgetHandler

    Code:
    package me.droppedorphan.spawnutil;
    
    import java.util.Set;
    
    import net.minecraft.server.v1_10_R1.EnumParticle;
    import net.minecraft.server.v1_10_R1.PacketPlayOutWorldParticles;
    
    import org.bukkit.Bukkit;
    import org.bukkit.Location;
    import org.bukkit.Material;
    import org.bukkit.World;
    import org.bukkit.block.Block;
    import org.bukkit.block.BlockState;
    import org.bukkit.craftbukkit.v1_10_R1.entity.CraftPlayer;
    import org.bukkit.entity.Player;
    import org.bukkit.entity.TNTPrimed;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.entity.EntityExplodeEvent;
    
    public class GadgetHandler implements Listener{
    
        public void throwTNT(Player p){
          
            Location l = p.getLocation();
            double f = l.getY() + 1;
            Location lTwo = new Location(p.getWorld(), l.getX(), f, l.getZ());
          
            World w = p.getWorld();
            TNTPrimed tnt = w.spawn(lTwo, TNTPrimed.class);
            tnt.setVelocity(p.getEyeLocation().getDirection().multiply(3));
          
        }
      
        @EventHandler
        public void stopTNTExplosion(EntityExplodeEvent e){
          
            for(Block b : e.blockList()){
              
                final BlockState state = b.getState();
              
                b.setType(Material.AIR);
              
                int delay = 20;
              
                if(b.getType().equals(Material.SAND) || b.getType().equals(Material.GRAVEL)){
                  
                    delay += 1;
                  
                }
              
                Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(SpawnUtil.plugin, new Runnable(){
                  
                    public void run(){
                      
                        state.update(true, false);
                      
                    }
                  
                }, delay);
              
            }
              
        }
      
        public void instaGib(Player p){
          
            for(Block b : p.getLineOfSight((Set<Material>)null, 25)){
              
                Location l = b.getLocation();
                World w = l.getWorld();
              
                int lx = (int) l.getX();
                int ly = (int) l.getY();
                int lz = (int) l.getZ();
              
                PacketPlayOutWorldParticles packet = new PacketPlayOutWorldParticles(EnumParticle.FIREWORKS_SPARK, false, lx, ly, lz, 0, 0, 0, 0, 1, null);
                ((CraftPlayer)p).getHandle().playerConnection.sendPacket(packet);
              
                if(b.getType() != Material.AIR){
                  
                    PacketPlayOutWorldParticles packetTwo = new PacketPlayOutWorldParticles(EnumParticle.DRAGON_BREATH, false, lx, ly, lz, 0, 0, 0, 0, 1, null);
                    ((CraftPlayer)p).getHandle().playerConnection.sendPacket(packetTwo);
                  
                    w.createExplosion(l, 5);
                  
                    break;
                  
                }
              
            }
          
        }
      
    }
    
    So the problem I'm having is that nothing is given to the player upon spawning and they are not given the message, but if I use /give to get a diamond hoe, tnt, a skull, a comparator, or a compass, all of the other utilities work fine - the railgun works, the tnt works, the GUIs open.

    I get the feeling that it has something to do with the way I registered events in my first class. Since this is the first plugin I've ever written that dealt with more than one class, I did follow a tutorial on how to register the events. The tutorial was written by JPG2000 and can be found here.

    https://bukkit.org/threads/tutorial-using-multiple-classes.179833/

    I checked around, I googled, and I looked through every line of code, but I just can't figure out what's going wrong with my events/listeners (if that even is the root of the problem.)

    Thanks! -Bling
     
  2. Offline

    thapengwin

    registerEvents(this, i, new GadgetHandler(), inv);
    should be
    registerEvents(this, this, i, new GadgetHandler(), inv);

    You forgot to register the main class, aka the plugin itself. The first "this" is interpreted as the plugin, the second "this" as the EventHandler.
     
  3. Before you progress in plugin development anymore please do not learn from BcBroz. Want to know how I knew? Well it was incredibly simple, there are many things wrong, some basic Java mistakes and other due to the way TheBCBroz teaches. Please fix these if you want to make good plugins and I urge you to learn Java before doing an API and find a good tutorial to use.

    Issues:
    Main class:
    1. @SupressWarning - SupressWarnings are something you should not have, if something is deprecated find the new way and fix it.
    2. Stop the static abuse
    3. No need for that entire onDisable remove it.
    4. You do not need to log enable/disable Bukkit does it for you.
    5. Stop using Java's Logger, there is a reason Bukkit has one.
    6. Your onEnable contains useless bits of code, for example why do you have your own register method? No need, why not just use Bukkit's espcially with 3 events.
    7. Again, stop the static abuse, pass the instance
    8. Can you give me a reason why that event is HIGH priority?
    9. Follow naming conventions
    10. Why do you put that message on so many lines? I understand not wanting to scroll across but you have every bit on a new line.

    ItemInteractHandlers:
    1. Again, don't have a SupressWarning, find the deprecated method and replace it.
    2. You are making class instnaces EVERY time a player interacts. Why not make 1 instance and call it.
    3. Compare enums with == not .equals - Learn why here

    InvHandlers:
    1. Stop the static abuse.
    2. You know you can always use a Map rather than 2 lists.
    3. Again, follow naming conventions
    4. Look into using a for loop and save yourself the effort - https://docs.oracle.com/javase/tutorial/java/nutsandbolts/for.html
    5. More code can be shortened but that is just basic Java knowledge so I'll let you sort that.

    GadgetHandler;
    1. You set the delay to 20 each block, when you make a variable it doesn't stay the same through a for loop. Basic java
    2. Again, == not .equals and ++ learn the operators
    3. Why are you scheduling a delay to update state of each block???
    4. Your casting null?
    5. Instead of casting int why not use the int method? Location javadocs
    6. I'd learn the basics of Java and Bukkit before doing packets
     
  4. Offline

    Tecno_Wizard

    @bling4525, as @bwfcwalshy said, your code has all the signature issues of The BCBroz. We, politely, wish he wouldn't make videos because he really doesn't know what he is doing.

    Don't think we are bashing you. We're just trying to help you prevent making the mistakes that hold people behind in the long run.
     
    bwfcwalshy likes this.
  5. Offline

    bling4525

    alright, thanks guys. I'll get a java book or something and figure out how to do this properly.

    And btw @bwcfwalshy thanks for taking the time to tell me what I had done wrong. Im just really messing around in Bukkit & Java right now, and I'm taking a course over the summer in Java so hopefully I'll get a better (and less spotty) knowledge of the language.

    Thanks for your time. -Bling


    Edit: ok supersniper had that covered nvm
     
    Last edited: Jul 17, 2016
  6. Offline

    SuperSniper

    if you don't want to read, I recommend PogoStick29Dev or SgtCaze (new videos) on YouTube
     
Thread Status:
Not open for further replies.

Share This Page