44import fr .bakaaless .Trader .plugin .TraderPlugin ;
55import lombok .AccessLevel ;
66import lombok .Getter ;
7+ import org .bukkit .Location ;
78import org .bukkit .command .Command ;
89import org .bukkit .command .CommandExecutor ;
910import org .bukkit .command .CommandSender ;
1011import org .bukkit .command .TabCompleter ;
12+ import org .bukkit .entity .Entity ;
1113import org .bukkit .entity .Player ;
1214
1315import java .util .ArrayList ;
@@ -26,7 +28,7 @@ public class Executor implements CommandExecutor, TabCompleter {
2628 public Executor () {
2729 this .main = TraderPlugin .getInstance ();
2830 this .accept = new HashMap <>();
29- this .getMain ().getCommand ("trader " ).setExecutor (this );
31+ this .getMain ().getCommand ("echange " ).setExecutor (this );
3032 }
3133
3234 @ Override
@@ -36,11 +38,16 @@ public boolean onCommand(CommandSender sender, Command command, String label, St
3638 }
3739 final Player player = (Player ) sender ;
3840 try {
41+ final int radius = this .getMain ().getFileManager ().getFile ("config" ).getInt ("radius" );
3942 if (args [0 ].equalsIgnoreCase ("accept" )) {
4043 if (!this .getAccept ().containsKey (player )) {
4144 player .sendMessage (this .getMain ().getFileManager ().getErrorWithPrefix ("none" ));
4245 return true ;
4346 }
47+ if (!this .getPlayerRadius (player .getLocation (), radius ).contains (this .getAccept ().get (player ))) {
48+ player .sendMessage (this .getMain ().getFileManager ().getErrorWithPrefix ("outrange" ));
49+ return true ;
50+ }
4451 player .sendMessage (this .getMain ().getFileManager ().getMessageWithPrefix ("accept" ));
4552 this .getAccept ().get (player ).sendMessage (this .getMain ().getFileManager ().getMessageWithPrefix ("accept" ));
4653 this .getMain ().addTraders (new Trader (player , this .getAccept ().get (player )));
@@ -62,14 +69,14 @@ public boolean onCommand(CommandSender sender, Command command, String label, St
6269 return true ;
6370 }
6471 }
65- for (final Player players : this .getMain (). getServer (). getOnlinePlayers ( )) {
72+ for (final Player players : this .getPlayerRadius ( player . getLocation (), radius )) {
6673 if (!players .getName ().toLowerCase ().equalsIgnoreCase (args [0 ])) continue ;
6774 if (player .equals (players )) {
6875 player .sendMessage (this .getMain ().getFileManager ().getErrorWithPrefix ("yourself" ));
6976 return true ;
7077 }
7178 if (this .getAccept ().containsKey (player ) && this .getAccept ().get (player ).equals (players )) {
72- return player .performCommand ("trader accept" );
79+ return player .performCommand (label + " accept" );
7380 }
7481 player .sendMessage (this .getMain ().getFileManager ().getMessageWithPrefix ("sent" ));
7582 players .sendMessage (this .getMain ().getFileManager ().getMessageWithPrefix ("receive" ).replace ("%player%" , player .getName ()));
@@ -80,7 +87,7 @@ public boolean onCommand(CommandSender sender, Command command, String label, St
8087 players .sendMessage (this .getMain ().getFileManager ().getErrorWithPrefix ("expired" ));
8188 this .getAccept ().remove (player );
8289 }
83- }, 120L * 20L );
90+ }, this . getMain (). getFileManager (). getFile ( "config" ). getInt ( "expired" ) * 20L );
8491 return true ;
8592 }
8693 player .sendMessage (this .getMain ().getFileManager ().getErrorWithPrefix ("found" ));
@@ -93,6 +100,11 @@ public boolean onCommand(CommandSender sender, Command command, String label, St
93100 @ Override
94101 public List <String > onTabComplete (CommandSender commandSender , Command command , String s , String [] args ) {
95102 final ArrayList <String > completions = new ArrayList <>();
103+ if (!(commandSender instanceof Player )) {
104+ if ("stop" .startsWith (args [0 ].toLowerCase ()))
105+ completions .add ("stop" );
106+ return completions ;
107+ }
96108 if (args .length == 1 ) {
97109 if ("accept" .startsWith (args [0 ].toLowerCase ()))
98110 completions .add ("accept" );
@@ -101,7 +113,7 @@ public List<String> onTabComplete(CommandSender commandSender, Command command,
101113 if (commandSender .hasPermission ("trader.stop" ))
102114 if ("stop" .startsWith (args [0 ].toLowerCase ()))
103115 completions .add ("stop" );
104- for (final Player player : this .getMain ().getServer ().getOnlinePlayers ( )) {
116+ for (final Player player : this .getPlayerRadius ((( Player ) commandSender ). getLocation (), this . getMain ().getFileManager ().getFile ( "config" ). getInt ( "radius" ) )) {
105117 if (player .getName ().toLowerCase ().startsWith (args [0 ].toLowerCase ())) {
106118 if (player .equals (commandSender )) continue ;
107119 completions .add (player .getName ());
@@ -111,4 +123,16 @@ public List<String> onTabComplete(CommandSender commandSender, Command command,
111123 }
112124 return new ArrayList <>();
113125 }
126+
127+ private List <Player > getPlayerRadius (final Location location , final int radius ) {
128+ final List <Player > players = new ArrayList <>();
129+ for (final Entity entity : location .getWorld ().getNearbyEntities (location , radius , radius , radius )) {
130+ if (entity instanceof Player ) {
131+ if (entity .getName ().contains ("&" ) || entity .getName ().contains ("§" ) || ((Player ) entity ).isSleepingIgnored () || !((Player ) entity ).isOnline ())
132+ continue ;
133+ players .add ((Player ) entity );
134+ }
135+ }
136+ return players ;
137+ }
114138}
0 commit comments