Solved How do you properly use onPlayerJoin() and onPlayerInteract()?

Discussion in 'Plugin Development' started by ryryanator, Jun 7, 2014.

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

    ryryanator

    So i'm pretty new to the Bukkit API and I wanted to make a plugin for a hub. In this I wanted it to give a player a block, and If they click with it they go to the server. Here is my code...
    Code:java
    1. public class HubMain extends JavaPlugin implements Listener{
    2.  
    3. Player p;
    4.  
    5. public Inventory creative = Bukkit.createInventory(null, 9, ChatColor.DARK_BLUE + "Teleport To Creative!");
    6.  
    7.  
    8. public void onEnable() {
    9. getLogger().info("Plugin Enabled!");
    10.  
    11. }
    12.  
    13. public void onDisable() {
    14. getLogger().info("Plugin Disabled!");
    15.  
    16. }
    17.  
    18. @EventHandler
    19. public void onPlayerJoin(PlayerJoinEvent e) { //called when player joins
    20. Inventory inv = p.getInventory();
    21. ItemStack creative = new ItemStack(Material.GRASS);
    22. ItemMeta creativeMeta = creative.getItemMeta();
    23. creativeMeta.setDisplayName(ChatColor.AQUA + "Creative!");
    24. creativeMeta.setLore(Arrays.asList(ChatColor.RED + "Join Creative!"));
    25. creative.setItemMeta(creativeMeta);
    26. inv.setItem(0, creative); //(inv being player.getInventory();)
    27.  
    28. }
    29. @EventHandler
    30. public void onPlayerInteract(PlayerInteractEvent e) {
    31.  
    32.  
    33. if(e.getAction() == Action.RIGHT_CLICK_AIR || e.getAction() == Action.RIGHT_CLICK_BLOCK || e.getAction() == Action.LEFT_CLICK_AIR || e.getAction() == Action.LEFT_CLICK_BLOCK){
    34. Player p = e.getPlayer();
    35. final ItemStack currentItem = e.getPlayer().getItemInHand();
    36. if(currentItem != null && currentItem.getType() != Material.AIR) {
    37. if(currentItem.getItemMeta().getDisplayName() == ChatColor.AQUA + "Creative!"){
    38. p.chat("/server Creative");
    39. e.setCancelled(true);
    40. }
    41. }
    42. }
    43. }
    44. }
    45.  


    I'm not sure where its going wrong, but when I go onto the server it doesn't give any grass block. I've looked around and couldn't find an answer. Any help is great! Thanks!
     
  2. ryryanator You haven't registered the events.
    Put "getServer().getPluginManager().registerEvents(this, this);" into your onEnable() method.
    It should work after that.

    Edit:As a rule of thumb, it's best not to store player variables outside of events, because they can cause memory leaks. (In reference to the Player variables on line 3)
     
  3. Offline

    ryryanator

    Thanks!
     
Thread Status:
Not open for further replies.

Share This Page