Skip to content

Commit

Permalink
feat: add files
Browse files Browse the repository at this point in the history
  • Loading branch information
rootEnginear committed Oct 14, 2023
1 parent fb75368 commit 4b1ae56
Show file tree
Hide file tree
Showing 13 changed files with 262 additions and 36 deletions.
14 changes: 8 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
<img align="right" height="128" width="128" alt="" loading="lazy" decoding="async" src="./src/main/resources/icon.png"/>

# Playground
# Proximity Chat

This is the beginning of awesome mods!
Select your chat channel and keep the chat within a circle.

> **Important**
> Required [Babric](https://github.com/Turnip-Labs/babric-instance-repo/releases) to run the mod.
> - 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

- Feature #1
- Feature #2
- Feature #3
- 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`.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ loader_version=0.14.19-babric.1-bta
# Mod
mod_version=1.0.0
mod_group=rootenginear
mod_name=playground
mod_name=proximitychat
15 changes: 0 additions & 15 deletions src/main/java/rootenginear/playground/Playground.java

This file was deleted.

24 changes: 24 additions & 0 deletions src/main/java/rootenginear/proximitychat/ProximityChat.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package rootenginear.proximitychat;

import net.fabricmc.api.DedicatedServerModInitializer;
import net.minecraft.core.net.PropertyManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;

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;

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

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

LOGGER.info("Proximity Chat initialized.");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
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.struct.PlayerChannelConfig;

import static rootenginear.proximitychat.store.PlayerChannelData.getPlayerChannelData;
import static rootenginear.proximitychat.store.PlayerChannelData.setPlayerChannelData;

public class ChannelCommand extends ServerCommand {
public ChannelCommand(MinecraftServer server) {
super(server, "channel", "ch");
}

@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 name = sender.getName();
if (args.length == 0) {
PlayerChannelConfig cfg = getPlayerChannelData(name);
sender.sendMessage(name + "'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"));
return true;
default:
return false;
}
}

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

@Override
public void sendCommandSyntax(CommandHandler handler, CommandSender sender) {
sender.sendMessage("/channel");
sender.sendMessage("/channel <global|prox>");
}
}
24 changes: 24 additions & 0 deletions src/main/java/rootenginear/proximitychat/mixin/CommandsMixin.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package rootenginear.proximitychat.mixin;

import net.minecraft.core.net.command.Command;
import net.minecraft.core.net.command.Commands;
import net.minecraft.server.MinecraftServer;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import rootenginear.proximitychat.command.ChannelCommand;

import java.util.List;

@Mixin(value = {Commands.class}, remap = false)
public class CommandsMixin {
@Shadow
public static List<Command> commands;

@Inject(method = "initServerCommands", at = @At("TAIL"))
private static void addChannelCmd(MinecraftServer server, CallbackInfo ci) {
commands.add(new ChannelCommand(server));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package rootenginear.proximitychat.mixin;

import net.minecraft.core.net.packet.Packet3Chat;
import net.minecraft.server.entity.player.EntityPlayerMP;
import net.minecraft.server.net.ServerConfigurationManager;
import net.minecraft.server.net.handler.NetServerHandler;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Redirect;
import rootenginear.proximitychat.struct.PlayerChannelConfig;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static rootenginear.proximitychat.store.PlayerChannelData.getPlayerChannelData;

@Mixin(value = {NetServerHandler.class}, remap = false)
public class NetServerHandlerMixin {
@Shadow
private EntityPlayerMP playerEntity;

@Redirect(
method = "handleChat(Lnet/minecraft/core/net/packet/Packet3Chat;)V",
at = @At(
value = "INVOKE",
target = "Lnet/minecraft/server/net/ServerConfigurationManager;sendEncryptedChatToAllPlayers(Ljava/lang/String;)V"
)
)
private void proximityChat(ServerConfigurationManager instance, String s) {
Pattern playerChatPattern = Pattern.compile("<(.+?)> §0(.+)");
Matcher result = playerChatPattern.matcher(s);

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

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

PlayerChannelConfig playerData = getPlayerChannelData(playerName);
if (playerData.isGlobal || msg.startsWith("# ")) {
instance.sendEncryptedChatToAllPlayers(s.replaceFirst("# ", ""));
return;
}
instance.sendPacketToPlayersAroundPoint(
this.playerEntity.x,
this.playerEntity.y,
this.playerEntity.z,
playerData.radius,
this.playerEntity.dimension,
new Packet3Chat("<✉ " + playerName + "> §0" + msg.replaceFirst("# ", ""))
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package rootenginear.proximitychat.mixin;

import net.minecraft.core.entity.player.EntityPlayer;
import net.minecraft.core.net.packet.Packet;
import net.minecraft.core.net.packet.Packet3Chat;
import net.minecraft.core.util.helper.AES;
import net.minecraft.server.entity.player.EntityPlayerMP;
import net.minecraft.server.net.ServerConfigurationManager;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

import java.util.List;

@Mixin(value = {ServerConfigurationManager.class}, remap = false)
public class ServerConfigurationManagerMixin {
@Shadow
public List<EntityPlayerMP> playerEntities;

@Inject(method = "func_28171_a", at = @At("HEAD"), cancellable = true)
public void sendEncryptedChatToPlayersAroundPoint(EntityPlayer entityplayer, double d, double d1, double d2, double d3, int i, Packet packet, CallbackInfo ci) {
if (packet instanceof Packet3Chat) {
for (EntityPlayerMP playerEntity : this.playerEntities) {
double d6;
double d5;
double d4;
if (playerEntity == entityplayer || playerEntity.dimension != i || !((d4 = d - playerEntity.x) * d4 + (d5 = d1 - playerEntity.y) * d5 + (d6 = d2 - playerEntity.z) * d6 < d3 * d3))
continue;
playerEntity.playerNetServerHandler.sendPacket(new Packet3Chat(((Packet3Chat) packet).message, AES.keyChain.get(playerEntity.username)));
}
ci.cancel();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package rootenginear.proximitychat.store;

import rootenginear.proximitychat.ProximityChat;
import rootenginear.proximitychat.struct.PlayerChannelConfig;

import java.util.HashMap;

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);

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

public static void setPlayerChannelData(String playerName, 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));
return;
}

PlayerChannelConfig t = playerProxData.get(playerName);
t.isGlobal = mode.equals("global");
playerProxData.replace(playerName, t);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package rootenginear.proximitychat.struct;

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

public PlayerChannelConfig(boolean isGlobal, int radius) {
this.isGlobal = isGlobal;
this.radius = radius;
}
}
6 changes: 3 additions & 3 deletions src/main/resources/fabric.mod.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@
"license": "CC0-1.0",
"environment": "*",
"entrypoints": {
"main": [
"rootenginear.playground.Playground"
"server": [
"rootenginear.proximitychat.ProximityChat"
]
},
"mixins": [
"playground.mixins.json"
"proximitychat.mixins.json"
],
"depends": {
"fabricloader": ">=0.13.3"
Expand Down
11 changes: 0 additions & 11 deletions src/main/resources/playground.mixins.json

This file was deleted.

14 changes: 14 additions & 0 deletions src/main/resources/proximitychat.mixins.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"required": true,
"minVersion": "0.8",
"package": "rootenginear.proximitychat.mixin",
"compatibilityLevel": "JAVA_8",
"server": [
"CommandsMixin",
"NetServerHandlerMixin",
"ServerConfigurationManagerMixin"
],
"injectors": {
"defaultRequire": 1
}
}

0 comments on commit 4b1ae56

Please sign in to comment.