Solved Player Visibility plugin

Discussion in 'Plugin Development' started by XxTimeSlinkyxX, May 7, 2018.

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

    XxTimeSlinkyxX

    Hi there, so I am trying to make my own Player Visibility Plugin, I have looked at some plugins and they're not really what I want so I have tried to make my own.

    I have no experience making plugins, I am very new and I am just gathering resources to learn how to make plugins and use java

    However, here is what I have done with inspiration from other plugins, and using my basic knowledge.

    Code:
    package me.timeslinky.playerhide;
    import org.bukkit.Bukkit;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.java.JavaPlugin;
    public class Main extends JavaPlugin{
       
        @Override
        public void onEnable() {
            getLogger().info("PlayerHide Enabled");
        }
       
        @Override
        public void onDisable() {
            getLogger().info("PlayerHide Disabled");
        }
       
       
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if (!(sender instanceof Player)) {
           
            Player player = (Player) sender;
            if (cmd.getName().equalsIgnoreCase("playerhide")) {
                        if (player.isOp()) {
                            for(Player otherPlayers : Bukkit.getOnlinePlayers()) {
                                if(player.canSee(otherPlayers)) {
                                    player.hidePlayer(otherPlayers);
                                    }
                            }
                        }
                        else {
                            player.sendMessage("noPerm");
                        }       
            } //end if
           
           
            else if (cmd.getName().equalsIgnoreCase("playershow")) {
                if (player.isOp()) {
                    for(Player otherPlayers : Bukkit.getOnlinePlayers()) {
                        if(player.canSee(otherPlayers)) {
                            player.hidePlayer(otherPlayers);
                        }
                    }
                }
            }
           
           
            else if (cmd.getName().equalsIgnoreCase("testph")) {
                player.sendMessage("Success!");
            }
           
           
           
           
           
           
           
           
        //end main if   
        }
            return false;   
       
        //end public boolean   
        }
       
       
       
    //end main class
    }
    
    Now my problem is, whenever I try to use the command, it gives me unknown command? Im not sure what im doing wrong?...

    (I made sure to mention im very new because last time I asked for help, people gave me responses that I didn't understand and got a little flustered for me wasting their time... so ya, I made sure to mention it this time haha)

    Any help is appreciated! Thank you!
     
  2. Online

    timtower Moderator Moderator

  3. Offline

    XxTimeSlinkyxX

    Oh do I have to put any command I want to use in the plugin.YML in the commands section?
     
  4. Make sure your commands are registered in your plugin.yml, and in your onEnable put this:
    getCommand("cmdname").setExecutor(this);

    Sent from my SM-G903F using Tapatalk
     
  5. Offline

    XxTimeSlinkyxX

    What will that line of code do? I’m not familiar with it?
     
  6. Yep. Once/if you get into advanced stuff, I like to use a more complex way that doesn't require a plugin.yml, and is much more organised, modular and nicer. If you wanna check it out, Google "bukkit register command without plugin.yml" and check out one of the spigot threads. Once again, not recommended without more advanced knowledge on Reflection, Inheritance, and other core java knowledge.

    Sent from my SM-G903F using Tapatalk

    sorry, was going to explain it in detail but got distracted.
    getCommand is a method inherited from your parent class JavaPlugin (if you're confused about that, don't worry, just imagine it as a gene passed down from your parent), it gets a Command object from the all command storage that matches your String of text you passed.
    setExecutor is a method of the Command object you got from getCommand. It sets the command's handler (the code that processes the user's input), which is your onCommand method. Hope this helps you out :)

    Sent from my SM-G903F using Tapatalk

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

    XxTimeSlinkyxX

    Thank you for the explanation, though I think im going to go with the plugin.yml for now haha, unless im mistaken in thinking its an either or thing and I have to put getCommand("cmdname").setExecutor(this); in my onEnable.

    Now though, Progress has been made. Here is my plugin.yml file
    Code:
    name: PlayerHide
    main: me.timeslinky.playerhide.Main
    version: 1.5
    commands:
       playerhide:
          description: Hides players
          usage: /playerhide
       playershow:
          description: Shows hidden players
          usage: /playershow
       testph:
          description: Tests to see if the plugin is running
          usage: /testph
    
    I now can type in the command in chat and it doesn't say unknown. However, It just repeats back the command to me? For example I type "/playerhide" then the chat says "/playerhide".
     
  8. Return true instead of false. Returning false assumes that the user typed something wrong, so it shows the usage.

    Sent from my SM-G903F using Tapatalk
     
  9. Online

    timtower Moderator Moderator

    That is not needed for the main class though, the plugin is the default executor, only needed when you move away from the JavaPlugin and make a separate CommandExecutor.
     
  10. Oh, really? My bad, never knew that.

    Sent from my SM-G903F using Tapatalk
     
  11. Offline

    XxTimeSlinkyxX

    Okay, so I set the return statement to true and I executed the command in chat. Nothing happened? Im not sure what im doing wrong now. I thought player.hidePlayer(otherPlayers); would have worked? My if else isnt working I think, Im not getting any message. I have the else saying no perm and the if saying success!. Neither of them show up. Although it could be the if player.isOp? Anyways here's the code.

    Code:
    package me.timeslinky.playerhide;
    import org.bukkit.Bukkit;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.java.JavaPlugin;
    public class Main extends JavaPlugin{
       
        @Override
        public void onEnable() {
            getLogger().info("PlayerHide Enabled");
        }
       
        @Override
        public void onDisable() {
            getLogger().info("PlayerHide Disabled");
        }
       
       
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if (!(sender instanceof Player)) {
           
            Player player = (Player) sender;
            if (cmd.getName().equalsIgnoreCase("playerhide")) {
                        if (player.isOp()) {
                            for(Player otherPlayers : Bukkit.getOnlinePlayers()) {
                                    player.hidePlayer(otherPlayers);
                                    player.sendMessage("Success!");
                            }//end for
                        }//end if
                        else {
                            player.sendMessage("noPerm");
                        }       
            } //end if
           
           
            else if (cmd.getName().equalsIgnoreCase("playershow")) {
                if (player.isOp()) {
                    for(Player otherPlayers : Bukkit.getOnlinePlayers()) {
                            player.hidePlayer(otherPlayers);
                            player.sendMessage("Success!");
                    }//end for
                }//end if
            }//end else if
           
           
            else if (cmd.getName().equalsIgnoreCase("testph")) {
                player.sendMessage("Success!");
            }
           
           
           
           
           
           
           
           
        //end main if   
        }
            return true;   
       
        //end public boolean   
        }
       
       
       
    //end main class
    }
    
    
     
  12. Offline

    XxTimeSlinkyxX

    I’m not sure where you’re mentioning to? I thought it have in the public Boolean, if sender is a player, player is sender, if command “playerhide”, if player is OP, select player (otherplayers or online players), hide otherplayers? I’m not sure where I went wrong?
     
  13. @XxTimeSlinkyxX
    Your code will only execute if
    !(sender instanceof Player)
    Which it never will be true. You have to remove the ! And just check if sender instanceof Player
     
  14. Offline

    XxTimeSlinkyxX

Thread Status:
Not open for further replies.

Share This Page