Can't give items with PlayerJoinEvent.

Discussion in 'Spigot Plugin Development' started by kugick, May 3, 2015.

  1. Hi everyone, I'm trying to give to my players each time they connect on the server some stuff with PlayerJoinEvent. I can send as well a message, set on the fly... But for a obscur reason, its not working. If someone could help me, it would be very appreciated.


    Here the code I tried:
    Code (Text):
        @EventHandler
        public void playerJoin(PlayerJoinEvent e){ //called when player joins
            Player p = e.getPlayer(); //get the player who joined
            if(p.hasPlayedBefore()){ //check if the player have played before

                p.getInventory().addItem(new ItemStack(Material.IRON_PICKAXE, 1));
                p.getInventory().addItem(new ItemStack(Material.IRON_AXE, 1));
                p.getInventory().addItem(new ItemStack(Material.IRON_SPADE, 1));
                //give the player an iron pick, iron axe, and iron shovel if it is their first time playing
            }
        }
     
    • Friendly Friendly x 2
  2. gigosaurus

    Supporter

    Are you sure you've registered the event?
     
    • Agree Agree x 1
  3. That will only give players who have played before the items though - remember to add an ! before the "p.hasPlayedBefore()" to negate it.
     
    • Agree Agree x 1
  4. @kugick I think you need to make a delay task for giving the player item.
     
  5. gigosaurus

    Supporter

    No you don't - as long as some other plugin isn't interfering.

    EDIT: seems like I have to explain myself here. The reason I didn't direct kugick down this route initially is because of this mentality which plagues so many developers.
     
    #5 gigosaurus, May 3, 2015
    Last edited: May 3, 2015
    • Agree Agree x 1
  6. Works fine for me. You're either not registering your events, or you're testing with accounts that haven't joined before, which your hasJoinedBefore check is interfering with.
     
    • Agree Agree x 1
  7. @gigosaurus

    This is just a part of my code, the events are registered because when I try to send a message, it work. If you want I can paste the entire class too.


    Edit: I removed the if has played and it still doesn't work...
    Edit 2: There is my plugin project: http://kugick.net/downloads/minecraft/plugins/EvoluCraft v2.zip
     
    #7 kugick, May 3, 2015
    Last edited: May 3, 2015
  8. You should be using:

    Code (Java):
    if(!p.hasPlayedBefore()){
    That will check if the player has never played before, then run the rest of the code.
     
    • Agree Agree x 1
  9. I removed this and I tried this already with the !p.. already.
     
  10. gigosaurus

    Supporter

    Do you have any other plugins on the server that might be clearing/changing a players inventory when they join?
     
    • Agree Agree x 1
  11. gigosaurus

    Supporter

    MultiInv is probably changing the inventory. Try adding a short delay between the player joining and you giving them the items.
     
  12. Normally, when I test a plugin, I run it on a server w/out any plugins so I can make sure there aren't any plugins that can interfere with it during that stage, and after finding a plugin that does interfere, I fix it.

    Either:
    or you aren't doing something correctly.
     
  13. Better use a higher priority for the listener than multiinv (if available).
     
  14. It doesn't work because you compiled final product as .zip instead of .jar. Compile it to .jar, not to .zip
     
    • Funny Funny x 2
  15. Uh the file he posted is the source code not a compiled jar. LOL
     
    • Agree Agree x 1
  16. Add some debug messages and see what gets called and what does not in your event listener (if any)
     
  17. gigosaurus

    Supporter

    I looked at multiinv's code and it appears it does it in MONITOR, which is why I suggested adding a delay instead.
     
  18. How should I do it? If you could give me an example, it would be cool. Otherwise I will search. It's just if you want, i'll save times if you give me an example.
     
  19. gigosaurus

    Supporter

    You should have a read of this: http://wiki.bukkit.org/Scheduler_Programming#BukkitScheduler
    You won't be "saving time" if I simply give you the answer, as you probably won't fully understand what I have given you, and so you won't easily be able to reproduce it if you ever need to do it again in the future. To save time in the long run, it would be better to read that page.