Skip to content

Commit

Permalink
Fixed bug causing styles to not work properly
Browse files Browse the repository at this point in the history
  • Loading branch information
abc123me committed Dec 11, 2022
1 parent 6aaca2b commit c089ee8
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 68 deletions.
28 changes: 19 additions & 9 deletions runClient.launch
Original file line number Diff line number Diff line change
@@ -1,12 +1,22 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<launchConfiguration type="org.eclipse.jdt.launching.localJavaApplication">
<stringAttribute key="org.eclipse.jdt.launching.PROJECT_ATTR" value="BetterForgeChat"/>
<stringAttribute key="org.eclipse.jdt.launching.MAIN_TYPE" value="cpw.mods.bootstraplauncher.BootstrapLauncher"/>
<stringAttribute key="org.eclipse.jdt.launching.VM_ARGUMENTS" value="-Dforge.logging.console.level=debug -Dforge.logging.markers=REGISTRIES -DlegacyClassPath.file=/home/jeremiah/Programming/BetterForgeChat/build/classpath/runClient_minecraftClasspath.txt -DignoreList=bootstraplauncher,securejarhandler,asm-commons,asm-util,asm-analysis,asm-tree,asm,client-extra,fmlcore,javafmllanguage,lowcodelanguage,mclanguage,forge- -DnativesDirectory=/home/jeremiah/Programming/BetterForgeChat/build/natives -DmergeModules=jna-5.10.0.jar,jna-platform-5.10.0.jar,java-objc-bridge-1.0.0.jar -Dforge.enabledGameTestNamespaces=bfcmod -Dforge.enableGameTest=true -Djava.net.preferIPv6Addresses=system -p /home/jeremiah/.gradle/caches/modules-2/files-2.1/cpw.mods/bootstraplauncher/1.0.0/f55ddbb35612c6745cefc47d1a0d6dbfb25d3982/bootstraplauncher-1.0.0.jar:/home/jeremiah/.gradle/caches/modules-2/files-2.1/cpw.mods/securejarhandler/1.0.3/acbf5e3c8a38fb8bf824a54fe45eee121c510392/securejarhandler-1.0.3.jar:/home/jeremiah/.gradle/caches/modules-2/files-2.1/org.ow2.asm/asm-commons/9.2/f4d7f0fc9054386f2893b602454d48e07d4fbead/asm-commons-9.2.jar:/home/jeremiah/.gradle/caches/modules-2/files-2.1/org.ow2.asm/asm-util/9.2/fbc178fc5ba3dab50fd7e8a5317b8b647c8e8946/asm-util-9.2.jar:/home/jeremiah/.gradle/caches/modules-2/files-2.1/org.ow2.asm/asm-analysis/9.2/7487dd756daf96cab9986e44b9d7bcb796a61c10/asm-analysis-9.2.jar:/home/jeremiah/.gradle/caches/modules-2/files-2.1/org.ow2.asm/asm-tree/9.2/d96c99a30f5e1a19b0e609dbb19a44d8518ac01e/asm-tree-9.2.jar:/home/jeremiah/.gradle/caches/modules-2/files-2.1/org.ow2.asm/asm/9.2/81a03f76019c67362299c40e0ba13405f5467bff/asm-9.2.jar --add-modules ALL-MODULE-PATH --add-opens java.base/java.util.jar=cpw.mods.securejarhandler --add-exports java.base/sun.security.util=cpw.mods.securejarhandler --add-exports jdk.naming.dns/com.sun.jndi.dns=java.naming"/>
<stringAttribute key="org.eclipse.jdt.launching.PROGRAM_ARGUMENTS" value="--launchTarget forgeclientuserdev --version MOD_DEV --assetIndex 1.18 --assetsDir /home/jeremiah/.gradle/caches/forge_gradle/assets --gameDir . --fml.forgeVersion 40.1.74 --fml.mcVersion 1.18.2 --fml.forgeGroup net.minecraftforge --fml.mcpVersion 20220404.173914"/>
<stringAttribute key="org.eclipse.jdt.launching.WORKING_DIRECTORY" value="/home/jeremiah/Programming/BetterForgeChat/run"/>
<mapAttribute key="org.eclipse.debug.core.environmentVariables">
<mapEntry key="MOD_CLASSES" value="bfcmod%%/home/jeremiah/Programming/BetterForgeChat/bin/main:bfcmod%%/home/jeremiah/Programming/BetterForgeChat/bin/main"/>
<mapEntry key="MCP_MAPPINGS" value="official_1.18.2"/>
</mapAttribute>
<listAttribute key="org.eclipse.debug.core.MAPPED_RESOURCE_PATHS">
<listEntry value="/BetterForgeChat"/>
</listAttribute>
<listAttribute key="org.eclipse.debug.core.MAPPED_RESOURCE_TYPES">
<listEntry value="4"/>
</listAttribute>
<mapAttribute key="org.eclipse.debug.core.environmentVariables">
<mapEntry key="MCP_MAPPINGS" value="official_1.18.2"/>
<mapEntry key="MOD_CLASSES" value="bfcmod%%/home/jeremiah/Programming/BetterForgeChat/bin/main:bfcmod%%/home/jeremiah/Programming/BetterForgeChat/bin/main"/>
</mapAttribute>
<booleanAttribute key="org.eclipse.jdt.launching.ATTR_ATTR_USE_ARGFILE" value="false"/>
<booleanAttribute key="org.eclipse.jdt.launching.ATTR_SHOW_CODEDETAILS_IN_EXCEPTION_MESSAGES" value="true"/>
<booleanAttribute key="org.eclipse.jdt.launching.ATTR_USE_CLASSPATH_ONLY_JAR" value="false"/>
<stringAttribute key="org.eclipse.jdt.launching.MAIN_TYPE" value="cpw.mods.bootstraplauncher.BootstrapLauncher"/>
<stringAttribute key="org.eclipse.jdt.launching.MODULE_NAME" value="BetterForgeChat"/>
<stringAttribute key="org.eclipse.jdt.launching.PROGRAM_ARGUMENTS" value="--launchTarget forgeclientuserdev --version MOD_DEV --assetIndex 1.18 --assetsDir /home/jeremiah/.gradle/caches/forge_gradle/assets --gameDir . --fml.forgeVersion 40.1.74 --fml.mcVersion 1.18.2 --fml.forgeGroup net.minecraftforge --fml.mcpVersion 20220404.173914"/>
<stringAttribute key="org.eclipse.jdt.launching.PROJECT_ATTR" value="BetterForgeChat"/>
<stringAttribute key="org.eclipse.jdt.launching.VM_ARGUMENTS" value="-Dforge.logging.console.level=debug -Dforge.logging.markers=REGISTRIES -DlegacyClassPath.file=/home/jeremiah/Programming/BetterForgeChat/build/classpath/runClient_minecraftClasspath.txt -DignoreList=bootstraplauncher,securejarhandler,asm-commons,asm-util,asm-analysis,asm-tree,asm,client-extra,fmlcore,javafmllanguage,lowcodelanguage,mclanguage,forge- -DnativesDirectory=/home/jeremiah/Programming/BetterForgeChat/build/natives -DmergeModules=jna-5.10.0.jar,jna-platform-5.10.0.jar,java-objc-bridge-1.0.0.jar -Dforge.enabledGameTestNamespaces=bfcmod -Dforge.enableGameTest=true -Djava.net.preferIPv6Addresses=system -p /home/jeremiah/.gradle/caches/modules-2/files-2.1/cpw.mods/bootstraplauncher/1.0.0/f55ddbb35612c6745cefc47d1a0d6dbfb25d3982/bootstraplauncher-1.0.0.jar:/home/jeremiah/.gradle/caches/modules-2/files-2.1/cpw.mods/securejarhandler/1.0.3/acbf5e3c8a38fb8bf824a54fe45eee121c510392/securejarhandler-1.0.3.jar:/home/jeremiah/.gradle/caches/modules-2/files-2.1/org.ow2.asm/asm-commons/9.2/f4d7f0fc9054386f2893b602454d48e07d4fbead/asm-commons-9.2.jar:/home/jeremiah/.gradle/caches/modules-2/files-2.1/org.ow2.asm/asm-util/9.2/fbc178fc5ba3dab50fd7e8a5317b8b647c8e8946/asm-util-9.2.jar:/home/jeremiah/.gradle/caches/modules-2/files-2.1/org.ow2.asm/asm-analysis/9.2/7487dd756daf96cab9986e44b9d7bcb796a61c10/asm-analysis-9.2.jar:/home/jeremiah/.gradle/caches/modules-2/files-2.1/org.ow2.asm/asm-tree/9.2/d96c99a30f5e1a19b0e609dbb19a44d8518ac01e/asm-tree-9.2.jar:/home/jeremiah/.gradle/caches/modules-2/files-2.1/org.ow2.asm/asm/9.2/81a03f76019c67362299c40e0ba13405f5467bff/asm-9.2.jar --add-modules ALL-MODULE-PATH --add-opens java.base/java.util.jar=cpw.mods.securejarhandler --add-exports java.base/sun.security.util=cpw.mods.securejarhandler --add-exports jdk.naming.dns/com.sun.jndi.dns=java.naming"/>
<stringAttribute key="org.eclipse.jdt.launching.WORKING_DIRECTORY" value="/home/jeremiah/Programming/BetterForgeChat/run"/>
</launchConfiguration>
45 changes: 45 additions & 0 deletions src/main/java/com/jeremiahbl/bfcmod/BitwiseStyling.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.jeremiahbl.bfcmod;

