Skip to content

Commit

Permalink
feat: add /radius command
Browse files Browse the repository at this point in the history
  • Loading branch information
rootEnginear committed Oct 15, 2023
1 parent 4b1ae56 commit 36ae7a6
Show file tree
Hide file tree
Showing 9 changed files with 147 additions and 55 deletions.
12 changes: 7 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,17 @@

# Proximity Chat

Select your chat channel and keep the chat within a circle.
Communicate with other players within a certain radius. Talking to your friends in private, or roleplaying with ease.

> **Important**
> - This is a **server-side mod**. You must install it on the server.
> - Required [Babric](https://github.com/Turnip-Labs/babric-instance-repo/releases) to run the mod.
## Features

- Chat can be sent to a set radius of the player using proximity channel.
- Select your channel with `/channel global` or `/channel prox`. Your later message will be sent into the selected channel.
- If you're in the global chat, you can quickly send a proximity message by adding `# ` (a hash and 1 space) in front of your message. Eg: `# Is anyone around me?`.
- Set your proximity radius with `/radius <radius>`. Eg: `/radius 50`.
- Select your channel with `/channel global` or `/channel prox`.
- Change the radius of your proximity chat with `/radius <radius>` (Eg: `/radius 50`).
- Quickly send a proximity message from the global channel (and vice versa!) by adding `# ` (hash and space) to the
beginning of your message. Eg: `# Is anyone around me?`.
- Server operators can change the default (`default-proximity-radius=50`) and maximum proximity
radii (`max-proximity-radius=50`) in the `server.properties` file.
11 changes: 9 additions & 2 deletions src/main/java/rootenginear/proximitychat/ProximityChat.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,20 @@ public class ProximityChat implements DedicatedServerModInitializer {
public static final String MOD_ID = "proximitychat";
public static final Logger LOGGER = LoggerFactory.getLogger(MOD_ID);

public static int RADIUS;
public static int DEFAULT_RADIUS;
public static int MAX_RADIUS;

@Override
public void onInitializeServer() {
PropertyManager propertyManagerObj = new PropertyManager(new File("server.properties"));

RADIUS = propertyManagerObj.getIntProperty("default-proximity-radius", 50);
DEFAULT_RADIUS = propertyManagerObj.getIntProperty("default-proximity-radius", 50);
MAX_RADIUS = propertyManagerObj.getIntProperty("max-proximity-radius", 50);

if (DEFAULT_RADIUS > MAX_RADIUS) {
MAX_RADIUS = DEFAULT_RADIUS;
propertyManagerObj.setProperty("max-proximity-radius", DEFAULT_RADIUS);
}

LOGGER.info("Proximity Chat initialized.");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,22 @@ public boolean execute(CommandHandler handler, CommandSender sender, String[] ar
if (args.length > 1) {
return false;
}
String name = sender.getName();

String colorfulName = sender.getName() + "§r";
String rawName = sender.getName().replaceFirst("§.", "");

if (args.length == 0) {
PlayerChannelConfig cfg = getPlayerChannelData(name);
sender.sendMessage(name + "'s current channel is: " + (cfg.isGlobal ? "Global" : "Proximity"));
PlayerChannelConfig cfg = getPlayerChannelData(rawName);
sender.sendMessage(colorfulName + "'s current channel is: " + (cfg.isGlobal ? "Global" : "Proximity"));
return true;
}

switch (args[0]) {
case "global":
case "proximity":
case "prox":
setPlayerChannelData(name, args[0]);
sender.sendMessage("Changed " + name + "'s channel to: " + (args[0].equals("global") ? "Global" : "Proximity"));
setPlayerChannelData(rawName, args[0]);
sender.sendMessage("Changed " + colorfulName + "'s channel to: " + (args[0].equals("global") ? "Global" : "Proximity"));
return true;
default:
return false;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package rootenginear.proximitychat.command;

import net.minecraft.core.net.command.CommandError;
import net.minecraft.core.net.command.CommandHandler;
import net.minecraft.core.net.command.CommandSender;
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;

public class RadiusCommand extends ServerCommand {
public RadiusCommand(MinecraftServer server) {
super(server, "radius", "rad");
}

@Override
public boolean execute(CommandHandler handler, CommandSender sender, String[] args) {
if (!sender.isPlayer()) {
throw new CommandError("Must be used by a player!");
}
if (args.length > 1) {
return false;
}

String colorfulName = sender.getName() + "§r";
String rawName = sender.getName().replaceFirst("§.", "");

if (args.length == 0) {
PlayerChannelConfig cfg = getPlayerChannelData(rawName);
sender.sendMessage(colorfulName + "'s proximity chat radius is: " + cfg.radius);
return true;
}

int newRadius;
try {
newRadius = Integer.parseInt(args[0]);
} catch (Exception e) {
throw new CommandError("Not a number: \"" + args[0] + "\"");
}

if (newRadius > ProximityChat.MAX_RADIUS) {
sender.sendMessage("Your radius (" + newRadius + ") exceeds the limit (" + ProximityChat.MAX_RADIUS + ").");
newRadius = ProximityChat.MAX_RADIUS;
}

setPlayerRadius(rawName, newRadius);
sender.sendMessage("Changed " + colorfulName + "'s proximity chat radius to: " + newRadius);
return true;
}

@Override
public boolean opRequired(String[] args) {
return false;
}

@Override
public void sendCommandSyntax(CommandHandler handler, CommandSender sender) {
sender.sendMessage("/radius");
sender.sendMessage("/radius <number>");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import rootenginear.proximitychat.command.ChannelCommand;
import rootenginear.proximitychat.command.RadiusCommand;

import java.util.List;

Expand All @@ -20,5 +21,6 @@ public class CommandsMixin {
@Inject(method = "initServerCommands", at = @At("TAIL"))
private static void addChannelCmd(MinecraftServer server, CallbackInfo ci) {
commands.add(new ChannelCommand(server));
commands.add(new RadiusCommand(server));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,19 @@ public class NetServerHandlerMixin {
)
)
private void proximityChat(ServerConfigurationManager instance, String s) {
Pattern playerChatPattern = Pattern.compile("<(.+?)> §0(.+)");
Pattern playerChatPattern = Pattern.compile("<§.(.+?)§r> §0(.+)");
Matcher result = playerChatPattern.matcher(s);

if (!result.matches()) {
instance.sendEncryptedChatToAllPlayers(s);
return;
}

String playerName = result.group(1);
String rawPlayerName = result.group(1);
String msg = result.group(2);

PlayerChannelConfig playerData = getPlayerChannelData(playerName);
if (playerData.isGlobal || msg.startsWith("# ")) {
PlayerChannelConfig playerData = getPlayerChannelData(rawPlayerName);
if (playerData.isGlobal && !msg.startsWith("# ") || !playerData.isGlobal && msg.startsWith("# ")) {
instance.sendEncryptedChatToAllPlayers(s.replaceFirst("# ", ""));
return;
}
Expand All @@ -50,7 +50,7 @@ private void proximityChat(ServerConfigurationManager instance, String s) {
this.playerEntity.z,
playerData.radius,
this.playerEntity.dimension,
new Packet3Chat("<✉ " + playerName + "> §0" + msg.replaceFirst("# ", ""))
new Packet3Chat("<✉ " + rawPlayerName + "> §0" + msg.replaceFirst("# ", ""))
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,37 @@
public class PlayerChannelData {
public static HashMap<String, PlayerChannelConfig> playerProxData = new HashMap<>();

public static PlayerChannelConfig getPlayerChannelData(String playerName) {
if (playerProxData.containsKey(playerName)) return playerProxData.get(playerName);
public static PlayerChannelConfig getPlayerChannelData(String rawPlayerName) {
if (playerProxData.containsKey(rawPlayerName)) {
return playerProxData.get(rawPlayerName);
}

PlayerChannelConfig t = new PlayerChannelConfig(true, ProximityChat.RADIUS);
playerProxData.put(playerName, t);
PlayerChannelConfig t = new PlayerChannelConfig(true, ProximityChat.DEFAULT_RADIUS);
playerProxData.put(rawPlayerName, t);
return t;
}

public static void setPlayerChannelData(String playerName, String mode) {
public static void setPlayerChannelData(String rawPlayerName, String mode) {
if (!mode.equals("global") && !mode.equals("proximity") && !mode.equals("prox")) return;

if (!playerProxData.containsKey(playerName)) {
playerProxData.put(playerName, new PlayerChannelConfig(mode.equals("global"), ProximityChat.RADIUS));
if (!playerProxData.containsKey(rawPlayerName)) {
playerProxData.put(rawPlayerName, new PlayerChannelConfig(mode.equals("global"), ProximityChat.DEFAULT_RADIUS));
return;
}

PlayerChannelConfig t = playerProxData.get(playerName);
PlayerChannelConfig t = playerProxData.get(rawPlayerName);
t.isGlobal = mode.equals("global");
playerProxData.replace(playerName, t);
playerProxData.replace(rawPlayerName, t);
}

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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

public class PlayerChannelConfig {
public boolean isGlobal;
public int radius;
public int radius;

public PlayerChannelConfig(boolean isGlobal, int radius) {
this.isGlobal = isGlobal;
public PlayerChannelConfig(boolean isGlobal, int radius) {
this.isGlobal = isGlobal;
this.radius = radius;
}
}
}
50 changes: 25 additions & 25 deletions src/main/resources/fabric.mod.json
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
{
"schemaVersion": 1,
"id": "playground",
"version": "${version}",
"name": "Playground",
"description": "This is the beginning of awesome mods!",
"authors": [
"rootEnginear"
],
"schemaVersion": 1,
"id": "proximitychat",
"version": "${version}",
"name": "Proximity Chat",
"description": "Communicate with other players within a certain radius.",
"authors": [
"rootEnginear"
],
"icon": "icon.png",
"contact": {
"homepage": "https://github.com/rootEnginear/bta-rootenginear-mods",
"sources": "https://github.com/rootEnginear/bta-rootenginear-mods",
"issues": "https://github.com/rootEnginear/bta-rootenginear-mods/issues"
},
"license": "CC0-1.0",
"environment": "*",
"entrypoints": {
"server": [
"contact": {
"homepage": "https://github.com/rootEnginear/bta-rootenginear-mods/tree/proximity-chat",
"sources": "https://github.com/rootEnginear/bta-rootenginear-mods/tree/proximity-chat",
"issues": "https://github.com/rootEnginear/bta-rootenginear-mods/issues"
},
"license": "CC0-1.0",
"environment": "*",
"entrypoints": {
"server": [
"rootenginear.proximitychat.ProximityChat"
]
},
"mixins": [
"proximitychat.mixins.json"
],
"depends": {
"fabricloader": ">=0.13.3"
}
]
},
"mixins": [
"proximitychat.mixins.json"
],
"depends": {
"fabricloader": ">=0.13.3"
}
}

0 comments on commit 36ae7a6

Please sign in to comment.