messenger can parse style

This commit is contained in:
gnembon committed 2020-04-28 19:39:13 -03:00
1 parent 5bb5f372b6
commit 0b72f85474
1 file changed
+34 -31
+34 -31
View File
@@ -3,14 +3,15 @@ package carpet.utils;
import net.minecraft.server.command.ServerCommandSource; import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.entity.EntityCategory; import net.minecraft.entity.EntityCategory;
import net.minecraft.entity.player.PlayerEntity; import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Vec3d;
import net.minecraft.text.LiteralText;
import net.minecraft.util.Formatting;
import net.minecraft.text.BaseText; import net.minecraft.text.BaseText;
import net.minecraft.server.MinecraftServer;
import net.minecraft.text.ClickEvent; import net.minecraft.text.ClickEvent;
import net.minecraft.text.HoverEvent; import net.minecraft.text.HoverEvent;
import net.minecraft.text.LiteralText;
import net.minecraft.text.Style;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Vec3d;
import net.minecraft.util.Formatting;
import net.minecraft.server.MinecraftServer;
import net.minecraft.world.dimension.DimensionType; import net.minecraft.world.dimension.DimensionType;
import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.Logger;
@@ -52,32 +53,33 @@ public class Messenger
/ = action added to the previous component / = action added to the previous component
*/ */
private static BaseText _applyStyleToTextComponent(BaseText comp, String style) public static Style parseStyle(String style)
{ {
//could be rewritten to be more efficient //could be rewritten to be more efficient
comp.getStyle().setItalic(style.indexOf('i')>=0); Style styleInstance = new Style();
comp.getStyle().setStrikethrough(style.indexOf('s')>=0); styleInstance.setItalic(style.indexOf('i')>=0);
comp.getStyle().setUnderline(style.indexOf('u')>=0); styleInstance.setStrikethrough(style.indexOf('s')>=0);
comp.getStyle().setBold(style.indexOf('b')>=0); styleInstance.setUnderline(style.indexOf('u')>=0);
comp.getStyle().setObfuscated(style.indexOf('o')>=0); styleInstance.setBold(style.indexOf('b')>=0);
comp.getStyle().setColor(Formatting.WHITE); styleInstance.setObfuscated(style.indexOf('o')>=0);
if (style.indexOf('w')>=0) comp.getStyle().setColor(Formatting.WHITE); // not needed styleInstance.setColor(Formatting.WHITE);
if (style.indexOf('y')>=0) comp.getStyle().setColor(Formatting.YELLOW); if (style.indexOf('w')>=0) styleInstance.setColor(Formatting.WHITE); // not needed
if (style.indexOf('m')>=0) comp.getStyle().setColor(Formatting.LIGHT_PURPLE); if (style.indexOf('y')>=0) styleInstance.setColor(Formatting.YELLOW);
if (style.indexOf('r')>=0) comp.getStyle().setColor(Formatting.RED); if (style.indexOf('m')>=0) styleInstance.setColor(Formatting.LIGHT_PURPLE);
if (style.indexOf('c')>=0) comp.getStyle().setColor(Formatting.AQUA); if (style.indexOf('r')>=0) styleInstance.setColor(Formatting.RED);
if (style.indexOf('l')>=0) comp.getStyle().setColor(Formatting.GREEN); if (style.indexOf('c')>=0) styleInstance.setColor(Formatting.AQUA);
if (style.indexOf('t')>=0) comp.getStyle().setColor(Formatting.BLUE); if (style.indexOf('l')>=0) styleInstance.setColor(Formatting.GREEN);
if (style.indexOf('f')>=0) comp.getStyle().setColor(Formatting.DARK_GRAY); if (style.indexOf('t')>=0) styleInstance.setColor(Formatting.BLUE);
if (style.indexOf('g')>=0) comp.getStyle().setColor(Formatting.GRAY); if (style.indexOf('f')>=0) styleInstance.setColor(Formatting.DARK_GRAY);
if (style.indexOf('d')>=0) comp.getStyle().setColor(Formatting.GOLD); if (style.indexOf('g')>=0) styleInstance.setColor(Formatting.GRAY);
if (style.indexOf('p')>=0) comp.getStyle().setColor(Formatting.DARK_PURPLE); if (style.indexOf('d')>=0) styleInstance.setColor(Formatting.GOLD);
if (style.indexOf('n')>=0) comp.getStyle().setColor(Formatting.DARK_RED); if (style.indexOf('p')>=0) styleInstance.setColor(Formatting.DARK_PURPLE);
if (style.indexOf('q')>=0) comp.getStyle().setColor(Formatting.DARK_AQUA); if (style.indexOf('n')>=0) styleInstance.setColor(Formatting.DARK_RED);
if (style.indexOf('e')>=0) comp.getStyle().setColor(Formatting.DARK_GREEN); if (style.indexOf('q')>=0) styleInstance.setColor(Formatting.DARK_AQUA);
if (style.indexOf('v')>=0) comp.getStyle().setColor(Formatting.DARK_BLUE); if (style.indexOf('e')>=0) styleInstance.setColor(Formatting.DARK_GREEN);
if (style.indexOf('k')>=0) comp.getStyle().setColor(Formatting.BLACK); if (style.indexOf('v')>=0) styleInstance.setColor(Formatting.DARK_BLUE);
return comp; if (style.indexOf('k')>=0) styleInstance.setColor(Formatting.BLACK);
return styleInstance;
} }
public static String heatmap_color(double actual, double reference) public static String heatmap_color(double actual, double reference)
{ {
@@ -143,7 +145,8 @@ public class Messenger
return previous_message; return previous_message;
} }
BaseText txt = new LiteralText(str); BaseText txt = new LiteralText(str);
return _applyStyleToTextComponent(txt, desc); txt.setStyle(parseStyle(desc));
return txt;
} }
public static BaseText tp(String desc, Vec3d pos) { return tp(desc, pos.x, pos.y, pos.z); } public static BaseText tp(String desc, Vec3d pos) { return tp(desc, pos.x, pos.y, pos.z); }
public static BaseText tp(String desc, BlockPos pos) { return tp(desc, pos.getX(), pos.getY(), pos.getZ()); } public static BaseText tp(String desc, BlockPos pos) { return tp(desc, pos.getX(), pos.getY(), pos.getZ()); }
@@ -263,7 +266,7 @@ public class Messenger
public static BaseText s(String text, String style) public static BaseText s(String text, String style)
{ {
BaseText message = new LiteralText(text); BaseText message = new LiteralText(text);
_applyStyleToTextComponent(message, style); message.setStyle(parseStyle(style));
return message; return message;
} }