From c810053558b9e2a41189697ffd893eb6446fc081 Mon Sep 17 00:00:00 2001 From: NorbiPeti Date: Sat, 29 Oct 2016 14:11:29 +0200 Subject: [PATCH] New player data handling added (WIP) --- .../java/buttondevteam/chat/ChatPlayer.java | 130 ++++++++++-------- .../buttondevteam/chat/ChatProcessing.java | 36 ++--- .../buttondevteam/chat/PlayerListener.java | 49 ++----- .../java/buttondevteam/chat/PluginMain.java | 2 +- .../chat/commands/ucmds/NameCommand.java | 2 +- .../ucmds/admin/PlayerInfoCommand.java | 2 +- 6 files changed, 110 insertions(+), 111 deletions(-) diff --git a/src/main/java/buttondevteam/chat/ChatPlayer.java b/src/main/java/buttondevteam/chat/ChatPlayer.java index 165f237..9977cb9 100644 --- a/src/main/java/buttondevteam/chat/ChatPlayer.java +++ b/src/main/java/buttondevteam/chat/ChatPlayer.java @@ -1,78 +1,104 @@ package buttondevteam.chat; -import java.util.HashMap; import java.util.List; -import java.util.UUID; - import org.bukkit.Bukkit; import org.bukkit.Location; -import org.bukkit.OfflinePlayer; import org.bukkit.entity.Player; import buttondevteam.chat.formatting.ChatFormatter; import buttondevteam.lib.TBMCPlayer; public class ChatPlayer extends TBMCPlayer { - public String UserName; - public List UserNames; - private short FlairTime; - public FlairStates FlairState; + public String getUserName() { + return getData(); + } + + public void setUserName(String name) { + setData(name); + } + + public List getUserNames() { + return getData(); + } + + public void setUserNames(List names) { + setData(names); + } + + public short getFlairTime() { + return getData(); + } + + private void setFlairTime(short time) { + setData(time); + } + + public FlairStates getFlairState() { + return getData(); + } + + public void setFlairState(FlairStates state) { + setData(state); + } + + public int getFCount() { + return getData(); + } + + public void setFCount(int count) { + setData(count); + } + + public int getFDeaths() { + return getData(); + } + + public void setFDeaths(int count) { + setData(count); + } + + public boolean getFlairCheater() { + return getData(); + } + + private void setFlairCheater(boolean cheater) { + setData(cheater); + } + public boolean RPMode = true; public boolean PressedF; public Location SavedLocation; public boolean Working; // public int Tables = 10; public Channel CurrentChannel = Channel.GlobalChat; - public int FCount; public boolean SendingLink = false; - public int FDeaths; public boolean RainbowPresserColorMode = false; public ChatFormatter.Color OtherColorMode = null; public boolean ChatOnly = false; - public boolean FlairCheater = false; public int LoginWarningCount = 0; + public static final short FlairTimeNonPresser = -1; public static final short FlairTimeCantPress = -2; public static final short FlairTimeNone = -3; - public static HashMap OnlinePlayers = new HashMap<>(); - - @SuppressWarnings("deprecation") - public static ChatPlayer GetFromName(String name) { - OfflinePlayer p = Bukkit.getOfflinePlayer(name); - if (p != null) { - if (!ChatPlayer.OnlinePlayers.containsKey(p.getUniqueId())) { - TBMCPlayer player = TBMCPlayer.LoadPlayer(p); - if (player == null) { - Bukkit.getServer().getLogger() - .warning("Can't load player " + p.getUniqueId() + " - " + p.getName()); - return null; - } - return ChatPlayer.OnlinePlayers.get(player.UUID); - } - return ChatPlayer.OnlinePlayers.get(p.getUniqueId()); - } else - return null; - } - public String GetFormattedFlair() { - if (FlairTime == FlairTimeCantPress) + if (getFlairTime() == FlairTimeCantPress) return String.format("§r(--s)§r"); - if (FlairTime == FlairTimeNonPresser) + if (getFlairTime() == FlairTimeNonPresser) return String.format("§7(--s)§r"); - if (FlairTime == FlairTimeNone) + if (getFlairTime() == FlairTimeNone) return ""; - return String.format("§%x(%ss)§r", GetFlairColor(), FlairTime); + return String.format("§%x(%ss)§r", GetFlairColor(), getFlairTime()); } public void SetFlair(short time) { - FlairTime = time; + setFlairTime(time); FlairUpdate(); } public void SetFlair(short time, boolean cheater) { - FlairTime = time; - FlairCheater = cheater; + setFlairTime(time); + setFlairCheater(cheater); FlairUpdate(); } @@ -80,38 +106,30 @@ public class ChatPlayer extends TBMCPlayer { // Flairs from Command Block The Button - Teams // PluginMain.Instance.getServer().getScoreboardManager().getMainScoreboard().getTeams().add() - Player p = Bukkit.getPlayer(UUID); + Player p = Bukkit.getPlayer(getUuid()); if (p != null) p.setPlayerListName(String.format("%s%s", p.getName(), GetFormattedFlair())); } public short GetFlairColor() { - if (FlairCheater) + if (getFlairCheater()) return 0x5; - if (FlairTime == -1) + if (getFlairTime() == -1) return 0x7; - else if (FlairTime == -2) + else if (getFlairTime() == -2) return 0xf; - else if (FlairTime <= 60 && FlairTime >= 52) + else if (getFlairTime() <= 60 && getFlairTime() >= 52) return 0x5; - else if (FlairTime <= 51 && FlairTime >= 42) + else if (getFlairTime() <= 51 && getFlairTime() >= 42) return 0x9; - else if (FlairTime <= 41 && FlairTime >= 32) + else if (getFlairTime() <= 41 && getFlairTime() >= 32) return 0xa; - else if (FlairTime <= 31 && FlairTime >= 22) + else if (getFlairTime() <= 31 && getFlairTime() >= 22) return 0xe; - else if (FlairTime <= 21 && FlairTime >= 11) + else if (getFlairTime() <= 21 && getFlairTime() >= 11) return 0x6; - else if (FlairTime <= 11 && FlairTime >= 0) + else if (getFlairTime() <= 11 && getFlairTime() >= 0) return 0xc; return 0xf; } - - public short GetFlairTime() { - return FlairTime; - } - - public static ChatPlayer GetFromPlayer(Player p) { - return ChatPlayer.OnlinePlayers.get(p.getUniqueId()); - } } diff --git a/src/main/java/buttondevteam/chat/ChatProcessing.java b/src/main/java/buttondevteam/chat/ChatProcessing.java index 646367a..eaccbb0 100644 --- a/src/main/java/buttondevteam/chat/ChatProcessing.java +++ b/src/main/java/buttondevteam/chat/ChatProcessing.java @@ -26,6 +26,7 @@ import buttondevteam.chat.formatting.TellrawEvent; import buttondevteam.chat.formatting.TellrawPart; import buttondevteam.chat.formatting.TellrawSerializableEnum; import buttondevteam.chat.formatting.TellrawSerializer; +import buttondevteam.lib.TBMCPlayer; import buttondevteam.chat.formatting.ChatFormatter.Color; import buttondevteam.chat.formatting.ChatFormatter.Priority; @@ -55,13 +56,13 @@ public class ChatProcessing { ChatPlayer mp = null; if (player != null) { - mp = ChatPlayer.OnlinePlayers.get(player.getUniqueId()); + mp = TBMCPlayer.getPlayer(player).asPluginPlayer(ChatPlayer.class); if (message.equalsIgnoreCase("F")) { if (!mp.PressedF && PlayerListener.ActiveF) { PlayerListener.FCount++; mp.PressedF = true; - if (PlayerListener.FPlayer != null && PlayerListener.FPlayer.FCount < Integer.MAX_VALUE - 1) - PlayerListener.FPlayer.FCount++; + if (PlayerListener.FPlayer != null && PlayerListener.FPlayer.getFCount() < Integer.MAX_VALUE - 1) + PlayerListener.FPlayer.setFCount(PlayerListener.FPlayer.getFCount() + 1); } } } @@ -132,7 +133,7 @@ public class ChatProcessing { .warning("Error: Can't find player " + match + " but was reported as online."); return "§c" + match + "§r"; } - ChatPlayer mpp = ChatPlayer.GetFromPlayer(p); + ChatPlayer mpp = TBMCPlayer.getPlayer(p).asPluginPlayer(ChatPlayer.class); if (PlayerListener.NotificationSound == null) p.playSound(p.getLocation(), Sound.ENTITY_ARROW_HIT_PLAYER, 1.0f, 0.5f); // TODO: Airhorn else @@ -212,18 +213,21 @@ public class ChatProcessing { .addExtra(new TellrawPart(String.format("World: %s\n", (player != null ? player.getWorld().getName() : "-")))) - .addExtra(new TellrawPart(String.format( - "Respect: %s%s%s", - (mp != null ? (mp.FCount / (double) mp.FDeaths) - : "Infinite"), - (mp != null && mp.UserName != null - && !mp.UserName.isEmpty() - ? "\nUserName: " + mp.UserName - : ""), - (mp != null && mp.PlayerName.equals( - "\nAlpha_Bacca44") ? "\nDeaths: " - + PlayerListener.AlphaDeaths - : ""))))))); + .addExtra( + new TellrawPart(String.format("Respect: %s%s%s", + (mp != null ? (mp.getFCount() + / (double) mp.getFDeaths()) + : "Infinite"), + (mp != null && mp.getUserName() != null + && !mp.getUserName().isEmpty() + ? "\nUserName: " + + mp.getUserName() + : ""), + (mp != null && mp.getPlayerName() + .equals("\nAlpha_Bacca44") + ? "\nDeaths: " + + PlayerListener.AlphaDeaths + : ""))))))); json.addExtra(new TellrawPart("> ")); long combinetime = System.nanoTime(); ChatFormatter.Combine(formatters, formattedmessage, json); diff --git a/src/main/java/buttondevteam/chat/PlayerListener.java b/src/main/java/buttondevteam/chat/PlayerListener.java index 13f52b0..c7b1679 100644 --- a/src/main/java/buttondevteam/chat/PlayerListener.java +++ b/src/main/java/buttondevteam/chat/PlayerListener.java @@ -69,30 +69,19 @@ public class PlayerListener implements Listener { @EventHandler public void onPlayerLoad(TBMCPlayerLoadEvent e) { YamlConfiguration yc = e.GetPlayerConfig(); - ChatPlayer cp = e.GetPlayer().AsPluginPlayer(ChatPlayer.class); - cp.UserName = yc.getString("username"); + ChatPlayer cp = e.GetPlayer().asPluginPlayer(ChatPlayer.class); short tcp = (short) yc.getInt("flairtime"); - cp.SetFlair(tcp); - String flairstate = yc.getString("flairstate"); - if (flairstate != null) - cp.FlairState = FlairStates.valueOf(flairstate); - else - cp.FlairState = FlairStates.NoComment; - cp.UserNames = yc.getStringList("usernames"); - cp.FCount = yc.getInt("fcount"); - cp.FDeaths = yc.getInt("fdeaths"); - cp.FlairCheater = yc.getBoolean("flaircheater"); - ChatPlayer.OnlinePlayers.put(cp.UUID, cp); + cp.FlairUpdate(); } @EventHandler public void onPlayerTBMCJoin(TBMCPlayerJoinEvent e) { if (essentials == null) essentials = ((Essentials) Bukkit.getPluginManager().getPlugin("Essentials")); - ChatPlayer cp = ChatPlayer.OnlinePlayers.get(e.GetPlayer().UUID); - Player p = Bukkit.getPlayer(cp.UUID); + ChatPlayer cp = e.GetPlayer().asPluginPlayer(ChatPlayer.class); + Player p = Bukkit.getPlayer(cp.getUuid()); - if (!cp.FlairState.equals(FlairStates.NoComment)) { + if (!cp.getFlairState().equals(FlairStates.NoComment)) { PluginMain.ConfirmUserMessage(cp); Timer timer = new Timer(); PlayerJoinTimerTask tt = new PlayerJoinTimerTask() { @@ -104,26 +93,26 @@ public class PlayerListener implements Listener { tt.mp = cp; timer.schedule(tt, 1000); } else { - if (cp.GetFlairTime() == 0x00) + if (cp.getFlairTime() == 0x00) cp.SetFlair(ChatPlayer.FlairTimeNone); Timer timer = new Timer(); PlayerJoinTimerTask tt = new PlayerJoinTimerTask() { @Override public void run() { - Player player = Bukkit.getPlayer(mp.PlayerName); + Player player = Bukkit.getPlayer(mp.getPlayerName()); if (player == null) return; - if (mp.FlairState.equals(FlairStates.NoComment)) { + if (mp.getFlairState().equals(FlairStates.NoComment)) { String json = String.format( "[\"\",{\"text\":\"If you're from Reddit and you'd like your /r/TheButton flair displayed ingame, write your Minecraft name to \",\"color\":\"aqua\"},{\"text\":\"[this thread].\",\"color\":\"aqua\",\"clickEvent\":{\"action\":\"open_url\",\"value\":\"%s\"},\"hoverEvent\":{\"action\":\"show_text\",\"value\":{\"text\":\"\",\"extra\":[{\"text\":\"Click here to go to the Reddit thread\",\"color\":\"aqua\"}]}}}]", PluginMain.FlairThreadURL); PluginMain.Instance.getServer().dispatchCommand(PluginMain.Console, - "tellraw " + mp.PlayerName + " " + json); + "tellraw " + mp.getPlayerName() + " " + json); json = "[\"\",{\"text\":\"If you aren't from Reddit or don't want the flair, type /u ignore to prevent this message after next login.\",\"color\":\"aqua\"}]"; PluginMain.Instance.getServer().dispatchCommand(PluginMain.Console, - "tellraw " + mp.PlayerName + " " + json); + "tellraw " + mp.getPlayerName() + " " + json); } } }; @@ -154,26 +143,14 @@ public class PlayerListener implements Listener { @EventHandler public void onPlayerSave(TBMCPlayerSaveEvent e) { - YamlConfiguration yc = e.GetPlayerConfig(); - ChatPlayer cp = ChatPlayer.OnlinePlayers.get(e.GetPlayer().UUID); - yc.set("username", cp.UserName); - yc.set("flairtime", cp.GetFlairTime()); - yc.set("flairstate", cp.FlairState.toString()); - yc.set("usernames", cp.UserNames); - yc.set("fcount", cp.FCount); - yc.set("fdeaths", cp.FDeaths); - yc.set("flaircheater", cp.FlairCheater); } @EventHandler public void onPlayerAdd(TBMCPlayerAddEvent event) { - TBMCPlayer tp = event.GetPlayer(); - ChatPlayer cp = new ChatPlayer(); - cp.UUID = tp.UUID; + ChatPlayer cp = event.GetPlayer().asPluginPlayer(ChatPlayer.class); cp.SetFlair(ChatPlayer.FlairTimeNone); - cp.FlairState = FlairStates.NoComment; - cp.UserNames = new ArrayList<>(); - ChatPlayer.OnlinePlayers.put(cp.UUID, cp); + cp.setFlairState(FlairStates.NoComment); + cp.setUserNames(new ArrayList<>()); } @EventHandler diff --git a/src/main/java/buttondevteam/chat/PluginMain.java b/src/main/java/buttondevteam/chat/PluginMain.java index 9e3d783..bdca296 100644 --- a/src/main/java/buttondevteam/chat/PluginMain.java +++ b/src/main/java/buttondevteam/chat/PluginMain.java @@ -138,7 +138,7 @@ public class PluginMain extends JavaPlugin { // Translated to Java: 2015.07.15. ign = ign.trim(); if (PlayersWithFlairs.contains(ign)) continue; - ChatPlayer mp = ChatPlayer.GetFromName(ign); // Loads player file + ChatPlayer mp = ChatPlayer.getFromName(ign); // Loads player file if (mp == null) continue; /* diff --git a/src/main/java/buttondevteam/chat/commands/ucmds/NameCommand.java b/src/main/java/buttondevteam/chat/commands/ucmds/NameCommand.java index abc6a7a..02adfb0 100644 --- a/src/main/java/buttondevteam/chat/commands/ucmds/NameCommand.java +++ b/src/main/java/buttondevteam/chat/commands/ucmds/NameCommand.java @@ -19,7 +19,7 @@ public class NameCommand extends UCommandBase { if (args.length == 0) { return false; } - ChatPlayer mp = ChatPlayer.GetFromName(args[0]); + ChatPlayer mp = ChatPlayer.getFromName(args[0]); if (mp == null) { sender.sendMessage("§cUnknown user: " + args[0]); diff --git a/src/main/java/buttondevteam/chat/commands/ucmds/admin/PlayerInfoCommand.java b/src/main/java/buttondevteam/chat/commands/ucmds/admin/PlayerInfoCommand.java index 0112b78..a28aef1 100644 --- a/src/main/java/buttondevteam/chat/commands/ucmds/admin/PlayerInfoCommand.java +++ b/src/main/java/buttondevteam/chat/commands/ucmds/admin/PlayerInfoCommand.java @@ -18,7 +18,7 @@ public class PlayerInfoCommand extends AdminCommandBase { if (args.length == 0) { return false; } - ChatPlayer p = ChatPlayer.GetFromName(args[0]); + ChatPlayer p = ChatPlayer.getFromName(args[0]); if (p == null) { sender.sendMessage("§cPlayer not found: " + args[0] + "§r"); return true;