Skip to content

Commit

Permalink
fix: clean up code
Browse files Browse the repository at this point in the history
  • Loading branch information
rootEnginear committed Oct 15, 2023
1 parent 5c313f4 commit 01370b9
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -25,19 +24,18 @@ 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;
}

switch (args[0]) {
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:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package rootenginear.proximitychat.store;

import net.minecraft.core.net.command.CommandError;
import rootenginear.proximitychat.ProximityChat;
import rootenginear.proximitychat.struct.PlayerChannelConfig;

Expand All @@ -9,36 +10,17 @@ public class PlayerChannelData {
public static HashMap<String, PlayerChannelConfig> 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;
}
}

0 comments on commit 01370b9

Please sign in to comment.