possible fix for getOfflinePlayer running in timeouts due to mojang

This commit is contained in:
Peda 2020-12-27 23:07:01 +01:00
parent 5b69c58bbb
commit 27b90b7343
8 changed files with 123 additions and 18 deletions

View File

@ -9,6 +9,7 @@ import de.butzlabben.world.event.WorldAddmemberEvent;
import de.butzlabben.world.event.WorldDeleteEvent; import de.butzlabben.world.event.WorldDeleteEvent;
import de.butzlabben.world.event.WorldRemovememberEvent; import de.butzlabben.world.event.WorldRemovememberEvent;
import de.butzlabben.world.util.PlayerPositions; import de.butzlabben.world.util.PlayerPositions;
import de.butzlabben.world.util.PlayerWrapper;
import de.butzlabben.world.wrapper.SystemWorld; import de.butzlabben.world.wrapper.SystemWorld;
import de.butzlabben.world.wrapper.WorldPlayer; import de.butzlabben.world.wrapper.WorldPlayer;
import org.apache.commons.io.FileUtils; import org.apache.commons.io.FileUtils;
@ -39,7 +40,7 @@ public class WorldAdministrateCommand {
} }
@SuppressWarnings("deprecation") @SuppressWarnings("deprecation")
OfflinePlayer a = Bukkit.getOfflinePlayer(args[1]); OfflinePlayer a = PlayerWrapper.getOfflinePlayer(args[1]);
WorldConfig wc = WorldConfig.getWorldConfig(dc.getWorldname()); WorldConfig wc = WorldConfig.getWorldConfig(dc.getWorldname());
if (a == null) { if (a == null) {
p.sendMessage(MessageConfig.getNotRegistered().replaceAll("%player", args[1])); p.sendMessage(MessageConfig.getNotRegistered().replaceAll("%player", args[1]));
@ -152,7 +153,7 @@ public class WorldAdministrateCommand {
return false; return false;
} }
@SuppressWarnings("deprecation") @SuppressWarnings("deprecation")
OfflinePlayer a = Bukkit.getOfflinePlayer(args[1]); OfflinePlayer a = PlayerWrapper.getOfflinePlayer(args[1]);
WorldConfig wc = WorldConfig.getWorldConfig(dc.getWorldname()); WorldConfig wc = WorldConfig.getWorldConfig(dc.getWorldname());
if (a == null) { if (a == null) {
p.sendMessage(MessageConfig.getNotRegistered().replaceAll("%player", args[1])); p.sendMessage(MessageConfig.getNotRegistered().replaceAll("%player", args[1]));
@ -196,7 +197,7 @@ public class WorldAdministrateCommand {
return false; return false;
} }
@SuppressWarnings("deprecation") @SuppressWarnings("deprecation")
OfflinePlayer a = Bukkit.getOfflinePlayer(args[1]); OfflinePlayer a = PlayerWrapper.getOfflinePlayer(args[1]);
WorldConfig wc = WorldConfig.getWorldConfig(dc.getWorldname()); WorldConfig wc = WorldConfig.getWorldConfig(dc.getWorldname());
if (!wc.isMember(a.getUniqueId())) { if (!wc.isMember(a.getUniqueId())) {
p.sendMessage(MessageConfig.getNoMemberOwn()); p.sendMessage(MessageConfig.getNoMemberOwn());
@ -233,7 +234,7 @@ public class WorldAdministrateCommand {
return false; return false;
} }
@SuppressWarnings("deprecation") @SuppressWarnings("deprecation")
OfflinePlayer a = Bukkit.getOfflinePlayer(args[1]); OfflinePlayer a = PlayerWrapper.getOfflinePlayer(args[1]);
WorldConfig wc = WorldConfig.getWorldConfig(dc.getWorldname()); WorldConfig wc = WorldConfig.getWorldConfig(dc.getWorldname());
if (!wc.isMember(a.getUniqueId())) { if (!wc.isMember(a.getUniqueId())) {
p.sendMessage(MessageConfig.getNoMemberOwn()); p.sendMessage(MessageConfig.getNoMemberOwn());
@ -270,7 +271,7 @@ public class WorldAdministrateCommand {
return false; return false;
} }
@SuppressWarnings("deprecation") @SuppressWarnings("deprecation")
OfflinePlayer a = Bukkit.getOfflinePlayer(args[1]); OfflinePlayer a = PlayerWrapper.getOfflinePlayer(args[1]);
WorldConfig wc = WorldConfig.getWorldConfig(dc.getWorldname()); WorldConfig wc = WorldConfig.getWorldConfig(dc.getWorldname());
if (!wc.isMember(a.getUniqueId())) { if (!wc.isMember(a.getUniqueId())) {
p.sendMessage(MessageConfig.getNoMemberOwn()); p.sendMessage(MessageConfig.getNoMemberOwn());
@ -307,7 +308,7 @@ public class WorldAdministrateCommand {
return false; return false;
} }
@SuppressWarnings("deprecation") @SuppressWarnings("deprecation")
OfflinePlayer a = Bukkit.getOfflinePlayer(args[1]); OfflinePlayer a = PlayerWrapper.getOfflinePlayer(args[1]);
WorldConfig wc = WorldConfig.getWorldConfig(dc.getWorldname()); WorldConfig wc = WorldConfig.getWorldConfig(dc.getWorldname());
if (!wc.isMember(a.getUniqueId())) { if (!wc.isMember(a.getUniqueId())) {
p.sendMessage(MessageConfig.getNoMemberOwn()); p.sendMessage(MessageConfig.getNoMemberOwn());

View File

@ -21,11 +21,11 @@ public class DependenceConfig {
public DependenceConfig(String s) { public DependenceConfig(String s) {
OfflinePlayer op = null; OfflinePlayer op = null;
try { try {
op = Bukkit.getOfflinePlayer(UUID.fromString(s)); op = PlayerWrapper.getOfflinePlayer(UUID.fromString(s));
} catch (Exception ignored) { } catch (Exception ignored) {
} }
if (op == null) { if (op == null) {
op = Bukkit.getOfflinePlayer(s); op = PlayerWrapper.getOfflinePlayer(s);
} }
uuid = op.getUniqueId(); uuid = op.getUniqueId();
} }
@ -85,7 +85,7 @@ public class DependenceConfig {
File dconfig = new File("plugins//WorldSystem//dependence.yml"); File dconfig = new File("plugins//WorldSystem//dependence.yml");
YamlConfiguration cfg = YamlConfiguration.loadConfiguration(dconfig); YamlConfiguration cfg = YamlConfiguration.loadConfiguration(dconfig);
String uuid = this.uuid.toString(); String uuid = this.uuid.toString();
cfg.set("Dependences." + uuid + ".ActualName", Bukkit.getOfflinePlayer(this.uuid).getName()); cfg.set("Dependences." + uuid + ".ActualName", PlayerWrapper.getOfflinePlayer(this.uuid).getName());
try { try {
cfg.save(dconfig); cfg.save(dconfig);
} catch (IOException e) { } catch (IOException e) {
@ -102,7 +102,7 @@ public class DependenceConfig {
id++; id++;
cfg.set("HighestID", id); cfg.set("HighestID", id);
cfg.set("Dependences." + uuid + ".ID", id); cfg.set("Dependences." + uuid + ".ID", id);
cfg.set("Dependences." + uuid + ".ActualName", Bukkit.getOfflinePlayer(this.uuid).getName()); cfg.set("Dependences." + uuid + ".ActualName", PlayerWrapper.getOfflinePlayer(this.uuid).getName());
try { try {
cfg.save(dconfig); cfg.save(dconfig);
} catch (IOException e) { } catch (IOException e) {
@ -158,6 +158,6 @@ public class DependenceConfig {
} }
public OfflinePlayer getOwner() { public OfflinePlayer getOwner() {
return Bukkit.getOfflinePlayer(uuid); return PlayerWrapper.getOfflinePlayer(uuid);
} }
} }

View File

@ -208,6 +208,12 @@ public class PluginConfig {
return getConfig().getString("database.players_table_name"); return getConfig().getString("database.players_table_name");
} }
public static String getUUIDTableName() {
return getConfig().getString("database.players_uuids");
}
public static String getDatabaseType() { public static String getDatabaseType() {
return getConfig().getString("database.type"); return getConfig().getString("database.type");
} }

View File

@ -94,7 +94,7 @@ public class WorldConfig {
YamlConfiguration cfg = YamlConfiguration.loadConfiguration(file); YamlConfiguration cfg = YamlConfiguration.loadConfiguration(file);
cfg.set("Informations.ID", dc.getID()); cfg.set("Informations.ID", dc.getID());
cfg.set("Informations.Owner.PlayerUUID", uuid.toString()); cfg.set("Informations.Owner.PlayerUUID", uuid.toString());
cfg.set("Informations.Owner.Actualname", Objects.requireNonNull(Bukkit.getOfflinePlayer(uuid)).getName()); cfg.set("Informations.Owner.Actualname", Objects.requireNonNull(PlayerWrapper.getOfflinePlayer(uuid)).getName());
cfg.set("Informations.template_key", template.getName()); cfg.set("Informations.template_key", template.getName());
cfg.set("Settings.TNTDamage", false); cfg.set("Settings.TNTDamage", false);
cfg.set("Settings.Fire", false); cfg.set("Settings.Fire", false);
@ -370,7 +370,7 @@ public class WorldConfig {
public HashMap<UUID, String> getMembersWithNames() { public HashMap<UUID, String> getMembersWithNames() {
HashMap<UUID, String> map = new HashMap<>(); HashMap<UUID, String> map = new HashMap<>();
for (UUID uuid : permissions.keySet()) { for (UUID uuid : permissions.keySet()) {
OfflinePlayer op = Bukkit.getOfflinePlayer(uuid); OfflinePlayer op = PlayerWrapper.getOfflinePlayer(uuid);
if (op == null || op.getName() == null) { if (op == null || op.getName() == null) {
if (PluginConfig.contactAuth()) { if (PluginConfig.contactAuth()) {
try { try {

View File

@ -22,7 +22,7 @@ public class PlayerOptionsGUI extends OrcInventory {
public PlayerOptionsGUI(Player loader, String otherPlayer, UUID other) { public PlayerOptionsGUI(Player loader, String otherPlayer, UUID other) {
super(GuiConfig.getTitle(GuiConfig.getConfig(), "options.player").replace("%player", otherPlayer), GuiConfig.getRows("options.player"), GuiConfig.isFill("options.player")); super(GuiConfig.getTitle(GuiConfig.getConfig(), "options.player").replace("%player", otherPlayer), GuiConfig.getRows("options.player"), GuiConfig.isFill("options.player"));
WorldPlayer wp = new WorldPlayer(Bukkit.getOfflinePlayer(other), loader.getWorld().getName()); WorldPlayer wp = new WorldPlayer(PlayerWrapper.getOfflinePlayer(other), loader.getWorld().getName());
loadItem("build", "/ws togglebuild " + otherPlayer, new BuildStatus(wp)); loadItem("build", "/ws togglebuild " + otherPlayer, new BuildStatus(wp));
loadItem("gamemode", "/ws togglegm " + otherPlayer, new GamemodeStatus(wp)); loadItem("gamemode", "/ws togglegm " + otherPlayer, new GamemodeStatus(wp));
loadItem("teleport", "/ws toggletp " + otherPlayer, new TeleportStatus(wp)); loadItem("teleport", "/ws toggletp " + otherPlayer, new TeleportStatus(wp));

View File

@ -39,7 +39,7 @@ public class MoneyUtil {
Preconditions.checkNotNull(uuid); Preconditions.checkNotNull(uuid);
Preconditions.checkNotNull(economy); Preconditions.checkNotNull(economy);
Preconditions.checkArgument(money > 0, "Money must not be negative"); Preconditions.checkArgument(money > 0, "Money must not be negative");
OfflinePlayer op = Bukkit.getOfflinePlayer(uuid); OfflinePlayer op = PlayerWrapper.getOfflinePlayer(uuid);
Economy economy = (Economy) MoneyUtil.economy; Economy economy = (Economy) MoneyUtil.economy;
EconomyResponse response = economy.withdrawPlayer(op, money); EconomyResponse response = economy.withdrawPlayer(op, money);
if (!response.transactionSuccess()) { if (!response.transactionSuccess()) {
@ -52,7 +52,7 @@ public class MoneyUtil {
Preconditions.checkNotNull(uuid); Preconditions.checkNotNull(uuid);
Preconditions.checkNotNull(economy); Preconditions.checkNotNull(economy);
Preconditions.checkArgument(money > 0, "Money must not be negative"); Preconditions.checkArgument(money > 0, "Money must not be negative");
OfflinePlayer op = Bukkit.getOfflinePlayer(uuid); OfflinePlayer op = PlayerWrapper.getOfflinePlayer(uuid);
Economy economy = (Economy) MoneyUtil.economy; Economy economy = (Economy) MoneyUtil.economy;
return economy.getBalance(op) >= money; return economy.getBalance(op) >= money;
} }

View File

@ -0,0 +1,97 @@
package de.butzlabben.world.util;
import com.google.common.base.Preconditions;
import de.butzlabben.world.config.PluginConfig;
import de.butzlabben.world.util.database.DatabaseProvider;
import de.butzlabben.world.util.database.DatabaseUtil;
import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer;
import org.bukkit.entity.Player;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.UUID;
public class PlayerWrapper {
private static final DatabaseUtil util = DatabaseProvider.instance.util;
static {
checkTables();
}
public static void checkTables() {
if (!util.isConnectionAvailable())
return;
String uuidTableName = PluginConfig.getUUIDTableName();
try {
PreparedStatement ps = util.prepareStatement("CREATE TABLE IF NOT EXISTS " + uuidTableName +
" ( `uuid` VARCHAR(36) NOT NULL , `name` VARCHAR(36) NOT NULL , " +
" PRIMARY KEY (`uuid`))");
util.executeUpdate(ps);
} catch (SQLException e) {
e.printStackTrace();
}
}
public static void updateDatabase(Player player) {
if (!util.isConnectionAvailable())
return;
Preconditions.checkNotNull(player);
String playersTableName = PluginConfig.getPlayersTableName();
try {
PreparedStatement ps = util.prepareStatement("REPLACE INTO " + playersTableName +
" (uuid, name) VALUES (?, ?)");
ps.setString(1, player.getUniqueId().toString());
ps.setString(2, player.getName());
util.executeUpdate(ps);
} catch (SQLException e) {
e.printStackTrace();
}
}
private static UUID getUUID(String name) {
String uuidTableName = PluginConfig.getUUIDTableName();
try {
PreparedStatement ps = util.prepareStatement("SELECT * FROM " + uuidTableName + " WHERE player=?");
ps.setString(1, name);
ResultSet rs = util.executeQuery(ps);
if (!rs.next())
return null;
return UUID.fromString(rs.getString("uuid"));
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
public static OfflinePlayer getOfflinePlayer(String name) {
Player player = Bukkit.getPlayer(name);
if(player != null)
return PlayerWrapper.getOfflinePlayer(player.getUniqueId());
UUID uuid = PlayerWrapper.getUUID(name);
if(uuid != null)
return PlayerWrapper.getOfflinePlayer(uuid);
return PlayerWrapper.getOfflinePlayer(name);
}
public static OfflinePlayer getOfflinePlayer(UUID uuid) {
return PlayerWrapper.getOfflinePlayer(uuid);
}
}

View File

@ -8,6 +8,7 @@
import de.butzlabben.world.event.WorldLoadEvent; import de.butzlabben.world.event.WorldLoadEvent;
import de.butzlabben.world.event.WorldUnloadEvent; import de.butzlabben.world.event.WorldUnloadEvent;
import de.butzlabben.world.util.PlayerPositions; import de.butzlabben.world.util.PlayerPositions;
import de.butzlabben.world.util.PlayerWrapper;
import de.butzlabben.world.util.VersionUtil; import de.butzlabben.world.util.VersionUtil;
import org.apache.commons.io.FileUtils; import org.apache.commons.io.FileUtils;
import org.bukkit.*; import org.bukkit.*;
@ -361,7 +362,7 @@ public class SystemWorld {
} }
}, 10L); }, 10L);
OfflinePlayer owner = Bukkit.getOfflinePlayer(WorldConfig.getWorldConfig(worldname).getOwner()); OfflinePlayer owner = PlayerWrapper.getOfflinePlayer(WorldConfig.getWorldConfig(worldname).getOwner());
DependenceConfig dc = new DependenceConfig(owner); DependenceConfig dc = new DependenceConfig(owner);
dc.setLastLoaded(); dc.setLastLoaded();
} }
@ -422,7 +423,7 @@ public class SystemWorld {
p.setGameMode(GameMode.CREATIVE); p.setGameMode(GameMode.CREATIVE);
} }
OfflinePlayer owner = Bukkit.getOfflinePlayer(WorldConfig.getWorldConfig(worldname).getOwner()); OfflinePlayer owner = PlayerWrapper.getOfflinePlayer(WorldConfig.getWorldConfig(worldname).getOwner());
DependenceConfig dc = new DependenceConfig(owner); DependenceConfig dc = new DependenceConfig(owner);
dc.setLastLoaded(); dc.setLastLoaded();
} }