From 01370b9aca29ab22e0b37bb863b2e4efc5b55b99 Mon Sep 17 00:00:00 2001 From: rootEnginear Date: Sun, 15 Oct 2023 12:35:17 +0700 Subject: [PATCH] fix: clean up code --- .../proximitychat/command/ChannelCommand.java | 10 +++--- .../proximitychat/command/RadiusCommand.java | 6 ++-- .../mixin/NetServerHandlerMixin.java | 2 +- .../store/PlayerChannelData.java | 32 ++++--------------- 4 files changed, 14 insertions(+), 36 deletions(-) diff --git a/src/main/java/rootenginear/proximitychat/command/ChannelCommand.java b/src/main/java/rootenginear/proximitychat/command/ChannelCommand.java index 2d16e41..50e25ef 100644 --- a/src/main/java/rootenginear/proximitychat/command/ChannelCommand.java +++ b/src/main/java/rootenginear/proximitychat/command/ChannelCommand.java @@ -5,10 +5,9 @@ import net.minecraft.core.net.command.CommandSender; import net.minecraft.core.net.command.ServerCommand; import net.minecraft.server.MinecraftServer; -import rootenginear.proximitychat.struct.PlayerChannelConfig; import static rootenginear.proximitychat.store.PlayerChannelData.getPlayerChannelData; -import static rootenginear.proximitychat.store.PlayerChannelData.setPlayerChannelData; +import static rootenginear.proximitychat.store.PlayerChannelData.setPlayerChannel; public class ChannelCommand extends ServerCommand { public ChannelCommand(MinecraftServer server) { @@ -25,11 +24,10 @@ public boolean execute(CommandHandler handler, CommandSender sender, String[] ar } String colorfulName = sender.getName() + "§r"; - String rawName = sender.getName().replaceFirst("§.", ""); + String rawName = sender.getName().substring(2); if (args.length == 0) { - PlayerChannelConfig cfg = getPlayerChannelData(rawName); - sender.sendMessage(colorfulName + "'s current channel is: " + (cfg.isGlobal ? "Global" : "Proximity")); + sender.sendMessage(colorfulName + "'s current channel is: " + (getPlayerChannelData(rawName).isGlobal ? "Global" : "Proximity")); return true; } @@ -37,7 +35,7 @@ public boolean execute(CommandHandler handler, CommandSender sender, String[] ar case "global": case "proximity": case "prox": - setPlayerChannelData(rawName, args[0]); + setPlayerChannel(rawName, args[0]); sender.sendMessage("Changed " + colorfulName + "'s channel to: " + (args[0].equals("global") ? "Global" : "Proximity")); return true; default: diff --git a/src/main/java/rootenginear/proximitychat/command/RadiusCommand.java b/src/main/java/rootenginear/proximitychat/command/RadiusCommand.java index f3e1d53..82328dc 100644 --- a/src/main/java/rootenginear/proximitychat/command/RadiusCommand.java +++ b/src/main/java/rootenginear/proximitychat/command/RadiusCommand.java @@ -6,7 +6,6 @@ import net.minecraft.core.net.command.ServerCommand; import net.minecraft.server.MinecraftServer; import rootenginear.proximitychat.ProximityChat; -import rootenginear.proximitychat.struct.PlayerChannelConfig; import static rootenginear.proximitychat.store.PlayerChannelData.getPlayerChannelData; import static rootenginear.proximitychat.store.PlayerChannelData.setPlayerRadius; @@ -26,11 +25,10 @@ public boolean execute(CommandHandler handler, CommandSender sender, String[] ar } String colorfulName = sender.getName() + "§r"; - String rawName = sender.getName().replaceFirst("§.", ""); + String rawName = sender.getName().substring(2); if (args.length == 0) { - PlayerChannelConfig cfg = getPlayerChannelData(rawName); - sender.sendMessage(colorfulName + "'s proximity chat radius is: " + cfg.radius); + sender.sendMessage(colorfulName + "'s proximity chat radius is: " + getPlayerChannelData(rawName).radius); return true; } diff --git a/src/main/java/rootenginear/proximitychat/mixin/NetServerHandlerMixin.java b/src/main/java/rootenginear/proximitychat/mixin/NetServerHandlerMixin.java index a670658..19c0447 100644 --- a/src/main/java/rootenginear/proximitychat/mixin/NetServerHandlerMixin.java +++ b/src/main/java/rootenginear/proximitychat/mixin/NetServerHandlerMixin.java @@ -44,7 +44,7 @@ private void proximityChat(ServerConfigurationManager instance, String s) { boolean isGlobal = playerData.isGlobal; if (msg.startsWith("# ")) { - msg = msg.replaceFirst("# ", ""); + msg = msg.substring(2); isGlobal = !isGlobal; } diff --git a/src/main/java/rootenginear/proximitychat/store/PlayerChannelData.java b/src/main/java/rootenginear/proximitychat/store/PlayerChannelData.java index c0e61fd..c1e0668 100644 --- a/src/main/java/rootenginear/proximitychat/store/PlayerChannelData.java +++ b/src/main/java/rootenginear/proximitychat/store/PlayerChannelData.java @@ -1,5 +1,6 @@ package rootenginear.proximitychat.store; +import net.minecraft.core.net.command.CommandError; import rootenginear.proximitychat.ProximityChat; import rootenginear.proximitychat.struct.PlayerChannelConfig; @@ -9,36 +10,17 @@ public class PlayerChannelData { public static HashMap playerProxData = new HashMap<>(); public static PlayerChannelConfig getPlayerChannelData(String rawPlayerName) { - if (playerProxData.containsKey(rawPlayerName)) { - return playerProxData.get(rawPlayerName); - } - - PlayerChannelConfig t = new PlayerChannelConfig(true, ProximityChat.DEFAULT_RADIUS); - playerProxData.put(rawPlayerName, t); - return t; + return playerProxData.computeIfAbsent(rawPlayerName, s -> new PlayerChannelConfig(true, ProximityChat.DEFAULT_RADIUS)); } - public static void setPlayerChannelData(String rawPlayerName, String mode) { - if (!mode.equals("global") && !mode.equals("proximity") && !mode.equals("prox")) return; - - if (!playerProxData.containsKey(rawPlayerName)) { - playerProxData.put(rawPlayerName, new PlayerChannelConfig(mode.equals("global"), ProximityChat.DEFAULT_RADIUS)); - return; - } + public static void setPlayerChannel(String rawPlayerName, String channel) { + if (!channel.equals("global") && !channel.equals("proximity") && !channel.equals("prox")) + throw new CommandError("Invalid channel name: " + channel); - PlayerChannelConfig t = playerProxData.get(rawPlayerName); - t.isGlobal = mode.equals("global"); - playerProxData.replace(rawPlayerName, t); + getPlayerChannelData(rawPlayerName).isGlobal = channel.equals("global"); } public static void setPlayerRadius(String rawPlayerName, int radius) { - if (!playerProxData.containsKey(rawPlayerName)) { - playerProxData.put(rawPlayerName, new PlayerChannelConfig(true, radius)); - return; - } - - PlayerChannelConfig t = playerProxData.get(rawPlayerName); - t.radius = radius; - playerProxData.replace(rawPlayerName, t); + getPlayerChannelData(rawPlayerName).radius = radius; } }