Solved Custom Player Visibility Plugin Not Working At All

Discussion in 'Plugin Development' started by cosmicARTS, Aug 29, 2014.

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

    cosmicARTS

    So a while back I posted a thread on this... but I don't want to necro-post and I have slightly different different code to work with.

    Code:java
    1. // Setting up Show/Hide Players code.
    2. @SuppressWarnings("deprecation")
    3. @EventHandler
    4. public void onVisibilityClickEvent(PlayerInteractEvent event) {
    5.  
    6. final Player player = event.getPlayer();
    7.  
    8. if (player.getItemInHand().getItemMeta().getDisplayName()
    9. .equalsIgnoreCase(ChatColor.GREEN + "Players -> On")) {
    10. if (event.getAction().equals(Action.RIGHT_CLICK_BLOCK)
    11. || event.getAction().equals(Action.RIGHT_CLICK_AIR)) {
    12. if (this.players.contains(event.getPlayer().getName())) {
    13. for (Player targetPlayers : Bukkit.getOnlinePlayers()) {
    14. if (player.canSee(targetPlayers)) {
    15. player.hidePlayer(targetPlayers);
    16. } else {
    17. event.setCancelled(true);
    18. }
    19. this.players.add(player.getName());
    20. }
    21. player.getInventory().removeItem(show);
    22. player.setItemInHand(hide);
    23. player.sendMessage(ChatColor.GOLD + "" + ChatColor.ITALIC
    24. + "Hub " + ChatColor.AQUA + "• " + ChatColor.GRAY
    25. + "Players have been toggled: " + ChatColor.RED
    26. + "Off.");
    27. }
    28. }
    29. }
    30. if (player.getItemInHand().getItemMeta().getDisplayName()
    31. .equalsIgnoreCase(ChatColor.RED + "Players -> Off")) {
    32. if (event.getAction().equals(Action.RIGHT_CLICK_BLOCK)
    33. || event.getAction().equals(Action.RIGHT_CLICK_AIR)) {
    34. if (this.players.contains(event.getPlayer().getName())) {
    35. for (Player targetPlayers : Bukkit.getOnlinePlayers()) {
    36. if (player.canSee(targetPlayers)) {
    37. event.setCancelled(true);
    38. } else {
    39. player.showPlayer(targetPlayers);
    40. }
    41. this.players.add(player.getName());
    42. }
    43. player.getInventory().removeItem(hide);
    44. player.setItemInHand(show);
    45. player.sendMessage(ChatColor.GOLD + "" + ChatColor.ITALIC
    46. + "Hub " + ChatColor.AQUA + "• " + ChatColor.GRAY
    47. + "Players have been toggled " + ChatColor.GREEN
    48. + "On.");
    49. }
    50. }
    51. }
    52.  
    53. }


    This is not working. The item doesn't change, it doesn't send the message, and no players are hidden. I did register my events, and I did add a plugin.yml. I have no clue what the problem is.

    I'll just add my Main class below and the rest of this class as well:

    Show Spoiler

    Code:java
    1. package me.cosmicarts.Hub;
    2.  
    3. import me.cosmicarts.Hub.Events.PlayerJoin;
    4.  
    5. import org.bukkit.Bukkit;
    6. import org.bukkit.plugin.PluginManager;
    7. import org.bukkit.plugin.java.JavaPlugin;
    8.  
    9. public class Main extends JavaPlugin {
    10.  
    11. public void onEnable() {
    12.  
    13. PluginManager pm = Bukkit.getPluginManager();
    14. pm.registerEvents(new PlayerJoin(), this);
    15.  
    16. System.out.println("Remember to add dependencies and register events!");
    17.  
    18. }
    19.  
    20. }
    21.  



    Show Spoiler

    Code:java
    1. package me.cosmicarts.Hub.Events;
    2.  
    3. import java.util.ArrayList;
    4. import java.util.List;
    5.  
    6. import org.bukkit.Bukkit;
    7. import org.bukkit.ChatColor;
    8. import org.bukkit.Material;
    9. import org.bukkit.entity.Player;
    10. import org.bukkit.event.EventHandler;
    11. import org.bukkit.event.Listener;
    12. import org.bukkit.event.block.Action;
    13. import org.bukkit.event.player.PlayerInteractEvent;
    14. import org.bukkit.event.player.PlayerJoinEvent;
    15. import org.bukkit.inventory.Inventory;
    16. import org.bukkit.inventory.ItemStack;
    17. import org.bukkit.inventory.meta.ItemMeta;
    18.  
    19. public class PlayerJoin implements Listener {
    20.  
    21. // Defining player list for show/hide players
    22. ArrayList<String> players = new ArrayList<String>();
    23.  
    24. // Inventories
    25. public Inventory menuinv;
    26. public Inventory gadgetsinv;
    27.  
    28. // Join Items
    29. ItemStack menu = new ItemStack(Material.NETHER_STAR);
    30. {
    31.  
    32. ItemMeta menumeta = menu.getItemMeta();
    33. menumeta.setDisplayName(ChatColor.AQUA + "Potatocraft Menu");
    34. List<String> menulore = new ArrayList<String>();
    35. menulore.add(ChatColor.YELLOW + "Right-click to open the server menu.");
    36. menumeta.setLore(menulore);
    37. menu.setItemMeta(menumeta);
    38.  
    39. }
    40.  
    41. ItemStack gadgets = new ItemStack(Material.RECORD_12);
    42. {
    43.  
    44. ItemMeta gadgetsmeta = gadgets.getItemMeta();
    45. gadgetsmeta.setDisplayName(ChatColor.GRAY + "Potatocraft Gadgets");
    46. List<String> gadgetslore = new ArrayList<String>();
    47. gadgetslore.add(ChatColor.YELLOW
    48. + "Right-click to open the gadgets menu.");
    49. gadgetsmeta.setLore(gadgetslore);
    50. gadgets.setItemMeta(gadgetsmeta);
    51.  
    52. }
    53.  
    54. ItemStack show = new ItemStack(Material.REDSTONE_TORCH_ON);
    55. {
    56.  
    57. ItemMeta showmeta = show.getItemMeta();
    58. showmeta.setDisplayName(ChatColor.GREEN + "Players -> On");
    59. List<String> showlore = new ArrayList<String>();
    60. showlore.add(ChatColor.YELLOW + "Right-click to turn players off.");
    61. showmeta.setLore(showlore);
    62. show.setItemMeta(showmeta);
    63.  
    64. }
    65.  
    66. ItemStack hide = new ItemStack(Material.REDSTONE_TORCH_OFF);
    67. {
    68.  
    69. ItemMeta hidemeta = hide.getItemMeta();
    70. hidemeta.setDisplayName(ChatColor.RED + "Players -> Off");
    71. List<String> hidelore = new ArrayList<String>();
    72. hidelore.add(ChatColor.YELLOW + "Right-click to turn players on.");
    73. hidemeta.setLore(hidelore);
    74. hide.setItemMeta(hidemeta);
    75.  
    76. }
    77.  
    78. // Player Join Stuff (Give Join Items, Send MOTD, Play sounds, Give boost)
    79. @EventHandler
    80. public void onPlayerJoin(PlayerJoinEvent event) {
    81.  
    82. Player player = event.getPlayer();
    83.  
    84. player.getInventory().setItem(3, gadgets);
    85. player.getInventory().setItem(4, menu);
    86. player.getInventory().setItem(5, show);
    87.  
    88. }
    89.  
    90. // Setting up Show/Hide Players code.
    91. @SuppressWarnings("deprecation")
    92. @EventHandler
    93. public void onVisibilityClickEvent(PlayerInteractEvent event) {
    94.  
    95. final Player player = event.getPlayer();
    96.  
    97. if (player.getItemInHand().getItemMeta().getDisplayName()
    98. .equalsIgnoreCase(ChatColor.GREEN + "Players -> On")) {
    99. if (event.getAction().equals(Action.RIGHT_CLICK_BLOCK)
    100. || event.getAction().equals(Action.RIGHT_CLICK_AIR)) {
    101. if (this.players.contains(event.getPlayer().getName())) {
    102. for (Player targetPlayers : Bukkit.getOnlinePlayers()) {
    103. if (player.canSee(targetPlayers)) {
    104. player.hidePlayer(targetPlayers);
    105. } else {
    106. event.setCancelled(true);
    107. }
    108. this.players.add(player.getName());
    109. }
    110. player.getInventory().removeItem(show);
    111. player.setItemInHand(hide);
    112. player.sendMessage(ChatColor.GOLD + "" + ChatColor.ITALIC
    113. + "Hub " + ChatColor.AQUA + "• " + ChatColor.GRAY
    114. + "Players have been toggled: " + ChatColor.RED
    115. + "Off.");
    116. }
    117. }
    118. }
    119. if (player.getItemInHand().getItemMeta().getDisplayName()
    120. .equalsIgnoreCase(ChatColor.RED + "Players -> Off")) {
    121. if (event.getAction().equals(Action.RIGHT_CLICK_BLOCK)
    122. || event.getAction().equals(Action.RIGHT_CLICK_AIR)) {
    123. if (this.players.contains(event.getPlayer().getName())) {
    124. for (Player targetPlayers : Bukkit.getOnlinePlayers()) {
    125. if (player.canSee(targetPlayers)) {
    126. event.setCancelled(true);
    127. } else {
    128. player.showPlayer(targetPlayers);
    129. }
    130. this.players.add(player.getName());
    131. }
    132. player.getInventory().removeItem(hide);
    133. player.setItemInHand(show);
    134. player.sendMessage(ChatColor.GOLD + "" + ChatColor.ITALIC
    135. + "Hub " + ChatColor.AQUA + "• " + ChatColor.GRAY
    136. + "Players have been toggled " + ChatColor.GREEN
    137. + "On.");
    138. }
    139. }
    140. }
    141.  
    142. }
    143. }
    144.  



    Please help me out! How do I get this plugin to work?! It is the one thing that has always stumped me. Also, how would I go about changing the look of the /list using a PlayerCommandPreprocessEvent. It worked with the /pl but I can't seem to get it to work for player list.

    Thanks guys!

    - cosmicARTS
     
  2. Offline

    AoH_Ruthless

    cosmicARTS
    • Any error log? Every time an item without a display name is clicked you will get a NullPointerException, so fix that.
    • Use == to compare enumerations
    • As a general Java convention thing, when looping, your variable should be singular; i.e targetPlayer and not targetPlayers
    • Um, you are adding the player to the list or whatever when it is already known that they are in the list .. shouldn't you be removing them instead?
    • Nowhere do you check if the player is not in the list/set, and then add them. So from the PlayerInteractEvent alone, it seems the player is never added to the list.
     
  3. Offline

    cosmicARTS

    Yes! I get that exact NullPointerException thing, I just have no idea how to fix it.
    What do you mean == for enumerations? Replace equalsIgnoreCase with that?
    Can I just change targetPlayers to targetPlayer or will that mess code up?
    How do I do the last two points? I don't understand that .-. And yes, I know Java xD I am just not super advanced.
     
  4. Offline

    AoH_Ruthless

    cosmicARTS
    I never said you don't know Java, but now I am saying it; it is pretty clear you don't know a decent enough amount of it because you don't seem to know what an enumeration (enum) is referring to or what I mean by == vs .equals().

    • You have to check if the item has a displayname .... ItemStack#getItemMeta()#hasDisplayName().
    • Read up on Enumerations.
    • Your variable shouldn't be messed up so long as you change accordingly ... kind of obvious.
    • As for the last point, it is your code. You decide where to add them to the list. If you don't know this works, it is very clear you don't know enough Java. You can add to lists whereever you want then remove them when you are done using them.
     
  5. Offline

    cosmicARTS

    Sorry! I actually figured this out right after I posted my answer. I just had a brain fart. But thanks for the display name thing xD I was tired last night when I wrote this... the plugin works perfectly xD No errors at all. Thanks a ton :)

    Code:java
    1. // Setting up Show/Hide Players code.
    2. @SuppressWarnings("deprecation")
    3. @EventHandler
    4. public void onPlayerClickEvent(PlayerInteractEvent event) {
    5.  
    6. Player player = event.getPlayer();
    7. ItemStack item = player.getItemInHand();
    8.  
    9. if (event.getAction() == Action.RIGHT_CLICK_BLOCK
    10. || event.getAction() == Action.RIGHT_CLICK_AIR) {
    11. if (!item.getItemMeta().hasDisplayName()) {
    12. return;
    13. }
    14. }
    15.  
    16. if (event.getAction() == Action.RIGHT_CLICK_BLOCK
    17. || event.getAction() == Action.RIGHT_CLICK_AIR) {
    18. if (item.getItemMeta()
    19. .getDisplayName()
    20. .equalsIgnoreCase(
    21. ChatColor.AQUA + "Gadgets" + ChatColor.GRAY
    22. + " - Right click to view our gadgets!")) {
    23. player.openInventory(gadgetsinv);
    24. }
    25. }
    26.  
    27. if (event.getAction() == Action.RIGHT_CLICK_BLOCK
    28. || event.getAction() == Action.RIGHT_CLICK_AIR) {
    29. if (item.getItemMeta()
    30. .getDisplayName()
    31. .equalsIgnoreCase(
    32. ChatColor.GOLD + "Menu" + ChatColor.GRAY
    33. + " - Right click to view our servers!")) {
    34. player.openInventory(menuinv);
    35. }
    36. }
    37.  
    38. if (event.getAction() == Action.RIGHT_CLICK_BLOCK
    39. || event.getAction() == Action.RIGHT_CLICK_AIR) {
    40. if (item.getItemMeta()
    41. .getDisplayName()
    42. .equalsIgnoreCase(
    43. ChatColor.AQUA + "Gadgets" + ChatColor.GRAY
    44. + " - Right click to view our gadgets!")) {
    45. player.openInventory(gadgetsinv);
    46. }
    47. }
    48.  
    49. if (event.getAction() == Action.RIGHT_CLICK_BLOCK
    50. || event.getAction() == Action.RIGHT_CLICK_AIR) {
    51. if (item.getItemMeta().getDisplayName()
    52. .equalsIgnoreCase(ChatColor.GREEN + "Players → Enabled")) {
    53.  
    54. if (players.contains(player.getName())) {
    55. for (Player targetPlayer : Bukkit.getOnlinePlayers()) {
    56. if (player.canSee(targetPlayer)) {
    57. player.hidePlayer(targetPlayer);
    58. }
    59. players.add(player.getName());
    60. }
    61. player.getInventory().removeItem(show);
    62. player.setItemInHand(hide);
    63. player.sendMessage(ChatColor.GOLD + "" + ChatColor.ITALIC
    64. + "Visibility " + ChatColor.DARK_GRAY + "→ "
    65. + ChatColor.GRAY + "Players have been toggled: "
    66. + ChatColor.RED + "Off");
    67. }
    68. } else if (item
    69. .getItemMeta()
    70. .getDisplayName()
    71. .equalsIgnoreCase(
    72. ChatColor.GREEN + "Players → " + ChatColor.RED
    73. + "Disabled")) {
    74. if (players.contains(event.getPlayer().getName())) {
    75. for (Player targetPlayer : Bukkit.getOnlinePlayers()) {
    76. if (player.canSee(targetPlayer)) {
    77. event.setCancelled(true);
    78. } else {
    79. player.showPlayer(targetPlayer);
    80. }
    81. players.remove(player.getName());
    82. }
    83. }
    84. player.getInventory().removeItem(hide);
    85. player.setItemInHand(show);
    86. player.sendMessage(ChatColor.GOLD + "" + ChatColor.ITALIC
    87. + "Visibility " + ChatColor.DARK_GRAY + "→ "
    88. + ChatColor.GRAY + "Players have been toggled: "
    89. + ChatColor.GREEN + "On");
    90. }
    91. }
    92. }


    There it is! Any problems in it?
     
Thread Status:
Not open for further replies.

Share This Page