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

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

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

    Zombie_Striker

    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. Offline

    Zombie_Striker

    @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. Offline

    ArsenArsen

    Zombie? Usage? By the way nice work, as always.
     
  5. Offline

    Zombie_Striker

    @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. Offline

    Zombie_Striker

    @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. Offline

    Zombie_Striker

    @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. Offline

    Zombie_Striker

    @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. Offline

    Zombie_Striker

    @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. Offline

    Zombie_Striker

    @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. 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. Offline

    Zombie_Striker

    @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
  21. Offline

    Dandunse

    this is not working... array index bound exeption
     
  22. Online

    timtower Administrator Administrator Moderator

    Could you post your used code?
     
  23. Offline

    Dandunse

    Oncommand Code:
    Code:
    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if(cmd.getName().equals("tp5"))
            {
                new Tp5().c(CommandUtils.getTargets(sender,args[0]),args);
                return new Tp5().a(args);
            }
            return true;
        }
    tp5 class code:
    Code:
    package tam3Main;
    
    import org.bukkit.Location;
    import org.bukkit.entity.Entity;
    import org.bukkit.util.Vector;
    
    public class Tp5 {
    
        boolean a = true;
        double b = 0;
        double[] c = new double[9];
    
        public boolean a(String A) {
            if (isd(A))
            {
                return true;
            }
       
            if (A.startsWith("~") || A.startsWith("x") || A.startsWith("X") || A.startsWith("y") || A.startsWith("Y")
                    || A.startsWith("z") || A.startsWith("Z")) {
                if(A.length()>=2){
                    if (isd(A.substring(1))) {
                        return true;
                    }
                }
                else
                {
                    return true;
                }
            }
            return false;
        }
    
        public boolean a(String[] A) {
            for (int i = 1; i <= A.length-1; i++) {
                if (!a(A[i]) || A.length != 9) {
                    a = false;
                }
            }
            return a;
        }
    
        public void c(Entity[] A, String[] A1) {
            for(Entity A2 : A)
            {
                c(A2,A1);
            }
        }
    
        public void c(Entity A, String[] A1) {
            if (A1.length == 9 && a(A1)) {
                for (int i = 1; i <= 8; i++) {
                    if (A1[i].startsWith("~")) {
                        if (i == 1){
                            b = A.getLocation().getX();
                        }
                        if (i == 2) {
                            b = A.getLocation().getY();
                        }
                        if (i == 3) {
                            b = A.getLocation().getZ();
                        }
                        if (i == 4) {
                            b = A.getLocation().getYaw();
                        }
                        if (i == 5) {
                            b = A.getLocation().getPitch();
                        }
                        if (i == 6) {
                            b = A.getVelocity().getX();
                        }
                        if (i == 7) {
                            b = A.getVelocity().getY();
                        }
                        if (i == 8) {
                            b = A.getVelocity().getZ();
                        }
                    }
                    if (i <= 4) {
                        if (A1[i].startsWith("x")) {
                            b = -A.getLocation().getX();
                        }
                        if (A1[i].startsWith("X")) {
                            b = A.getLocation().getX();
                        }
                        if (A1[i].startsWith("y")) {
                            b = -A.getLocation().getY();
                        }
                        if (A1[i].startsWith("Y")) {
                            b = A.getLocation().getY();
                        }
                        if (A1[i].startsWith("z")) {
                            b = -A.getLocation().getZ();
                        }
                        if (A1[i].startsWith("Z")) {
                            b = A.getLocation().getZ();
                        }
                    }
                    if (i >= 6) {
                        if (A1[i].startsWith("x")) {
                            b = -A.getVelocity().getX();
                        }
                        if (A1[i].startsWith("X")) {
                            b = A.getVelocity().getX();
                        }
                        if (A1[i].startsWith("y")) {
                            b = -A.getVelocity().getY();
                        }
                        if (A1[i].startsWith("Y")) {
                            b = A.getVelocity().getY();
                        }
                        if (A1[i].startsWith("z")) {
                            b = -A.getVelocity().getZ();
                        }
                        if (A1[i].startsWith("Z")) {
                            b = A.getVelocity().getZ();
                        }
                    }
               
                    if(isd(A1[i]))
                    {
                        c[i]=Double.parseDouble(A1[i]);
                    }
                    else
                    {
                        String A2 = A1[i].substring(1);
                        if(A2.isEmpty())
                        {
                            c[i]=b;
                        }
                        else
                        {
                            c[i]=b+Double.parseDouble(A2);
                        }
                    }
                }       
                A.teleport(new Location(A.getLocation().getWorld(), c[1],c[2],c[3],(float)c[4],(float)c[5]));
                A.setVelocity(new Vector(c[6],c[7],c[8]));
            }
        }
    
        public boolean isd(String A)
        {
            try {
                @SuppressWarnings("unused")
                double asdasd = Double.parseDouble(A);
                return true;
            } catch(Exception ex) {
                return false;
            }
       
        }
    }
    
    error:
    Code:
    [12:50:17 INFO]: Kang issued server command: /tp5 @a ~ ~ ~ ~ ~ ~ ~ ~
    [12:50:17 ERROR]: null
    org.bukkit.command.CommandException: Unhandled exception executing command 'tp5' in plugin tam3 v1.0
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[spigot-1.12.1.jar:git-Spigot-7754231-94b0980]
            at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141) ~[spigot-1.12.1.jar:git-Spigot-7754231-94b0980]
            at org.bukkit.craftbukkit.v1_12_R1.CraftServer.dispatchCommand(CraftServer.java:651) ~[spigot-1.12.1.jar:git-Spigot-7754231-94b0980]
            at net.minecraft.server.v1_12_R1.PlayerConnection.handleCommand(PlayerConnection.java:1392) [spigot-1.12.1.jar:git-Spigot-7754231-94b0980]
            at net.minecraft.server.v1_12_R1.PlayerConnection.a(PlayerConnection.java:1227) [spigot-1.12.1.jar:git-Spigot-7754231-94b0980]
            at net.minecraft.server.v1_12_R1.PacketPlayInChat.a(PacketPlayInChat.java:45) [spigot-1.12.1.jar:git-Spigot-7754231-94b0980]
            at net.minecraft.server.v1_12_R1.PacketPlayInChat.a(PacketPlayInChat.java:1) [spigot-1.12.1.jar:git-Spigot-7754231-94b0980]
            at net.minecraft.server.v1_12_R1.PlayerConnectionUtils$1.run(SourceFile:13) [spigot-1.12.1.jar:git-Spigot-7754231-94b0980]
            at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.8.0_131]
            at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_131]
            at net.minecraft.server.v1_12_R1.SystemUtils.a(SourceFile:46) [spigot-1.12.1.jar:git-Spigot-7754231-94b0980]
            at net.minecraft.server.v1_12_R1.MinecraftServer.D(MinecraftServer.java:748) [spigot-1.12.1.jar:git-Spigot-7754231-94b0980]
            at net.minecraft.server.v1_12_R1.DedicatedServer.D(DedicatedServer.java:406) [spigot-1.12.1.jar:git-Spigot-7754231-94b0980]
            at net.minecraft.server.v1_12_R1.MinecraftServer.C(MinecraftServer.java:679) [spigot-1.12.1.jar:git-Spigot-7754231-94b0980]
            at net.minecraft.server.v1_12_R1.MinecraftServer.run(MinecraftServer.java:577) [spigot-1.12.1.jar:git-Spigot-7754231-94b0980]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_131]
    Caused by: java.lang.ArrayIndexOutOfBoundsException: 1
            at tam3Main.CommandUtils.getTags(CommandUtils.java:278) ~[?:?]
            at tam3Main.CommandUtils.getTargets(CommandUtils.java:81) ~[?:?]
            at tam3Main.Main.onCommand(Main.java:98) ~[?:?]
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[spigot-1.12.1.jar:git-Spigot-7754231-94b0980]
            ... 15 more
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited: Oct 28, 2017
  24. Offline

    Zombie_Striker

    @Dandunse
    Fixed. Copy the new class from the github page

    You should also consider checking the args before getting args[0]. If the player does not include any args, that will throw an exception.
     
  25. Offline

    Dandunse

    tag is not working
    @a[r=3] is same @a.....................................................................................................................................................................
     
Thread Status:
Not open for further replies.

Share This Page