Solved Give Item on player join

Discussion in 'Plugin Development' started by ian_ketje, Sep 2, 2014.

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

    ian_ketje

    I want when someone joins my server, to give them the compass to navigate, But when i give the compass, it just doesn't recognize it, but when i make a command for it it works

    Code:

    Main.java
    Code:java
    1. public ItemStack compass = new ItemStack(Material.COMPASS, 1);
    2. public String world = this.getConfig().getString("spawnworld");
    3.  
    4. public Main plugin;
    5.  
    6. private LoginHandler a = new LoginHandler(this);
    7. private ItemDropHandler b = new ItemDropHandler(this);
    8. private ItemMoveHandler c = new ItemMoveHandler(this);
    9.  
    10. public void giveItem(Player p){
    11. p.getInventory().setItem(4, compass);
    12. }
    13.  
    14. @Override
    15. public void onEnable() {
    16.  
    17. ItemMeta compassi = compass.getItemMeta();
    18. compassi.setDisplayName(ChatColor.BLACK + "[" + ChatColor.DARK_AQUA + "Golem" + ChatColor.GREEN + "Navigator" + ChatColor.BLACK + "]");
    19. List<String> l = Arrays.asList(ChatColor.LIGHT_PURPLE + "Right click to use!");
    20. compassi.setLore(l);
    21. compass.setItemMeta(compassi);
    22.  
    23. PluginManager pm = getServer().getPluginManager();
    24.  
    25. pm.registerEvents(this.a, this);
    26. pm.registerEvents(this.b, this);
    27. pm.registerEvents(this.c, this);
    28.  
    29. this.getConfig().options().copyDefaults();
    30. if (this.getConfig().getString("spawnworld") == null) {
    31. this.getConfig().set("spawnworld", "world");
    32. }
    33. this.saveConfig();
    34.  
    35. // TODO Auto-generated method stub
    36. super.onEnable();
    37. }
    38.  
    39. @Override
    40. public void onDisable() {
    41. // TODO Auto-generated method stub
    42. super.onDisable();
    43. }
    44.  
    45. @Override
    46. public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
    47. if(command.getName().equalsIgnoreCase("apple")){
    48. Player p = (Player) sender;
    49. //WORKS
    50. giveItem(p);
    51. }
    52. return super.onCommand(sender, command, label, args);
    53. }


    LoginHandler.java
    Code:java
    1. public class LoginHandler implements Listener {
    2.  
    3. private Main plugin;
    4.  
    5. public LoginHandler(Main plugin) {
    6. this.plugin = plugin;
    7. }
    8.  
    9. @EventHandler(priority = EventPriority.HIGHEST)
    10. public void PlayerJoin(PlayerLoginEvent e) {
    11. Player p = e.getPlayer();
    12. System.out.println("DEBUG1");
    13. if (p.getWorld() == plugin.getServer().getWorld(plugin.world)) {
    14. System.out.println("DEBUG2");
    15. //DOESN'T WORK
    16. plugin.giveItem(p);
    17. }
    18. }
    19.  
    20. }



    Does anyone know the answer to this? Thanks already
     
  2. Offline

    TheMcScavenger

  3. Offline

    mine-care

    Please read my signature: Player p = (Player) sender;
    also does spawnworld world exist?
    try instead of == using .equals(obj);
    EDIT: as TheMcScavenger said (i didnt notice) use player join event.
     
  4. Offline

    ian_ketje

    Thank you very much, that was indeed the problem, Thanks <3
     
Thread Status:
Not open for further replies.

Share This Page