vanish command?

Discussion in 'Plugin Development' started by FreakyPear5, Mar 14, 2019.

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

    FreakyPear5

    what is going on I don't understand. why doesn't this code work???? please tell me. I am trying to make a Vanish command but whenever I run it in game it just returns with '/vanish'. pls help. thanks. code below.
    Code:
    
    package me.FreakyPear5.HGMCCore.Commands;
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    public class VanishCommand implements CommandExecutor {
    @Override
    @SuppressWarnings("deprecation")
    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    Player player = (Player) sender;
    if (sender instanceof Player) {
    if (player.hasPermission("hgmc.vanish")) {
    if (args.length == 0) {
    player.sendMessage(ChatColor.translateAlternateColorCodes('&', "&c&lHyperGames MC >> &aUsage: /vanish <on/off>"));
    }
    else {
    if (args.equals("on")) {
    player.performCommand("btlp hide on");
    Bukkit.broadcastMessage(ChatColor.translateAlternateColorCodes('&', "&e" + Bukkit.getPlayer("username") + " has left the game"));
    player.hidePlayer(player);
    }
    else {
    if (args.equals("off")) {
    player.performCommand("btlp hide off");
    Bukkit.broadcastMessage(ChatColor.translateAlternateColorCodes('&', "&e" + Bukkit.getPlayer("username") + " has joined the game"));
    player.showPlayer(player);
    }
    }
    }
    }
    else {
    player.sendMessage(ChatColor.translateAlternateColorCodes('&', "&c&lHyperGames MC >> &aYou do not have permission to perform this command!"));
    }
    }
    else {
    
    }
    return true;
    }
    }
     
    Last edited by a moderator: Mar 14, 2019
  2. Offline

    timtower Ninja on the waves Moderator

    @FreakyPear5 Did you register the class as Commandexecutor?
     
  3. Offline

    Shadow_tingBRO

    Ima explain this for you. (Lets say u started java coding bukkit). a boolean is true or false right, and its a public boolean onCommand... its a boolean so it has to return a boolean. EG: public boolean hi(){} <- that would get a error saying you have to return a value (true or false cuase its a booelan). In this case if oncommand returns false, your telling bukkit that the command is wrongly entered for what you want, if you return true it means you have entered the command how you want it.

    Back to this, you have returned true, which means that all the code you wrote inside onCommand, if it does not do what you want todo AT ALL, so this is the first thing you checked for which is if it is a player, if its not , well nothing will happend because the else has nothing inside it, but the second if statement, your checking the command, etc..

    What you need to do: Make the return true at the bottom return false, as this is what you want to return if nothing goes the right way in the if statemtns (not the command , not a player etc..). Then, each time you want to end the code eg if they have no permission, after you have said you dont have permission as a message or whatever, add a line saying return true, saying to bukkit that yes you have checked the command is /hgmc.vanish and other if statements.

    So, each time your code ends somewhere you want, which can be no permission, not a player, everything is correct and make the player vanish etc (in this case), add a return true. So yuor code should look like this:

    Code:
    package me.FreakyPear5.HGMCCore.Commands;
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    public class VanishCommand implements CommandExecutor {
    @Override
    @SuppressWarnings("deprecation")
    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    Player player = (Player) sender;
    if (sender instanceof Player) {
    if (player.hasPermission("hgmc.vanish")) {
    if (args.length == 0) {
    player.sendMessage(ChatColor.translateAlternateColorCodes('&', "&c&lHyperGames MC >> &aUsage: /vanish <on/off>"));
    return true;
    }
    else {
    if (args.equals("on")) {
    player.performCommand("btlp hide on");
    Bukkit.broadcastMessage(ChatColor.translateAlternateColorCodes('&', "&e" + Bukkit.getPlayer("username") + " has left the game"));
    player.hidePlayer(player);
    return true;
    }
    else {
    if (args.equals("off")) {
    player.performCommand("btlp hide off");
    Bukkit.broadcastMessage(ChatColor.translateAlternateColorCodes('&', "&e" + Bukkit.getPlayer("username") + " has joined the game"));
    player.showPlayer(player);
    return true;
    }
    }
    }
    }
    else {
    player.sendMessage(ChatColor.translateAlternateColorCodes('&', "&c&lHyperGames MC >> &aYou do not have permission to perform this command!"));
    return true;
    }
    }
    else {
    
    }
    return false;
    }
    }
    Sorry if my explanation is not good, Im not that good at explaining ask if your confused!

    Yh as timtower said, also in your onEnable() method, add this line: getServer().getPluginCommand("hgmc.vanish").setExecutor(new VanishCommand());

    This tells bukkit get the command and to set the code for it to the class you made!

    Also, the lines where you did if(args.equals(...)){}, you need to specifiy which argument (arg) you want to check, so if you wanted your command to do this: /vanish <on:eek:ff> , args number 0 would be the on / off. args number 1 would be lets say /vanish <on or off> [player], so the player would be args 1. you can have billions of args , but in java counting starts at 0, not 1 remember that
     
    Last edited: Mar 14, 2019
  4. Offline

    Shqep

    At
    Code:Java
    1. player.hidePlayer(player);

    Why did he hide the player from itself? Does it make you invisible to others?
    I’m just asking because I use a loop to hide a player from all others, except players with same rank or higher...
     
  5. Offline

    FreakyPear5

    @Shqep yeah it told me to fill it in and I didn't know how to do it so I just put player. Could you pls tell me how to do what you said? I want to make a permission so if they have the 'see' perm then they can see the vanished players

    @Shadow_tingBRO also, I wrote down the extra code that you told me to do and added the line that timtower suggested. it still returns with /vanish in chat
     
  6. Offline

    timtower Ninja on the waves Moderator

    @FreakyPear5 Please post your new code and where you register the executor.
     
  7. Offline

    Shqep

    Ehhh i would
    Code:Java
    1.  
    2. Player player = //something from ur code
    3. for(Player online : Bukkit.getOnlinePlayers() ) {
    4. if (!online.hasPermission(“see”)) online.hidePlayer(player);
    5. } //player is hidden from online
    6.  


    Any better way?
     
    Last edited: Mar 15, 2019
  8. Offline

    FreakyPear5

    @timtower

    Code:
    package me.FreakyPear5.HGMCCore.Commands;
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    public class VanishCommand implements CommandExecutor {
    @Override
    @SuppressWarnings("deprecation")
    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    Player player = (Player) sender;
    if (sender instanceof Player) {
    if (player.hasPermission("hgmc.vanish")) {
    if (args.length == 0) {
    player.sendMessage(ChatColor.translateAlternateColorCodes('&', "&c&lHyperGames MC >> &aUsage: /vanish <on/off>"));
    return true;
    }
    else {
    if (args[0].equals("on")) {
    player.performCommand("btlp hide on");
    Bukkit.broadcastMessage(ChatColor.translateAlternateColorCodes('&', "&e" + Bukkit.getPlayer("username") + " has left the game"));
    for (Player online : Bukkit.getOnlinePlayers()) {
    if (!online.hasPermission("hgmc.vanish.see")) online.hidePlayer(player);
    
    }
    return true;
    }
    else {
    if (args[0].equals("off")) {
    player.performCommand("btlp hide off");
    Bukkit.broadcastMessage(ChatColor.translateAlternateColorCodes('&', "&e" + Bukkit.getPlayer("username") + " has joined the game"));
    player.showPlayer(player);
    return true;
    }
    }
    }
    }
    else {
    player.sendMessage(ChatColor.translateAlternateColorCodes('&', "&c&lHyperGames MC >> &aYou do not have permission to perform this command!"));
    }
    }
    else {
    
    }
    return false;
    }
    }
    
    Also, this is in my onEnable() statement:
    this.getCommand("vanish").setExecutor((CommandExecutor)new VanishCommand());

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Mar 15, 2019
  9. Offline

    Shqep

    I think
    Code:Java
    1. player.showPlayer(other);

    is the same as hidePlayer. So idk, do you need another for loop to make sure that everyone can see the previously hidden player?
    Also, why are you casting a class implementing CommandExecutor to CommandExecutor? Redundant cast?
    Finally, any errors from the that version? If not, then solved? If yes, then what is the error?

    (If you can make the code look easier/cleaner, please do that lol my eyes exploded.)
    (Like proper spacing and stuff..)

    @FreakyPear5
     
  10. Offline

    MrDaniel

    @FreakyPear5,

    Let me translate this down to you.

    player.hidePlayer(player)

    The first player, is the player that you want to hide the player for. The second player is the player you want to hide for that player. What you want to do is hide that player from everyone, you can do that life this:

    Code:
     for (Bukkit.getOnlinePlayers : online) {
      online.hidePlayer(player);
    }
    
     
  11. Offline

    KarimAKL

    @MrDaniel You mean like this?
    Code:Java
    1. for (Player online : Bukkit.getOnlinePlayers()) {
    2. online.hidePlayer(player);
    3. }
     
  12. Offline

    MrDaniel

    @KarimAKL, yea, sorry for the mistake, didn't have my ide with me then and was lazy, but yea, that's how you do it
     
  13. Offline

    MrDaniel

  14. Offline

    FreakyPear5

    @MrDaniel Ah yes sorry I forgot to update the post, Shadow_tingBRO gave me a hand and now it's fixed :D
     
  15. Offline

    MrDaniel

Thread Status:
Not open for further replies.

Share This Page