Util Add commnand tags: @a, @p, @e, @r, and ~

Discussion in 'Resources' started by Zombie_Striker, Jul 29, 2016.

  1. Description:
    This util allows plugins to use the '@' and '~' tags for their commands. It also supports additional data that is stored in any addition brackets. Using this, you will be able to accept and use base minecraft's command tags for your own commands.

    Code:
    The class is now too big for this post. The code can now be found here on my github page.

    Usage:
    Code:
    //The following are examples of what a player can use. All of the following are supported.
    String allEntities = "@a";
    String allEntitiesWithinRadius = "@a[r=10]";
    String closestPlayer = "@p";
    String closestCow = "@p[type=Cow]";
    String allEntitiesFartherThanRadius = "@a[rm=100]";
    String threeEntities = "@a[c=3]";
    String threePlayersWithin100Blocks = "@a[c=3,type=player,r=100]";
    
    //integers
    String relativeX = "~";
    String relativeYPlus5 = "~+5";
    String relativeZPlus5MultipliedBy2= "~+5*2";
    
    //================================
    
    //If you want all the entities possible, use this method
    Entity[] allNearbyEntities = CommandUtil.getTargets(Argument);
    
    //If you only want one the entity, use this method
    Entity allNearbyEntities = CommandUtil.getTarget(Argument);
    
    //If you want a coordinant relative to an entitiy, use the following
    int x = CommandUtils.getIntRelitive(Argument,"x",Entity);
    int y = CommandUtils.getIntRelitive(Argument,"y",Entity);
    int z = CommandUtils.getIntRelitive(Argument,"z",Entity);
    
    Example of an onCommand method using CommandUtils.
    Code:
    public boolean onCommand(CommandSender sender, Command cmd, String albel, String[] args){
    if(cmd.getName().equalsIgnorecase("killEntities")){
      if(args.length>0){
         Entity[] entities = CommandUtils.getTargets(args[0]);
          for(int i = 0; i < entities.length;i++){
           if(entities[i]==null)
            break;
           entities[i].setHealth(0);
         }
       }
    }
    if(cmd.getName().equalsIgnorecase("fetchEntity")){
      if(args.length>0){
        if(sender instanceof Player){
           CommandUtils.getTarget(args[0]).teleport((Player)sender);
        }
       }
    }
    }
    Changelog:
    7/31/2016:
    Added more specifications. Added invertedspecifications. Revised and reformatted code. Fixed other minor bugs.
    7/30/2016: Added support for minecarts and commandblocks. Added a new example. Fixed minor bugs.
    7/29/2016: Init post
     
    Last edited: Aug 31, 2016
  2. Shouldn't "entities" be @e? @a, @r and @p are for players only afaik.
     
  3. @FisheyLP
    @E accepts both players and other entities. However, the @p tag is meant for players unless it has a specified type. (I.e [type=Type]). More variables have been added and existing variable names have been fixed.

    @AlvinB
    Adding support for commandblocks later.
     
  4. Zombie? Usage? By the way nice work, as always.
     
  5. @ArsenArsen
    Thanks for point that out. It is now fixed.
     
  6. Since when can an Entity be equal to a Location? :D

    Relative*

    And please add support for
     
    Last edited: Jul 31, 2016
    Zombie_Striker likes this.
  7. @FisheyLP
    Thanks for pointing those things out.

    I was planning to add all the base objects anyways. The thing is, I have questions/notes about the those other suggestions.

    First, the rest are all new ideas (functions that are not in base minecraft). Although I will want to add them, this requires anyone using the commands to also understand that they exist, which means they may not be used.

    This seams like something that should have been in base minecraft. Will definitely add this.

    Minecraft does not necessarily support names with spaces. The only way to get spaces into a players name would be through bukkit. Also, this is for commands and these methods take in an argument which are separated by a space. This would means that two arguments would have to be put together, which means you would have to expect that there would be a space even though it is unlikely.

    I can do this by using spaces and underscores interchangeably, allowing for command blocks to accept underscores and check if an entity has either spaces or underscores in their name, but this feels more like a hack. I will have to do this for the "[name=]" tag, but I don't think I will be able to have this actually support spaces in a non-hacky way.

    Is this what you had in mind, or am I misunderstanding you?

    Do you mean to sort an entity/player by their world? That is what both this and base MC already do. They loop through all the entities/players in the world the command was sent and retrieve only those.
     
  8. I didn't really think about it when I posted it... Then with no spaces.

    if a world argument is included in the [], use only entities within that world. Else use the world the sender is in.
     
  9. @FisheyLP
    Updated code. Now it includes all those specifications.
     
  10. I know for a fact this is not true (base MC). I use some item frames scattered around multiple worlds and by doing /minecraft:kill @e[type=!Player] this kill thems. Also, unless this updated in 1.9, the selector is m, not gm.

    Please add:

    + Team
    + scoreboard values

    + Rename the class Selectors
     
    Last edited: Aug 21, 2016
  11. @TheEnderCrafter9
    Alright, I have fixed some of those things. I have added team support, and have renamed the 'gm' tag to 'm' (the reason I swapped selectors before was because a lot of others tags had 'm' at the end, and at the time, I did not know how to filter those out) As for the world issue, I have fixes this. If the world is loaded, the command will attempt to get all the entities in those worlds.
     
  12. @Zombie_Striker did you add the @p[team=!TEAM] tag? I get a null when doing so. Also, add it to the top list of selectors.
     
  13. @TheEnderCrafter9
    I have just updated the code. Before, I either forgot or did not fully implement the inverted team feature. The tag [team=!] has now been added. How can copy the new code from the github page.
     
  14. Offline

    PhantomUnicorns

    This is a suggestion and if there is anything wrong with it please alert me. But couldn't you make a function that would have the parameters of the CommandSender, Command and a Runnable and you would pretty much run the code in the runnable, adjusted to the Command (you could get the String label by doing command#getName())? Just something to that effect, it just seems like it would be less code.
     
  15. Offline

    iClipse

    Don't know if necroposting is a thing here, but I don't see @r in the code here. Does @r still work somehow? It's in the title.
     
  16. @iClipse
    First, necro-posting is allowed here. For these types of threads, you should necro since it brings the threads to the top of the page (meaning more members can see this thread).

    For @r, it is in the code. Go to the github page to line 143. So yes, it does still work.
     
    iClipse likes this.
  17. Offline

    elancha98

    I managed to complete this with your approach, but it required some bitcode manipulation. It seems like PlayerSelector.getPlayers() would return null unless it's called from a VanillaCommandWrapper instance. Here's the implementation: https://bukkit.org/index.php?threads/447540/

    Enviado desde mi Aquaris E4.5 mediante Tapatalk
     
  18. Offline

    xDash_Flipperx

    How would i edit commands for the PlayerCommandPreprocessEvent using this @a, @p, @e, and @r format?
     
  19. @xDash_Flipperx
    I do not know. This util is meant to just interpret the codes. What are you trying to edit?
     
  20. Offline

    PhantomUnicorns

    Could you do something like a runnable with an argument (An interface you create) then run it with arguments of @p, @e

    Nvm about this idea...
     
    Last edited: Apr 3, 2017

Share This Page