From b99520b49142703638253791aca33ddc9cbc9f57 Mon Sep 17 00:00:00 2001 From: spacerocket62 Date: Thu, 29 Jul 2021 15:50:04 -0700 Subject: [PATCH] FS-177: Part 1: This completely revamps the admin and player saving system, as well as removed multiple methods which involved getting data via player name. Most of the data is now UUID based and only thing that should be IP based is imposter system --- pom.xml | 18 +- .../totalfreedommod/ChatManager.java | 2 +- .../totalfreedom/totalfreedommod/Muter.java | 8 +- .../totalfreedommod/admin/ActivityLog.java | 32 +- .../admin/ActivityLogEntry.java | 45 +- .../totalfreedommod/admin/Admin.java | 33 +- .../totalfreedommod/admin/AdminList.java | 49 +- .../totalfreedommod/banning/Ban.java | 2 + .../bridge/BukkitTelnetBridge.java | 3 +- .../totalfreedommod/command/Command_ban.java | 2 +- .../totalfreedommod/command/Command_doom.java | 2 +- .../command/Command_linkdiscord.java | 3 +- .../command/Command_manageshop.java | 10 +- .../command/Command_mbconfig.java | 14 +- .../command/Command_myadmin.java | 4 +- .../command/Command_notes.java | 4 +- .../command/Command_saconfig.java | 20 +- .../command/Command_settotalvotes.java | 3 +- .../totalfreedommod/command/Command_tban.java | 2 +- .../command/Command_tempban.java | 2 +- .../command/Command_unban.java | 2 +- .../command/Command_unlinkdiscord.java | 3 +- .../discord/PrivateMessageListener.java | 2 +- .../totalfreedommod/player/FPlayer.java | 424 ++++++------------ .../totalfreedommod/player/PlayerData.java | 30 +- .../totalfreedommod/player/PlayerList.java | 122 +++-- .../totalfreedommod/rank/RankManager.java | 2 +- .../totalfreedommod/shop/Votifier.java | 2 +- .../totalfreedommod/sql/SQLite.java | 77 +++- .../totalfreedommod/util/FUtil.java | 66 ++- .../world/WorldRestrictions.java | 2 +- 31 files changed, 507 insertions(+), 483 deletions(-) diff --git a/pom.xml b/pom.xml index 9687c927..56ddf826 100644 --- a/pom.xml +++ b/pom.xml @@ -14,6 +14,8 @@ ${project.name} ${maven.build.timestamp} MM/dd/yyyy HH:mm + + master-4ec00d977c-1 TotalFreedomMod @@ -242,7 +244,7 @@ com.github.AtlasMediaGroup TFGuilds - master-SNAPSHOT + ${tfguilds.version} provided @@ -270,6 +272,18 @@ org.eclipse.sisu.inject 0.3.4 + + org.apache.httpcomponents.client5 + httpclient5 + 5.0.1 + compile + + + org.json + json + 20210307 + compile + @@ -446,6 +460,8 @@ com.github.speedxx:Mojangson org.bstats:bstats-bukkit org.jetbrains:annotations + org.apache.httpcomponents.client5:httpclient5 + org.apache.httpcomponents.core5:httpcore5 diff --git a/src/main/java/me/totalfreedom/totalfreedommod/ChatManager.java b/src/main/java/me/totalfreedom/totalfreedommod/ChatManager.java index eebefc44..48cc489d 100644 --- a/src/main/java/me/totalfreedom/totalfreedommod/ChatManager.java +++ b/src/main/java/me/totalfreedom/totalfreedommod/ChatManager.java @@ -126,7 +126,7 @@ public class ChatManager extends FreedomService boolean mentionEveryone = ChatColor.stripColor(message).toLowerCase().contains("@everyone") && plugin.al.isAdmin(player); for (Player p : server.getOnlinePlayers()) { - if (ChatColor.stripColor(message).toLowerCase().contains("@" + p.getName().toLowerCase()) || mentionEveryone) + if ((ChatColor.stripColor(message).toLowerCase().contains("@" + p.getName().toLowerCase()) || mentionEveryone) && !event.isCancelled()) { p.playSound(p.getLocation(), Sound.BLOCK_NOTE_BLOCK_PLING, SoundCategory.MASTER, 1337F, 0.9F); } diff --git a/src/main/java/me/totalfreedom/totalfreedommod/Muter.java b/src/main/java/me/totalfreedom/totalfreedommod/Muter.java index c6b336ae..dd2ec963 100644 --- a/src/main/java/me/totalfreedom/totalfreedommod/Muter.java +++ b/src/main/java/me/totalfreedom/totalfreedommod/Muter.java @@ -2,6 +2,8 @@ package me.totalfreedom.totalfreedommod; import java.util.ArrayList; import java.util.List; +import java.util.UUID; + import me.totalfreedom.totalfreedommod.config.ConfigEntry; import me.totalfreedom.totalfreedommod.player.FPlayer; import me.totalfreedom.totalfreedommod.util.FLog; @@ -18,7 +20,7 @@ import org.bukkit.event.player.PlayerJoinEvent; public class Muter extends FreedomService { - public final List MUTED_PLAYERS = new ArrayList<>(); + public final List MUTED_PLAYERS = new ArrayList<>(); @Override public void onStart() @@ -45,7 +47,7 @@ public class Muter extends FreedomService if (plugin.al.isAdminSync(player)) { fPlayer.setMuted(false); - MUTED_PLAYERS.remove(player.getName()); + MUTED_PLAYERS.remove(player.getUniqueId()); return; } @@ -105,7 +107,7 @@ public class Muter extends FreedomService Player player = event.getPlayer(); FPlayer playerdata = plugin.pl.getPlayer(player); - if (MUTED_PLAYERS.contains(player.getName())) + if (MUTED_PLAYERS.contains(player.getUniqueId())) { playerdata.setMuted(true); } diff --git a/src/main/java/me/totalfreedom/totalfreedommod/admin/ActivityLog.java b/src/main/java/me/totalfreedom/totalfreedommod/admin/ActivityLog.java index 8c29fdac..0989a674 100644 --- a/src/main/java/me/totalfreedom/totalfreedommod/admin/ActivityLog.java +++ b/src/main/java/me/totalfreedom/totalfreedommod/admin/ActivityLog.java @@ -2,6 +2,8 @@ package me.totalfreedom.totalfreedommod.admin; import com.google.common.collect.Maps; import java.util.Map; +import java.util.UUID; + import me.totalfreedom.totalfreedommod.FreedomService; import me.totalfreedom.totalfreedommod.config.YamlConfig; import me.totalfreedom.totalfreedommod.util.FLog; @@ -14,13 +16,14 @@ import org.bukkit.event.EventPriority; import org.bukkit.event.player.PlayerJoinEvent; import org.bukkit.event.player.PlayerQuitEvent; +//TODO: convert to uuids public class ActivityLog extends FreedomService { public static final String FILENAME = "activitylog.yml"; - private final Map allActivityLogs = Maps.newHashMap(); - private final Map nameTable = Maps.newHashMap(); + private final Map allActivityLogs = Maps.newHashMap(); + private final Map activityLogs = Maps.newHashMap(); private final Map ipTable = Maps.newHashMap(); private final YamlConfig config; @@ -52,7 +55,7 @@ public class ActivityLog extends FreedomService config.load(); allActivityLogs.clear(); - nameTable.clear(); + activityLogs.clear(); ipTable.clear(); for (String key : config.getKeys(false)) { @@ -72,7 +75,7 @@ public class ActivityLog extends FreedomService continue; } - allActivityLogs.put(key, activityLogEntry); + allActivityLogs.put(UUID.fromString(key), activityLogEntry); } updateTables(); @@ -102,12 +105,12 @@ public class ActivityLog extends FreedomService return getActivityLog((Player)sender); } - return getEntryByName(sender.getName()); + return getEntryByUUID(FUtil.getUUIDFromName(sender.getName())); } public ActivityLogEntry getActivityLog(Player player) { - ActivityLogEntry activityLog = getEntryByName(player.getName()); + ActivityLogEntry activityLog = getEntryByUUID(player.getUniqueId()); if (activityLog == null) { String ip = FUtil.getIp(player); @@ -115,14 +118,13 @@ public class ActivityLog extends FreedomService if (activityLog != null) { // Set the new username - activityLog.setName(player.getName()); save(); updateTables(); } else { activityLog = new ActivityLogEntry(player); - allActivityLogs.put(activityLog.getConfigKey(), activityLog); + allActivityLogs.put(activityLog.getUUID(), activityLog); updateTables(); activityLog.saveTo(config.createSection(activityLog.getConfigKey())); @@ -139,9 +141,9 @@ public class ActivityLog extends FreedomService return activityLog; } - public ActivityLogEntry getEntryByName(String name) + public ActivityLogEntry getEntryByUUID(UUID uuid) { - return nameTable.get(name.toLowerCase()); + return activityLogs.get(uuid); } public ActivityLogEntry getEntryByIp(String ip) @@ -151,12 +153,12 @@ public class ActivityLog extends FreedomService public void updateTables() { - nameTable.clear(); + activityLogs.clear(); ipTable.clear(); for (ActivityLogEntry activityLog : allActivityLogs.values()) { - nameTable.put(activityLog.getName().toLowerCase(), activityLog); + activityLogs.put(activityLog.getUUID(), activityLog); for (String ip : activityLog.getIps()) { @@ -190,14 +192,14 @@ public class ActivityLog extends FreedomService } } - public Map getAllActivityLogs() + public Map getAllActivityLogs() { return allActivityLogs; } - public Map getNameTable() + public Map getActivityLogs() { - return nameTable; + return activityLogs; } public Map getIpTable() diff --git a/src/main/java/me/totalfreedom/totalfreedommod/admin/ActivityLogEntry.java b/src/main/java/me/totalfreedom/totalfreedommod/admin/ActivityLogEntry.java index eb056fcb..218931ac 100644 --- a/src/main/java/me/totalfreedom/totalfreedommod/admin/ActivityLogEntry.java +++ b/src/main/java/me/totalfreedom/totalfreedommod/admin/ActivityLogEntry.java @@ -4,6 +4,8 @@ import com.google.common.collect.Lists; import java.time.Instant; import java.util.Date; import java.util.List; +import java.util.UUID; + import me.totalfreedom.totalfreedommod.config.IConfig; import me.totalfreedom.totalfreedommod.util.FUtil; import org.apache.commons.lang.Validate; @@ -18,17 +20,20 @@ public class ActivityLogEntry implements IConfig private final List timestamps = Lists.newArrayList(); private final List durations = Lists.newArrayList(); private String configKey; - private String name; public ActivityLogEntry(Player player) { - this.configKey = player.getName().toLowerCase(); - this.name = player.getName(); + this.configKey = player.getUniqueId().toString(); } - public ActivityLogEntry(String configKey) + public ActivityLogEntry(UUID uuid) { - this.configKey = configKey; + this.configKey = uuid.toString(); + } + + public ActivityLogEntry(String uuid) + { + this.configKey = uuid; } public static String getFILENAME() @@ -39,13 +44,11 @@ public class ActivityLogEntry implements IConfig public void loadFrom(Player player) { configKey = player.getName().toLowerCase(); - name = player.getName(); } @Override public void loadFrom(ConfigurationSection cs) { - name = cs.getString("username", configKey); ips.clear(); ips.addAll(cs.getStringList("ips")); timestamps.clear(); @@ -57,8 +60,8 @@ public class ActivityLogEntry implements IConfig @Override public void saveTo(ConfigurationSection cs) { - Validate.isTrue(isValid(), "Could not save activity entry: " + name + ". Entry not valid!"); - cs.set("username", name); + Validate.isTrue(isValid(), "Could not save activity entry: " + getUUID() + ". Entry not valid!"); + cs.set("uuid", getUUID().toString()); cs.set("ips", Lists.newArrayList(ips)); cs.set("timestamps", Lists.newArrayList(timestamps)); cs.set("durations", Lists.newArrayList(durations)); @@ -73,11 +76,11 @@ public class ActivityLogEntry implements IConfig public void addLogout() { // Fix of Array index out of bonds issue: FS-131 - String lastLoginString; + String lastLoginString = ""; if(timestamps.size() > 1) { lastLoginString = timestamps.get(timestamps.size() - 1); - }else + } else if (timestamps.size() == 1) { lastLoginString = timestamps.get(0); } @@ -136,7 +139,7 @@ public class ActivityLogEntry implements IConfig public boolean isValid() { return configKey != null - && name != null; + && getUUID() != null; } public String getConfigKey() @@ -149,16 +152,6 @@ public class ActivityLogEntry implements IConfig this.configKey = configKey; } - public String getName() - { - return name; - } - - public void setName(String name) - { - this.name = name; - } - public List getIps() { return ips; @@ -173,4 +166,12 @@ public class ActivityLogEntry implements IConfig { return durations; } + + public UUID getUUID() { + return UUID.fromString(configKey); + } + + public String getName() { + return FUtil.getNameFromUUID(getUUID()); + } } diff --git a/src/main/java/me/totalfreedom/totalfreedommod/admin/Admin.java b/src/main/java/me/totalfreedom/totalfreedommod/admin/Admin.java index 17920853..7d49e470 100644 --- a/src/main/java/me/totalfreedom/totalfreedommod/admin/Admin.java +++ b/src/main/java/me/totalfreedom/totalfreedommod/admin/Admin.java @@ -2,11 +2,8 @@ package me.totalfreedom.totalfreedommod.admin; import java.sql.ResultSet; import java.sql.SQLException; -import java.util.ArrayList; -import java.util.Date; -import java.util.HashMap; -import java.util.List; -import java.util.Map; +import java.util.*; + import me.totalfreedom.totalfreedommod.LogViewer.LogsRegistrationMode; import me.totalfreedom.totalfreedommod.TotalFreedomMod; import me.totalfreedom.totalfreedommod.rank.Rank; @@ -21,7 +18,7 @@ public class Admin private final List ips = new ArrayList<>(); - private String name; + private UUID uuid; private boolean active = true; private Rank rank = Rank.ADMIN; private Date lastLogin = new Date(); @@ -32,7 +29,7 @@ public class Admin public Admin(Player player) { - this.name = player.getName(); + this.uuid = player.getUniqueId(); this.ips.add(FUtil.getIp(player)); } @@ -40,7 +37,7 @@ public class Admin { try { - this.name = resultSet.getString("username"); + this.uuid = UUID.fromString(resultSet.getString("uuid")); this.active = resultSet.getBoolean("active"); this.rank = Rank.findRank(resultSet.getString("rank")); this.ips.clear(); @@ -62,7 +59,7 @@ public class Admin { final StringBuilder output = new StringBuilder(); - output.append("Admin: ").append(name).append("\n") + output.append("Admin: ").append(uuid.toString()).append("\n") .append("- IPs: ").append(StringUtils.join(ips, ", ")).append("\n") .append("- Last Login: ").append(FUtil.dateToString(lastLogin)).append("\n") .append("- Rank: ").append(rank.getName()).append("\n") @@ -78,7 +75,7 @@ public class Admin { Map map = new HashMap() {{ - put("username", name); + put("uuid", uuid.toString()); put("active", active); put("rank", rank.toString()); put("ips", FUtil.listToString(ips)); @@ -120,20 +117,20 @@ public class Admin public boolean isValid() { - return name != null + return uuid != null && rank != null && !ips.isEmpty() && lastLogin != null; } - public String getName() + public UUID getUniqueId() { - return name; + return uuid; } - public void setName(String name) + public void setUniqueId(UUID uuid) { - this.name = name; + this.uuid = uuid; } public boolean isActive() @@ -168,6 +165,7 @@ public class Admin } } + public Rank getRank() { return rank; @@ -232,4 +230,9 @@ public class Admin { this.pteroID = pteroID; } + + public String getName() + { + return FUtil.getNameFromUUID(uuid); + } } \ No newline at end of file diff --git a/src/main/java/me/totalfreedom/totalfreedommod/admin/AdminList.java b/src/main/java/me/totalfreedom/totalfreedommod/admin/AdminList.java index 8cbb49b8..58fdfcb5 100644 --- a/src/main/java/me/totalfreedom/totalfreedommod/admin/AdminList.java +++ b/src/main/java/me/totalfreedom/totalfreedommod/admin/AdminList.java @@ -4,11 +4,7 @@ import com.google.common.collect.Maps; import com.google.common.collect.Sets; import java.sql.ResultSet; import java.sql.SQLException; -import java.util.ArrayList; -import java.util.Date; -import java.util.List; -import java.util.Map; -import java.util.Set; +import java.util.*; import java.util.concurrent.TimeUnit; import me.totalfreedom.totalfreedommod.FreedomService; import me.totalfreedom.totalfreedommod.config.ConfigEntry; @@ -26,7 +22,7 @@ public class AdminList extends FreedomService private final Set allAdmins = Sets.newHashSet(); // Includes disabled admins // Only active admins below private final Set activeAdmins = Sets.newHashSet(); - private final Map nameTable = Maps.newHashMap(); + private final Map adminTable = Maps.newHashMap(); private final Map ipTable = Maps.newHashMap(); public static List getVanished() @@ -66,7 +62,7 @@ public class AdminList extends FreedomService } updateTables(); - FLog.info("Loaded " + allAdmins.size() + " admins (" + nameTable.size() + " active, " + ipTable.size() + " IPs)"); + FLog.info("Loaded " + allAdmins.size() + " admins (" + adminTable.size() + " active, " + ipTable.size() + " IPs)"); } public void messageAllAdmins(String message) @@ -97,14 +93,16 @@ public class AdminList extends FreedomService return isAdmin(sender); } + @Deprecated public List getActiveAdminNames() { - List names = new ArrayList(); + /*List names = new ArrayList(); for (Admin admin : activeAdmins) { names.add(admin.getName()); } - return names; + return names;*/ + throw new UnsupportedOperationException("Admin no longer stores names in the object, may be changed in a future update"); } public boolean isAdmin(CommandSender sender) @@ -142,21 +140,21 @@ public class AdminList extends FreedomService return admin.getRank().ordinal() >= Rank.SENIOR_ADMIN.ordinal(); } - public Admin getAdmin(CommandSender sender) + public Admin getAdmin(CommandSender sender) //TODO: Fix { if (sender instanceof Player) { return getAdmin((Player)sender); } - return getEntryByName(sender.getName()); + return getEntryByUUID(FUtil.getUUIDFromName(sender.getName())); } public Admin getAdmin(Player player) { // Find admin String ip = FUtil.getIp(player); - Admin admin = getEntryByName(player.getName()); + Admin admin = getEntryByUUID(player.getUniqueId()); // Admin by name if (admin != null) @@ -181,18 +179,16 @@ public class AdminList extends FreedomService if (admin != null) { // Set the new username - String oldName = admin.getName(); - admin.setName(player.getName()); - plugin.sql.updateAdminName(oldName, admin.getName()); + //plugin.sql.updateAdminName(oldName, admin.getName()); updateTables(); } return null; } - public Admin getEntryByName(String name) + public Admin getEntryByUUID(UUID uuid) { - return nameTable.get(name.toLowerCase()); + return adminTable.get(uuid); } public Admin getEntryByIp(String ip) @@ -200,6 +196,7 @@ public class AdminList extends FreedomService return ipTable.get(ip); } + @Deprecated // TODO: no ips maybe ? idk soontm public Admin getEntryByIpFuzzy(String needleIp) { final Admin directAdmin = getEntryByIp(needleIp); @@ -228,13 +225,12 @@ public class AdminList extends FreedomService } admin.setLastLogin(new Date()); - admin.setName(player.getName()); save(admin); } public boolean isAdminImpostor(Player player) { - return getEntryByName(player.getName()) != null && !isAdmin(player) && !isVerifiedAdmin(player); + return getEntryByUUID(player.getUniqueId()) != null && !isAdmin(player) && !isVerifiedAdmin(player); } public boolean isVerifiedAdmin(Player player) @@ -297,7 +293,7 @@ public class AdminList extends FreedomService public void updateTables() { activeAdmins.clear(); - nameTable.clear(); + adminTable.clear(); ipTable.clear(); for (Admin admin : allAdmins) @@ -308,7 +304,7 @@ public class AdminList extends FreedomService } activeAdmins.add(admin); - nameTable.put(admin.getName().toLowerCase(), admin); + adminTable.put(admin.getUniqueId(), admin); for (String ip : admin.getIps()) { @@ -318,9 +314,9 @@ public class AdminList extends FreedomService } } - public Set getAdminNames() + public Set getAdminUUIDs() { - return nameTable.keySet(); + return adminTable.keySet(); } public Set getAdminIps() @@ -332,7 +328,7 @@ public class AdminList extends FreedomService { try { - ResultSet currentSave = plugin.sql.getAdminByName(admin.getName()); + ResultSet currentSave = plugin.sql.getAdminByUUID(admin.getUniqueId()); for (Map.Entry entry : admin.toSQLStorable().entrySet()) { Object storedValue = plugin.sql.getValue(currentSave, entry.getKey(), entry.getValue()); @@ -392,11 +388,6 @@ public class AdminList extends FreedomService return activeAdmins; } - public Map getNameTable() - { - return nameTable; - } - public Map getIpTable() { return ipTable; diff --git a/src/main/java/me/totalfreedom/totalfreedommod/banning/Ban.java b/src/main/java/me/totalfreedom/totalfreedommod/banning/Ban.java index 42871f45..1c3f9c9c 100644 --- a/src/main/java/me/totalfreedom/totalfreedommod/banning/Ban.java +++ b/src/main/java/me/totalfreedom/totalfreedommod/banning/Ban.java @@ -19,6 +19,8 @@ import org.bukkit.entity.Player; public class Ban { + //TODO: fuck your usernames and getting by ips, bans will be in uuid maybe soon tm idk + public static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd 'at' HH:mm:ss z"); private final List ips = Lists.newArrayList(); private String username = null; diff --git a/src/main/java/me/totalfreedom/totalfreedommod/bridge/BukkitTelnetBridge.java b/src/main/java/me/totalfreedom/totalfreedommod/bridge/BukkitTelnetBridge.java index 479a30e1..f85aed20 100644 --- a/src/main/java/me/totalfreedom/totalfreedommod/bridge/BukkitTelnetBridge.java +++ b/src/main/java/me/totalfreedom/totalfreedommod/bridge/BukkitTelnetBridge.java @@ -12,6 +12,7 @@ import me.totalfreedom.totalfreedommod.FreedomService; import me.totalfreedom.totalfreedommod.admin.Admin; import me.totalfreedom.totalfreedommod.rank.Rank; import me.totalfreedom.totalfreedommod.util.FLog; +import me.totalfreedom.totalfreedommod.util.FUtil; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; @@ -126,7 +127,7 @@ public class BukkitTelnetBridge extends FreedomService { for (ClientSession session : telnet.appender.getSessions()) { - Admin admin = plugin.al.getEntryByName(session.getUserName().toLowerCase()); + Admin admin = plugin.al.getEntryByUUID(FUtil.getUUIDFromName(session.getUserName())); if (admin != null && !admins.contains(admin)) { admins.add(admin); diff --git a/src/main/java/me/totalfreedom/totalfreedommod/command/Command_ban.java b/src/main/java/me/totalfreedom/totalfreedommod/command/Command_ban.java index c6b38381..79c3da1f 100644 --- a/src/main/java/me/totalfreedom/totalfreedommod/command/Command_ban.java +++ b/src/main/java/me/totalfreedom/totalfreedommod/command/Command_ban.java @@ -65,7 +65,7 @@ public class Command_ban extends FreedomCommand final Player player = getPlayer(args[0]); if (player == null) { - final PlayerData entry = plugin.pl.getData(args[0]); + final PlayerData entry = plugin.pl.getData(FUtil.getUUIDFromName(args[0])); if (entry == null) { diff --git a/src/main/java/me/totalfreedom/totalfreedommod/command/Command_doom.java b/src/main/java/me/totalfreedom/totalfreedommod/command/Command_doom.java index d6711948..33704b6c 100644 --- a/src/main/java/me/totalfreedom/totalfreedommod/command/Command_doom.java +++ b/src/main/java/me/totalfreedom/totalfreedommod/command/Command_doom.java @@ -56,7 +56,7 @@ public class Command_doom extends FreedomCommand plugin.ptero.updateAccountStatus(admin); if (plugin.dc.enabled && ConfigEntry.DISCORD_ROLE_SYNC.getBoolean()) { - Discord.syncRoles(admin, plugin.pl.getData(admin.getName()).getDiscordID()); + Discord.syncRoles(admin, plugin.pl.getData(admin.getUniqueId()).getDiscordID()); } } diff --git a/src/main/java/me/totalfreedom/totalfreedommod/command/Command_linkdiscord.java b/src/main/java/me/totalfreedom/totalfreedommod/command/Command_linkdiscord.java index 40df6d93..c6f57ba8 100644 --- a/src/main/java/me/totalfreedom/totalfreedommod/command/Command_linkdiscord.java +++ b/src/main/java/me/totalfreedom/totalfreedommod/command/Command_linkdiscord.java @@ -3,6 +3,7 @@ package me.totalfreedom.totalfreedommod.command; import me.totalfreedom.totalfreedommod.discord.Discord; import me.totalfreedom.totalfreedommod.player.PlayerData; import me.totalfreedom.totalfreedommod.rank.Rank; +import me.totalfreedom.totalfreedommod.util.FUtil; import org.bukkit.ChatColor; import org.bukkit.command.Command; import org.bukkit.command.CommandSender; @@ -24,7 +25,7 @@ public class Command_linkdiscord extends FreedomCommand if (args.length > 1 && plugin.al.isAdmin(playerSender)) { - PlayerData playerData = plugin.pl.getData(args[0]); + PlayerData playerData = plugin.pl.getData(FUtil.getUUIDFromName(args[0])); if (playerData == null) { msg(PLAYER_NOT_FOUND); diff --git a/src/main/java/me/totalfreedom/totalfreedommod/command/Command_manageshop.java b/src/main/java/me/totalfreedom/totalfreedommod/command/Command_manageshop.java index 1c0a7a81..b451d128 100644 --- a/src/main/java/me/totalfreedom/totalfreedommod/command/Command_manageshop.java +++ b/src/main/java/me/totalfreedom/totalfreedommod/command/Command_manageshop.java @@ -42,7 +42,7 @@ public class Command_manageshop extends FreedomCommand int amount = Math.max(0, Math.min(1000000, Integer.parseInt(args[2]))); if (!args[3].equals("all")) { - PlayerData playerData = plugin.pl.getData(args[3]); + PlayerData playerData = plugin.pl.getData(FUtil.getUUIDFromName(args[3])); if (playerData == null) { msg(PLAYER_NOT_FOUND); @@ -83,7 +83,7 @@ public class Command_manageshop extends FreedomCommand int amount = Math.max(0, Math.min(1000000, Integer.parseInt(args[2]))); if (!args[3].equals("all")) { - PlayerData playerData = plugin.pl.getData(args[3]); + PlayerData playerData = plugin.pl.getData(FUtil.getUUIDFromName(args[3])); if (playerData == null) { msg(PLAYER_NOT_FOUND); @@ -130,7 +130,7 @@ public class Command_manageshop extends FreedomCommand try { int amount = Math.max(0, Math.min(1000000, Integer.parseInt(args[2]))); - PlayerData playerData = plugin.pl.getData(args[3]); + PlayerData playerData = plugin.pl.getData(FUtil.getUUIDFromName(args[3])); if (playerData == null) { msg(PLAYER_NOT_FOUND); @@ -180,7 +180,7 @@ public class Command_manageshop extends FreedomCommand return true; } - PlayerData playerData = plugin.pl.getData(args[3]); + PlayerData playerData = plugin.pl.getData(FUtil.getUUIDFromName(args[3])); if (playerData == null) { msg(PLAYER_NOT_FOUND); @@ -205,7 +205,7 @@ public class Command_manageshop extends FreedomCommand return true; } - PlayerData playerData = plugin.pl.getData(args[3]); + PlayerData playerData = plugin.pl.getData(FUtil.getUUIDFromName(args[3])); if (playerData == null) { msg(PLAYER_NOT_FOUND); diff --git a/src/main/java/me/totalfreedom/totalfreedommod/command/Command_mbconfig.java b/src/main/java/me/totalfreedom/totalfreedommod/command/Command_mbconfig.java index 23320e41..94204ce2 100644 --- a/src/main/java/me/totalfreedom/totalfreedommod/command/Command_mbconfig.java +++ b/src/main/java/me/totalfreedom/totalfreedommod/command/Command_mbconfig.java @@ -45,7 +45,7 @@ public class Command_mbconfig extends FreedomCommand return true; } - PlayerData data = plugin.pl.getData(sender.getName()); + PlayerData data = plugin.pl.getData(FUtil.getUUIDFromName(sender.getName())); if (!data.isMasterBuilder()) { msg("You are not a master builder!", ChatColor.RED); @@ -74,7 +74,7 @@ public class Command_mbconfig extends FreedomCommand return true; } - PlayerData data = plugin.pl.getData(sender.getName()); + PlayerData data = plugin.pl.getData(FUtil.getUUIDFromName(sender.getName())); final String targetIp = FUtil.getIp(playerSender); if (!data.isMasterBuilder()) @@ -101,14 +101,14 @@ public class Command_mbconfig extends FreedomCommand return false; } - if (plugin.pl.canManageMasterBuilders(sender.getName())) + if (plugin.pl.canManageMasterBuilders(FUtil.getUUIDFromName(sender.getName()))) { return noPerms(); } final Player player = getPlayer(args[1]); - PlayerData data = player != null ? plugin.pl.getData(player) : plugin.pl.getData(args[1]); + PlayerData data = player != null ? plugin.pl.getData(player) : plugin.pl.getData(FUtil.getUUIDFromName(args[1])); if (data == null) { @@ -157,13 +157,13 @@ public class Command_mbconfig extends FreedomCommand return false; } - if (plugin.pl.canManageMasterBuilders(sender.getName())) + if (plugin.pl.canManageMasterBuilders(FUtil.getUUIDFromName(sender.getName()))) { return noPerms(); } Player player = getPlayer(args[1]); - PlayerData data = player != null ? plugin.pl.getData(player) : plugin.pl.getData(args[1]); + PlayerData data = player != null ? plugin.pl.getData(player) : plugin.pl.getData(FUtil.getUUIDFromName(args[1])); if (data == null || !data.isMasterBuilder()) { @@ -210,7 +210,7 @@ public class Command_mbconfig extends FreedomCommand } else if (args[0].equalsIgnoreCase("clearip")) { - PlayerData data = plugin.pl.getData(sender.getName()); + PlayerData data = plugin.pl.getData(FUtil.getUUIDFromName(sender.getName())); if (data.isMasterBuilder()) { return data.getIps(); diff --git a/src/main/java/me/totalfreedom/totalfreedommod/command/Command_myadmin.java b/src/main/java/me/totalfreedom/totalfreedommod/command/Command_myadmin.java index 1c38639c..0382c8b5 100644 --- a/src/main/java/me/totalfreedom/totalfreedommod/command/Command_myadmin.java +++ b/src/main/java/me/totalfreedom/totalfreedommod/command/Command_myadmin.java @@ -170,7 +170,7 @@ public class Command_myadmin extends FreedomCommand msg("Role syncing is not enabled.", ChatColor.RED); return true; } - PlayerData playerData = plugin.pl.getData(target.getName()); + PlayerData playerData = plugin.pl.getData(target.getUniqueId()); if (playerData.getDiscordID() == null) { msg("Please run /linkdiscord first!", ChatColor.RED); @@ -248,7 +248,7 @@ public class Command_myadmin extends FreedomCommand { if (args[0].equals("-o") && args[2].equals("clearip")) { - Admin admin = plugin.al.getEntryByName(args[1]); + Admin admin = plugin.al.getEntryByUUID(FUtil.getUUIDFromName(args[1])); if (admin != null) { return admin.getIps(); diff --git a/src/main/java/me/totalfreedom/totalfreedommod/command/Command_notes.java b/src/main/java/me/totalfreedom/totalfreedommod/command/Command_notes.java index 4a4cd2ab..7ff97b73 100644 --- a/src/main/java/me/totalfreedom/totalfreedommod/command/Command_notes.java +++ b/src/main/java/me/totalfreedom/totalfreedommod/command/Command_notes.java @@ -31,7 +31,7 @@ public class Command_notes extends FreedomCommand final Player player = getPlayer(args[0]); if (player == null) { - final PlayerData entry = plugin.pl.getData(args[0]); + final PlayerData entry = plugin.pl.getData(FUtil.getUUIDFromName(args[0])); if (entry == null) { @@ -39,7 +39,7 @@ public class Command_notes extends FreedomCommand return true; } - playerData = plugin.pl.getData(entry.getName()); + playerData = entry; } else { diff --git a/src/main/java/me/totalfreedom/totalfreedommod/command/Command_saconfig.java b/src/main/java/me/totalfreedom/totalfreedommod/command/Command_saconfig.java index 0a1c06dc..83aaf008 100644 --- a/src/main/java/me/totalfreedom/totalfreedommod/command/Command_saconfig.java +++ b/src/main/java/me/totalfreedom/totalfreedommod/command/Command_saconfig.java @@ -5,6 +5,8 @@ import java.util.Arrays; import java.util.Collections; import java.util.Date; import java.util.List; +import java.util.stream.Collectors; + import me.totalfreedom.totalfreedommod.admin.Admin; import me.totalfreedom.totalfreedommod.config.ConfigEntry; import me.totalfreedom.totalfreedommod.discord.Discord; @@ -34,7 +36,7 @@ public class Command_saconfig extends FreedomCommand { case "list": { - msg("Admins: " + StringUtils.join(plugin.al.getAdminNames(), ", "), ChatColor.GOLD); + msg("Admins: " + StringUtils.join(plugin.al.getAdminUUIDs().stream().map(FUtil::getNameFromUUID).collect(Collectors.toList()), ", "), ChatColor.GOLD); return true; } @@ -45,7 +47,7 @@ public class Command_saconfig extends FreedomCommand FUtil.adminAction(sender.getName(), "Cleaning the admin list", true); plugin.al.deactivateOldEntries(true); - msg("Admins: " + StringUtils.join(plugin.al.getAdminNames(), ", "), ChatColor.GOLD); + msg("Admins: " + StringUtils.join(plugin.al.getAdminUUIDs().stream().map(FUtil::getNameFromUUID).collect(Collectors.toList()), ", "), ChatColor.GOLD); return true; } @@ -89,7 +91,7 @@ public class Command_saconfig extends FreedomCommand return true; } - Admin admin = plugin.al.getEntryByName(args[1]); + Admin admin = plugin.al.getEntryByUUID(FUtil.getUUIDFromName(args[1])); if (admin == null) { msg("Unknown admin: " + args[1]); @@ -109,7 +111,7 @@ public class Command_saconfig extends FreedomCommand if (plugin.dc.enabled && ConfigEntry.DISCORD_ROLE_SYNC.getBoolean()) { - Discord.syncRoles(admin, plugin.pl.getData(admin.getName()).getDiscordID()); + Discord.syncRoles(admin, plugin.pl.getData(admin.getUniqueId()).getDiscordID()); } plugin.ptero.updateAccountStatus(admin); @@ -127,7 +129,7 @@ public class Command_saconfig extends FreedomCommand checkRank(Rank.ADMIN); - Admin admin = plugin.al.getEntryByName(args[1]); + Admin admin = plugin.al.getEntryByUUID(FUtil.getUUIDFromName(args[1])); if (admin == null) { @@ -206,12 +208,12 @@ public class Command_saconfig extends FreedomCommand { FUtil.adminAction(sender.getName(), "Re-adding " + player.getName() + " to the admin list", true); - String oldName = admin.getName(); + /*String oldName = admin.getName(); if (!oldName.equals(player.getName())) { admin.setName(player.getName()); plugin.sql.updateAdminName(oldName, admin.getName()); - } + }*/ admin.addIp(FUtil.getIp(player)); admin.setActive(true); @@ -259,7 +261,7 @@ public class Command_saconfig extends FreedomCommand checkRank(Rank.ADMIN); Player player = getPlayer(args[1]); - Admin admin = player != null ? plugin.al.getAdmin(player) : plugin.al.getEntryByName(args[1]); + Admin admin = player != null ? plugin.al.getAdmin(player) : plugin.al.getEntryByUUID(FUtil.getUUIDFromName(args[1])); if (admin == null) { @@ -280,7 +282,7 @@ public class Command_saconfig extends FreedomCommand if (plugin.dc.enabled && ConfigEntry.DISCORD_ROLE_SYNC.getBoolean()) { - Discord.syncRoles(admin, plugin.pl.getData(admin.getName()).getDiscordID()); + Discord.syncRoles(admin, plugin.pl.getData(admin.getUniqueId()).getDiscordID()); } plugin.ptero.updateAccountStatus(admin); diff --git a/src/main/java/me/totalfreedom/totalfreedommod/command/Command_settotalvotes.java b/src/main/java/me/totalfreedom/totalfreedommod/command/Command_settotalvotes.java index 3c6de7be..ce7e52ac 100644 --- a/src/main/java/me/totalfreedom/totalfreedommod/command/Command_settotalvotes.java +++ b/src/main/java/me/totalfreedom/totalfreedommod/command/Command_settotalvotes.java @@ -3,6 +3,7 @@ package me.totalfreedom.totalfreedommod.command; import me.totalfreedom.totalfreedommod.config.ConfigEntry; import me.totalfreedom.totalfreedommod.player.PlayerData; import me.totalfreedom.totalfreedommod.rank.Rank; +import me.totalfreedom.totalfreedommod.util.FUtil; import org.bukkit.ChatColor; import org.bukkit.command.Command; import org.bukkit.command.CommandSender; @@ -38,7 +39,7 @@ public class Command_settotalvotes extends FreedomCommand return true; } - PlayerData playerData = plugin.pl.getData(args[1]); + PlayerData playerData = plugin.pl.getData(FUtil.getUUIDFromName(args[1])); if (playerData == null) { diff --git a/src/main/java/me/totalfreedom/totalfreedommod/command/Command_tban.java b/src/main/java/me/totalfreedom/totalfreedommod/command/Command_tban.java index c4c4c62f..4bb9bb4d 100644 --- a/src/main/java/me/totalfreedom/totalfreedommod/command/Command_tban.java +++ b/src/main/java/me/totalfreedom/totalfreedommod/command/Command_tban.java @@ -46,7 +46,7 @@ public class Command_tban extends FreedomCommand final PlayerData entry; if (player == null) { - entry = plugin.pl.getData(args[0]); + entry = plugin.pl.getData(FUtil.getUUIDFromName(args[0])); if (entry == null) { diff --git a/src/main/java/me/totalfreedom/totalfreedommod/command/Command_tempban.java b/src/main/java/me/totalfreedom/totalfreedommod/command/Command_tempban.java index dfc412ce..741d15c4 100644 --- a/src/main/java/me/totalfreedom/totalfreedommod/command/Command_tempban.java +++ b/src/main/java/me/totalfreedom/totalfreedommod/command/Command_tempban.java @@ -53,7 +53,7 @@ public class Command_tempban extends FreedomCommand final PlayerData entry; if (player == null) { - entry = plugin.pl.getData(args[0]); + entry = plugin.pl.getData(FUtil.getUUIDFromName(args[0])); if (entry == null) { diff --git a/src/main/java/me/totalfreedom/totalfreedommod/command/Command_unban.java b/src/main/java/me/totalfreedom/totalfreedommod/command/Command_unban.java index b6ca9452..dae8b031 100644 --- a/src/main/java/me/totalfreedom/totalfreedommod/command/Command_unban.java +++ b/src/main/java/me/totalfreedom/totalfreedommod/command/Command_unban.java @@ -22,7 +22,7 @@ public class Command_unban extends FreedomCommand if (args.length > 0) { String username; - final PlayerData entry = plugin.pl.getData(args[0]); + final PlayerData entry = plugin.pl.getData(FUtil.getUUIDFromName(args[0])); if (entry == null) { diff --git a/src/main/java/me/totalfreedom/totalfreedommod/command/Command_unlinkdiscord.java b/src/main/java/me/totalfreedom/totalfreedommod/command/Command_unlinkdiscord.java index 922db3a4..3b15ae08 100644 --- a/src/main/java/me/totalfreedom/totalfreedommod/command/Command_unlinkdiscord.java +++ b/src/main/java/me/totalfreedom/totalfreedommod/command/Command_unlinkdiscord.java @@ -2,6 +2,7 @@ package me.totalfreedom.totalfreedommod.command; import me.totalfreedom.totalfreedommod.player.PlayerData; import me.totalfreedom.totalfreedommod.rank.Rank; +import me.totalfreedom.totalfreedommod.util.FUtil; import org.bukkit.ChatColor; import org.bukkit.command.Command; import org.bukkit.command.CommandSender; @@ -23,7 +24,7 @@ public class Command_unlinkdiscord extends FreedomCommand if (args.length != 0 && plugin.al.isAdmin(playerSender)) { - PlayerData playerData = plugin.pl.getData(args[0]); + PlayerData playerData = plugin.pl.getData(FUtil.getUUIDFromName(args[0])); if (playerData == null) { msg(PLAYER_NOT_FOUND); diff --git a/src/main/java/me/totalfreedom/totalfreedommod/discord/PrivateMessageListener.java b/src/main/java/me/totalfreedom/totalfreedommod/discord/PrivateMessageListener.java index aee47a51..af99f261 100644 --- a/src/main/java/me/totalfreedom/totalfreedommod/discord/PrivateMessageListener.java +++ b/src/main/java/me/totalfreedom/totalfreedommod/discord/PrivateMessageListener.java @@ -24,7 +24,7 @@ public class PrivateMessageListener extends ListenerAdapter player.setDiscordID(event.getMessage().getAuthor().getId()); player.setVerification(true); - Admin admin = TotalFreedomMod.getPlugin().al.getEntryByName(name); + Admin admin = TotalFreedomMod.getPlugin().al.getEntryByUUID(player.getUniqueId()); if (admin != null) { Discord.syncRoles(admin, player.getDiscordID()); diff --git a/src/main/java/me/totalfreedom/totalfreedommod/player/FPlayer.java b/src/main/java/me/totalfreedom/totalfreedommod/player/FPlayer.java index 03e0c075..bea8e490 100644 --- a/src/main/java/me/totalfreedom/totalfreedommod/player/FPlayer.java +++ b/src/main/java/me/totalfreedom/totalfreedommod/player/FPlayer.java @@ -16,23 +16,22 @@ import org.bukkit.scheduler.BukkitTask; import java.util.ArrayList; import java.util.List; +import java.util.UUID; -public class FPlayer -{ +public class FPlayer { public static final long AUTO_PURGE_TICKS = 5L * 60L * 20L; private final TotalFreedomMod plugin; - private final String name; - - private final String ip; + private final UUID uuid; // private final FreezeData freezeData = new FreezeData(this); private final CageData cageData = new CageData(this); private final List mobThrowerQueue = new ArrayList<>(); private Player player; + private PlayerData playerData; // private BukkitTask unmuteTask; private double fuckoffRadius = 0; @@ -71,376 +70,295 @@ public class FPlayer private boolean invSee = false; - public FPlayer(TotalFreedomMod plugin, Player player) - { - this(plugin, player.getName(), FUtil.getIp(player)); + + public FPlayer(TotalFreedomMod plugin, Player player) { + this(plugin, player.getUniqueId()); } - private FPlayer(TotalFreedomMod plugin, String name, String ip) - { + private FPlayer(TotalFreedomMod plugin, UUID uuid) { this.plugin = plugin; - this.name = name; - this.ip = ip; + this.uuid = uuid; } - public static long getAutoPurgeTicks() - { + public static long getAutoPurgeTicks() { return AUTO_PURGE_TICKS; } - public Player getPlayer() - { - if (player != null && !player.isOnline()) - { - player = null; - } - - if (player == null) - { - for (Player onlinePlayer : Bukkit.getOnlinePlayers()) - { - if (FUtil.getIp(onlinePlayer).equals(ip)) - { - player = onlinePlayer; - break; + public Player getPlayer() { + if (this.player == null) { + for (Player onlinePlayer : Bukkit.getOnlinePlayers()) { + if (onlinePlayer.getUniqueId().equals(this.uuid)) { + this.player = onlinePlayer; } } } - return player; + return this.player; } - public void setPlayer(Player player) - { + public void setPlayer(Player player) { this.player = player; } - public boolean isOrbiting() - { + public boolean isOrbiting() { return isOrbiting; } - public void setOrbiting(boolean orbiting) - { + public void setOrbiting(boolean orbiting) { isOrbiting = orbiting; } - public void startOrbiting(double strength) - { + public void startOrbiting(double strength) { this.isOrbiting = true; this.orbitStrength = strength; } - public void stopOrbiting() - { + public void stopOrbiting() { this.isOrbiting = false; } - public double orbitStrength() - { + public double orbitStrength() { return orbitStrength; } - public boolean isFuckOff() - { + public boolean isFuckOff() { return fuckoffRadius > 0; } - public void setFuckoff(double radius) - { + public void setFuckoff(double radius) { this.fuckoffRadius = radius; } - public void disableFuckoff() - { + public void disableFuckoff() { this.fuckoffRadius = 0; } - public void resetMsgCount() - { + public void resetMsgCount() { this.messageCount = 0; } - public int incrementAndGetMsgCount() - { + public int incrementAndGetMsgCount() { return this.messageCount++; } - public int incrementAndGetBlockDestroyCount() - { + public int incrementAndGetBlockDestroyCount() { return this.totalBlockDestroy++; } - public void resetBlockDestroyCount() - { + public void resetBlockDestroyCount() { this.totalBlockDestroy = 0; } - public int incrementAndGetBlockPlaceCount() - { + public int incrementAndGetBlockPlaceCount() { return this.totalBlockPlace++; } - public void resetBlockPlaceCount() - { + public void resetBlockPlaceCount() { this.totalBlockPlace = 0; } - public int incrementAndGetFreecamDestroyCount() - { + public int incrementAndGetFreecamDestroyCount() { return this.freecamDestroyCount++; } - public void resetFreecamDestroyCount() - { + public void resetFreecamDestroyCount() { this.freecamDestroyCount = 0; } - public int incrementAndGetFreecamPlaceCount() - { + public int incrementAndGetFreecamPlaceCount() { return this.freecamPlaceCount++; } - public void resetFreecamPlaceCount() - { + public void resetFreecamPlaceCount() { this.freecamPlaceCount = 0; } - public void enableMobThrower(EntityType mobThrowerCreature, double mobThrowerSpeed) - { + public void enableMobThrower(EntityType mobThrowerCreature, double mobThrowerSpeed) { this.mobThrowerEnabled = true; this.mobThrowerEntity = mobThrowerCreature; this.mobThrowerSpeed = mobThrowerSpeed; } - public void disableMobThrower() - { + public void disableMobThrower() { this.mobThrowerEnabled = false; } - public EntityType mobThrowerCreature() - { + public EntityType mobThrowerCreature() { return this.mobThrowerEntity; } - public double mobThrowerSpeed() - { + public double mobThrowerSpeed() { return this.mobThrowerSpeed; } - public boolean mobThrowerEnabled() - { + public boolean mobThrowerEnabled() { return this.mobThrowerEnabled; } - public void enqueueMob(LivingEntity mob) - { + public void enqueueMob(LivingEntity mob) { mobThrowerQueue.add(mob); - if (mobThrowerQueue.size() > 4) - { + if (mobThrowerQueue.size() > 4) { LivingEntity oldmob = mobThrowerQueue.remove(0); - if (oldmob != null) - { + if (oldmob != null) { oldmob.damage(500.0); } } } - public void startArrowShooter(TotalFreedomMod plugin) - { + public void startArrowShooter(TotalFreedomMod plugin) { this.stopArrowShooter(); this.mp44ScheduleTask = new ArrowShooter(getPlayer()).runTaskTimer(plugin, 1L, 1L); this.mp44Firing = true; } - public void stopArrowShooter() - { - if (this.mp44ScheduleTask != null) - { + public void stopArrowShooter() { + if (this.mp44ScheduleTask != null) { this.mp44ScheduleTask.cancel(); this.mp44ScheduleTask = null; } this.mp44Firing = false; } - public void armMP44() - { + public void armMP44() { this.mp44Armed = true; this.stopArrowShooter(); } - public void disarmMP44() - { + public void disarmMP44() { this.mp44Armed = false; this.stopArrowShooter(); } - public boolean isMP44Armed() - { + public boolean isMP44Armed() { return this.mp44Armed; } - public boolean toggleMP44Firing() - { + public boolean toggleMP44Firing() { this.mp44Firing = !this.mp44Firing; return mp44Firing; } - public boolean isMuted() - { + public boolean isMuted() { return unmuteTask != null; } - public void setMuted(boolean muted, int minutes) - { + public void setMuted(boolean muted, int minutes) { FUtil.cancel(unmuteTask); - plugin.mu.MUTED_PLAYERS.remove(getPlayer().getName()); + plugin.mu.MUTED_PLAYERS.remove(getPlayer().getUniqueId()); unmuteTask = null; - if (!muted) - { + if (!muted) { return; } - if (getPlayer() == null) - { + if (getPlayer() == null) { return; } - plugin.mu.MUTED_PLAYERS.add(getPlayer().getName()); + plugin.mu.MUTED_PLAYERS.add(getPlayer().getUniqueId()); // TODO: Simplify this into a Consumer lambda? - unmuteTask = new BukkitRunnable() - { + unmuteTask = new BukkitRunnable() { @Override - public void run() - { - if (getPlayer() != null) - { + public void run() { + if (getPlayer() != null) { FUtil.adminAction(ConfigEntry.SERVER_NAME.getString(), "Unmuting " + getPlayer().getName(), false); setMuted(false); - } - else - { - FUtil.adminAction(ConfigEntry.SERVER_NAME.getString(), "Unmuting " + getName(), false); - plugin.mu.MUTED_PLAYERS.remove(getName()); + } else { + FUtil.adminAction(ConfigEntry.SERVER_NAME.getString(), "Unmuting " + Bukkit.getOfflinePlayer(uuid).getName(), false); + plugin.mu.MUTED_PLAYERS.remove(uuid); } } }.runTaskLater(plugin, minutes * (60L * 20L)); } - public void setMuted(boolean muted) - { + public void setMuted(boolean muted) { setMuted(muted, 5); } - public BukkitTask getLockupScheduleID() - { + public BukkitTask getLockupScheduleID() { return this.lockupScheduleTask; } - public void setLockupScheduleId(BukkitTask id) - { + public void setLockupScheduleId(BukkitTask id) { this.lockupScheduleTask = id; } - public boolean isLockedUp() - { + public boolean isLockedUp() { return this.lockedUp; } - public void setLockedUp(boolean lockedUp) - { + public void setLockedUp(boolean lockedUp) { this.lockedUp = lockedUp; } - public String getLastMessage() - { + public String getLastMessage() { return lastMessage; } - public void setLastMessage(String message) - { + public void setLastMessage(String message) { this.lastMessage = message; } - public void setAdminChat(boolean inAdminchat) - { + public void setAdminChat(boolean inAdminchat) { this.inAdminchat = inAdminchat; } - public boolean inAdminChat() - { + public boolean inAdminChat() { return this.inAdminchat; } - public boolean allCommandsBlocked() - { + public boolean allCommandsBlocked() { return this.allCommandsBlocked; } - public void setCommandsBlocked(boolean commandsBlocked) - { + public void setCommandsBlocked(boolean commandsBlocked) { this.allCommandsBlocked = commandsBlocked; } - public String getLastCommand() - { + public String getLastCommand() { return lastCommand; } - public void setLastCommand(String lastCommand) - { + public void setLastCommand(String lastCommand) { this.lastCommand = lastCommand; } - public void setCommandSpy(boolean enabled) - { + public void setCommandSpy(boolean enabled) { this.cmdspyEnabled = enabled; } - public boolean cmdspyEnabled() - { + public boolean cmdspyEnabled() { return cmdspyEnabled; } - public String getTag() - { + public String getTag() { return this.tag; } - public void setTag(String tag) - { - if (tag == null) - { + public void setTag(String tag) { + if (tag == null) { this.tag = null; - } - else - { + } else { this.tag = FUtil.colorize(tag) + ChatColor.WHITE; } } - public int getWarningCount() - { + public int getWarningCount() { return this.warningCount; } - public void setWarningCount(int warningCount) - { + public void setWarningCount(int warningCount) { this.warningCount = warningCount; } - public void incrementWarnings(boolean quiet) - { + public void incrementWarnings(boolean quiet) { this.warningCount++; - if (this.warningCount % 2 == 0) - { + if (this.warningCount % 2 == 0) { Player p = getPlayer(); - if (!quiet) - { + if (!quiet) { p.getWorld().strikeLightning(p.getLocation()); } @@ -448,271 +366,221 @@ public class FPlayer } } - public TotalFreedomMod getPlugin() - { + public TotalFreedomMod getPlugin() { return plugin; } - public String getName() - { - return name; + public UUID getUniqueId() { + return uuid; } - public String getIp() - { - return ip; + public PlayerData getPlayerData() { + return playerData; } - public BukkitTask getUnmuteTask() - { + public BukkitTask getUnmuteTask() { return unmuteTask; } - public void setUnmuteTask(BukkitTask unmuteTask) - { + public void setUnmuteTask(BukkitTask unmuteTask) { this.unmuteTask = unmuteTask; } - public FreezeData getFreezeData() - { + public FreezeData getFreezeData() { return freezeData; } - public double getFuckoffRadius() - { + public double getFuckoffRadius() { return fuckoffRadius; } - public void setFuckoffRadius(double fuckoffRadius) - { + public void setFuckoffRadius(double fuckoffRadius) { this.fuckoffRadius = fuckoffRadius; } - public int getMessageCount() - { + public int getMessageCount() { return messageCount; } - public void setMessageCount(int messageCount) - { + public void setMessageCount(int messageCount) { this.messageCount = messageCount; } - public int getTotalBlockDestroy() - { + public int getTotalBlockDestroy() { return totalBlockDestroy; } - public void setTotalBlockDestroy(int totalBlockDestroy) - { + public void setTotalBlockDestroy(int totalBlockDestroy) { this.totalBlockDestroy = totalBlockDestroy; } - public int getTotalBlockPlace() - { + public int getTotalBlockPlace() { return totalBlockPlace; } - public void setTotalBlockPlace(int totalBlockPlace) - { + public void setTotalBlockPlace(int totalBlockPlace) { this.totalBlockPlace = totalBlockPlace; } - public int getFreecamDestroyCount() - { + public int getFreecamDestroyCount() { return freecamDestroyCount; } - public void setFreecamDestroyCount(int freecamDestroyCount) - { + public void setFreecamDestroyCount(int freecamDestroyCount) { this.freecamDestroyCount = freecamDestroyCount; } - public int getFreecamPlaceCount() - { + public int getFreecamPlaceCount() { return freecamPlaceCount; } - public void setFreecamPlaceCount(int freecamPlaceCount) - { + public void setFreecamPlaceCount(int freecamPlaceCount) { this.freecamPlaceCount = freecamPlaceCount; } - public CageData getCageData() - { + public CageData getCageData() { return cageData; } - public double getOrbitStrength() - { + public double getOrbitStrength() { return orbitStrength; } - public void setOrbitStrength(double orbitStrength) - { + public void setOrbitStrength(double orbitStrength) { this.orbitStrength = orbitStrength; } - public boolean isMobThrowerEnabled() - { + public boolean isMobThrowerEnabled() { return mobThrowerEnabled; } - public void setMobThrowerEnabled(boolean mobThrowerEnabled) - { + public void setMobThrowerEnabled(boolean mobThrowerEnabled) { this.mobThrowerEnabled = mobThrowerEnabled; } - public EntityType getMobThrowerEntity() - { + public EntityType getMobThrowerEntity() { return mobThrowerEntity; } - public void setMobThrowerEntity(EntityType mobThrowerEntity) - { + public void setMobThrowerEntity(EntityType mobThrowerEntity) { this.mobThrowerEntity = mobThrowerEntity; } - public double getMobThrowerSpeed() - { + public double getMobThrowerSpeed() { return mobThrowerSpeed; } - public void setMobThrowerSpeed(double mobThrowerSpeed) - { + public void setPlayerData(PlayerData playerData) { + this.playerData = playerData; + } + + public void setMobThrowerSpeed(double mobThrowerSpeed) { this.mobThrowerSpeed = mobThrowerSpeed; } - public List getMobThrowerQueue() - { + public List getMobThrowerQueue() { return mobThrowerQueue; } - public BukkitTask getMp44ScheduleTask() - { + public BukkitTask getMp44ScheduleTask() { return mp44ScheduleTask; } - public void setMp44ScheduleTask(BukkitTask mp44ScheduleTask) - { + public void setMp44ScheduleTask(BukkitTask mp44ScheduleTask) { this.mp44ScheduleTask = mp44ScheduleTask; } - public boolean isMp44Armed() - { + public boolean isMp44Armed() { return mp44Armed; } - public void setMp44Armed(boolean mp44Armed) - { + public void setMp44Armed(boolean mp44Armed) { this.mp44Armed = mp44Armed; } - public boolean isMp44Firing() - { + public boolean isMp44Firing() { return mp44Firing; } - public void setMp44Firing(boolean mp44Firing) - { + public void setMp44Firing(boolean mp44Firing) { this.mp44Firing = mp44Firing; } - public BukkitTask getLockupScheduleTask() - { + public BukkitTask getLockupScheduleTask() { return lockupScheduleTask; } - public void setLockupScheduleTask(BukkitTask lockupScheduleTask) - { + public void setLockupScheduleTask(BukkitTask lockupScheduleTask) { this.lockupScheduleTask = lockupScheduleTask; } - public boolean isInAdminchat() - { + public boolean isInAdminchat() { return inAdminchat; } - public void setInAdminchat(boolean inAdminchat) - { + public void setInAdminchat(boolean inAdminchat) { this.inAdminchat = inAdminchat; } - public boolean isAllCommandsBlocked() - { + public boolean isAllCommandsBlocked() { return allCommandsBlocked; } - public void setAllCommandsBlocked(boolean allCommandsBlocked) - { + public void setAllCommandsBlocked(boolean allCommandsBlocked) { this.allCommandsBlocked = allCommandsBlocked; } - public boolean isSuperadminIdVerified() - { + public boolean isSuperadminIdVerified() { return superadminIdVerified; } - public void setSuperadminIdVerified(boolean superadminIdVerified) - { + public void setSuperadminIdVerified(boolean superadminIdVerified) { this.superadminIdVerified = superadminIdVerified; } - public boolean isCmdspyEnabled() - { + public boolean isCmdspyEnabled() { return cmdspyEnabled; } - public void setCmdspyEnabled(boolean cmdspyEnabled) - { + public void setCmdspyEnabled(boolean cmdspyEnabled) { this.cmdspyEnabled = cmdspyEnabled; } - public boolean isEditBlocked() - { + public boolean isEditBlocked() { return editBlocked; } - public void setEditBlocked(boolean editBlocked) - { + public void setEditBlocked(boolean editBlocked) { this.editBlocked = editBlocked; } - public boolean isPvpBlocked() - { + public boolean isPvpBlocked() { return pvpBlocked; } - public void setPvpBlocked(boolean pvpBlocked) - { + public void setPvpBlocked(boolean pvpBlocked) { this.pvpBlocked = pvpBlocked; } - public boolean isInvSee() - { + public boolean isInvSee() { return invSee; } - public void setInvSee(boolean invSee) - { + public void setInvSee(boolean invSee) { this.invSee = invSee; } - private static class ArrowShooter extends BukkitRunnable - { + private static class ArrowShooter extends BukkitRunnable { private final Player player; - private ArrowShooter(Player player) - { + private ArrowShooter(Player player) { this.player = player; } @Override - public void run() - { - if (player != null) - { + public void run() { + if (player != null) { Arrow shot = player.launchProjectile(Arrow.class); shot.setVelocity(shot.getVelocity().multiply(2.0)); } diff --git a/src/main/java/me/totalfreedom/totalfreedommod/player/PlayerData.java b/src/main/java/me/totalfreedom/totalfreedommod/player/PlayerData.java index 942f9db4..96b19d84 100644 --- a/src/main/java/me/totalfreedom/totalfreedommod/player/PlayerData.java +++ b/src/main/java/me/totalfreedom/totalfreedommod/player/PlayerData.java @@ -3,10 +3,8 @@ package me.totalfreedom.totalfreedommod.player; import com.google.common.collect.Lists; import java.sql.ResultSet; import java.sql.SQLException; -import java.util.Collections; -import java.util.HashMap; -import java.util.List; -import java.util.Map; +import java.util.*; + import me.totalfreedom.totalfreedommod.TotalFreedomMod; import me.totalfreedom.totalfreedommod.shop.ShopItem; import me.totalfreedom.totalfreedommod.util.FLog; @@ -20,7 +18,7 @@ public class PlayerData private final List ips = Lists.newArrayList(); private final List notes = Lists.newArrayList(); private final List backupCodes = Lists.newArrayList(); - private String name; + private UUID uuid; private String tag = null; private String discordID = null; private Boolean masterBuilder = false; @@ -48,7 +46,7 @@ public class PlayerData { try { - name = resultSet.getString("username"); + uuid = UUID.fromString(resultSet.getString("uuid")); ips.clear(); ips.addAll(FUtil.stringToList(resultSet.getString("ips"))); notes.clear(); @@ -88,14 +86,13 @@ public class PlayerData public PlayerData(Player player) { - this.name = player.getName(); + this.uuid = player.getUniqueId(); } @Override public String toString() { - - return "Player: " + name + "\n" + + return "Player: " + uuid.toString() + "\n" + "- IPs: " + StringUtils.join(ips, ", ") + "\n" + "- Discord ID: " + discordID + "\n" + "- Master Builder: " + masterBuilder + "\n" + @@ -232,7 +229,7 @@ public class PlayerData { return new HashMap() {{ - put("username", name); + put("uuid", uuid); put("ips", FUtil.listToString(ips)); put("notes", FUtil.listToString(notes)); put("tag", tag); @@ -255,14 +252,14 @@ public class PlayerData return displayDiscord; } - public String getName() + public UUID getUniqueId() { - return name; + return uuid; } - public void setName(String name) + public void setUniqueId(UUID uuid) { - this.name = name; + this.uuid = uuid; } public String getTag() @@ -364,4 +361,9 @@ public class PlayerData { this.inspect = inspect; } + + public String getName() + { + return FUtil.getNameFromUUID(uuid); + } } \ No newline at end of file diff --git a/src/main/java/me/totalfreedom/totalfreedommod/player/PlayerList.java b/src/main/java/me/totalfreedom/totalfreedommod/player/PlayerList.java index 222e54d2..c693a5f9 100644 --- a/src/main/java/me/totalfreedom/totalfreedommod/player/PlayerList.java +++ b/src/main/java/me/totalfreedom/totalfreedommod/player/PlayerList.java @@ -3,11 +3,9 @@ package me.totalfreedom.totalfreedommod.player; import com.google.common.collect.Maps; import java.sql.ResultSet; import java.sql.SQLException; -import java.util.ArrayList; -import java.util.Date; -import java.util.List; -import java.util.Map; -import java.util.Objects; +import java.util.*; +import java.util.stream.Collectors; + import me.totalfreedom.totalfreedommod.FreedomService; import me.totalfreedom.totalfreedommod.admin.Admin; import me.totalfreedom.totalfreedommod.config.ConfigEntry; @@ -20,13 +18,12 @@ import org.bukkit.entity.Player; public class PlayerList extends FreedomService { - public final Map playerMap = Maps.newHashMap(); // ip,dataMap - public final Map dataMap = Maps.newHashMap(); // username, data + public final Map playerMap = Maps.newHashMap(); // uuid,dataMap + //public final Map dataMap = Maps.newHashMap(); // username, data @Override public void onStart() { - dataMap.clear(); loadMasterBuilders(); } @@ -57,7 +54,6 @@ public class PlayerList extends FreedomService while (resultSet.next()) { PlayerData playerData = load(resultSet); - dataMap.put(playerData.getName(), playerData); } } catch (SQLException e) @@ -73,27 +69,20 @@ public class PlayerList extends FreedomService return FUtil.getIp(Objects.requireNonNull(player.getPlayer())); } - final PlayerData entry = getData(player.getName()); + final PlayerData entry = getData(player.getUniqueId()); return (entry == null ? null : entry.getIps().iterator().next()); } public List getMasterBuilderNames() { - List masterBuilders = new ArrayList<>(); - for (PlayerData playerData : plugin.pl.dataMap.values()) - { - if (playerData.isMasterBuilder()) - { - masterBuilders.add(playerData.getName()); - } - } - return masterBuilders; + return playerMap.values().stream().map(FPlayer::getPlayerData).filter(PlayerData::isMasterBuilder).map(p -> FUtil.getNameFromUUID(p.getUniqueId())).collect(Collectors.toList()); } - public boolean canManageMasterBuilders(String name) + public boolean canManageMasterBuilders(UUID uuid) { - PlayerData data = getData(name); + PlayerData data = getData(uuid); + String name = FUtil.getNameFromUUID(uuid); return (!ConfigEntry.HOST_SENDER_NAMES.getStringList().contains(name.toLowerCase()) && data != null && !ConfigEntry.SERVER_OWNERS.getStringList().contains(data.getName())) && !ConfigEntry.SERVER_EXECUTIVES.getStringList().contains(data.getName()) @@ -103,31 +92,41 @@ public class PlayerList extends FreedomService public boolean isTelnetMasterBuilder(PlayerData playerData) { - Admin admin = plugin.al.getEntryByName(playerData.getName()); + Admin admin = plugin.al.getEntryByUUID(playerData.getUniqueId()); return admin != null && admin.getRank().isAtLeast(Rank.ADMIN) && playerData.isMasterBuilder(); } // May not return null public FPlayer getPlayer(Player player) { - FPlayer tPlayer = playerMap.get(FUtil.getIp(player)); + FPlayer tPlayer = playerMap.get(player.getUniqueId()); if (tPlayer != null) { return tPlayer; } tPlayer = new FPlayer(plugin, player); - playerMap.put(FUtil.getIp(player), tPlayer); + playerMap.put(player.getUniqueId(), tPlayer); return tPlayer; } - public PlayerData loadByName(String name) + /*public PlayerData loadByName(String name) { return load(plugin.sql.getPlayerByName(name)); } public PlayerData loadByIp(String ip) + { + return load(plugin.sql.getPlayerByIp(ip)); + }*/ + + public PlayerData loadByUUID(UUID uuid) + { + return load(plugin.sql.getPlayerByUUID(uuid)); + } + + private PlayerData loadByIp(String ip) { return load(plugin.sql.getPlayerByIp(ip)); } @@ -168,7 +167,7 @@ public class PlayerList extends FreedomService if (plugin.al.isAdminImpostor(player)) { - Admin admin = plugin.al.getEntryByName(player.getName()); + Admin admin = plugin.al.getEntryByUUID(player.getUniqueId()); admin.setLastLogin(new Date()); admin.addIp(FUtil.getIp(player)); plugin.al.updateTables(); @@ -180,7 +179,7 @@ public class PlayerList extends FreedomService public void syncIps(Admin admin) { - PlayerData playerData = getData(admin.getName()); + PlayerData playerData = getData(admin.getUniqueId()); playerData.clearIps(); playerData.addIps(admin.getIps()); plugin.pl.save(playerData); @@ -188,7 +187,7 @@ public class PlayerList extends FreedomService public void syncIps(PlayerData playerData) { - Admin admin = plugin.al.getEntryByName(playerData.getName()); + Admin admin = plugin.al.getEntryByUUID(playerData.getUniqueId()); if (admin != null && admin.isActive()) { @@ -204,7 +203,7 @@ public class PlayerList extends FreedomService { try { - ResultSet currentSave = plugin.sql.getPlayerByName(player.getName()); + ResultSet currentSave = plugin.sql.getPlayerByUUID(player.getUniqueId()); for (Map.Entry entry : player.toSQLStorable().entrySet()) { Object storedValue = plugin.sql.getValue(currentSave, entry.getKey(), entry.getValue()); @@ -223,31 +222,32 @@ public class PlayerList extends FreedomService public PlayerData getData(Player player) { // Check for existing data - PlayerData playerData = dataMap.get(player.getName()); + PlayerData playerData = playerMap.get(player.getUniqueId()).getPlayerData(); if (playerData != null) { return playerData; } // Load data - playerData = loadByName(player.getName()); + playerData = loadByUUID(player.getUniqueId()); - if (playerData == null) + /*if (playerData == null) { playerData = loadByIp(FUtil.getIp(player)); if (playerData != null) { plugin.sql.updatePlayerName(playerData.getName(), player.getName()); - playerData.setName(player.getName()); - dataMap.put(player.getName(), playerData); + playerMap.get(player.getUniqueId()).setPlayerData(playerData); return playerData; } } else { - dataMap.put(player.getName(), playerData); + playerMap.get(player.getUniqueId()).setPlayerData(playerData); return playerData; - } + }*/ + + playerMap.get(player.getUniqueId()).setPlayerData(playerData); // Create new data if nonexistent FLog.info("Creating new player verification entry for " + player.getName()); @@ -257,7 +257,7 @@ public class PlayerList extends FreedomService playerData.addIp(FUtil.getIp(player)); // Store player - dataMap.put(player.getName(), playerData); + playerMap.get(player.getUniqueId()).setPlayerData(playerData); // Save player plugin.sql.addPlayer(playerData); @@ -265,8 +265,10 @@ public class PlayerList extends FreedomService } + /*@Deprecated public PlayerData getData(String username) { + throw new UnsupportedOperationException(); // Check for existing data PlayerData playerData = dataMap.get(username); if (playerData != null) @@ -285,28 +287,58 @@ public class PlayerList extends FreedomService return null; } + return playerData; + }*/ + + public PlayerData getData(UUID uuid) + { + PlayerData playerData = playerMap.get(uuid).getPlayerData(); + if (playerData != null) + { + return playerData; + } + + playerData = loadByUUID(uuid); + + if (playerData != null) + { + playerMap.get(uuid).setPlayerData(playerData); + } + else + { + return null; + } + return playerData; } - public PlayerData getDataByIp(String ip) + public PlayerData getDataByUUID(UUID uuid) { - PlayerData player = loadByIp(ip); + PlayerData player = loadByUUID(uuid); if (player != null) { - dataMap.put(player.getName(), player); + playerMap.get(uuid).setPlayerData(player); } return player; } - public Map getPlayerMap() + public PlayerData getDataByIp(String ip) + { + PlayerData player = loadByIp(ip); + + if (player != null) + { + playerMap.get(player.getUniqueId()).setPlayerData(player); + } + + return player; + } + + public Map getPlayerMap() { return playerMap; } - public Map getDataMap() - { - return dataMap; - } } \ No newline at end of file diff --git a/src/main/java/me/totalfreedom/totalfreedommod/rank/RankManager.java b/src/main/java/me/totalfreedom/totalfreedommod/rank/RankManager.java index d246b19f..bb333797 100644 --- a/src/main/java/me/totalfreedom/totalfreedommod/rank/RankManager.java +++ b/src/main/java/me/totalfreedom/totalfreedommod/rank/RankManager.java @@ -117,7 +117,7 @@ public class RankManager extends FreedomService } // Console admin, get by name - Admin admin = plugin.al.getEntryByName(sender.getName()); + Admin admin = plugin.al.getEntryByUUID(FUtil.getUUIDFromName(sender.getName())); // Unknown console: RCON? if (admin == null) diff --git a/src/main/java/me/totalfreedom/totalfreedommod/shop/Votifier.java b/src/main/java/me/totalfreedom/totalfreedommod/shop/Votifier.java index c559d061..f5420ac5 100644 --- a/src/main/java/me/totalfreedom/totalfreedommod/shop/Votifier.java +++ b/src/main/java/me/totalfreedom/totalfreedommod/shop/Votifier.java @@ -37,7 +37,7 @@ public class Votifier extends FreedomService } else { - data = plugin.pl.getData(name); + data = plugin.pl.getData(FUtil.getUUIDFromName(name)); } if (data != null) diff --git a/src/main/java/me/totalfreedom/totalfreedommod/sql/SQLite.java b/src/main/java/me/totalfreedom/totalfreedommod/sql/SQLite.java index 09551e02..87197887 100644 --- a/src/main/java/me/totalfreedom/totalfreedommod/sql/SQLite.java +++ b/src/main/java/me/totalfreedom/totalfreedommod/sql/SQLite.java @@ -7,6 +7,8 @@ import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.text.MessageFormat; +import java.util.UUID; + import me.totalfreedom.totalfreedommod.FreedomService; import me.totalfreedom.totalfreedommod.admin.Admin; import me.totalfreedom.totalfreedommod.banning.Ban; @@ -70,6 +72,7 @@ public class SQLite extends FreedomService { try { + //TODO: move bans to uuid connection.createStatement().execute("CREATE TABLE `bans` (`name` VARCHAR, `uuid` VARCHAR, `ips` VARCHAR, `by` VARCHAR NOT NULL, `at` LONG NOT NULL, `expires` LONG, `reason` VARCHAR);"); } catch (SQLException e) @@ -82,7 +85,7 @@ public class SQLite extends FreedomService { try { - connection.createStatement().execute("CREATE TABLE `admins` (`username` VARCHAR NOT NULL, `ips` VARCHAR NOT NULL, `rank` VARCHAR NOT NULL, `active` BOOLEAN NOT NULL, `last_login` LONG NOT NULL, `command_spy` BOOLEAN NOT NULL, `potion_spy` BOOLEAN NOT NULL, `ac_format` VARCHAR, `ptero_id` VARCHAR);"); + connection.createStatement().execute("CREATE TABLE `admins` (`uuid` VARCHAR NOT NULL, `ips` VARCHAR NOT NULL, `rank` VARCHAR NOT NULL, `active` BOOLEAN NOT NULL, `last_login` LONG NOT NULL, `command_spy` BOOLEAN NOT NULL, `potion_spy` BOOLEAN NOT NULL, `ac_format` VARCHAR, `ptero_id` VARCHAR);"); } catch (SQLException e) { @@ -93,7 +96,7 @@ public class SQLite extends FreedomService { try { - connection.createStatement().execute("CREATE TABLE `players` (`username` VARCHAR NOT NULL, `ips` VARCHAR NOT NULL, `notes` VARCHAR, `tag` VARCHAR, `discord_id` VARCHAR, `backup_codes` VARCHAR, `master_builder` BOOLEAN NOT NULL,`verification` BOOLEAN NOT NULL, `ride_mode` VARCHAR NOT NULL, `coins` INT, `items` VARCHAR, `total_votes` INT NOT NULL, `display_discord` BOOLEAN NOT NULL, `login_message` VARCHAR, `inspect` BOOLEAN NOT NULL);"); + connection.createStatement().execute("CREATE TABLE `players` (`uuid` VARCHAR NOT NULL, `ips` VARCHAR NOT NULL, `notes` VARCHAR, `tag` VARCHAR, `discord_id` VARCHAR, `backup_codes` VARCHAR, `master_builder` BOOLEAN NOT NULL,`verification` BOOLEAN NOT NULL, `ride_mode` VARCHAR NOT NULL, `coins` INT, `items` VARCHAR, `total_votes` INT NOT NULL, `display_discord` BOOLEAN NOT NULL, `login_message` VARCHAR, `inspect` BOOLEAN NOT NULL);"); } catch (SQLException e) { @@ -133,8 +136,8 @@ public class SQLite extends FreedomService { try { - Object[] data = {key, admin.getName()}; - PreparedStatement statement = connection.prepareStatement(MessageFormat.format("UPDATE admins SET {0}=? WHERE username=''{1}''", data)); + Object[] data = {key, admin.getUniqueId().toString()}; + PreparedStatement statement = connection.prepareStatement(MessageFormat.format("UPDATE admins SET {0}=? WHERE uuid=''{1}''", data)); statement = setUnknownType(statement, 1, value); statement.executeUpdate(); @@ -150,8 +153,8 @@ public class SQLite extends FreedomService { try { - Object[] data = {key, player.getName()}; - PreparedStatement statement = connection.prepareStatement(MessageFormat.format("UPDATE players SET {0}=? WHERE username=''{1}''", data)); + Object[] data = {key, player.getUniqueId().toString()}; + PreparedStatement statement = connection.prepareStatement(MessageFormat.format("UPDATE players SET {0}=? WHERE uuid=''{1}''", data)); statement = setUnknownType(statement, 1, value); statement.executeUpdate(); @@ -162,9 +165,11 @@ public class SQLite extends FreedomService } } - public void updateAdminName(String oldName, String newName) + @Deprecated + public void oldUpdateAdminName(String oldName, String newName) { - try + throw new UnsupportedOperationException("Admin no longer stores names in the object, may be changed in a future update"); + /*try { PreparedStatement statement = connection.prepareStatement(MessageFormat.format("UPDATE admins SET username=? WHERE username=''{0}''", oldName)); statement = setUnknownType(statement, 1, newName); @@ -174,10 +179,10 @@ public class SQLite extends FreedomService catch (SQLException e) { FLog.severe("Failed to update admin name: " + e.getMessage()); - } + }*/ } - public void updatePlayerName(String oldName, String newName) + /*public void updatePlayerName(String oldName, String newName) { try { @@ -190,7 +195,7 @@ public class SQLite extends FreedomService { FLog.severe("Failed to update player name: " + e.getMessage()); } - } + }*/ public PreparedStatement setUnknownType(PreparedStatement statement, int index, Object value) throws SQLException { @@ -248,7 +253,7 @@ public class SQLite extends FreedomService try { PreparedStatement statement = connection.prepareStatement("INSERT INTO admins VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)"); - statement.setString(1, admin.getName()); + statement.setString(1, admin.getUniqueId().toString()); statement.setString(2, FUtil.listToString(admin.getIps())); statement.setString(3, admin.getRank().toString()); statement.setBoolean(4, admin.isActive()); @@ -271,7 +276,7 @@ public class SQLite extends FreedomService try { PreparedStatement statement = connection.prepareStatement("INSERT INTO players VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"); - statement.setString(1, player.getName()); + statement.setString(1, player.getUniqueId().toString()); statement.setString(2, FUtil.listToString(player.getIps())); statement.setString(3, FUtil.listToString(player.getNotes())); statement.setString(4, player.getTag()); @@ -295,7 +300,7 @@ public class SQLite extends FreedomService } } - public ResultSet getAdminByName(String name) + /*public ResultSet getAdminByName(String name) { try { @@ -311,10 +316,29 @@ public class SQLite extends FreedomService FLog.severe(e); } + return null; + }*/ + + public ResultSet getAdminByUUID(UUID uuid) + { + try + { + ResultSet resultSet = connection.createStatement().executeQuery(MessageFormat.format("SELECT * FROM admins WHERE uuid=''{0}''", uuid.toString())); + if (resultSet.next()) + { + return resultSet; + } + } + catch (SQLException e) + { + FLog.severe("Failed to get admin by name:"); + FLog.severe(e); + } + return null; } - public ResultSet getPlayerByName(String name) + /*public ResultSet getPlayerByName(String name) { try { @@ -332,6 +356,8 @@ public class SQLite extends FreedomService return null; } +*/ + public ResultSet getMasterBuilders() { @@ -348,6 +374,25 @@ public class SQLite extends FreedomService return null; } + public ResultSet getPlayerByUUID(UUID uuid) + { + try + { + ResultSet resultSet = connection.createStatement().executeQuery(MessageFormat.format("SELECT * FROM players WHERE uuid LIKE ''%{0}%''", uuid.toString())); + if (resultSet.next()) + { + return resultSet; + } + } + catch (SQLException e) + { + FLog.severe("Failed to get player by uuid:"); + FLog.severe(e); + } + + return null; + } + public ResultSet getPlayerByIp(String ip) { try @@ -371,7 +416,7 @@ public class SQLite extends FreedomService { try { - connection.createStatement().executeUpdate(MessageFormat.format("DELETE FROM admins where name=''{0}''", admin.getName())); + connection.createStatement().executeUpdate(MessageFormat.format("DELETE FROM admins where uuid=''{0}''", admin.getUniqueId())); } catch (SQLException e) { diff --git a/src/main/java/me/totalfreedom/totalfreedommod/util/FUtil.java b/src/main/java/me/totalfreedom/totalfreedommod/util/FUtil.java index a269a296..abee9917 100644 --- a/src/main/java/me/totalfreedom/totalfreedommod/util/FUtil.java +++ b/src/main/java/me/totalfreedom/totalfreedommod/util/FUtil.java @@ -1,16 +1,18 @@ package me.totalfreedom.totalfreedommod.util; +import com.google.common.collect.Maps; import me.totalfreedom.totalfreedommod.TotalFreedomMod; import me.totalfreedom.totalfreedommod.config.ConfigEntry; import org.apache.commons.io.FileUtils; import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.WordUtils; -import org.bukkit.Bukkit; -import org.bukkit.ChatColor; -import org.bukkit.Color; -import org.bukkit.Location; -import org.bukkit.Material; -import org.bukkit.OfflinePlayer; +import org.apache.hc.client5.http.classic.methods.HttpGet; +import org.apache.hc.client5.http.impl.classic.CloseableHttpClient; +import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse; +import org.apache.hc.client5.http.impl.classic.HttpClients; +import org.apache.hc.core5.http.HttpResponse; +import org.apache.hc.core5.http.io.entity.EntityUtils; +import org.bukkit.*; import org.bukkit.command.CommandSender; import org.bukkit.entity.Entity; import org.bukkit.entity.Player; @@ -19,12 +21,14 @@ import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.meta.ItemMeta; import org.bukkit.scheduler.BukkitRunnable; import org.bukkit.scheduler.BukkitTask; +import org.json.JSONObject; import org.json.simple.JSONArray; import java.io.*; import java.lang.reflect.Field; import java.net.HttpURLConnection; import java.net.URL; +import java.nio.charset.StandardCharsets; import java.text.ParseException; import java.text.SimpleDateFormat; import java.time.Instant; @@ -37,6 +41,8 @@ import static org.bukkit.Bukkit.getServer; public class FUtil { + private static final Map NAME_CACHE = Maps.newHashMap(); + public static final String SAVED_FLAGS_FILENAME = "savedflags.dat"; /* See https://github.com/TotalFreedom/License - None of the listed names may be removed. Leaving this list here for anyone running TFM on a cracked server: @@ -883,4 +889,52 @@ public class FUtil return subList(startIndex, endIndex + 1); } } + + public static String getNameFromUUID(UUID uuid) { + + OfflinePlayer player = Bukkit.getOfflinePlayer(uuid); + if (player.hasPlayedBefore()) return player.getName(); + if (NAME_CACHE.containsKey(uuid)) return NAME_CACHE.get(uuid); + + CloseableHttpClient client = HttpClients.createDefault(); + HttpGet get = new HttpGet("https://api.ashcon.app/mojang/v2/user/" + uuid.toString().replace("-", "")); + try { + CloseableHttpResponse response = client.execute(get); + String json = EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8); + JSONObject obj = new JSONObject(json); + response.close(); + if (response.getCode() == 200) { + String name = NAME_CACHE.put(uuid, obj.getString("username")); + + Bukkit.getScheduler().runTaskLaterAsynchronously(TotalFreedomMod.getPlugin(), () -> NAME_CACHE.remove(uuid), 20 * 60L * 5); // remove the uuid from cache as names can be changed + return name; + } + client.close(); + } catch (IOException | org.apache.hc.core5.http.ParseException e) { + e.printStackTrace(); + } + return ""; + } + + public static UUID getUUIDFromName(String name) { + //get offline player is deprecated so we'll just see if they're online to get their uuid + Player player = Bukkit.getPlayer(name); + if (player != null) return player.getUniqueId(); + + CloseableHttpClient client = HttpClients.createDefault(); + HttpGet get = new HttpGet("https://api.ashcon.app/mojang/v2/user/" + name); + try { + CloseableHttpResponse response = client.execute(get); + String json = EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8); + JSONObject obj = new JSONObject(json); + response.close(); + if (response.getCode() == 200) { + return UUID.fromString(obj.getString("uuid")); + } + client.close(); + } catch (IOException | org.apache.hc.core5.http.ParseException e) { + e.printStackTrace(); + } + return null; + } } diff --git a/src/main/java/me/totalfreedom/totalfreedommod/world/WorldRestrictions.java b/src/main/java/me/totalfreedom/totalfreedommod/world/WorldRestrictions.java index 4aef7377..9c85b569 100644 --- a/src/main/java/me/totalfreedom/totalfreedommod/world/WorldRestrictions.java +++ b/src/main/java/me/totalfreedom/totalfreedommod/world/WorldRestrictions.java @@ -52,7 +52,7 @@ public class WorldRestrictions extends FreedomService public boolean doRestrict(Player player) { - if (!plugin.pl.getData(player).isMasterBuilder() && plugin.pl.canManageMasterBuilders(player.getName())) + if (!plugin.pl.getData(player).isMasterBuilder() && plugin.pl.canManageMasterBuilders(player.getUniqueId())) { if (player.getWorld().equals(plugin.wm.masterBuilderWorld.getWorld())) {