Magic Clock Code?

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

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

    infopaste

    Hello,
    I was wondering if someone could give me the code, to make all players invisible when right click on a item.
    Someone thing Like the one on Hive:
    [​IMG]
    How would you code that,

    Thank you.
     
  2. infopaste check if player clicks on that item in his inventory, if the item is that green die, go through all online players and set the player for them hidden. Then change the idem to the gray die and change the display name to disabled. If someone clicks on the gray die go through all online players and set the player for them to shown.
     
  3. Offline

    RizzelDazz

    Spoon feeding code is not the way to learn. To make something like this, you use something like the code I posted bellow. This is from the official "How to make a plugin" wiki. This can be found here.

    This is the code from the wiki which relates to making player invisible to others:

    Code:java
    1. //Hiding a Player From Another Player
    2. //This will hide the player who used this command from a specified player. Everyone else will be able to see the player.
    3. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    4. if(cmd.getName().equalsIgnoreCase("HideMe") && args.length == 1) {
    5. if (!(sender instanceof Player)) {
    6. sender.sendMessage("Only players can use this command!");
    7. return true;
    8. }
    9. // After checking to make sure that the sender is a Player, we can safely case it to one.
    10. Player s = (Player) sender;
    11.  
    12. // Gets the player who shouldn't see the sender.
    13. Player target = Bukkit.getServer().getPlayer(args[0]);
    14. if (target == null) {
    15. sender.sendMessage("Player " + args[0] + " is not online.");
    16. return true;
    17. }
    18. // Hides a given Player (s) from someone (target).
    19. target.hidePlayer(s);
    20. return true;
    21. }
    22. return false;
    23. }


    You would use arraylsits and booleans to see if the putty is a certain color.
     
    Vekh likes this.
  4. Offline

    infopaste

    I'm new to Bukkit coding, I can read it but have trouble writing it. If someone doesn't mind give me the code to make the magic clock, and changing putty with lores. I could modify it to my liking.

    I understand that's what I have to do, but I don't understand what the code is to do so.
     
  5. Offline

    RizzelDazz

    Then you need to learn how to code in java before attempting bukkit API.
     
    Garris0n and CeramicTitan like this.
  6. Offline

    infopaste

    I have been working on that, but I really like the code for my hub.
     
  7. Offline

    Deleted user

    infopaste

    There's no good knowing how to read it if you want to be a developer. You need to be able to understand how it works.

    Maybe read the Plugin Tutorial, then try making something as simple as sending a message, and continue from there.
     
  8. Offline

    RizzelDazz

    You can't just take code, change a few words and call it your own. That unethical and just not right. Find the plugin you need from the Dev List and just use it. No harm in giving the proper author credit.
     
  9. Offline

    CeramicTitan

    I only feel it is acceptable to spoonfeed people after they have actually give the problem a legitimate go. infopaste
    This should be moved to the request section.
     
  10. Offline

    Shinxs

    Theres an very easy way, Just listen to when theres a click and then check what material it is and then set it to another material and loop through all the online players and if they'r not equal to you then player.hidePlayer(target)
     
  11. Offline

    infopaste

    I will give it A try and display my code.
     
  12. infopaste Yeah, I'm actually too lazy to code you that. Why don't you just use commands instead of this and when you've collected more experience go back to this and add the inventory thing? Just look if the player has the right permission and entered the right command and then go through all online players and set the sender to hidden for them.
     
  13. Offline

    RizzelDazz

    Agreed.
     
  14. Offline

    infopaste

    This is my legitmate try:
    Code:java
    1. @EventHandler
    2. public void onPlayerFristClickEvent(PlayerInteractEvent event) {
    3.  
    4. Player player = event.getPlayer();
    5.  
    6. ItemStack enable = new ItemStack(Material.INK_SACK, 1, (short) 10);
    7. ItemMeta enableMeta = (ItemMeta) enable.getItemMeta();
    8. enableMeta.setDisplayName(ChatColor.GREEN + "Players >> Enabled");
    9. List<String> enableList = new ArrayList<String>();
    10. enableList.add(ChatColor.WHITE + "Right click to enable to disable player visability");
    11. enableMeta.setLore(enableList);
    12. enable.setItemMeta(enableMeta);
    13.  
    14. ItemStack disable = new ItemStack(Material.INK_SACK, 1, (short) 8);;
    15. ItemMeta disableMeta = (ItemMeta) disable.getItemMeta();
    16. disableMeta.setDisplayName(ChatColor.RED + "Players >> Disabled");
    17. List<String> disableList = new ArrayList<String>();
    18. disableList.add(ChatColor.WHITE + "Right click to enable player visability");
    19. disableMeta.setLore(disableList);
    20. disable.setItemMeta(disableMeta);
    21.  
    22.  
    23.  
    24. if(event.getPlayer().getItemInHand().equals(enable)) {
    25. if(event.getAction().equals(Action.RIGHT_CLICK_BLOCK) || event.getAction().equals(Action.RIGHT_CLICK_AIR)){
    26. for(Player targetPlayers : Bukkit.getOnlinePlayers()){
    27. if(player.canSee(targetPlayers)){
    28. player.hidePlayer(targetPlayers);
    29. player.getInventory().removeItem(enable);
    30. player.setItemInHand(disable);
    31. } else{
    32. event.setCancelled(true);
    33. }
    34. }
    35. }
    36. }
    37. if(event.getPlayer().getItemInHand().equals(enable)) {
    38. if(event.getAction().equals(Action.RIGHT_CLICK_BLOCK) || event.getAction().equals(Action.RIGHT_CLICK_AIR)){
    39. for(Player targetPlayers : Bukkit.getOnlinePlayers()){
    40. if(player.canSee(targetPlayers)){
    41. event.setCancelled(true);
    42. } else {
    43. player.showPlayer(targetPlayers);
    44. player.getInventory().removeItem(disable);
    45. player.setItemInHand(enable);
    46. }
    47. }
    48. }
    49. }
    50. }
     
  15. Offline

    CeramicTitan


    Check the name instead of the ItemStack. p.getItemInHand() this returns and ItemStack, so we get the meta, getItemMeta(). Once we have the meta we can get the Display name. getDisplayName(); From there check the whole string.
    It should look something like this:
    Code:java
    1. if(player.getItemInHand().getItemMeta().getDisplayName().equalsIgnoreCase(ChatColor.GREEN+"Player >> Enabled")){//code here}


    EDIT: You also don't need two of the same checks.
     
  16. Offline

    infopaste


    Code:java
    1. @EventHandler
    2. public void onPlayerFristClickEvent(PlayerInteractEvent event) {
    3.  
    4. Player player = event.getPlayer();
    5.  
    6. ItemStack enable = new ItemStack(Material.INK_SACK, 1, (short) 10);
    7. ItemMeta enableMeta = (ItemMeta) enable.getItemMeta();
    8. enableMeta.setDisplayName(ChatColor.GREEN + "Players >> Enabled");
    9. List<String> enableList = new ArrayList<String>();
    10. enableList.add(ChatColor.WHITE + "Right click to enable to disable player visability");
    11. enableMeta.setLore(enableList);
    12. enable.setItemMeta(enableMeta);
    13.  
    14. ItemStack disable = new ItemStack(Material.INK_SACK, 1, (short) 8);;
    15. ItemMeta disableMeta = (ItemMeta) disable.getItemMeta();
    16. disableMeta.setDisplayName(ChatColor.RED + "Players >> Disabled");
    17. List<String> disableList = new ArrayList<String>();
    18. disableList.add(ChatColor.WHITE + "Right click to enable player visability");
    19. disableMeta.setLore(disableList);
    20. disable.setItemMeta(disableMeta);
    21.  
    22. if(player.getItemInHand().getItemMeta().getDisplayName().equalsIgnoreCase(ChatColor.GREEN+"Player >> Enabled")){
    23. if(event.getAction().equals(Action.RIGHT_CLICK_BLOCK) || event.getAction().equals(Action.RIGHT_CLICK_AIR)){
    24. for(Player targetPlayers : Bukkit.getOnlinePlayers()){
    25. if(player.canSee(targetPlayers)){
    26. player.hidePlayer(targetPlayers);
    27. player.getInventory().removeItem(enable);
    28. player.setItemInHand(disable);
    29. } else{
    30. event.setCancelled(true);
    31. }
    32. }
    33. }
    34. }
    35. if(player.getItemInHand().getItemMeta().getDisplayName().equalsIgnoreCase(ChatColor.RED+"Player >> Disable")){
    36. if(event.getAction().equals(Action.RIGHT_CLICK_BLOCK) || event.getAction().equals(Action.RIGHT_CLICK_AIR)){
    37. for(Player targetPlayers : Bukkit.getOnlinePlayers()){
    38. if(player.canSee(targetPlayers)){
    39. event.setCancelled(true);
    40. } else {
    41. player.showPlayer(targetPlayers);
    42. player.getInventory().removeItem(disable);
    43. player.setItemInHand(enable);
    44. }
    45. }
    46. }
    47. }
    48. }


    I when in-game spawned myself Lime Dye, gave it the Lore 'Player >> Enabled' in green, and nothing happends.

    I don't think I'm understanding what your saying.
     
  17. Offline

    CeramicTitan

    I think youre doing something wrong, I'll take a look when I get home.
     
  18. Offline

    infopaste

    Thank you very much I do really appreciate your time.
     
  19. Offline

    mrkirby153

    infopaste

    Did you register your events? And also, instead of giving the player disabled and removing enabled every time you hide a player, do it AFTER you hide the players.
     
  20. Offline

    infopaste

    I did register my events. :/
    Do you mean doing it like this:
    Code:java
    1. if(player.getItemInHand().getItemMeta().getDisplayName().equalsIgnoreCase(ChatColor.GREEN+"Player >> Enabled")){
    2. if(event.getAction().equals(Action.RIGHT_CLICK_BLOCK) || event.getAction().equals(Action.RIGHT_CLICK_AIR)){
    3. for(Player targetPlayers : Bukkit.getOnlinePlayers()){
    4. if(player.canSee(targetPlayers)){
    5. player.hidePlayer(targetPlayers);
    6. } else{
    7. event.setCancelled(true);
    8. }
    9. }
    10. player.getInventory().removeItem(enable);
    11. player.setItemInHand(disable);
    12. }
    13. }
    14. if(player.getItemInHand().getItemMeta().getDisplayName().equalsIgnoreCase(ChatColor.RED+"Player >> Disable")){
    15. if(event.getAction().equals(Action.RIGHT_CLICK_BLOCK) || event.getAction().equals(Action.RIGHT_CLICK_AIR)){
    16. for(Player targetPlayers : Bukkit.getOnlinePlayers()){
    17. if(player.canSee(targetPlayers)){
    18. event.setCancelled(true);
    19. } else {
    20. player.showPlayer(targetPlayers);
    21. }
    22. }
    23. player.getInventory().removeItem(disable);
    24. player.setItemInHand(enable);
    25. }
    26. }
    27. }
     
  21. Offline

    Niknea

    infopaste No he means registering it in your main class.
     
  22. Offline

    infopaste

    I did do that, I was asking about what he met of taking the item change outside of the loop.
     
  23. Offline

    RizzelDazz

    You should check out some java tutorials before continuing, the best thing to do is to learn java before attempting to work with the Bukkit API. When I first started I did what you did but later learned I needed to stop and learn java first. Trust me.

    Click here for the official oracle tutorial site
     
  24. Offline

    infopaste


    I have been learning, but I really need this plugin for my hub 'now'. That's why I'm asking if someone could fix it for me, pointing out what I did wrong and where I could make it more clean.
     
  25. Offline

    Garris0n

    If you need it now there are already plugins for it...
     
  26. Offline

    infopaste

  27. Offline

    Garris0n

    Well if you're so insistent upon it I'll give you some help with it.

    How to properly check the name of an ItemStack while avoiding plenty of potential NullPointerExceptions:
    Code:java
    1. public void checkItem(ItemStack item, String name){
    2. return item != null && item.hasItemMeta() && item.getItemMeta().hasDisplayName() && item.getItemMeta().getDisplayName().equals(name);
    3. }


    As for hiding, showing, you need to loop through the rest of the online players and use yourPlayer.hide/showPlayer(onlinePlayerFromLoop) or something to that effect.
     
  28. Offline

    infopaste

    Let me show you the hole thing so you see if theirs another mistakes,

    Code:java
    1. public class Main extends JavaPlugin implements Listener {
    2.  
    3. public void onEnable() {
    4. getServer().getPluginManager().registerEvents(this, this);
    5. }
    6.  
    7. ArrayList<String> players = new ArrayList<String>();
    8.  
    9. @EventHandler
    10. public void OnJoin(PlayerJoinEvent event) {
    11. Player player = event.getPlayer();
    12. players.add(player.getName());
    13. }
    14.  
    15. //Magic Clock
    16. @EventHandler
    17. public void onPlayerClickEvent(PlayerInteractEvent event) {
    18.  
    19. Player player = event.getPlayer();
    20.  
    21. ItemStack enable = new ItemStack(Material.INK_SACK, 1, (short) 10);
    22. ItemMeta enableMeta = (ItemMeta) enable.getItemMeta();
    23. enableMeta.setDisplayName(ChatColor.GREEN + "Players >> Enabled");
    24. List<String> enableList = new ArrayList<String>();
    25. enableList.add(ChatColor.WHITE + "Right click to enable to disable player visability");
    26. enableMeta.setLore(enableList);
    27. enable.setItemMeta(enableMeta);
    28.  
    29. ItemStack disable = new ItemStack(Material.INK_SACK, 1, (short) 8);;
    30. ItemMeta disableMeta = (ItemMeta) disable.getItemMeta();
    31. disableMeta.setDisplayName(ChatColor.RED + "Players >> Disabled");
    32. List<String> disableList = new ArrayList<String>();
    33. disableList.add(ChatColor.WHITE + "Right click to enable player visability");
    34. disableMeta.setLore(disableList);
    35. disable.setItemMeta(disableMeta);
    36.  
    37. if(player.getItemInHand().getItemMeta().getDisplayName().equalsIgnoreCase(ChatColor.GREEN+"Player >> Enabled")){
    38. if(event.getAction().equals(Action.RIGHT_CLICK_BLOCK) || event.getAction().equals(Action.RIGHT_CLICK_AIR)){
    39. if(!this.players.contains(event.getPlayer().getName())){
    40. for(Player targetPlayers : Bukkit.getOnlinePlayers()){
    41. if(player.canSee(targetPlayers)){
    42. player.hidePlayer(targetPlayers);
    43. } else{
    44. event.setCancelled(true);
    45. }
    46. this.players.add(event.getPlayer().getName());
    47. }
    48. player.getInventory().removeItem(enable);
    49. player.setItemInHand(disable);
    50. player.sendMessage(ChatColor.DARK_GRAY + "[" + ChatColor.YELLOW + "Visibility" + ChatColor.DARK_GRAY + "] " + ChatColor.DARK_GRAY + "You have toggled player visibility: " + ChatColor.GREEN + "On");
    51. }
    52. }
    53. }
    54. if(player.getItemInHand().getItemMeta().getDisplayName().equalsIgnoreCase(ChatColor.RED+"Player >> Disable")){
    55. if(event.getAction().equals(Action.RIGHT_CLICK_BLOCK) || event.getAction().equals(Action.RIGHT_CLICK_AIR)){
    56. for(Player targetPlayers : Bukkit.getOnlinePlayers()){
    57. if(player.canSee(targetPlayers)){
    58. event.setCancelled(true);
    59. } else {
    60. player.showPlayer(targetPlayers);
    61. }
    62. this.players.remove(event.getPlayer().getName());
    63. }
    64. player.getInventory().removeItem(disable);
    65. player.setItemInHand(enable);
    66. player.sendMessage(ChatColor.DARK_GRAY + "[" + ChatColor.YELLOW + "Visibility" + ChatColor.DARK_GRAY + "] " + ChatColor.DARK_GRAY + "You have toggled player visibility: " + ChatColor.RED + "Off");
    67. }
    68. }
    69. }
    70. }
     
  29. Offline

    L33m4n123

    before I work through your code.. What is not working? Any Stacktrace? Can you please provide the full code?
     
  30. Offline

    infopaste

    There are no errors, It just does't work

    Here is my main class:

    Code:java
    1. package com.elyniacraft;
    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.ItemStack;
    16. import org.bukkit.inventory.meta.ItemMeta;
    17. import org.bukkit.plugin.java.JavaPlugin;
    18.  
    19. public class Main extends JavaPlugin implements Listener {
    20.  
    21. public void onEnable() {
    22. getServer().getPluginManager().registerEvents(this, this);
    23. }
    24.  
    25. ArrayList<String> players = new ArrayList<String>();
    26.  
    27. @EventHandler
    28. public void OnJoin(PlayerJoinEvent event) {
    29. Player player = event.getPlayer();
    30. players.add(player.getName());
    31. }
    32.  
    33. //Magic Clock
    34. @EventHandler
    35. public void onPlayerClickEvent(PlayerInteractEvent event) {
    36.  
    37. Player player = event.getPlayer();
    38.  
    39. ItemStack enable = new ItemStack(Material.INK_SACK, 1, (short) 10);
    40. ItemMeta enableMeta = (ItemMeta) enable.getItemMeta();
    41. enableMeta.setDisplayName(ChatColor.GREEN + "Players >> Enabled");
    42. List<String> enableList = new ArrayList<String>();
    43. enableList.add(ChatColor.WHITE + "Right click to enable to disable player visability");
    44. enableMeta.setLore(enableList);
    45. enable.setItemMeta(enableMeta);
    46.  
    47. ItemStack disable = new ItemStack(Material.INK_SACK, 1, (short) 8);;
    48. ItemMeta disableMeta = (ItemMeta) disable.getItemMeta();
    49. disableMeta.setDisplayName(ChatColor.RED + "Players >> Disabled");
    50. List<String> disableList = new ArrayList<String>();
    51. disableList.add(ChatColor.WHITE + "Right click to enable player visability");
    52. disableMeta.setLore(disableList);
    53. disable.setItemMeta(disableMeta);
    54.  
    55. if(player.getItemInHand().getItemMeta().getDisplayName().equalsIgnoreCase(ChatColor.GREEN+"Player >> Enabled")){
    56. if(event.getAction().equals(Action.RIGHT_CLICK_BLOCK) || event.getAction().equals(Action.RIGHT_CLICK_AIR)){
    57. if(!this.players.contains(event.getPlayer().getName())){
    58. for(Player targetPlayers : Bukkit.getOnlinePlayers()){
    59. if(player.canSee(targetPlayers)){
    60. player.hidePlayer(targetPlayers);
    61. } else{
    62. event.setCancelled(true);
    63. }
    64. this.players.add(player.getName());
    65. }
    66. player.getInventory().removeItem(enable);
    67. player.setItemInHand(disable);
    68. player.sendMessage(ChatColor.DARK_GRAY + "[" + ChatColor.YELLOW + "Visibility" + ChatColor.DARK_GRAY + "] " + ChatColor.DARK_GRAY + "You have toggled player visibility: " + ChatColor.GREEN + "On");
    69. }
    70. }
    71. }
    72. if(player.getItemInHand().getItemMeta().getDisplayName().equalsIgnoreCase(ChatColor.RED+"Player >> Disable")){
    73. if(event.getAction().equals(Action.RIGHT_CLICK_BLOCK) || event.getAction().equals(Action.RIGHT_CLICK_AIR)){
    74. for(Player targetPlayers : Bukkit.getOnlinePlayers()){
    75. if(player.canSee(targetPlayers)){
    76. event.setCancelled(true);
    77. } else {
    78. player.showPlayer(targetPlayers);
    79. }
    80. this.players.remove(player.getName());
    81. }
    82. player.getInventory().removeItem(disable);
    83. player.setItemInHand(enable);
    84. player.sendMessage(ChatColor.DARK_GRAY + "[" + ChatColor.YELLOW + "Visibility" + ChatColor.DARK_GRAY + "] " + ChatColor.DARK_GRAY + "You have toggled player visibility: " + ChatColor.RED + "Off");
    85. }
    86. }
    87. }
    88. }


    Here is my plugin.yml

    Code:
    name: EPlayerVisability
    main: com.elyniacraft.Main
    version: 1.7.4
    description: Custom Plugin for Elynia's Hub Server.
    website: elyniacraft.com
    author: Lightningrocks27
     
Thread Status:
Not open for further replies.

Share This Page