Skip to content

Commit 23f3aa0

Browse files
Refactor PlayerFinder and solve lag issue for real
1 parent fc33678 commit 23f3aa0

17 files changed

+239
-94
lines changed

src/main/java/org/maxgamer/quickshop/QuickShop.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -568,7 +568,7 @@ public boolean loadEcon() {
568568
if (Util.isUUID(taxAccount)) {
569569
tax = PlayerFinder.findOfflinePlayerByUUID(UUID.fromString(taxAccount));
570570
} else {
571-
tax = PlayerFinder.findOfflinePlayerByName((Objects.requireNonNull(taxAccount)));
571+
tax = PlayerFinder.findOfflinePlayerByUUID(PlayerFinder.findUUIDByName(Objects.requireNonNull(taxAccount), true));
572572
}
573573
Economy_Vault vault = (Economy_Vault) economy;
574574
if (vault.isValid()) {
@@ -2289,7 +2289,7 @@ public void setupBootError(BootError bootError, boolean unregisterListeners) {
22892289

22902290
public void registerCustomCommands() {
22912291
List<String> customCommands = getConfig().getStringList("custom-commands");
2292-
PluginCommand quickShopCommand = getCommand("qs");
2292+
PluginCommand quickShopCommand = Bukkit.getPluginCommand("qs");
22932293
if (quickShopCommand == null) {
22942294
getLogger().warning("Failed to get QuickShop PluginCommand instance.");
22952295
return;

src/main/java/org/maxgamer/quickshop/command/subcommand/SubCommand_RemoveAll.java

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import org.maxgamer.quickshop.QuickShop;
2929
import org.maxgamer.quickshop.api.command.CommandHandler;
3030
import org.maxgamer.quickshop.api.shop.Shop;
31+
import org.maxgamer.quickshop.util.PlayerFinder;
3132
import org.maxgamer.quickshop.util.Util;
3233
import org.maxgamer.quickshop.util.logging.container.ShopRemoveLog;
3334

@@ -47,26 +48,20 @@ public void onCommand(@NotNull CommandSender sender, @NotNull String commandLabe
4748
if (cmdArg.length == 1) {
4849
//copy it first
4950
List<Shop> tempList = new ArrayList<>(plugin.getShopManager().getAllShops());
50-
OfflinePlayer shopOwner = null;
51-
for (OfflinePlayer player : plugin.getServer().getOfflinePlayers()) {
52-
if (player.getName() != null && player.getName().equalsIgnoreCase(cmdArg[0])) {
53-
shopOwner = player;
54-
break;
55-
}
56-
}
51+
PlayerFinder.PlayerProfile shopOwner = PlayerFinder.findPlayerProfileByName(cmdArg[0], false);
5752
if (shopOwner == null) {
5853
plugin.text().of(sender, "unknown-player").send();
5954
return;
6055
}
6156

6257
int i = 0;
63-
if (!shopOwner.equals(sender)) { //Non-self shop
58+
if (sender instanceof OfflinePlayer && !shopOwner.getUuid().equals(((OfflinePlayer) sender).getUniqueId())) { //Non-self shop
6459
if (!sender.hasPermission("quickshop.removeall.other")) {
6560
plugin.text().of(sender, "no-permission").send();
6661
return;
6762
}
6863
for (Shop shop : tempList) {
69-
if (shop.getOwner().equals(shopOwner.getUniqueId())) {
64+
if (shop.getOwner().equals(shopOwner.getUuid())) {
7065
plugin.logEvent(new ShopRemoveLog(Util.getSenderUniqueId(sender), "Deleting shop " + shop + " as requested by the /qs removeall command.", shop.saveToInfoStorage()));
7166
shop.delete();
7267
i++;

src/main/java/org/maxgamer/quickshop/command/subcommand/SubCommand_SetOwner.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
package org.maxgamer.quickshop.command.subcommand;
2121

2222
import lombok.AllArgsConstructor;
23-
import org.bukkit.OfflinePlayer;
2423
import org.bukkit.block.Block;
2524
import org.bukkit.entity.Player;
2625
import org.bukkit.util.BlockIterator;
@@ -58,13 +57,13 @@ public void onCommand(@NotNull Player sender, @NotNull String commandLabel, @Not
5857
continue;
5958
}
6059

61-
final OfflinePlayer newShopOwner = PlayerFinder.findOfflinePlayerByName(cmdArg[0]);
62-
if (newShopOwner.getName() == null) {
60+
final PlayerFinder.PlayerProfile newShopOwnerProfile = PlayerFinder.findPlayerProfileByName(cmdArg[0], false);
61+
if (newShopOwnerProfile == null) {
6362
plugin.text().of(sender, "unknown-player").send();
6463
return;
6564
}
66-
shop.setOwner(newShopOwner.getUniqueId());
67-
plugin.text().of(sender, "command.new-owner", newShopOwner.getName()).send();
65+
shop.setOwner(newShopOwnerProfile.getUuid());
66+
plugin.text().of(sender, "command.new-owner", newShopOwnerProfile.getName()).send();
6867
return;
6968
}
7069

src/main/java/org/maxgamer/quickshop/command/subcommand/SubCommand_Staff.java

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,7 @@
2020
package org.maxgamer.quickshop.command.subcommand;
2121

2222
import lombok.AllArgsConstructor;
23-
import org.bukkit.Bukkit;
2423
import org.bukkit.ChatColor;
25-
import org.bukkit.OfflinePlayer;
2624
import org.bukkit.block.Block;
2725
import org.bukkit.entity.Player;
2826
import org.bukkit.util.BlockIterator;
@@ -76,7 +74,7 @@ public void onCommand(@NotNull Player sender, @NotNull String commandLabel, @Not
7674
MsgUtil.sendDirectMessage(sender,
7775
ChatColor.GREEN
7876
+ plugin.text().of(sender, "tableformat.left_begin").forLocale()
79-
+ Bukkit.getOfflinePlayer(uuid).getName());
77+
+ PlayerFinder.findNameByUUID(uuid));
8078
}
8179
return;
8280
case "add":
@@ -86,19 +84,22 @@ public void onCommand(@NotNull Player sender, @NotNull String commandLabel, @Not
8684
return;
8785
}
8886
case 2:
89-
final OfflinePlayer offlinePlayer = PlayerFinder.findOfflinePlayerByName(cmdArg[1]);
90-
String offlinePlayerName = offlinePlayer.getName();
91-
87+
final PlayerFinder.PlayerProfile profile = PlayerFinder.findPlayerProfileByName(cmdArg[1], false);
88+
if (profile == null) {
89+
plugin.text().of(sender, "unknown-player").send();
90+
return;
91+
}
92+
String offlinePlayerName = profile.getName();
9293
if (offlinePlayerName == null) {
9394
offlinePlayerName = "null";
9495
}
9596
switch (cmdArg[0]) {
9697
case "add":
97-
shop.addStaff(offlinePlayer.getUniqueId());
98+
shop.addStaff(profile.getUuid());
9899
plugin.text().of(sender, "shop-staff-added", offlinePlayerName).send();
99100
return;
100101
case "del":
101-
shop.delStaff(offlinePlayer.getUniqueId());
102+
shop.delStaff(profile.getUuid());
102103
plugin.text().of(sender,
103104
"shop-staff-deleted", offlinePlayerName).send();
104105
return;

src/main/java/org/maxgamer/quickshop/command/subcommand/SubCommand_TaxAccount.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,12 @@ public void onCommand(@NotNull Player sender, @NotNull String commandLabel, @Not
5454
if (Util.isUUID(cmdArg[0])) {
5555
shop.setTaxAccount(UUID.fromString(cmdArg[0]));
5656
} else {
57-
shop.setTaxAccount(PlayerFinder.findUUIDByName(cmdArg[0]));
57+
UUID uuid = PlayerFinder.findUUIDByName(cmdArg[0], false);
58+
if (uuid == null) {
59+
plugin.text().of(sender, "unknown-player").send();
60+
return;
61+
}
62+
shop.setTaxAccount(uuid);
5863
}
5964
plugin.text().of(sender, "taxaccount-set", cmdArg[0]).send();
6065
return;

src/main/java/org/maxgamer/quickshop/command/subcommand/SubCommand_Transfer.java

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919

2020
package org.maxgamer.quickshop.command.subcommand;
2121

22-
import org.bukkit.OfflinePlayer;
2322
import org.bukkit.entity.Player;
2423
import org.jetbrains.annotations.NotNull;
2524
import org.jetbrains.annotations.Nullable;
@@ -45,12 +44,16 @@ public SubCommand_Transfer(QuickShop plugin) {
4544
@Override
4645
public void onCommand(@NotNull Player sender, @NotNull String commandLabel, @NotNull String[] cmdArg) {
4746
if (cmdArg.length == 1) {
48-
final OfflinePlayer targetPlayer = PlayerFinder.findOfflinePlayerByName(cmdArg[0]);
47+
final PlayerFinder.PlayerProfile targetPlayer = PlayerFinder.findPlayerProfileByName(cmdArg[0], false);
48+
if (targetPlayer == null) {
49+
plugin.text().of(sender, "unknown-player").send();
50+
return;
51+
}
4952
String targetPlayerName = targetPlayer.getName();
5053
if (targetPlayerName == null) {
5154
targetPlayerName = "null";
5255
}
53-
final UUID targetPlayerUUID = targetPlayer.getUniqueId();
56+
final UUID targetPlayerUUID = targetPlayer.getUuid();
5457
List<Shop> shopList = plugin.getShopManager().getPlayerAllShops(sender.getUniqueId());
5558
for (Shop shop : shopList) {
5659
if (!shop.isBuying()) {
@@ -64,19 +67,27 @@ public void onCommand(@NotNull Player sender, @NotNull String commandLabel, @Not
6467
return;
6568
}
6669

67-
final OfflinePlayer fromPlayer = PlayerFinder.findOfflinePlayerByName(cmdArg[0]);
70+
final PlayerFinder.PlayerProfile fromPlayer = PlayerFinder.findPlayerProfileByName(cmdArg[0], false);
71+
if (fromPlayer == null) {
72+
plugin.text().of(sender, "unknown-player").send();
73+
return;
74+
}
6875
String fromPlayerName = fromPlayer.getName();
6976
if (fromPlayerName == null) {
7077
fromPlayerName = "null";
7178
}
7279
//FIXME: Update this when drop 1.15 supports
73-
final OfflinePlayer targetPlayer = PlayerFinder.findOfflinePlayerByName(cmdArg[1]);
80+
final PlayerFinder.PlayerProfile targetPlayer = PlayerFinder.findPlayerProfileByName(cmdArg[1], false);
81+
if (targetPlayer == null) {
82+
plugin.text().of(sender, "unknown-player").send();
83+
return;
84+
}
7485
String targetPlayerName = targetPlayer.getName();
7586
if (targetPlayerName == null) {
7687
targetPlayerName = "null";
7788
}
78-
final UUID targetPlayerUUID = targetPlayer.getUniqueId();
79-
List<Shop> shopList = plugin.getShopManager().getPlayerAllShops(fromPlayer.getUniqueId());
89+
final UUID targetPlayerUUID = targetPlayer.getUuid();
90+
List<Shop> shopList = plugin.getShopManager().getPlayerAllShops(fromPlayer.getUuid());
8091
for (Shop shop : shopList) {
8192
shop.setOwner(targetPlayerUUID);
8293
}

src/main/java/org/maxgamer/quickshop/economy/Economy_TNE.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,14 @@
2525
import net.tnemc.core.TNE;
2626
import net.tnemc.core.common.api.TNEAPI;
2727
import net.tnemc.core.common.currency.TNECurrency;
28-
import org.bukkit.Bukkit;
2928
import org.bukkit.OfflinePlayer;
3029
import org.bukkit.World;
3130
import org.bukkit.plugin.Plugin;
3231
import org.jetbrains.annotations.NotNull;
3332
import org.jetbrains.annotations.Nullable;
3433
import org.maxgamer.quickshop.QuickShop;
3534
import org.maxgamer.quickshop.api.economy.AbstractEconomy;
35+
import org.maxgamer.quickshop.util.PlayerFinder;
3636
import org.maxgamer.quickshop.util.reload.ReloadResult;
3737
import org.maxgamer.quickshop.util.reload.ReloadStatus;
3838

@@ -90,7 +90,7 @@ private TNECurrency getCurrency(@NotNull World world, @Nullable String currency)
9090
*/
9191
@Override
9292
public boolean deposit(@NotNull UUID name, double amount, @NotNull World world, @Nullable String currency) {
93-
deposit(Bukkit.getOfflinePlayer(name), amount, world, currency);
93+
deposit(PlayerFinder.findOfflinePlayerByUUID(name), amount, world, currency);
9494
return false;
9595
}
9696

@@ -141,7 +141,7 @@ public String format(double balance, @NotNull World world, @Nullable String curr
141141
*/
142142
@Override
143143
public double getBalance(@NotNull UUID name, @NotNull World world, @Nullable String currency) {
144-
return getBalance(Bukkit.getOfflinePlayer(name), world, currency);
144+
return getBalance(PlayerFinder.findOfflinePlayerByUUID(name), world, currency);
145145
}
146146

147147
/**
@@ -173,7 +173,7 @@ public double getBalance(@NotNull OfflinePlayer player, @NotNull World world, @N
173173
*/
174174
@Override
175175
public boolean withdraw(@NotNull UUID name, double amount, @NotNull World world, @Nullable String currency) {
176-
return withdraw(Bukkit.getOfflinePlayer(name), amount, world, currency);
176+
return withdraw(PlayerFinder.findOfflinePlayerByUUID(name), amount, world, currency);
177177
}
178178

179179
/**

src/main/java/org/maxgamer/quickshop/economy/Economy_Vault.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import org.jetbrains.annotations.Nullable;
3737
import org.maxgamer.quickshop.QuickShop;
3838
import org.maxgamer.quickshop.api.economy.AbstractEconomy;
39+
import org.maxgamer.quickshop.util.PlayerFinder;
3940
import org.maxgamer.quickshop.util.Util;
4041
import org.maxgamer.quickshop.util.economyformatter.BuiltInEconomyFormatter;
4142
import org.maxgamer.quickshop.util.reload.ReloadResult;
@@ -132,7 +133,7 @@ public boolean deposit(@NotNull UUID name, double amount, @NotNull World world,
132133
if (!isValid()) {
133134
return false;
134135
}
135-
return deposit(plugin.getServer().getOfflinePlayer(name), amount, world, currency);
136+
return deposit(PlayerFinder.findOfflinePlayerByUUID(name), amount, world, currency);
136137

137138
}
138139

@@ -190,7 +191,7 @@ public double getBalance(@NotNull UUID name, @NotNull World world, @Nullable Str
190191
return 0.0;
191192
}
192193

193-
return getBalance(plugin.getServer().getOfflinePlayer(name), world, currency);
194+
return getBalance(PlayerFinder.findOfflinePlayerByUUID(name), world, currency);
194195

195196
}
196197

@@ -216,7 +217,7 @@ public boolean withdraw(@NotNull UUID name, double amount, @NotNull World world,
216217
if (!isValid()) {
217218
return false;
218219
}
219-
return withdraw(plugin.getServer().getOfflinePlayer(name), amount, world, currency);
220+
return withdraw(PlayerFinder.findOfflinePlayerByUUID(name), amount, world, currency);
220221
}
221222

222223
@Override

src/main/java/org/maxgamer/quickshop/economy/Trader.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
package org.maxgamer.quickshop.economy;
2121

2222
import lombok.AllArgsConstructor;
23+
import org.bukkit.Bukkit;
2324
import org.bukkit.Location;
2425
import org.bukkit.Material;
2526
import org.bukkit.OfflinePlayer;
@@ -29,30 +30,36 @@
2930
import org.bukkit.profile.PlayerProfile;
3031
import org.jetbrains.annotations.NotNull;
3132
import org.jetbrains.annotations.Nullable;
33+
import org.maxgamer.quickshop.util.PlayerFinder;
3234

3335
import java.util.Map;
3436
import java.util.UUID;
3537

3638
@AllArgsConstructor
3739
public class Trader implements OfflinePlayer {
3840

41+
3942
@Nullable
40-
private final String name;
43+
private String lastKnownName;
4144
@NotNull
4245
private final OfflinePlayer offlinePlayer;
4346

4447
public static Trader adapt(OfflinePlayer offlinePlayer) {
4548
return new Trader(offlinePlayer.getName(), offlinePlayer);
4649
}
4750

51+
public static Trader adapt(PlayerFinder.PlayerProfile profile) {
52+
return new Trader(profile.getName(), Bukkit.getOfflinePlayer(profile.getUuid()));
53+
}
54+
4855
@Override
4956
public boolean isOnline() {
5057
return offlinePlayer.isOnline();
5158
}
5259

5360
@Override
5461
public @Nullable String getName() {
55-
return name == null ? offlinePlayer.getName() : name;
62+
return lastKnownName == null && offlinePlayer.isOnline() ? lastKnownName = offlinePlayer.getName() : lastKnownName;
5663
}
5764

5865
@Override

src/main/java/org/maxgamer/quickshop/listener/PlayerListener.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -390,12 +390,12 @@ public void onInventoryClose(InventoryCloseEvent e) {
390390
@EventHandler(ignoreCancelled = true, priority = EventPriority.MONITOR)
391391
public void onJoin(PlayerJoinEvent e) {
392392
Util.debugLog("Player " + e.getPlayer().getName() + " using locale " + e.getPlayer().getLocale() + ": " + plugin.text().of(e.getPlayer(), "file-test").forLocale());
393-
PlayerFinder.updateStashIfNeeded(e.getPlayer());
393+
PlayerFinder.updateIfNeeded(e.getPlayer());
394394
// Notify the player any messages they were sent
395395
if (plugin.getConfig().getBoolean("shop.auto-fetch-shop-messages")) {
396396
//Run Task later to make sure locale is correct
397397
plugin.getServer().getScheduler().runTaskLater(plugin, () ->
398-
MsgUtil.flush(plugin.getServer().getOfflinePlayer(e.getPlayer().getUniqueId()))
398+
MsgUtil.flush(PlayerFinder.findOfflinePlayerByUUID(e.getPlayer().getUniqueId()))
399399
, 50);
400400
}
401401
}

0 commit comments

Comments
 (0)