import net.minecraft.ChatFormatting;
import net.minecraft.network.chat.TextComponent;

public class BitwiseStyling {
public static final byte BOLD_BIT = 1;
public static final byte UNDERLINE_BIT = 2;
public static final byte ITALIC_BIT = 4;
public static final byte OBFUSCATED_BIT = 8;
public static final byte STRIKETHROUGH_BIT = 16;

public static final byte NO_STYLE = 0;
public static final byte ALL_STYLES =
BOLD_BIT | UNDERLINE_BIT | ITALIC_BIT | OBFUSCATED_BIT | STRIKETHROUGH_BIT;

public static final String styleString(byte mask) {
String out = "";
if((mask & BitwiseStyling.BOLD_BIT) != 0) out += TextFormatter.BOLD_FORMAT;
if((mask & BitwiseStyling.ITALIC_BIT) != 0) out += TextFormatter.ITALIC_FORMAT;
if((mask & BitwiseStyling.UNDERLINE_BIT) != 0) out += TextFormatter.UNDERLINE_FORMAT;
if((mask & BitwiseStyling.STRIKETHROUGH_BIT) != 0) out += TextFormatter.STRIKETHROUGH_FORMAT;
if((mask & BitwiseStyling.OBFUSCATED_BIT) != 0) out += TextFormatter.OBFUSCATED_FORMAT;
return out;
}
public static final TextComponent makeEncapsulatingTextComponent(String msg, byte mask) {
TextComponent out = new TextComponent(msg);
if((mask & BitwiseStyling.BOLD_BIT) != 0) out.withStyle(ChatFormatting.BOLD);
if((mask & BitwiseStyling.ITALIC_BIT) != 0) out.withStyle(ChatFormatting.ITALIC);
if((mask & BitwiseStyling.UNDERLINE_BIT) != 0) out.withStyle(ChatFormatting.UNDERLINE);
if((mask & BitwiseStyling.STRIKETHROUGH_BIT) != 0) out.withStyle(ChatFormatting.STRIKETHROUGH);
if((mask & BitwiseStyling.OBFUSCATED_BIT) != 0) out.withStyle(ChatFormatting.OBFUSCATED);
return out;
}
public static final byte getStyleBit(char c) {
switch(c) {
case 'l': return BOLD_BIT;
case 'n': return UNDERLINE_BIT;
case 'o': return ITALIC_BIT;
case 'k': return OBFUSCATED_BIT;
case 'm': return STRIKETHROUGH_BIT;
default: return 0;
}
}
}
35 changes: 9 additions & 26 deletions src/main/java/com/jeremiahbl/bfcmod/MarkdownFormatter.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,8 @@
package com.jeremiahbl.bfcmod;

