@a, @r and @p tags for commands.

Discussion in 'Plugin Development' started by Bammerbom, Jan 30, 2015.

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

    Bammerbom

    What is the best way to make "Player Selectors" work for custom created commands?
     
  2. Offline

    CraftCreeper6

    @Bammerbom
    Get the argument, check its letter. E.G:
    if(myArgument equals "@a"){
    for(All players online){
    // Do my thing here
    }
    }
     
  3. Offline

    timtower Administrator Administrator Moderator

    @Bammerbom I agree with @CraftCreeper6 but I have a slightly different idea:
    A list of targets.
    Then you can go through the argument, add players, if all, if random or something.
    And when executing: for(players : list){ Do stuff }
     
    Konato_K likes this.
  4. Offline

    nverdier

    @Bammerbom You mean for a command from Console/a Player? Because if you want this for a command block, it's already implemented, and you don't need to put it into your plugins :D
     
  5. Offline

    Bammerbom

    I mean for players etc.

    Is there a simple way to convert things like @a[r=10,etc] to a list of players?
     
  6. Offline

    timtower Administrator Administrator Moderator

    @Bammerbom You probably have to make your own parser for that
     
  7. Offline

    Bammerbom

    If bukkit did this already for command blocks, can't I just copy that?
     
  8. Code:java
    1.  
    2. public Player getPlayers() {
    3. for(Player player : Bukkit.getOnlinePlayers()) {
    4. return player;
    5. }
    6. return null;
    7. }
    8.  
    9. if(args[1].equalsIgnoreCase("@a") {
    10. getPlayers().runcommand
    11. }
    12.  

    May this works.
     
  9. Offline

    Bammerbom

    @MaTaMoR_
    That code wouldnt even work.

    Which class is used in vanilla for playerselector?
     
  10. Code:java
    1.  
    2. //--------------------- @a ---------------------\\
    3. public void runCommandAll(String command) {
    4. for(Player player : Bukkit.getOnlinePlayers()) {
    5. player.performCommand(command);
    6. }
    7. }
    8. //--------------------- @r ---------------------\\
    9. public void runCommandRandom(String command) {
    10. List<Player> players = Arrays.asList(Bukkit.getOnlinePlayers());
    11. Player player = players.get(new Random().nextInt(players.size()));
    12. player.performCommand(command);
    13. }
    14. //--------------------- @p ---------------------\\
    15. public HashMap<Player, Double> getNearPlayers(Player player) {
    16. List<Entity> entitys = player.getNearbyEntities(50, 50, 50);
    17. HashMap<Player, Double> players = new HashMap<Player, Double>();
    18. for(Entity entity : entitys) {
    19. if(entity instanceof Player) {
    20. Player nearplayer = (Player) entity;
    21. double distance = nearplayer.getLocation().distance(player.getLocation());
    22. players.put(nearplayer, distance);
    23. }
    24. }
    25. return players;
    26. }
    27. public void runCommandNear(Player player, String command) {
    28. Map.Entry<Player,Double> maxEntry = null;
    29. for(Map.Entry<Player, Double> entry : getNearPlayers(player).entrySet()) {
    30. if (maxEntry == null || entry.getValue() > maxEntry.getValue()) {
    31. Player nearplayer = entry.getKey();
    32. nearplayer.performCommand(command);
    33. }
    34. }
    35. }
    36.  
     
    Last edited: Jan 31, 2015
  11. Offline

    Bammerbom

    @MaTaMoR_ But this doesnt support things like @a[r=10]
     
  12. That's easy...
    Code:java
    1.  
    2. public void runCommandAll(Player player, String command, Integer radius) {
    3. List<Entity> entitys = player.getNearbyEntities(radius, radius, radius);
    4. for(Entity entity : entitys) {
    5. if(entity instanceof Player) {
    6. Player players = (Player) entity;
    7. players.performCommand(command);
    8. }
    9. }
    10. }
    11.  
     
  13. Offline

    Bammerbom

    Yes, but there are more than just radius.
     
  14. So create a system for everything, i'm not gonna do all for you . ... That's all .
     
  15. Offline

    LordDarthBob

    @Bammerbom
    You could try digging around in the shadowy realm of NMS and copy what you need.
    EDIT: After a few minutes of browsing, I remembered that the shadowy realm of NMS is so for a reason. Writing your own parser will be way faster.
     
    Last edited: Jan 31, 2015
  16. Offline

    PreFiXAUT

    @Bammerbom I have made such a Parser really quick since I think it's usefull and allot of people would surely like to have it :) I'm just gonna try it out a bit, but I think it's gonna be done tomorrow :) It'll be added to my Library (PreLib) when it's done and I'm gonna hit up
     
Thread Status:
Not open for further replies.

Share This Page