Solved Why this code isn't working ?

Discussion in 'Plugin Development' started by maestro34, May 9, 2014.

Thread Status:
Not open for further replies.
  1. I tried to give an item when a player join.
    This is my code:
    Code:java
    1. public void onJoin(PlayerJoinEvent e)
    2. {
    3. ItemManager.givePH(e.getPlayer());
    4. }


    Code:java
    1. @SuppressWarnings("deprecation")
    2. public static void givePH(Player p)
    3. {
    4. ItemStack on = new ItemStack(Material.SLIME_BALL);
    5. ItemMeta im = on.getItemMeta();
    6. im.setDisplayName(ChatColor.YELLOW + "Joueurs" + ChatColor.RESET + " \u279F " + ChatColor.GREEN + "Activés");
    7. on.setItemMeta(im);
    8. p.getInventory().setItem(8, on);
    9. p.updateInventory();
    10. }
     
  2. Offline

    SainttX

    maestro34 Have you registered the listener and marked the method with an @EventHandler annotation?
     
  3. Offline

    BeastCraft3

    to give someone a item when they join you need to do this:
    Code:java
    1. ItemStack on = new ItemStack(Material.SLIME_BALL);{
    2. ItemMeta im = on.getItemMeta();
    3. im.setDisplayName(ChatColor.YELLOW + "Joueurs" + ChatColor.RESET + " \u279F " + ChatColor.GREEN + "Activés");
    4. on.setItemMeta(im);
    5. }
    6.  
    7. @EventHandler
    8. public void OnPlayerJoin(PlayerJoinEvent e){
    9.  
    10. if(!(e.getPlayer().hasPlayedBefore())){
    11. e.getPlayer().getInventory().addItem(new ItemStack(on));
    12. }
    13. }


    I've also fixed the Itemstack

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

    Konkz

    You are aware there is an edit button?

    Also, @OP, looks like you're not registering the events and another thing is that
    you may want to add a half a second delay (10 ticks) when they join. Just for all of their stuff to load else you won't be giving them anything as it will be overloaded.
     
  5. Offline

    BeastCraft3

    Im just trying to help someone here and im kinda new. Why dont you help him instead??
     
  6. BeastCraft3 It's not working :/ Any other idea ?
    Konkz I registred the event
     
  7. Offline

    Garris0n

    Make sure it's firing by printing a message. Also, that updateInventory is not doing anything.
     
  8. Garris0n
    I tried this but it's not working:
    The "kit given" is shown but I don't receive the kit :/
    Code:java
    1. @EventHandler
    2. public void onJoin(PlayerJoinEvent e)
    3. {
    4. Player p = e.getPlayer();
    5. Hub.log.info("[Hub] kit given.");
    6. p.getInventory().setItem(8, ItemManager.getOn());
    7. }


    Code:java
    1. package me.maestro34.Hub.Manager;
    2.  
    3. import org.bukkit.ChatColor;
    4. import org.bukkit.Material;
    5. import org.bukkit.inventory.ItemStack;
    6. import org.bukkit.inventory.meta.ItemMeta;
    7.  
    8. import me.maestro34.Hub.Hub;
    9.  
    10. public class ItemManager {
    11.  
    12. public ItemManager(Hub plugin){
    13. Hub.plugin = plugin;
    14. }
    15.  
    16. public static ItemStack getOn()
    17. {
    18. ItemStack on = new ItemStack(Material.SLIME_BALL);
    19. {
    20. ItemMeta im = on.getItemMeta();
    21. im.setDisplayName(ChatColor.YELLOW + "Joueurs" + ChatColor.RESET + " \u279F " + ChatColor.GREEN + "Activés");
    22. on.setItemMeta(im);
    23. }
    24. return on;
    25. }
    26.  
    27. public static ItemStack getOff()
    28. {
    29. ItemStack off = new ItemStack(Material.MAGMA_CREAM);
    30. {
    31. ItemMeta im = off.getItemMeta();
    32. im.setDisplayName(ChatColor.YELLOW + "Joueurs" + ChatColor.RESET + " \u279F " + ChatColor.RED + "Désactivés");
    33. off.setItemMeta(im);
    34. }
    35. return off;
    36. }
    37. }
     
  9. Offline

    FabeGabeMC

    maestro34
    I see that you have the meta surrounded in {} (curly brackets), which you don't need to have. Just remove the lines I added "//These aren't needed." into.
    Code:java
    1. public static ItemStack getOn()
    2. {
    3. ItemStack on = new ItemStack(Material.SLIME_BALL);
    4. { // These aren't needed.
    5. ItemMeta im = on.getItemMeta();
    6. im.setDisplayName(ChatColor.YELLOW + "Joueurs" + ChatColor.RESET + " \u279F " + ChatColor.GREEN + "Activés");
    7. on.setItemMeta(im);
    8. } // These aren't needed.
    9. return on;
    10. }
    11.  


    EDIT: Also do it with the other methods which return ItemStacks.
     
  10. FabeGabeMC I removed the curly brackets and It's still not working :/
     
  11. Offline

    SainttX

    maestro34
    Code:java
    1. private static final ItemStack active = new ItemStack(Material.SLIME_BALL);
    2. private static final ItemStack inactive = new ItemStack(Material.MAGMA_CREAM);
    3. static {
    4. ItemMeta im = active.getItemMeta();
    5. im.setDisplayName(ChatColor.YELLOW + "Joueurs" + ChatColor.RESET + " \u279F " + ChatColor.GREEN + "Activés");
    6. active.setItemMeta(im);
    7. im = inactive.getItemMeta();
    8. im.setDisplayName(ChatColor.YELLOW + "Joueurs" + ChatColor.RESET + " \u279F " + ChatColor.RED + "Désactivés");
    9. inactive.setItemMeta(im);
    10. }
    11.  
    12. @EventHandler
    13. public void onJoin(PlayerJoinEvent event) {
    14. Player player = event.getPlayer();
    15. Bukkit.getLogger().info("Giving hub kit to " + player.getName());
    16. player.getInventory().setItem(8, active);
    17. }
     
  12. Not working :/
    Code:java
    1. private static final ItemStack active = new ItemStack(Material.SLIME_BALL);
    2. static {
    3. ItemMeta im = active.getItemMeta();
    4. im.setDisplayName(ChatColor.YELLOW + "Joueurs" + ChatColor.RESET + " \u279F " + ChatColor.GREEN + "Activés");
    5. active.setItemMeta(im);
    6. }
    7.  
    8. @EventHandler
    9. public void onJoin(PlayerJoinEvent e)
    10. {
    11. Player p = e.getPlayer();
    12. Hub.log.info("[Hub] kit given.");
    13.  
    14. p.getInventory().setItem(8, active);
    15.  
    16. e.getPlayer().setHealth(20);
    17. e.getPlayer().setFoodLevel(20);
    18. e.getPlayer().setSaturation(20);
    19. e.getPlayer().setGameMode(GameMode.ADVENTURE);
    20.  
    21. e.setJoinMessage(null);
    22.  
    23. FileConfiguration config = Hub.plugin.getConfig();
    24.  
    25. if(config.contains("spawn"))
    26. {
    27. World w = Bukkit.getServer().getWorld(config.getString("spawn.world"));
    28. Double x = config.getDouble("spawn.x");
    29. Double y = config.getDouble("spawn.y");
    30. Double z = config.getDouble("spawn.z");
    31. Location l = new Location(w, x, y, z);
    32. e.getPlayer().teleport(l);
    33. }else{
    34. if(e.getPlayer().isOp()){
    35. e.getPlayer().sendMessage(ConfigManager.prefix + ChatColor.RED + "Le spawn n'a pas encore été défini!");
    36. }
    37. }
    38. }
     
  13. Offline

    SainttX

    maestro34 I tested the code and it worked, make sure your listeners are being registered properly.
     
  14. SainttX They are registered because the "e.setJoinMessage(null);" is working, only the "p.getInventory().setItem(8, active);" is not working :/
     
  15. Offline

    tryy3

    make a timer/delay of atleast 1 tick, the onJoinEvent is fired before the player is actually loaded, so you are giving the player an item and then his inventory is getting loaded and overrides it!
     
  16. Offline

    BeastCraft3

    Want me to make the plugin for you?
     
  17. Offline

    Garris0n

    I think this might be the only remotely valid answer on this thread.

    Whoever else answered, there's a bunch of you...no, you're not correct. Making it static doesn't fix anything and it will create more problems for a few different reasons.

    @OP Undo whatever changes you've made based on this thread's feedback and try adding a delay.
     
  18. Offline

    Konkz

    Also quoting this I found a spelling issue.


    But Garris0n - I said that thing too, why don't you notice me senpai ;-;

    [​IMG]
     
  19. Offline

    Garris0n

    When I read this thread this morning I was tired. And then when I read through it again I assumed I'd seen all the answers and skipped to the new posts...

    To be fair, it doesn't look like the OP noticed you either...
     
  20. Offline

    Konkz

    He did notice me as he said he registered the events but decided to skip the other side
    of my advice which is probably the cause of his error. Hate it when people do that to be honest.
     
  21. Offline

    SainttX

    Garris0n Considering it worked on localhost I wouldn't say that it's "wrong". I'm aware of what the static modifier does and it would practically do the same thing had I kept it as an instance variable within the onJoin method.
     
Thread Status:
Not open for further replies.

Share This Page