Solved Hide player plugin

Discussion in 'Plugin Development' started by PolarCraft, Oct 22, 2013.

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

    PolarCraft

    Okay so i am trying to make a command to hide the certain player. But when i type /hide it keeps saying you are now hidden.

    Main class:

    Code:java
    1. package net.bc.minecraft;
    2.  
    3. import org.bukkit.Bukkit;
    4. import org.bukkit.ChatColor;
    5. import org.bukkit.command.Command;
    6. import org.bukkit.command.CommandSender;
    7. import org.bukkit.entity.Player;
    8. import org.bukkit.plugin.java.JavaPlugin;
    9.  
    10. public class Hide extends JavaPlugin {
    11.  
    12. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
    13. Player player = (Player) sender;
    14. if(cmd.getName().equalsIgnoreCase("hide")){
    15. Player p = null;
    16. if (sender.hasPermission("bc.hide")){
    17. player.hidePlayer(player);
    18. for(Player ps : Bukkit.getOnlinePlayers()){
    19. p = ps;
    20. p.hidePlayer(player);
    21. }
    22. player.sendMessage(ChatColor.GRAY +"You are now hidden!");
    23. } else if(player.canSee(player)) {
    24. for(Player ps : Bukkit.getOnlinePlayers()){
    25. p = ps;
    26. p.showPlayer(player);
    27. }
    28. player.sendMessage(ChatColor.GRAY +"You are now unhidden!");
    29. }
    30.  
    31. }
    32. return false;
    33.  
    34. }
    35. }
     
  2. Offline

    Sweatyyyy

    PolarCraft
    Is: player.sendMessage(ChatColor.GRAY +"You are now hidden!"); inside the loop?
     
  3. Offline

    PolarCraft

  4. Offline

    Sweatyyyy

  5. Offline

    PolarCraft

    Sweatyyyy Do you know how to read my main file?? Or did you just skip over it?? It is not.. And when i put it in the loop it still says it.
     
  6. Offline

    Sweatyyyy

    PolarCraft
    No need to get mad :confused:, I'm only trying to help... At least I was trying -_- I'm not good at this stuff, just trying to learn
     
    jokie666 likes this.
  7. Offline

    PolarCraft

    Then why are trying to help people on plugin developing if you still learning??
     
    Sweatyyyy likes this.
  8. Offline

    Deleted user

    PolarCraft

    Hello,

    Here is what I can see is wrong:

    1) (This is unconfirmed, because I don't use commandexecutors in my main class) Don't you need to implement CommandExecutor?

    2) Check if the sender is a player before casting

    3) Just use the player in the for each loop. No need to do 'p = ps;'

    4) Why return false? It'll make Bukkit assume the command didn't go through

    5) In that statement where you check if the player doesn't have the permission, the else goes straight into un-hiding them. Perhaps a message saying they don't have permission?

    Do you mean that the player is spammed with the message "You are now hidden"? It shouldn't be doing that, from what I can tell (then again, who knows).

    Perhaps some other opinions?
     
  9. Offline

    PolarCraft

    One the return false statement is for the ending not to do with the actual command.
    Two i never do check if sender is a player. And my plugins work. All ready have commandexecutors...
     
  10. Offline

    Goblom

    line 17 you are trying to hide the player from them self
    Code:java
    1. player.hidePlayer(player);

    Line 15, 19 & 25 are not required its just cluttered code
    Code:java
    1. /* 15 */ Player p = null;
    2. /* 19 */ p = ps;
    3. /* 25 */ p = ps;
    Change line 20 & 26 to
    Code:java
    1. /* 20 */ps.hidePlayer(player);
    2. /* 26 */ ps.showPlayer(player);
    line 30 add
    Code:java
    1. return true;


    ------------------------------------------------------------------------------------------------------------------------

    That will fix your current code that you have posted. Which incidentally only hides/unhides the player that runs the command, it will not hide a player your specify.

    If you wish to hide a certain player that is not yourself (eg: /hide Goblom) you will need to add some args statements in there and check if Goblom is a player and if Goblom is online etc...
     
    zThana likes this.
  11. Offline

    GusGold

    It is doing it because your plugin currently has the structure of:
    Does player have permissions?
    Yes: Hide player to all online​
    No: Can the player see them self?​
    Yes: Show player to all online​
    You need to add another if after checking if they have permissions which checks if they can't see them self.
     
  12. Offline

    PolarCraft

    Goblom Now for the player.hidePlayer(player) what would i put instead?
     
  13. Offline

    GusGold

    You pretty much need to swap
    Code:java
    1. player.hidePlayer(player);
    2. for(Player ps : Bukkit.getOnlinePlayers()){
    3. p = ps;
    4. p.hidePlayer(player);
    5. }
    6. player.sendMessage(ChatColor.GRAY +"You are now hidden!");

    with
    Code:java
    1. if(player.canSee(player)) {
    2. for(Player ps : Bukkit.getOnlinePlayers()){
    3. p = ps;
    4. p.showPlayer(player);
    5. }
    6. player.sendMessage(ChatColor.GRAY +"You are now unhidden!");
     
  14. Offline

    Goblom

    PolarCraft remove it.. Its not required and you are just trying to hide the player from them self which shouldnt work anyways.
     
  15. Offline

    PolarCraft

    Goblom Still doesn't work.
    Current code:

    Code:java
    1. package net.bc.minecraft;
    2.  
    3. import org.bukkit.Bukkit;
    4. import org.bukkit.ChatColor;
    5. import org.bukkit.command.Command;
    6. import org.bukkit.command.CommandSender;
    7. import org.bukkit.entity.Player;
    8. import org.bukkit.plugin.java.JavaPlugin;
    9.  
    10. public class Hide extends JavaPlugin {
    11.  
    12. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
    13. Player player = (Player) sender;
    14. if(cmd.getName().equalsIgnoreCase("hide")){
    15. if (sender.hasPermission("bc.hide")){
    16. for(Player ps : Bukkit.getOnlinePlayers()){
    17. ps.hidePlayer(player);
    18. }
    19. player.sendMessage(ChatColor.GRAY +"You are now hidden!");
    20. } else if(player.canSee(player)) {
    21. for(Player ps : Bukkit.getOnlinePlayers()){
    22. ps.showPlayer(player);
    23. player.sendMessage(ChatColor.GRAY +"You are now unhidden!");
    24. }
    25. }
    26.  
    27. }
    28. return false;
    29.  
    30. }
    31. }
     
  16. Offline

    Goblom

    PolarCraft ok. How are you testing this plugin ? Do you have another account to see if its not working ?

    ------------------------------------------------------------------------------------------------------------------------

    Found another problem... line 20 (Player is checking if he can see himself..)
    Code:java
    1. } else if (player.canSee(player) {
    Ima re-write some of this plugin for you...

    Code:java
    1. private List<String> hiddenPlayers = new ArrayList<String>();
    2. //onCommand
    3.  
    4. if (label.equalsIgnoreCase("hide")) {
    5. if (sender instanceof Player) {
    6. Player player = (Player) sender;
    7. if (player.hasPermission("bc.hide")) {
    8. if (hiddenPlayers.contains(player.getName())) {
    9. hiddenPlayers.remove(player.getName());
    10. for (Player otherPlayer : Bukkit.getOnlinePlayers()) {
    11. if (!otherPlayer.canSee(player)) otherPlayer.showPlayer(player);
    12. }
    13. } else {
    14. for (Player otherPlayer : Bukkit.getOnlinePlayers()) {
    15. otherPlayer.hidePlayer(player);
    16. hiddenPlayers.add(player.getName());
    17. }
    18. }
    19. }
    20. return true;
    21. } else sender.sendMessage("Only players can use this");
    22. }
    23. return false;
     
    zThana likes this.
  17. Offline

    PolarCraft

    Goblom now it multiples by how many people are online... And does not make me invisible...

    [EDIT] I have friends join...
     
  18. Offline

    whitehooder

    Here..
    Code:java
    1. package net.bc.minecraft;
    2.  
    3. import org.bukkit.ChatColor;
    4. import org.bukkit.metadata.FixedMetadataValue;
    5. import org.bukkit.plugin.java.JavaPlugin;
    6. import org.bukkit.Bukkit;
    7. import org.bukkit.command.Command;
    8. import org.bukkit.command.CommandExecutor;
    9. import org.bukkit.command.CommandSender;
    10. import org.bukkit.entity.Player;
    11.  
    12. public class Hide extends JavaPlugin implements CommandExecutor {
    13.  
    14. @Override
    15. public void onEnable() {
    16. getCommand("hide").setExecutor(this);
    17. }
    18.  
    19. @Override
    20. public void onDisable() {
    21.  
    22. }
    23.  
    24. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
    25. Player player = (Player) sender;
    26. if(cmd.getName().equalsIgnoreCase("hide")){
    27. if (sender.hasPermission("bc.hide") && !player.hasMetadata("---HIDDEN---")){
    28. for(Player ps : Bukkit.getOnlinePlayers()){
    29. ps.hidePlayer(player);
    30. }
    31. player.setMetadata("---HIDDEN---", new FixedMetadataValue(this, null));
    32. player.sendMessage(ChatColor.GRAY +"You are now hidden!");
    33. return true;
    34. } else if (sender.hasPermission("bc.hide")) {
    35. for(Player ps : Bukkit.getOnlinePlayers()){
    36. ps.showPlayer(player);
    37. }
    38. player.removeMetadata("---HIDDEN---", this);
    39. player.sendMessage(ChatColor.GRAY +"You are now unhidden!");
    40. return true;
    41. }
    42. }
    43. return false;
    44. }
    45. }

    In case your plugin.yml is wrong too:
    Code:
    main: net.bc.minecraft.Hide
    name: Hide
    description: Toggle your visibility.
    author: PolarCraft
    version: 1.0
    commands:
        hide:
            usage: Syntax error! /hide is the proper syntax.
            description: Hides you
    permissions:
        bc.hide:
            default: op
            description: Allows you to hide yourself from other players.
     
  19. Offline

    PolarCraft

    whitehooder Following error.
    Code:
    15:28:59 [INFO] tripps41 issued server command: /hide
    15:28:59 [SEVERE] null
    org.bukkit.command.CommandException: Unhandled exception executing command 'hide
    ' in plugin BasicCommands v10.22.2013
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46)
            at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:19
    2)
            at org.bukkit.craftbukkit.v1_6_R3.CraftServer.dispatchCommand(CraftServe
    r.java:523)
            at net.minecraft.server.v1_6_R3.PlayerConnection.handleCommand(PlayerCon
    nection.java:959)
            at net.minecraft.server.v1_6_R3.PlayerConnection.chat(PlayerConnection.j
    ava:877)
            at net.minecraft.server.v1_6_R3.PlayerConnection.a(PlayerConnection.java
    :834)
            at net.minecraft.server.v1_6_R3.Packet3Chat.handle(SourceFile:49)
            at net.minecraft.server.v1_6_R3.NetworkManager.b(NetworkManager.java:296
    )
            at net.minecraft.server.v1_6_R3.PlayerConnection.e(PlayerConnection.java
    :116)
            at net.minecraft.server.v1_6_R3.ServerConnection.b(SourceFile:37)
            at net.minecraft.server.v1_6_R3.DedicatedServerConnection.b(SourceFile:3
    0)
            at net.minecraft.server.v1_6_R3.MinecraftServer.t(MinecraftServer.java:5
    92)
            at net.minecraft.server.v1_6_R3.DedicatedServer.t(DedicatedServer.java:2
    27)
            at net.minecraft.server.v1_6_R3.MinecraftServer.s(MinecraftServer.java:4
    88)
            at net.minecraft.server.v1_6_R3.MinecraftServer.run(MinecraftServer.java
    :421)
            at net.minecraft.server.v1_6_R3.ThreadServerApplication.run(SourceFile:5
    83)
    Caused by: java.lang.NullPointerException
            at org.bukkit.plugin.PluginBase.getName(PluginBase.java:29)
            at org.bukkit.plugin.PluginBase.hashCode(PluginBase.java:11)
            at java.util.WeakHashMap.hash(Unknown Source)
            at java.util.WeakHashMap.remove(Unknown Source)
            at org.bukkit.metadata.MetadataStoreBase.removeMetadata(MetadataStoreBas
    e.java:88)
            at org.bukkit.craftbukkit.v1_6_R3.entity.CraftPlayer.removeMetadata(Craf
    tPlayer.java:895)
            at net.bc.minecraft.Hide.onCommand(Hide.java:38)
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44)
            ... 15 more
    >
     
  20. Offline

    whitehooder

    Did you use the exact code I provided you? If not, do you have multiple classes?
     
  21. Offline

    PolarCraft

    Yes i did. It is the following lines:
    player.setMetadata("---HIDDEN---", new FixedMetadataValue(this, null));

    and:
    player.removeMetadata("---HIDDEN---", this);

    And yes i have multiple classes.
     
  22. Offline

    whitehooder

    Try with this jar file.

    I suspect you don't have the permission, cause nothing is wrong with the code.
     
  23. Offline

    PolarCraft

    whitehooder i do have permission nodes.

    [EDIT] Hmm. When i try yours it works... But when i do mine it does not work. I can not use the on enable or onDisable due to the fact i have a main class using that. But i have the commandexecutor on there.
     
  24. Offline

    PolarCraft

  25. Offline

    negative_codezZ

    Alright. Look, if you do return false; it is not going to return anything. I never use return false in any of my plugins. Change all return false's to return true;
     
  26. Offline

    PolarCraft

    kk. I will test it now.
     
  27. Offline

    negative_codezZ

    Decided to recode it all from scratch.

    Code:java
    1. package me.negative_codezZ.playerhider;
    2.  
    3. import java.util.ArrayList;
    4.  
    5. import org.bukkit.Bukkit;
    6. import org.bukkit.ChatColor;
    7. import org.bukkit.command.Command;
    8. import org.bukkit.command.CommandSender;
    9. import org.bukkit.entity.Player;
    10. import org.bukkit.plugin.java.JavaPlugin;
    11.  
    12. public class Main extends JavaPlugin {
    13.  
    14. private ArrayList<String> HiddenPlayers = new ArrayList<String>();
    15.  
    16. public boolean onCommand(CommandSender sender, Command cmd, String commandLable, String[] args) {
    17.  
    18. if(!(sender instanceof Player)) {
    19. sender.sendMessage(ChatColor.RED +"Silly console... You can't hide!");
    20. return true;
    21. }
    22.  
    23. Player p = (Player) sender;
    24.  
    25. if(cmd.getName().equalsIgnoreCase("hide")) {
    26. if(p.hasPermission("bc.hide")) {
    27. if(HiddenPlayers.contains(p.getName())) {
    28. HiddenPlayers.remove(p.getName());
    29. for (Player pls : Bukkit.getOnlinePlayers()) {
    30. if (!pls.canSee(p)) {
    31. pls.showPlayer(p);
    32. return true;
    33. }
    34. return true;
    35. }
    36. return true;
    37. }
    38. if(!(HiddenPlayers.contains(p.getName()))) {
    39. HiddenPlayers.add(p.getName());
    40. for (Player pls : Bukkit.getOnlinePlayers()) {
    41. pls.hidePlayer(p);
    42. return true;
    43. }
    44. return true;
    45. }
    46. } else {
    47. p.sendMessage(ChatColor.RED +"You do not have access to that command!");
    48. return true;
    49. }
    50. }
    51. return true;
    52. }
    53.  
    54. }
    55.  


    If what I told you before doesn't work, try this.
     
  28. Offline

    PolarCraft

  29. Offline

    negative_codezZ

    That's odd... I don't know what to tell you then... Sorry!
     
  30. Offline

    xTrollxDudex

    zombiekiller753 whitehooder
    Omg you guys. I don't mean to be offensive but for zombie killer, number one. You inherit onCommand from JavaPlugin so the main class does not need to be a command executer in order to execute the command.
     
    whitehooder likes this.
Thread Status:
Not open for further replies.

Share This Page