Skip to content

Commit 33f8ce2

Browse files
author
Aless
committed
Fix some bugs
Add radius & time expired
1 parent c5848c7 commit 33f8ce2

File tree

7 files changed

+48
-11
lines changed

7 files changed

+48
-11
lines changed

.idea/git_toolbox_prj.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/main/java/fr/bakaaless/Trader/commands/Executor.java

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@
44
import fr.bakaaless.Trader.plugin.TraderPlugin;
55
import lombok.AccessLevel;
66
import lombok.Getter;
7+
import org.bukkit.Location;
78
import org.bukkit.command.Command;
89
import org.bukkit.command.CommandExecutor;
910
import org.bukkit.command.CommandSender;
1011
import org.bukkit.command.TabCompleter;
12+
import org.bukkit.entity.Entity;
1113
import org.bukkit.entity.Player;
1214

1315
import 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
}

src/main/java/fr/bakaaless/Trader/object/Trader.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ private void cancel(final Player cancelled) {
9999
InventoryDragEvent.getHandlerList().unregister(this);
100100
InventoryClickEvent.getHandlerList().unregister(this);
101101
if (this.getStatus().equals(TransactStatus.FINISH)) return;
102+
this.status = TransactStatus.CANCELLED;
102103
for (final Player player : this.getPlayerInventoryMap().keySet()) {
103104
if (!player.equals(cancelled)) {
104105
player.sendMessage(this.main.getFileManager().getErrorWithPrefix("cancel.other"));
@@ -114,6 +115,7 @@ private void cancel(final Player cancelled) {
114115
}
115116
player.closeInventory();
116117
}
118+
this.status = TransactStatus.FINISH;
117119
this.main.removeTraders(this);
118120
}
119121

@@ -122,7 +124,7 @@ public void stop() {
122124
InventoryDragEvent.getHandlerList().unregister(this);
123125
InventoryClickEvent.getHandlerList().unregister(this);
124126
if (this.getStatus().equals(TransactStatus.FINISH)) return;
125-
127+
this.status = TransactStatus.CANCELLED;
126128
for (final Player player : this.getPlayerInventoryMap().keySet()) {
127129
player.sendMessage(this.main.getFileManager().getErrorWithPrefix("cancel.console"));
128130
final Inventory inventory = player.getOpenInventory().getTopInventory();
@@ -135,6 +137,7 @@ public void stop() {
135137
}
136138
player.closeInventory();
137139
}
140+
this.status = TransactStatus.FINISH;
138141
this.main.removeTraders(this);
139142
}
140143

@@ -227,7 +230,7 @@ public void onInventoryClick(final InventoryClickEvent e) {
227230
e.getClickedInventory().setItem(39, ItemUtils.get(Material.GREEN_CONCRETE, this.titles.get("status.ready")));
228231
e.getClickedInventory().setItem(45, ItemUtils.get(Material.ORANGE_CONCRETE, this.titles.get("edit")));
229232
this.getPlayerStatusMap().replace(player, PlayerStatus.WAITING);
230-
if (this.getPlayerStatusMap().get(other).equals(PlayerStatus.WAITING)) {
233+
if (this.getPlayerStatusMap().get(other).equals(PlayerStatus.WAITING) && this.getStatus().equals(TransactStatus.WAITING)) {
231234
this.progress();
232235
}
233236
return;
@@ -360,6 +363,7 @@ private synchronized void addItem(final Player player, final ItemStack itemStack
360363
private enum TransactStatus {
361364
WAITING,
362365
STARTING,
366+
CANCELLED,
363367
FINISH
364368
}
365369

src/main/java/fr/bakaaless/Trader/plugin/TraderPlugin.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public void onEnable() {
4040
}
4141
TraderPlugin.instance = this;
4242
try {
43-
this.fileManager = new FileManager(this, "messages");
43+
this.fileManager = new FileManager(this, "messages", "config");
4444
} catch (IOException | InvalidConfigurationException e) {
4545
this.getLogger().log(Level.SEVERE, ChatColor.translateAlternateColorCodes('&', "§c§lTrading §4§l» §cError while retrieve creating files."));
4646
}

src/main/resources/config.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
radius: 10 # in blocks
2+
expired: 60 # in seconds

src/main/resources/messages.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,5 @@ messages:
3131
deny: "&cDemande d'échange refusée."
3232
expired: "&cDemande d'échange expirée."
3333
yourself: "&cVous ne pouvez pas échanger avec votre alter ego."
34-
found: "&cAucun joueur trouvé."
34+
found: "&cAucun joueur trouvé. Il est peut-être trop éloigné."
35+
outrange: "&Le joueur est hors de portée."

src/main/resources/plugin.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
main: fr.bakaaless.Trader.plugin.TraderPlugin
2-
version: 0.0.1-ALPHA
2+
version: 0.1.3-BETA
33
name: Trader
44
author: BakaAless
55
api-version: 1.15
66
softdepend:
77
- Vault
88
commands:
9-
trader:
9+
echange:
1010
usage: ""

0 commit comments

Comments
 (0)