public class MarkdownFormatter {
public static final byte MARKDOWN_BOLD_BIT = 1;
public static final byte MARKDOWN_ITALIC_BIT = 2;
public static final byte MARKDOWN_UNLINE_BIT = 4;
public static final byte MARKDOWN_STRIKE_BIT = 8;
public static final byte MARKDOWN_MAGIC_BIT = 16;
public static final byte MARKDOWN_ALL_FEATURES
= MARKDOWN_BOLD_BIT | MARKDOWN_ITALIC_BIT | MARKDOWN_UNLINE_BIT | MARKDOWN_STRIKE_BIT | MARKDOWN_MAGIC_BIT;

public static final String markdownStringToFormattedString(String md) {
return markdownStringToFormattedString(md, MARKDOWN_ALL_FEATURES);
return markdownStringToFormattedString(md, BitwiseStyling.ALL_STYLES);
}
public static final String markdownStringToFormattedString(String md, byte allowedMask) {
String newStr = "";
Expand Down Expand Up @@ -48,21 +40,21 @@ public static final String markdownStringToFormattedString(String md, byte allow
newStr += c;
}
}
return newStr + TextFormatter.RESET_ALL_FORMAT;
return newStr.trim();
}
private static byte convertMD(byte mask, char[] chrs, int len) {
char c1 = len > 0 ? chrs[0] : ' ';
char c2 = len > 1 ? chrs[1] : ' ';
if((c1 == '*' && c2 == ' ') || (c1 == '_' && c2 == ' '))
return bitToggle(mask, MARKDOWN_ITALIC_BIT);
return bitToggle(mask, BitwiseStyling.ITALIC_BIT);
else if((c1 == '~' && c2 == ' '))
return bitToggle(mask, MARKDOWN_MAGIC_BIT);
return bitToggle(mask, BitwiseStyling.OBFUSCATED_BIT);
else if((c1 == '*' && c2 == '*'))
return bitToggle(mask, MARKDOWN_BOLD_BIT);
return bitToggle(mask, BitwiseStyling.BOLD_BIT);
else if((c1 == '~' && c2 == '~'))
return bitToggle(mask, MARKDOWN_STRIKE_BIT);
return bitToggle(mask, BitwiseStyling.STRIKETHROUGH_BIT);
else if((c1 == '_' && c2 == '_'))
return bitToggle(mask, MARKDOWN_UNLINE_BIT);
return bitToggle(mask, BitwiseStyling.UNDERLINE_BIT);
else return mask;
}
private static String maskDiff(byte newMask, byte oldMask, byte allowedMask) {
Expand All @@ -71,9 +63,9 @@ private static String maskDiff(byte newMask, byte oldMask, byte allowedMask) {
byte stylesNew = bitCount(newMask);
byte stylesOld = bitCount(oldMask);
if(stylesNew > stylesOld) // Styles were added
return styleString((byte) (newMask & (~oldMask) & allowedMask));
return BitwiseStyling.styleString((byte) (newMask & (~oldMask) & allowedMask));
else if(stylesNew < stylesOld) { // Styles were removed
return TextFormatter.RESET_ALL_FORMAT + styleString((byte)(newMask & allowedMask));
return TextFormatter.RESET_ALL_FORMAT + BitwiseStyling.styleString((byte)(newMask & allowedMask));
} else { // Styles shifted
return "MARKDOWN FORMATTER INVALID STATE - REPORT TO DEVELOPER";
}
Expand All @@ -89,15 +81,6 @@ private static byte bitToggle(byte mask, byte bit) {
else mask |= bit;
return mask;
}
private static String styleString(byte mask) {
String out = "";
if((mask & MARKDOWN_BOLD_BIT) != 0) out += TextFormatter.BOLD_FORMAT;
if((mask & MARKDOWN_ITALIC_BIT) != 0) out += TextFormatter.ITALIC_FORMAT;
if((mask & MARKDOWN_UNLINE_BIT) != 0) out += TextFormatter.UNDERLINE_FORMAT;
if((mask & MARKDOWN_STRIKE_BIT) != 0) out += TextFormatter.STRIKETHROUGH_FORMAT;
if((mask & MARKDOWN_MAGIC_BIT) != 0) out += TextFormatter.OBFUSCATED_FORMAT;
return out;
}
}

// Debugging porpoises only
Expand Down
65 changes: 36 additions & 29 deletions src/main/java/com/jeremiahbl/bfcmod/TextFormatter.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
public final class TextFormatter {
public static final String RESET_ALL_FORMAT = "&r";

public static final String BOLD_FORMAT = "&l";
public static final String UNDERLINE_FORMAT = "&l";
public static final String ITALIC_FORMAT = "&o";
public static final String OBFUSCATED_FORMAT = "&k";
public static final String BOLD_FORMAT = "&l";
public static final String UNDERLINE_FORMAT = "&n";
public static final String ITALIC_FORMAT = "&o";
public static final String OBFUSCATED_FORMAT = "&k";
public static final String STRIKETHROUGH_FORMAT = "&m";

public static final String COLOR_BLACK = "&0";
Expand Down Expand Up @@ -38,7 +38,7 @@ public static final TextComponent stringToFormattedText(String msg, boolean enab
boolean nextIsStyle = false;
String curStr = "";
ChatFormatting curColor = ChatFormatting.WHITE;
ChatFormatting curStyle = ChatFormatting.RESET;
byte curStyle = 0;
for(int i = 0; i < msg.length(); i++) {
char c = msg.charAt(i);
if(c == '&') {
Expand All @@ -47,21 +47,28 @@ public static final TextComponent stringToFormattedText(String msg, boolean enab
curStr += "&";
} else nextIsStyle = true;
} else if(nextIsStyle) {
if(isColorOrStyle(c)) {
TextComponent tmp = new TextComponent(curStr);
tmp.withStyle(curStyle);
boolean makeNewComponent = false;
if(isStyle(c)) {
curStyle |= BitwiseStyling.getStyleBit(c);
makeNewComponent = c == 'r';
} else if(isColor(c)) {
makeNewComponent = true;
} else curStr += ("&" + c);
if(makeNewComponent) {
TextComponent tmp = BitwiseStyling.makeEncapsulatingTextComponent(curStr, enableStyles ? curStyle : 0);
tmp.withStyle(curColor);
newMsg.append(tmp);
if(enableColors) curColor = getColor(c, curColor);
if(enableStyles) curStyle = getStyle(c, curStyle);
if(enableColors)
curColor = getColor(c, curColor);
curStr = "";
} else curStr += ("&" + c);
if(makeNewComponent && c == 'r')
curStyle = 0;
}
nextIsStyle = false;
} else curStr += c;
}
if(curStr.length() > 0) {
TextComponent tmp = new TextComponent(curStr);
tmp.withStyle(curStyle);
TextComponent tmp = BitwiseStyling.makeEncapsulatingTextComponent(curStr, enableStyles ? curStyle : 0);
tmp.withStyle(curColor);
newMsg.append(tmp);
}
Expand Down Expand Up @@ -99,8 +106,8 @@ public static final boolean messageContainsColorsOrStyles(String msg, boolean ch
if(checkNext) checkNext = false;
else checkNext = true;
} else if(checkNext) {
if(checkColors) { if(getColor(c, null) != null) return true; }
else { if(getStyle(c, null) != null) return true; }
if(checkColors) { if(isColor(c)) return true; }
else { if(isStyle(c)) return true; }
}
}
return false;
Expand All @@ -110,10 +117,8 @@ public static final String colorString() {
"&fDark: &4&&4 &6&&6 &1&&1 &2&&2 &3&&3 &5&&5 &0&&0 &8&&8\n" +
"&fStyles: &l&&l&r &n&&n&r &o&&o&r &m&&m&r &k&&k&r\n";
}
private static final boolean isColorOrStyle(char c) {
return getColor(c, null) != null || getStyle(c, null) != null;
}
private static final ChatFormatting getColor(char c, ChatFormatting cur) {

private static final ChatFormatting getColor(char c, ChatFormatting cur) {
switch(c) {
case '0': return ChatFormatting.BLACK;
case '1': return ChatFormatting.DARK_BLUE;
Expand All @@ -135,15 +140,17 @@ private static final ChatFormatting getColor(char c, ChatFormatting cur) {
default: return cur;
}
}
private static final ChatFormatting getStyle(char c, ChatFormatting cur) {
switch(c) {
case 'l': return ChatFormatting.BOLD;
case 'n': return ChatFormatting.UNDERLINE;
case 'o': return ChatFormatting.ITALIC;
case 'k': return ChatFormatting.OBFUSCATED;
case 'm': return ChatFormatting.STRIKETHROUGH;
case 'r': return ChatFormatting.RESET;
default: return cur;
}
private static final boolean isColorOrStyle(char c) {
return isColor(c) || isStyle(c);
}
private static final boolean isColor(char c) {
if(c >= '0' && c <= '9') return true;
if(c >= 'a' && c <= 'f') return true;
return false;
}
private static final boolean isStyle(char c) {
if(c >= 'k' && c <= 'o') return true;
if(c == 'r') return true;
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import java.util.Date;
import java.util.UUID;

import com.jeremiahbl.bfcmod.BetterForgeChat;
import com.jeremiahbl.bfcmod.MarkdownFormatter;
import com.jeremiahbl.bfcmod.TextFormatter;
import com.jeremiahbl.bfcmod.config.ConfigHandler;
Expand All @@ -15,9 +14,7 @@

import net.minecraft.ChatFormatting;
import net.minecraft.network.chat.ChatType;
import net.minecraft.network.chat.ClickEvent;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.HoverEvent;
import net.minecraft.network.chat.Style;
import net.minecraft.network.chat.TextComponent;
import net.minecraft.network.chat.TranslatableComponent;
Expand Down Expand Up @@ -94,6 +91,6 @@ public void onServerChat(ServerChatEvent e) {
TextComponent ecmp = new TextComponent("");
if(sty != null && sty.getHoverEvent() != null)
ecmp.setStyle(sty);
e.setComponent(ecmp.append(beforeMsg.append(msgComp.append(afterMsg))));
e.setComponent(beforeMsg.append(msgComp.append(afterMsg)));
}
}

0 comments on commit c089ee8

Please sign in to comment.