Skip to content

Commit

Permalink
Fix wrapped chat lines missing colors
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexdoru committed Sep 16, 2023
1 parent 01d5c9e commit d78ae73
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/main/java/com/mitchej123/hodgepodge/LoadingConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public class LoadingConfig {
public boolean enlargePotionArray;
public boolean fixBibliocraftPackets;
public boolean fixBibliocraftPathSanitization;
public boolean fixChatWrappedColors;
public boolean fixComponentsPoppingOff;
public boolean fixContainerPutStacksInSlots;
public boolean fixDebugBoundingBox;
Expand Down Expand Up @@ -209,6 +210,7 @@ public LoadingConfig(File file) {

changeSprintCategory = config.get(Category.TWEAKS.toString(), "changeSprintCategory", true, "Moves the sprint keybind to the movement category").getBoolean();
fixContainerPutStacksInSlots = config.get(Category.FIXES.toString(), "fixContainerPutStacksInSlots", true, "Prevents crash if server sends container with wrong itemStack size").getBoolean();
fixChatWrappedColors = config.get(Category.FIXES.toString(), "fixChatWrappedColors", true, "Fix wrapped chat lines missing colors").getBoolean();
fixComponentsPoppingOff = config.get(Category.TWEAKS.toString(), "fixComponentsPoppingOff", true, "Fix Project Red components popping off on unloaded chunks").getBoolean();
fixDebugBoundingBox = config.get(Category.FIXES.toString(), "fixDebugBoundingBox", true, "Fixes the debug hitbox of the player beeing offset").getBoolean();
fixDimensionChangeHearts = config.get(Category.FIXES.toString(), "fixDimensionChangeHearts", true, "Fix losing bonus hearts on dimension change").getBoolean();
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/com/mitchej123/hodgepodge/mixins/Mixins.java
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,9 @@ public enum Mixins {
DISABLE_CREATIVE_TAB_ALL_SEARCH(new Builder("Disable the creative tab with search bar").setPhase(Phase.EARLY)
.addMixinClasses("minecraft.MixinGuiContainerCreative").setSide(Side.CLIENT)
.setApplyIf(() -> Common.config.removeCreativeSearchTab).addTargetedMod(TargetedMod.VANILLA)),
FIX_CHAT_COLOR_WRAPPING(new Builder("Fix wrapped chat lines missing colors").setPhase(Phase.EARLY)
.addMixinClasses("minecraft.MixinGuiNewChat_FixColorWrapping").setSide(Side.CLIENT)
.setApplyIf(() -> Common.config.fixChatWrappedColors).addTargetedMod(TargetedMod.VANILLA)),

// Ic2 adjustments
IC2_UNPROTECTED_GET_BLOCK_FIX(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.mitchej123.hodgepodge.mixins.early.minecraft;

import net.minecraft.client.gui.GuiNewChat;

import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.ModifyVariable;

import com.mitchej123.hodgepodge.util.StringUtil;

@Mixin(GuiNewChat.class)
public class MixinGuiNewChat_FixColorWrapping {

@Unique
private String hodgepodge$s1;

@ModifyVariable(
method = "func_146237_a",
at = @At(
value = "NEW",
target = "(Ljava/lang/String;)Lnet/minecraft/util/ChatComponentText;",
ordinal = 2,
shift = At.Shift.BEFORE),
name = "s1")
private String hodgepodge$captureS1(String s1) {
this.hodgepodge$s1 = s1;
return s1;
}

@ModifyVariable(
method = "func_146237_a",
at = @At(
value = "NEW",
target = "(Ljava/lang/String;)Lnet/minecraft/util/ChatComponentText;",
ordinal = 2,
shift = At.Shift.BEFORE),
name = "s2")
private String hodgepodge$fixColorWrapping(String s2) {
return StringUtil.getFormatFromString(this.hodgepodge$s1) + s2;
}

}
43 changes: 43 additions & 0 deletions src/main/java/com/mitchej123/hodgepodge/util/StringUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.mitchej123.hodgepodge.util;

// Methods stolen from FontRenderer
public class StringUtil {

/**
* Checks if the char code is a hexadecimal character, used to set colour.
*/
public static boolean isFormatColor(char colorChar) {
return colorChar >= 48 && colorChar <= 57 || colorChar >= 97 && colorChar <= 102
|| colorChar >= 65 && colorChar <= 70;
}

/**
* Checks if the char code is O-K...lLrRk-o... used to set special formatting.
*/
public static boolean isFormatSpecial(char formatChar) {
return formatChar >= 107 && formatChar <= 111 || formatChar >= 75 && formatChar <= 79
|| formatChar == 114
|| formatChar == 82;
}

/**
* Digests a string for nonprinting formatting characters then returns a string containing only that formatting.
*/
public static String getFormatFromString(String p_78282_0_) {
String s1 = "";
int i = -1;
int j = p_78282_0_.length();
while ((i = p_78282_0_.indexOf(167, i + 1)) != -1) {
if (i < j - 1) {
char c0 = p_78282_0_.charAt(i + 1);
if (isFormatColor(c0)) {
s1 = "\u00a7" + c0;
} else if (isFormatSpecial(c0)) {
s1 = s1 + "\u00a7" + c0;
}
}
}
return s1;
}

}

0 comments on commit d78ae73

Please sign in to comment.