Newbie needs a hand :) Need to let a player send a command to console.

Discussion in 'Plugin Development' started by Flenix, Oct 20, 2011.

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

    Flenix

    Hey guys,
    I'm working on a plugin and need to do the following:
    Player types a command, then the server checks if they have the permission, then a DIFFERENT command is sent to the console. That console command needs some information added: The players name, and a name from the config file.

    For example, the player types:
    /plugin command
    They have the permission of:
    plugin.use.command

    And the config file has this:
    Code:
    plugin:
        command:
            name: group1
    I then want the console to send an actual PEX command, like this:
    pex user [player name] group set [group1, from the config]


    Also, I want the admins to be able to define their own "command" and group, so they can add as many groups as they like. The idea is then, players in-game can switch between specified groups but not jump to any group at all, and they have a nice easy command for it.

    I'm a noob but trying to learn, so any help is much appreciated! Ideally, just point me in the right direction so I still learn stuff (I've looked through a few tutorials, none seem to cover the sub commands or letting users add their own stuff to a YAML config.
    If its easier just tell me what to do, I still learn a bit from that.


    Thanks :D
     
  2. Offline

    Jogy34

  3. Offline

    Zaros

    Well to check to see if they have permissions you could do something like this(pseudocode):

    Code:
    public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
      if cmd.equals.ignoreCase("plugin") {
        String perm = args[0]
        if player.hasPermission(perm) {
          write.toconfigstuff;
          return player message;
          return true;
        }
      }
    }
    As for writing to a config, there are threads all over the place about it. Just set the argument you want to a variable and write it to the config.
     
  4. Offline

    Flenix

    And how about sending the command to the cursor and inputting the various data into that command? Thats the main bit I'm stuck with :p
     
  5. Offline

    Father Of Time

    I mean this with all due respect... This make absolutely no sense. Do you actually need information from the cursor, or was that just the wrong use of terminology? If you need to get what the cursor is pointing at the player class has a function that returns the object that the players is focused on (cursor is pointing at).

    As far as inputing data... if you mean arguements to the command ( /command <arg1> <arg2> ) then Zaros's example above demonstrates that:

    Code:
        String perm = args[0]
    When you enter a command anything after /command is placed in an array of strings knows as arguments ( args[] ). So if a player were to enter /command Bob fly then you could access those arguements with:

    Code:
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
        if cmd.equals.ignoreCase("plugin")
        {
            String arg1 = args[0]; // will equal "bob"
            String arg2 = args[1]; // will equal "fly"
        }
    I hope this helps, good luck with your project! :D
     
  6. Offline

    Flenix

    No that was a total typo. I meant Server... no idea why I put cursor.
     
  7. server.dispatchCommand(server.getConsoleSender(), (your command here))
    is what you are looking for
     
  8. Offline

    thehutch

    May I ask why you used pseudocode which is wrong anyway? there isnt a "=" in pseudocode its an arrow going to the left :) anyway just write in java thank you :D
     
  9. Offline

    Father Of Time

    This is a bit of a rude statement... I use pseudo code all the time because I don't always have access to programming software so I have to rely on writing code from memory, and frankly example code that doesn’t execute “As Is” is way more beneficial to beginning programmers than no code at all…

    Also, I have no idea where you get:
    There is no standard for pseudo code, its just that… pseudo. The point of pseudo code is to get concepts out of the non physical world of your mind to a physical usable medium without the need for exact syntax. Pseudo code ignores syntax to easily convey the concept in discussion…

    I think it’s pretty inappropriate for you to instruct other people how to offer advice. I think you should worry more about offering constructive feedback to the original poster than critiquing other plug-in designers post…
     
  10. Offline

    thehutch

    Ok ok no need to just shoot me on the spot jeezus :p well its only I have just started learning pseudocode and on my examples they were arrows so sorry for saying something I believed in :) anyway I was being constructive in a way because IMO it is much more beneficial to write in Java since thats what everyone is using especially to newcomers who dont know Java very well so when you type it like they would just copy and paste and expect to work. Anyway im not offended nor should he and my comment wasnt intended on being rude anyway just pointing something out.
     
  11. Offline

    Father Of Time

    I speak honestly and openly, that’s all… It’s nothing personal I just took offense to your comment to the other poster…

    That’s fine, I’m not faulting you for not being completely informed, but I will fault you for stating assumptions as fact then correcting others on false assumptions. Make sure you either know what you are talking about or state clearly “I don’t know for sure, so you may want to clarify on this”, or something similar. It’s completely fine to not know something, after all who knows everything; but don’t misinform others to make it look like you know what you are talking about, that’s doesn’t help anyone.

    As you stated “In your opinion”, but not everyone shares your opinion. Just because psudo code isn’t useful to you doesn’t mean that the other community members here at Bukkit forums find it just as useless. If it doesn’t help you do a search and find another thread that does, but don’t complain about it because your understanding of java isn’t developed enough to convert non functional code to functional.

    Besides, the goal of this plug-in development thread is to teach people to fish, not give them one. If you just provide people the code to cut and paste they will never learn and keep posting the same problems over and over. Instead its more important to teach the person how to fix the errors themselves so that they can become an independent programmer and not rely on outside assistance.

    I’m glad you’re not offended; the point of my post was not to insult, but to inform. I also wasn’t trying to speak for Zaros, I was speaking on my own behalf because I personally found your comment insulting. I’m not trying to act like it was world ending, just simply pointing out that it isn’t good practice to tell other people what to do, especially when you are only learning yourself…

    Just please take my post as constructive criticism and not a personal attack, because doing anything else makes the whole discussion a moot point.
     
    Bone008 likes this.
  12. Offline

    Flenix

    Thanks :) What would I put in there to get information from the player and the config though?
    I'm actually sending a PEX command to the server, so I need the players name who sent the other command, and also need to read a result in the config... the bits in red are what are read (see what I did there? :D)
    pex user [name] group set group

    Thanks :)
    (EDIT: at the guys discussing pseudocode, I personally find it useful right now, because I still have to do a bit of work and don't just get it handed to me on a silver platter. I learn by doing stuff, so if I have to write the code from a rough example that helps me :))
     
  13. Offline

    Father Of Time

    Thank you! this is exactly what I meant by "teach a man to fish". Now I will stop derailing your post, please accept my apology for that.
     
  14. Offline

    Flenix

    No worries :) You helped with arguments earlier, just wondering if you could clarify one last thing. Some plugins read the arguments from the YAML config file, thats what I'm trying to do. How would I reference it to read the config?
    (Effectively, its reading what the admin put into the config as part of the command... because its a group setting plugin, admins need to add groups into the config and this plugin would then read them)
     
  15. Offline

    Father Of Time

    I’m sorry, unfortunately I wasn’t even aware that arguments could be declares inside your plugin.yml. I will do some reading around the forums to see if I can find any examples of handling arguments in this fashion, but at this point I’d only be guessing.

    If I do manage to unearth anything I will make sure to report back, either way good luck with your search!
     
  16. Offline

    Flenix

    Thanks :)
    Again, PermissionsEX is a good example... when setting a group you obviously have to specify which group you are setting to, so it must read that from the permissions.yml file - the groups aren't coded into the plugin itself. Maybe I'm just phrasing it wrongly and I'm trying to do something totally different? :p
     
  17. Offline

    thehutch

    Ok well after reading your essay :p I understand your point but coming from my view when I asked for help I asked for the code but then wasnt an idiot and just used it but then went through it to understand it then applied logic and common sense when using it again. Thats how I learn personally but yeah thank you very much for enlightening my day :)
     
    Father Of Time likes this.
  18. Offline

    Father Of Time


    Ohh, I think I'm beginning to pick up what you are laying down! So let me see if I have this right?

    Say you have a plugin that has the command /region, and with this region command you wanted to change a variable related to a specific user group.

    /region VARIABLE USERGROUP NEWVALUE

    So if I understand you correctly you are trying to take the 2nd argument (USERGROUP) and locate the data that is associated with the usergroup that the player entered?

    So if you did /region canfly elves true

    It would enable the flying ability for all people within the elves group?

    Am I in the ballpark or just picking up trash in the parking lot? :D
     
  19. Offline

    Flenix

    Thats pretty much right :) I guess it'd probably help a lot if I actually explain my end goal:

    I'm trying to make a plugin which lets players switch PEX groups on their own. It limits them to only specific groups, based on permissions (one permission per potential group) and charges them a cost to switch. The config file specifies how much to charge, depending on what group they're switching to.
    To do it, they type /command group - that then checks they have the permission, charges their iconomy balance, and sends a PEX command to the server which switches their group :) The group can be read either from PEX itself, or the config file of this plugin, but from what I've read reading from the plugins own config is MUCH easier, so I'll just get the admins to specify the actual group in the config. That way, they can even have the command in-game specify a different name than the group's name. (Sometimes they're messy.. I know mine are)
     
  20. Offline

    Father Of Time

    I agree with you, I learn in a very similar way to you. It's just I've belonged to enough programming communities to know that most people aren't like us and will take the easy road of simply cut/paste and move on. I've found that if you make people work a little for their answers it gives them a greater sense of accomplishment, and makes it less likely that the same issues will give them a problem in the future.

    Regardless, thank you for being level headed and open to suggestions, its a great quality that I respect.

    Okay, now things are coming together for me. :p I had no idea that PEX was PermissionsEX, that is what was throwing me off. Now that I know what you are trying to do a little better...

    Try looking here:
    https://github.com/t3hk0d3/PermissionsEx/tree/master/src/main/java/ru/tehkode/permissions

    That is the source code for permissionsEX, here you should be able to locate the functions that Add/Remove players from groups. I've never used PEX, but I'm assuming it has the ability for admins to add/remove players to/from groups via command then you should be able to tap into the same functions with your plug-in.

    I would just use the config.yml tutorial on how to save/load basic variables

    http://wiki.bukkit.org/HUGE_Plugin_Tutorial#Plugin_Configuration.2FSettings

    then use those variables to control which groups players are permitted to join and so forth, then once you know which they can and can't be added to tap into the PEX API to utilize its functions to do the actual adding/removing.

    I'm sorry that I am only providing "suggestions" and not actually code, but I am currently at work and don't have the time to dig into this any further at the moment. Hopefully you will be able to find what you need from the provided resources.

    Good luck with your project, let us know if you hit any more snags!

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 20, 2016
    thehutch likes this.
  21. Offline

    Flenix

    Just as a quick review, here is what I came up with for a very basic version. All this (should) do, is when someone types /buyclass, it changes their PEX group to farmer, regardless of what permissions they have.

    Its sort of maccled together with various things I've learnt from looking around.. just wondered if someone could point blatant errors and stuff I missed? I will change it to my above specs eventually, this is just to make sure its sending the command to the server right.

    On a related note, "sender" is showing as an error in Eclipse, with:
    "Duplicate local variable sender" - I guess that means sender is also somewhere else..?

    Code:
    public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] ags){
            if(cmd.getName().equalsIgnoreCase("/buyclass")){ //If the player typed /buyclass, do this
                String perm = args[0]
                    ConsoleCommandSender sender = new ConsoleCommandSender(server)
                {
                    @Override
                    public void sendMessage(String message)
                    {
                        System.out.println("You are now a farmer!");
                    }
                return server.dispatchCommand(server, "pex user" + sender + "group set Farmer");
     
  22. You are NOT supposed to create your own CommandSender (and even less creating your own ConsoleCommandSender sub-class) when sending a command as the console. Use the method I posted above (server.getConsoleSender()) to provide a sender.
    The error is showing because you are trying to declare another local variable called "sender" when it is also the sender of the original command declared in the "onCommand" head.

    I've no idea why you override sendMessage in your ConsoleCommandSender (which you shouldn't have as stated above) and then print a message to the console insidde of it ...
    Are you just trying to inform the player with a message? That is waaay easier:
    Code:
    sender.sendMessage("my message");
    (outside of all that custom class thingy)

    also cmd.getName() contains the command WITHOUT the leading /, so unless your command works like "//buyclass" that check at the top is also wrong.
     
  23. Offline

    Father Of Time

    I'm not big on watching people struggle... makes me sad... and honestly some of the advice offered so far is less than clear... sooo, I decided to just throw this together, it should work in it's current state, hopefully it will give you an idea of how to do the basic task of getting a command with arguments to work.

    Code:
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args)
        {
            Player player = (Player)sender;
            if(cmd.getName().equalsIgnoreCase("test"))
            {
                if(args.length == 1) // was only a single argument passed?
                {
                    String arg1 = args[0];
                    player.sendMessage("You have used the /test command with the argument: " + arg1 );
                }
    
                else  // more or less than one argument was passed, this command needs 1 and 1 only
                player.sendMessage("You have entered invalid arguments.");
            }
        }
    What this will do is create a command

    this command expects 1 argument to be passed, so if you try any of the following:

    it will just return the following error:

    But if you do the correct amount of arguments:

    then the player using the command should get the following message sent to them:

    This is a quick overview of command functionality. Let me know if I can clarify on this any further.

    Good luck! :D
     
  24. Offline

    Flenix


    I tried what you suggested earlier and it had just given me a load of errors. As I said, what I had currently was just put together from other threads I'd read through, so you can't exactly blame me for other peoples mistakes.

    @Father Of Time - Thanks! I'll have a mess around with that :)
     
  25. Offline

    thehutch

    No offence but the thing after this line in what you said is kinda wrong because where you put <args2> is actually <agrs1> and the first one is <args0> Constructive criticism (might be spelt wrong) :)
     
  26. I don't blame you for anything, but it still won't make the code any better if it is from other people ;)
    And just because it gives you errors doesn't mean that you gotta discard the whole concept, then it is just applied incorrectly.

    First of all, you need to somehow get a real value for "server", it isn't just there without doing anything. The easiest way to get the server is through Bukkit.getServer(). I thought you'd already know what to do with methods of the server, so I just simplified my instruction to server.dispatchCommand(...).
    Code:
    Bukkit.getServer().dispatchCommand(Bukkit.getServer().getConsoleSender(), "pex command foobar stuff");
    As for the rest of your method: Read tutorials about commands and other basic stuff.
     
  27. Offline

    Father Of Time

    Actually no, that is incorrect. It would be argument one and argument two, they just reside in the 0 and 1 index position of the String array. There is no such thing as argument zero, that would mean the command was argumentless. You are confusing the count of the arguments with arraylist indexes.
     
  28. Offline

    thehutch

    Stop correcting me ;'( and yes I probably just wrote it down without thinking :p args[0] would be argument 1
     
  29. Offline

    Zaros

    Just because it's in a example doesn't make it write or good practice. Here is an actual sample from my programming class(college).

    No, you were acting on a belief that you were right and I was wrong, with little to no backing. Also, I put (pseudocode) before the code so any decent programmer would know. Even if they did copy and paste it, the compiler would stop them.

    I'm not offended, but I find it a little irritating that you jump to conclusions without proper backing. If you were going to say that '= is not in pseudo code', at least post a link or document that proves me otherwise and leaves us all knowing better.

    Also, instead of making a post criticizing me, why not just post the correct information?
     
  30. Offline

    thehutch

    Ok well i sorry I wasted my time fighting over a pointless argument and yes now I am offended :( hope your happy
     
Thread Status:
Not open for further replies.

Share This Page