Magic Clock Code?

Discussion in 'Plugin Development' started by infopaste, Jan 22, 2014.

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

    L33m4n123

    what exactly is not working?

    The item does not show in the inventory? - You give it nowhere in your code to the player in the first place
    The Players do not get hidden? - Insert a debug message to see if it gets into the if-clause
    The Item does not get switched - Insert a debug message
     
  2. Offline

    infopaste

    Essentially, I join the server, spawned myself the dye, add the required lore to it, right click with it and nothing happened.

    I did just add this:

    Code:java
    1. @EventHandler
    2. public void OnJoin(PlayerJoinEvent event) {
    3.  
    4. ItemStack enable = new ItemStack(Material.INK_SACK, 1, (short) 10);
    5. ItemMeta enableMeta = (ItemMeta) enable.getItemMeta();
    6. enableMeta.setDisplayName(ChatColor.GREEN + "Players >> Enabled");
    7. List<String> enableList = new ArrayList<String>();
    8. enableList.add(ChatColor.WHITE + "Right click to enable to disable player visability");
    9. enableMeta.setLore(enableList);
    10. enable.setItemMeta(enableMeta);
    11.  
    12. Player player = event.getPlayer();
    13. PlayerInventory inv = player.getInventory();
    14.  
    15. players.add(player.getName());
    16.  
    17. if(player.hasPlayedBefore() == false) {
    18. inv.setItem(9, enable);
    19. }
    20.  
    21. }


    Still didn't work
     
  3. Offline

    L33m4n123

    Code:java
    1. if(!this.players.contains(event.getPlayer().getName())){


    Makes no sense. You add them to the players-list upon joining, yet you check if they are NOT in there
     
  4. Offline

    infopaste

    Sorry, but what could like do I check if they haven't been added them?
     
  5. Offline

    L33m4n123

    No idea what you even want to achieve here.

    What you do right now is

    OnPlayerJoin -> They get added into this list

    OnPlayerInteract -> Only works if they are not in the list

    ==> It cannot work the way you have it right now, because they are ALWAYS in the list as you add them as soon as they join
     
  6. Offline

    infopaste

    Basically, I want to make players invisible when the person right clicks with the dye. What I am trying to do is when a player joins the server it add them to the list of people its going to make invisible, then when a person right clicks with dye everyone on that list will disappear for them.
     
  7. Offline

    RizzelDazz

    If you want a custom item, and do not know java simply make a plugin request. The bukkit community is full of people who would love to help you. infopaste
     
  8. Offline

    mrkirby153

    infopaste

    OMG OMG OMG! (Sorry, got a little excited XD) I just found your problem! If the code you posted here is 100% accurate, then on line 41, you are executing
    Code:java
    1. enableMeta.setDisplayName(ChatColor.GREEN + "Players >> Enabled");
    but on Line 55, you are executing
    Code:java
    1. if(player.getItemInHand().getItemMeta().getDisplayName().equalsIgnoreCase(ChatColor.GREEN+"Player >> Enabled")

    You are setting the display name to Players >> Enabled but checking for Player >> Enabled (Without the "s"). You can either add the "s" or you can simply do
    Code:java
    1. if(player.getItemInHand().equals(<Your Control Item>){
    2. // Do your stuff
    3. }
     
  9. Offline

    infopaste

    @mrkirby153 , that is a step in the right direction (dumb on me). Thank you!

    buutt.....

    The plugins still doesn't work.
    I would do that, but I do know the basic's of java and do enjoy coding and learning more.
    If I/people can't help me figure what I'm doing wrong with my code, I will request for one.

    RizzelDazz, you seem well rounded in java, could you look at my plugin and see what I doing wrong?

    Here's the code with mrkirby153 modification:

    Code:java
    1. import java.util.ArrayList;
    2. import java.util.List;
    3.  
    4. import org.bukkit.Bukkit;
    5. import org.bukkit.ChatColor;
    6. import org.bukkit.Material;
    7. import org.bukkit.entity.Player;
    8. import org.bukkit.event.EventHandler;
    9. import org.bukkit.event.Listener;
    10. import org.bukkit.event.block.Action;
    11. import org.bukkit.event.inventory.InventoryClickEvent;
    12. import org.bukkit.event.player.PlayerInteractEvent;
    13. import org.bukkit.event.player.PlayerJoinEvent;
    14. import org.bukkit.event.player.PlayerQuitEvent;
    15. import org.bukkit.inventory.ItemStack;
    16. import org.bukkit.inventory.PlayerInventory;
    17. import org.bukkit.inventory.meta.ItemMeta;
    18. import org.bukkit.plugin.java.JavaPlugin;
    19.  
    20. public class Main extends JavaPlugin implements Listener {
    21.  
    22. //Register plugins
    23. public void onEnable() {
    24. getServer().getPluginManager().registerEvents(this, this);
    25. }
    26.  
    27. //List for hiding players
    28. ArrayList<String> players = new ArrayList<String>();
    29.  
    30. public void onInventoryClick(InventoryClickEvent event) {
    31. event.setCancelled(true);
    32. }
    33.  
    34. //Removing player from list
    35. public void onQuitEvent(PlayerQuitEvent event) {
    36. Player player = event.getPlayer();
    37. players.remove(player.getName());
    38. }
    39.  
    40. //Adding people to the list and giving them spawn items
    41. @EventHandler
    42. public void onPlayerJoin(PlayerJoinEvent event) {
    43.  
    44. Player player = event.getPlayer();
    45.  
    46. players.add(player.getName());
    47.  
    48. ItemStack compass = new ItemStack(Material.COMPASS);
    49. ItemMeta compassMeta = (ItemMeta) compass.getItemMeta();
    50. compassMeta.setDisplayName(ChatColor.GOLD + "Quick Tranist");
    51. List<String> compassList = new ArrayList<String>();
    52. compassList.add(ChatColor.GRAY + "Right click to open meanu");
    53. compassMeta.setLore(compassList);
    54. compass.setItemMeta(compassMeta);
    55.  
    56. ItemStack enable = new ItemStack(Material.INK_SACK, 1, (short) 10);
    57. ItemMeta enableMeta = (ItemMeta) enable.getItemMeta();
    58. enableMeta.setDisplayName(ChatColor.GREEN + "Players >> Enabled");
    59. List<String> enableList = new ArrayList<String>();
    60. enableList.add(ChatColor.WHITE + "Right click to enable to disable player visability");
    61. enableMeta.setLore(enableList);
    62. enable.setItemMeta(enableMeta);
    63.  
    64. PlayerInventory inv = player.getInventory();
    65.  
    66. inv.setItem(9, enable);
    67.  
    68. if(player.hasPlayedBefore() == false) {
    69. inv.setItem(9, enable);
    70. inv.setItem(1, compass);
    71. }
    72.  
    73. }
    74.  
    75. //Visibility Dye
    76. @EventHandler
    77. public void onPlayerClickEvent(PlayerInteractEvent event) {
    78.  
    79. Player player = event.getPlayer();
    80.  
    81. ItemStack enable = new ItemStack(Material.INK_SACK, 1, (short) 10);
    82. ItemMeta enableMeta = (ItemMeta) enable.getItemMeta();
    83. enableMeta.setDisplayName(ChatColor.GREEN + "Players >> Enabled");
    84. List<String> enableList = new ArrayList<String>();
    85. enableList.add(ChatColor.WHITE + "Right click to enable to disable player visability");
    86. enableMeta.setLore(enableList);
    87. enable.setItemMeta(enableMeta);
    88.  
    89. ItemStack disable = new ItemStack(Material.INK_SACK, 1, (short) 8);;
    90. ItemMeta disableMeta = (ItemMeta) disable.getItemMeta();
    91. disableMeta.setDisplayName(ChatColor.RED + "Players >> Disabled");
    92. List<String> disableList = new ArrayList<String>();
    93. disableList.add(ChatColor.WHITE + "Right click to enable player visability");
    94. disableMeta.setLore(disableList);
    95. disable.setItemMeta(disableMeta);
    96.  
    97. if(player.getItemInHand().getItemMeta().getDisplayName().equalsIgnoreCase(ChatColor.GREEN + "Players >> Enabled")){
    98. if(event.getAction().equals(Action.RIGHT_CLICK_BLOCK) || event.getAction().equals(Action.RIGHT_CLICK_AIR)){
    99. if(!this.players.contains(event.getPlayer().getName())){
    100. for(Player targetPlayers : Bukkit.getOnlinePlayers()){
    101. if(player.canSee(targetPlayers)){
    102. player.hidePlayer(targetPlayers);
    103. } else{
    104. event.setCancelled(true);
    105. }
    106. this.players.add(player.getName());
    107. }
    108. player.getInventory().removeItem(enable);
    109. player.setItemInHand(disable);
    110. player.sendMessage(ChatColor.DARK_GRAY + "[" + ChatColor.YELLOW + "Visibility" + ChatColor.DARK_GRAY + "] " + ChatColor.DARK_GRAY + "You have toggled player visibility: " + ChatColor.GREEN + "On");
    111. }
    112. }
    113. }
    114. if(player.getItemInHand().getItemMeta().getDisplayName().equalsIgnoreCase(ChatColor.RED + "Players >> Disabled")){
    115. if(event.getAction().equals(Action.RIGHT_CLICK_BLOCK) || event.getAction().equals(Action.RIGHT_CLICK_AIR)){
    116. for(Player targetPlayers : Bukkit.getOnlinePlayers()){
    117. if(player.canSee(targetPlayers)){
    118. event.setCancelled(true);
    119. } else {
    120. player.showPlayer(targetPlayers);
    121. }
    122. this.players.remove(player.getName());
    123. }
    124. player.getInventory().removeItem(disable);
    125. player.setItemInHand(enable);
    126. player.sendMessage(ChatColor.DARK_GRAY + "[" + ChatColor.YELLOW + "Visibility" + ChatColor.DARK_GRAY + "] " + ChatColor.DARK_GRAY + "You have toggled player visibility: " + ChatColor.RED + "Off");
    127. }
    128. }
    129. }
    130.  
    131. }
    132.  
     
  10. Offline

    L33m4n123

    STILL

    you add them to the players list when they join.
    BUT check if they are NOT in the list for it to work

    It cannot work as I stated already.
     
  11. Offline

    infopaste


    How do I change that?
     
  12. Offline

    L33m4n123

    Either remove the part were you check if they are NOT in the list or remove the part were they get added to the list when they join
     
  13. Offline

    infopaste


    If i removed the list any new players who join will be visable
     
  14. Offline

    L33m4n123

    nowhere in your code you set people invisible if they are on this list
     
  15. Offline

    Jalau

    you simply just do if(!this.players.contains(event.getPlayer().getName())){...
    Just remove the ! in the line...
     
  16. Offline

    infopaste


    Thank you!

    But this is what happens.... [​IMG]
    with 1 click it toggles twice???
     
  17. Offline

    Jalau

    after

    After you checked one of them just do return; Example:

    player.getInventory().removeItem(enable);
    player.setItemInHand(disable);
    player.sendMessage(ChatColor.DARK_GRAY + "[" + ChatColor.YELLOW + "Visibility" + ChatColor.DARK_GRAY + "] " + ChatColor.DARK_GRAY + "You have toggled player visibility: " + ChatColor.GREEN + "On");
    return;
    }
     
  18. Offline

    L33m4n123

    or to make it codewise "nicer" juse propper else-if construct
     
  19. Offline

    infopaste


    Thank you Jalau, but now it just says
    ".... On"
    ".... Off"
    with one right click

    How would I save Array Lists so on reload the lists are not lost.
     
  20. Offline

    Jalau


    Save it to a config file.... Also that can't be.... Add a return after the off say and on say in the same {} as the sendMessage is...
     
  21. Offline

    infopaste


    Do you mind just correcting it for me and adding in the saving to the config, because I've been stuck on this forever, plus I did move this to plugin request forums but none has corrected it for me.

    This is what I have:

    Code:java
    1. import java.util.ArrayList;
    2. import java.util.List;
    3.  
    4. import org.bukkit.Bukkit;
    5. import org.bukkit.ChatColor;
    6. import org.bukkit.Material;
    7. import org.bukkit.entity.Player;
    8. import org.bukkit.event.EventHandler;
    9. import org.bukkit.event.Listener;
    10. import org.bukkit.event.block.Action;
    11. import org.bukkit.event.inventory.InventoryClickEvent;
    12. import org.bukkit.event.player.PlayerInteractEvent;
    13. import org.bukkit.event.player.PlayerJoinEvent;
    14. import org.bukkit.event.player.PlayerQuitEvent;
    15. import org.bukkit.inventory.ItemStack;
    16. import org.bukkit.inventory.PlayerInventory;
    17. import org.bukkit.inventory.meta.ItemMeta;
    18. import org.bukkit.plugin.java.JavaPlugin;
    19.  
    20. public class Main extends JavaPlugin implements Listener {
    21.  
    22. public ArrayList<String> cooldown = new ArrayList<>();
    23. //List for hiding players
    24. ArrayList<String> players = new ArrayList<String>();
    25.  
    26. public void onEnable() {
    27. getServer().getPluginManager().registerEvents(this, this);
    28. }
    29.  
    30. public void onInventoryClick(InventoryClickEvent event) {
    31. event.setCancelled(true);
    32. }
    33.  
    34. //Removing player from list
    35. public void onQuitEvent(PlayerQuitEvent event) {
    36. Player player = event.getPlayer();
    37. players.remove(player.getName());
    38. }
    39.  
    40. //Adding people to the list and giving them spawn items
    41. @EventHandler
    42. public void onPlayerJoin(PlayerJoinEvent event) {
    43. Player player = event.getPlayer();
    44. PlayerInventory inv = player.getInventory();
    45. players.add(player.getName());
    46.  
    47. ItemStack enable = new ItemStack(Material.INK_SACK, 1, (short) 10);
    48. ItemMeta enableMeta = (ItemMeta) enable.getItemMeta();
    49. enableMeta.setDisplayName(ChatColor.GREEN + "Players >> Enabled");
    50. List<String> enableList = new ArrayList<String>();
    51. enableList.add(ChatColor.WHITE + "Right click to enable to disable player visability");
    52. enableMeta.setLore(enableList);
    53. enable.setItemMeta(enableMeta);
    54.  
    55. // Just here for testing
    56. inv.setItem(8, enable);
    57. }
    58.  
    59. //Visibility Dye
    60. @EventHandler
    61. public void onPlayerClickEvent(PlayerInteractEvent event) {
    62.  
    63. final Player player = event.getPlayer();
    64.  
    65. ItemStack enable = new ItemStack(Material.INK_SACK, 1, (short) 10);
    66. ItemMeta enableMeta = (ItemMeta) enable.getItemMeta();
    67. enableMeta.setDisplayName(ChatColor.GREEN + "Players >> Enabled");
    68. List<String> enableList = new ArrayList<String>();
    69. enableList.add(ChatColor.WHITE + "Right click disable player visability");
    70. enableMeta.setLore(enableList);
    71. enable.setItemMeta(enableMeta);
    72.  
    73. ItemStack disable = new ItemStack(Material.INK_SACK, 1, (short) 8);;
    74. ItemMeta disableMeta = (ItemMeta) disable.getItemMeta();
    75. disableMeta.setDisplayName(ChatColor.RED + "Players >> Disabled");
    76. List<String> disableList = new ArrayList<String>();
    77. disableList.add(ChatColor.WHITE + "Right click to enable player visability");
    78. disableMeta.setLore(disableList);
    79. disable.setItemMeta(disableMeta);
    80.  
    81. if(player.getItemInHand().getItemMeta().getDisplayName().equalsIgnoreCase(ChatColor.GREEN + "Players >> Enabled")){
    82. if (cooldown.contains(player)) {
    83. player.sendMessage(ChatColor.DARK_GRAY + "[" + ChatColor.YELLOW + "Visibility" + ChatColor.DARK_GRAY + "] " + ChatColor.GRAY + "Please wait " + ChatColor.YELLOW +"5 seconds " + ChatColor.GRAY + "to toggled players visibility: " + ChatColor.GREEN + "On");
    84. event.setCancelled(true);
    85. } else {
    86. if(event.getAction().equals(Action.RIGHT_CLICK_BLOCK) || event.getAction().equals(Action.RIGHT_CLICK_AIR)){
    87. if(this.players.contains(event.getPlayer().getName())){
    88. for(Player targetPlayers : Bukkit.getOnlinePlayers()){
    89. if(player.canSee(targetPlayers)){
    90. player.hidePlayer(targetPlayers);
    91. } else{
    92. event.setCancelled(true);
    93. }
    94. this.players.add(player.getName());
    95. }
    96. player.getInventory().removeItem(enable);
    97. player.setItemInHand(disable);
    98. player.sendMessage(ChatColor.DARK_GRAY + "[" + ChatColor.YELLOW + "Visibility" + ChatColor.DARK_GRAY + "] " + ChatColor.GRAY + "You have toggled players visibility: " + ChatColor.GREEN + "On");
    99. cooldown.add(player.getName());
    100. Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable() {
    101. public void run() {
    102. cooldown.remove(player.getName());
    103. }
    104. }, 100);
    105. return;
    106. }
    107. }
    108. }
    109. }
    110. if(player.getItemInHand().getItemMeta().getDisplayName().equalsIgnoreCase(ChatColor.RED + "Players >> Disabled")){
    111. if (cooldown.contains(player)) {
    112. player.sendMessage(ChatColor.DARK_GRAY + "[" + ChatColor.YELLOW + "Visibility" + ChatColor.DARK_GRAY + "] " + ChatColor.GRAY + "Please wait " + ChatColor.YELLOW +"5 seconds " + ChatColor.GRAY + "to toggled players visibility: " + ChatColor.RED + "Off");
    113. event.setCancelled(true);
    114. } else {
    115. if(event.getAction().equals(Action.RIGHT_CLICK_BLOCK) || event.getAction().equals(Action.RIGHT_CLICK_AIR)){
    116. if(this.players.contains(event.getPlayer().getName())){
    117. for(Player targetPlayers : Bukkit.getOnlinePlayers()){
    118. if(player.canSee(targetPlayers)){
    119. event.setCancelled(true);
    120. } else {
    121. player.showPlayer(targetPlayers);
    122. }
    123. this.players.remove(player.getName());
    124. }
    125. }
    126. player.getInventory().removeItem(disable);
    127. player.setItemInHand(enable);
    128. player.sendMessage(ChatColor.DARK_GRAY + "[" + ChatColor.YELLOW + "Visibility" + ChatColor.DARK_GRAY + "] " + ChatColor.GRAY + "You have toggled players visibility: " + ChatColor.RED + "Off");
    129. cooldown.add(player.getName());
    130. Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable() {
    131. public void run() {
    132. cooldown.remove(player.getName());
    133. }
    134. }, 100);
    135. return;
    136. }
    137. }
    138. }
    139. }
    140.  
    141. }
    142.  
     
  22. Offline

    infopaste

  23. Offline

    ipr0james

    so does this work now or did no one ever fix it for u?
     
  24. Offline

    infopaste

    That code there still doesn't work. But someone helped me fix it yesterday.
     
  25. Offline

    mattibijnens

    infopaste Any chance I could get the code? I am trying to do the same thing, but I cant get it to work :)
     
  26. Offline

    DS1995

    You are changing the item in line 97. Then in line 110 you are check the item in hand. Because you changed the item, the if-statement is true too. So change the if-statement in line 110 to a else-statement
     
  27. Offline

    Camaflicks

    infopaste Could you add me on skype? I need your help :(
     
Thread Status:
Not open for further replies.

Share This Page