Browse Source

new features for chat tweak: colored chat, chat radius

pull/193/head
Adrian Siekierka 1 year ago
parent
commit
db197b7a94

+ 9
- 0
src/main/java/pl/asie/charset/lib/config/ConfigUtils.java View File

@@ -43,6 +43,15 @@ public final class ConfigUtils {
return prop.getBoolean();
}

public static String getString(Configuration config, String category, String name, String defaultValue, String comment, boolean requiresRestart) {
prepareCategory(config, category);
Property prop = config.get(category, name, defaultValue);
prop.setComment(comment);
prop.setRequiresMcRestart(requiresRestart);
prop.setLanguageKey("config.charset." + ModuleLoader.moduleConfigs.inverse().get(config) + "." + name + ".name");
return prop.getString();
}

public static String[] getStringList(Configuration config, String category, String name, String[] defaultValue, String comment, boolean requiresRestart) {
prepareCategory(config, category);
Property prop = config.get(category, name, defaultValue);

+ 95
- 11
src/main/java/pl/asie/charset/module/tweak/chat/CharsetTweakChat.java View File

@@ -19,7 +19,12 @@

package pl.asie.charset.module.tweak.chat;

import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.server.MinecraftServer;
import net.minecraft.util.text.ITextComponent;
import net.minecraft.util.text.TextComponentString;
import net.minecraft.util.text.TextComponentTranslation;
import net.minecraft.util.text.TextFormatting;
import net.minecraftforge.common.config.Configuration;
import net.minecraftforge.event.ServerChatEvent;
@@ -29,8 +34,11 @@ import pl.asie.charset.lib.config.CharsetLoadConfigEvent;
import pl.asie.charset.lib.config.ConfigUtils;
import pl.asie.charset.lib.loader.CharsetModule;
import pl.asie.charset.lib.loader.ModuleProfile;
import pl.asie.charset.lib.utils.ColorUtils;
import pl.asie.charset.lib.utils.EntityUtils;

import java.awt.*;

@CharsetModule(
name = "tweak.chat",
description = "Various chat tweaks",
@@ -41,29 +49,105 @@ public class CharsetTweakChat {
@CharsetModule.Configuration
public static Configuration config;

public static boolean enableGreentext;
public static String shoutPrefix;
public static int chatRadius;
public static boolean chatRadiusEnableShout, enableChatRadius, enableGreentext, enableColoredChat;

private String applyColors(String playerMsgStr) {
for (int i = 0; i < playerMsgStr.length() - 1; i++) {
if (playerMsgStr.charAt(i) == '&') {
String comp = "\u00a7" + playerMsgStr.charAt(i + 1);
for (TextFormatting formatting : TextFormatting.values()) {
if (comp.equals(formatting.toString())) {
playerMsgStr = playerMsgStr.substring(0, i) + "\u00a7" + playerMsgStr.substring(i + 1);
break;
}
}
break;
}
}
return playerMsgStr;
}

@Mod.EventHandler
public void loadConfig(CharsetLoadConfigEvent event) {
enableGreentext = ConfigUtils.getBoolean(config, "features","greentext", true, "Enables >implications, I suppose.", false);
enableColoredChat = ConfigUtils.getBoolean(config, "features", "coloredChat", false, "Colored chat! We all love colored chat, don't we? &cOf course we do!", false);
enableChatRadius = ConfigUtils.getBoolean(config, "features", "chatDistanceLimit", false, "Adds a distance limit system for chat.", false);

chatRadius = ConfigUtils.getInt(config, "chatDistanceLimit", "radius", 32, 0, Integer.MAX_VALUE, "The maximum chat distance in blocks.", false);
chatRadiusEnableShout = ConfigUtils.getBoolean(config, "chatDistanceLimit", "shoutEnabled", true, "Is the shout prefix (!msg) enabled?", false);
shoutPrefix = applyColors(ConfigUtils.getString(config, "chatDistanceLimit", "shoutPrefix", "&e[Shout]", "Prefix for shout messages.", false));
}

@SubscribeEvent
public void onServerChat(ServerChatEvent event) {
if (!EntityUtils.isPlayerFake(event.getPlayer())) {
String message = event.getMessage();
ITextComponent message = event.getComponent();
if (message instanceof TextComponentTranslation
&& ((TextComponentTranslation) message).getFormatArgs().length == 2
&& "chat.type.text".equals(((TextComponentTranslation) message).getKey())) {
TextComponentTranslation messageTr = (TextComponentTranslation) message;
Object playerMessage = messageTr.getFormatArgs()[1];
if (playerMessage instanceof TextComponentString) {
String playerMsgStr = ((TextComponentString) playerMessage).getText();

for (int i = 0; i < message.length() - 1; i++) {
if (message.charAt(i) == '>'
&& (i == 0 || Character.isWhitespace(message.codePointAt(i - 1)))
&& Character.isLetterOrDigit(message.codePointAt(i + 1))) {
message = message.substring(0, i) + TextFormatting.GREEN + message.substring(i);
break;
for (int i = 0; i < playerMsgStr.length() - 1; i++) {
if (enableGreentext) {
if (playerMsgStr.charAt(i) == '>'
&& (i == 0 || Character.isWhitespace(playerMsgStr.codePointAt(i - 1)))
&& Character.isLetterOrDigit(playerMsgStr.codePointAt(i + 1))) {
playerMsgStr = playerMsgStr.substring(0, i) + TextFormatting.GREEN + playerMsgStr.substring(i);
break;
}
}

if (enableColoredChat) {
if (playerMsgStr.charAt(i) == '&') {
String comp = "\u00a7" + playerMsgStr.charAt(i + 1);
for (TextFormatting formatting : TextFormatting.values()) {
if (comp.equals(formatting.toString())) {
playerMsgStr = playerMsgStr.substring(0, i) + "\u00a7" + playerMsgStr.substring(i + 1);
break;
}
}
break;
}
}
}

boolean isShout = false;
if (enableChatRadius && chatRadiusEnableShout && playerMsgStr.startsWith("!")) {
isShout = true;
playerMsgStr = playerMsgStr.substring(1);
}

ITextComponent msgResult = new TextComponentTranslation(
messageTr.getKey(),
messageTr.getFormatArgs()[0],
new TextComponentString(playerMsgStr)
);

if (isShout) {
msgResult = new TextComponentTranslation(
"chat.charset.shout",
shoutPrefix,
msgResult
);
}

if (enableChatRadius) {
event.setCanceled(true);
for (EntityPlayer player : event.getPlayer().getServerWorld().playerEntities) {
if (chatRadius <= 0 || player.getDistance(event.getPlayer()) <= chatRadius) {
player.sendMessage(msgResult);
}
}
} else {
event.setComponent(msgResult);
}
}
}

String messageFull = "<" + event.getUsername() + "> " + message;
event.setComponent(new TextComponentString(messageFull));
}
}
}

+ 2
- 0
src/main/resources/assets/charset/lang/en_us.lang View File

@@ -1,3 +1,5 @@
chat.charset.shout=%1$s %2$s

# Config
config.charset.title=Charset Configuration
config.charset.profile.name=Profile

Loading…
Cancel
Save