Vanish Plugin

Discussion in 'Plugin Development' started by CeramicTitan, Nov 17, 2012.

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

    CeramicTitan

    Code:JAVA
    1. package me.ceramictitan.spy;
    2.  
    3. import java.util.HashSet;
    4. import java.util.Set;
    5.  
    6. import org.bukkit.Bukkit;
    7. import org.bukkit.Effect;
    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.inventory.ItemStack;
    15.  
    16. public class myPlayerInteractListener implements Listener{
    17.  
    18. public Set<String> vanished = new HashSet<String>();
    19. public spy plugin;
    20. private int id;
    21.  
    22. public myPlayerInteractListener(spy plugin){
    23. this.plugin = plugin;
    24. }
    25. public void setTaskId(int id) {
    26. this.id = id;
    27. }
    28. @EventHandler
    29. public void onPlayerInteract(PlayerInteractEvent event){
    30. final Player p = event.getPlayer();
    31. if(event.getAction() == Action.RIGHT_CLICK_AIR){
    32. if(p.getItemInHand().getType() == Material.REDSTONE){
    33. if(vanished.contains(p.getName())){
    34. p.sendMessage("Already Vanished");
    35. event.setCancelled(true);
    36. }else if(!vanished.contains(p.getName())){
    37. this.vanished.add(p.getName());
    38. p.hidePlayer(p);
    39. p.sendMessage("You are now hidden!");
    40. p.getWorld().playEffect(p.getLocation(), Effect.STEP_SOUND, Material.SNOW_BLOCK.getId());
    41. if(vanished.contains(p.getName())){
    42. id = Bukkit.getScheduler().scheduleAsyncRepeatingTask(plugin, new Runnable(){
    43.  
    44. @Override
    45. public void run() {
    46. if(p.getItemInHand().getType() == Material.REDSTONE){
    47. p.getItemInHand().setAmount(p.getItemInHand().getAmount() -1);
    48. if(p.getItemInHand().getType() == Material.REDSTONE){
    49. if(p.getItemInHand().getAmount() == 1){
    50. p.setItemInHand(new ItemStack(Material.AIR, 1));
    51. }
    52. if(p.getItemInHand().getType() != Material.REDSTONE && !p.getInventory().contains(Material.REDSTONE)){
    53. p.showPlayer(p);
    54. p.sendMessage("No longer hidden");
    55. vanished.remove(p);
    56. System.out.println("removed!");
    57. Bukkit.getServer().getScheduler().cancelTask(id);
    58. }
    59. }
    60. if(p.getItemInHand().getType() != Material.REDSTONE){
    61. p.showPlayer(p);
    62. p.sendMessage("No longer hidden");
    63. vanished.remove(p);
    64. System.out.println("removed!");
    65. Bukkit.getServer().getScheduler().cancelTask(id);
    66. }
    67. }
    68. }
    69. },20L, 20L);
    70. }
    71. }
    72.  
    73. }
    74. }
    75. }
    76. }
    77.  

    A few problems:
    1. I don't think the player disappears.
    2. If I player is vanished and they change their item in had it doesn't do anything.
    3. I don't think i'm cancelling the Async Repeating task properly
     
  2. Offline

    leiger

    1. I don't think the player disappears.

    You aren't hiding the player properly. You need to specify who to hide from. For example, if you wanted to hide yourself ('me') from another player ('anotherPlayer') you would do this:
    Code:
    anotherPlayer.hidePlayer(me);
    Therefore, change this line:
    Code:
    p.hidePlayer(p);
    To something like this:
    Code:
    for (Player player : server.getOnlinePlayers())
    {
        player.hidePlayer(p);
    }
    
    You would also have to listen for PlayerLoginEvent, to ensure that anyone logging in can't see players that are supposed to be invisible.

    2. If I player is vanished and they change their item in had it doesn't do anything.

    You would need an event listener for this as well. Whenever someone changes their held item, check what the item is and react accordingly.
     
    CeramicTitan likes this.
  3. Offline

    CeramicTitan

    What event listener?

    Also how would I hide players from newly logged in players with the PlayerLoginEvent?

    Like this:
    Code:JAVA
    1. @EventHandler
    2. public void onPlayerLogin(PlayerLoginEvent event){
    3. Player loginPlayer = event.getPlayer();
    4. if(vanished.contains(p)){
    5. p.hidePlayer(loginPlayer);?
    6. }
    7. }
    8.  
     
  4. Offline

    leiger

    I wasn't sure, so I've been searching since I posted that to find out.

    The PlayerItemHeldEvent looks like what you want: "Fired when a player changes their currently held item."

    When you iterate through players and make sure none of them can see you, you've done all online players.

    When someone new logs in (which activates a PlayerLoginEvent) you would need to run the hidePlayer() method on them too.
     
  5. Offline

    CeramicTitan

    did you see my code? would that be on the right track?
     
  6. Offline

    leiger

    It's on the right track, but won't work ;-) Remember what I said before:
    Code:
    anotherPlayer.hidePlayer(me);
    You've got that the wrong way around.

    This part is incorrect:
    Code:
    if(vanished.contains(p)) {
        p.hidePlayer(loginPlayer);?
    }
    
    Go through every Player listed in 'vanished', and then do:
    Code:
    loginPlayer.hidePlayer(p);
    ... where p is an instance of Player taken from the 'vanished' Set.
     
  7. Offline

    CeramicTitan

    hmm alright i understood all of that except for this bit:
    leiger

    It would be a for loop but i don't know what arguments the for loop would take?
     
  8. Offline

    leiger

    I'm trying not to do it all for you :)

    You have a vanished variable, which is a Set of Strings. Do you know how to iterate through it? [Hint: I iterated through the list of online players earlier...]

    For each String in vanished, get a Player object using that name. Then do something like this:
    Code:
    loginPlayer.hidePlayer(playerObjectYouJustCreated);
    Basically, when someone new logs into your server they can see everyone, including those people that they are not supposed to see (the people in vanished). What you are trying to do is to make all of the people in vanished not visible any more to this new player.
     
  9. Offline

    CeramicTitan

    Thank, I'm sorry i had no intention for you to do everthing

    Also would i do the same thing for PlayerItemHeldEvent because its out of the other method and p is an unknown variable
     
  10. Offline

    leiger


    PlayerItemHeldEvent should give you access to the player that performed the action, most likely with something like event.getPlayer(). Just use that to get a reference to the player:
    Code:
    Player p = event.getPlayer();
    Then ... either show or hide that player using similar code to what you did in the PlayerInteractEvent handler. You would either make them hidden to every other online player, or you'd make them visible again to every online player.
     
  11. Offline

    CeramicTitan

    Great thank you
     
  12. Offline

    leiger

    Everything's working now? :)
     
  13. Offline

    CeramicTitan

    Yeah except the fact the when the player is invisible, the invisible player doesn't take damage.
     
  14. Offline

    leiger

    Hmm. Are you doing anything with your code that might cause this?

    Posting the updated code that you are using now (since making all of those modifications) might help to find the cause.
     
  15. Offline

    CeramicTitan

    Alright. Well I'm not at home right now but as soon as I get home I will post it.

    Code:JAVA
    1. package me.ceramictitan.spy;
    2.  
    3. import java.util.HashSet;
    4. import java.util.Set;
    5.  
    6. import org.bukkit.Bukkit;
    7. import org.bukkit.Effect;
    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.entity.EntityDamageByEntityEvent;
    14. import org.bukkit.event.player.PlayerInteractEvent;
    15. import org.bukkit.event.player.PlayerItemHeldEvent;
    16. import org.bukkit.event.player.PlayerQuitEvent;
    17.  
    18. import org.bukkit.event.player.PlayerLoginEvent;
    19. import org.bukkit.inventory.ItemStack;
    20.  
    21. public class myPlayerInteractListener implements Listener{
    22.  
    23. public Set<String> vanished = new HashSet<String>();
    24. public spy plugin;
    25. private int id;
    26.  
    27. public myPlayerInteractListener(spy plugin){
    28. this.plugin = plugin;
    29. }
    30. @EventHandler
    31. public void onPlayerInteract(PlayerInteractEvent event){
    32. final Player p = event.getPlayer();
    33. if(event.getAction() == Action.RIGHT_CLICK_AIR){
    34. if(p.getItemInHand().getType() == Material.REDSTONE){
    35. if(vanished.contains(p.getName())){
    36. p.sendMessage("Already Vanished");
    37. event.setCancelled(true);
    38. }else if(!vanished.contains(p.getName())){
    39. this.vanished.add(p.getName());
    40. for (Player player : Bukkit.getServer().getOnlinePlayers())
    41. {
    42. player.hidePlayer(p);
    43. }
    44. p.sendMessage("You are now hidden!");
    45. p.getWorld().playEffect(p.getLocation(), Effect.STEP_SOUND, Material.SNOW_BLOCK.getId());
    46. if(vanished.contains(p.getName())){
    47. id = Bukkit.getScheduler().scheduleAsyncRepeatingTask(plugin, new Runnable(){
    48.  
    49. @Override
    50. public void run() {
    51. if(p.getItemInHand().getType() == Material.REDSTONE){
    52. p.getItemInHand().setAmount(p.getItemInHand().getAmount() -1);
    53. if(p.getItemInHand().getType() == Material.REDSTONE){
    54. if(p.getItemInHand().getAmount() == 1){
    55. p.setItemInHand(new ItemStack(Material.AIR, 1));
    56. }
    57. if(p.getItemInHand().getType() != Material.REDSTONE){
    58. for(Player player : Bukkit.getServer().getOnlinePlayers()){
    59. player.showPlayer(p);
    60. p.sendMessage("No longer hidden");
    61. if(vanished.contains(p.getName())){
    62. vanished.remove(p.getName());
    63. }
    64. System.out.println("removed!");
    65. stopTask();
    66. }
    67. }
    68. }
    69. }
    70. }
    71. },20L, 20L);
    72. }
    73. }
    74.  
    75. }
    76. }
    77. }
    78. @EventHandler
    79. public void onItemChange(PlayerItemHeldEvent event){
    80. if(event.getPlayer().getItemInHand().getType() == Material.REDSTONE){
    81. if(!vanished.contains(event.getPlayer().getName())){
    82. return;
    83. }
    84. for(Player player : Bukkit.getServer().getOnlinePlayers()){
    85. player.showPlayer(event.getPlayer());
    86. event.getPlayer().sendMessage("No longer hidden");
    87. if(vanished.contains(event.getPlayer().getName())){
    88. vanished.remove(event.getPlayer().getName());
    89. System.out.println("removed!");
    90. }
    91. stopTask();
    92. }
    93. }
    94. }
    95. @EventHandler
    96. public void onPlayerLogin(PlayerLoginEvent event){
    97. for(String playerName : vanished) {
    98. Player player = Bukkit.getPlayerExact(playerName);
    99. Player loginPlayer = event.getPlayer();
    100. if (player != null) {
    101. loginPlayer.hidePlayer(player);
    102. }
    103. }
    104.  
    105. }
    106. @EventHandler
    107. public void onDamage(EntityDamageByEntityEvent event){
    108. Player p = (Player)event.getEntity();
    109. if(p instanceof Player){
    110. if(vanished.contains(p.getName())){
    111. for(Player players : Bukkit.getServer().getOnlinePlayers()){
    112. players.showPlayer(p);
    113. }
    114. event.setCancelled(false);
    115. p.getWorld().playEffect(p.getLocation(), Effect.STEP_SOUND, Material.LAVA.getId());
    116. vanished.remove(p.getName());
    117. p.sendMessage("You have been Spotted!");
    118. stopTask();
    119.  
    120. }
    121. }
    122. }
    123. @EventHandler
    124. public void onPlayerQuit(PlayerQuitEvent event){
    125. if(vanished.contains(event.getPlayer().getName())){
    126. vanished.remove(event.getPlayer().getName());
    127. }
    128. }
    129. private void stopTask(){
    130. Bukkit.getServer().getScheduler().cancelTask(id);
    131. }
    132. }
    133.  
    134.  


    Here is the full class.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 29, 2016
  16. Offline

    leiger

    If you remove the onDamage(EntityDamageByEntityEvent event) method, e.g. comment it out, do you still have the problem with invincible players?

    Once you've isolated the cause of the problem, you can start looking for a solution.
     
  17. Offline

    CeramicTitan

    alright, will try within the next few days

    I think this is the problem ^

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 29, 2016
Thread Status:
Not open for further replies.

Share This Page