Replace deprecated ColoredString with MutableStringTextComponent

This commit is contained in:
Marcel Konrad
2019-09-18 14:59:47 +02:00
parent e737f089f3
commit ba3f460e6d
13 changed files with 204 additions and 456 deletions

View File

@@ -1,7 +1,7 @@
package exopandora.worldhandler.builder.component.impl;
import exopandora.worldhandler.builder.component.IBuilderComponent;
import exopandora.worldhandler.format.text.ColoredString;
import exopandora.worldhandler.text.MutableStringTextComponent;
import net.minecraft.nbt.CompoundNBT;
import net.minecraft.nbt.INBT;
import net.minecraft.nbt.ListNBT;
@@ -14,7 +14,7 @@ import net.minecraftforge.api.distmarker.OnlyIn;
@OnlyIn(Dist.CLIENT)
public class ComponentDisplay implements IBuilderComponent
{
private ColoredString name = new ColoredString();
private MutableStringTextComponent name = new MutableStringTextComponent();
private String[] lore = new String[2];
@Override
@@ -22,9 +22,7 @@ public class ComponentDisplay implements IBuilderComponent
{
CompoundNBT display = new CompoundNBT();
String name = this.name.getText();
if(name != null && !name.isEmpty())
if(this.name.getText() != null && !this.name.getText().isEmpty())
{
display.putString("Name", ITextComponent.Serializer.toJson(new StringTextComponent(this.name.toString())));
}
@@ -52,12 +50,12 @@ public class ComponentDisplay implements IBuilderComponent
return null;
}
public void setName(ColoredString name)
public void setName(MutableStringTextComponent name)
{
this.name = name;
}
public ColoredString getName()
public MutableStringTextComponent getName()
{
return this.name;
}

View File

@@ -7,7 +7,7 @@ import exopandora.worldhandler.builder.component.impl.ComponentDisplay;
import exopandora.worldhandler.builder.component.impl.ComponentEnchantment;
import exopandora.worldhandler.builder.impl.abstr.EnumAttributes;
import exopandora.worldhandler.builder.impl.abstr.EnumAttributes.Applyable;
import exopandora.worldhandler.format.text.ColoredString;
import exopandora.worldhandler.text.MutableStringTextComponent;
import net.minecraft.enchantment.Enchantment;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.api.distmarker.Dist;
@@ -68,12 +68,12 @@ public class BuilderCustomItem extends BuilderGive
return this.attribute.getAttributes();
}
public void setName(ColoredString name)
public void setName(MutableStringTextComponent name)
{
this.display.setName(name);
}
public ColoredString getName()
public MutableStringTextComponent getName()
{
return this.display.getName();
}

View File

@@ -3,8 +3,8 @@ package exopandora.worldhandler.builder.impl;
import javax.annotation.Nullable;
import exopandora.worldhandler.builder.component.impl.ComponentTag;
import exopandora.worldhandler.format.text.ColoredString;
import exopandora.worldhandler.format.text.SignText;
import exopandora.worldhandler.text.MutableStringTextComponent;
import exopandora.worldhandler.text.SignText;
import net.minecraft.nbt.StringNBT;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
@@ -40,16 +40,16 @@ public class BuilderSignEditor extends BuilderData
}
@Nullable
public ColoredString getColoredString(int line)
public MutableStringTextComponent getColoredString(int line)
{
if(this.checkBounds(line))
{
return this.sign[line].getValue().getColoredString();
return this.sign[line].getValue().getString();
}
return null;
}
@Nullable
public String getCommand(int line)
{
@@ -60,7 +60,7 @@ public class BuilderSignEditor extends BuilderData
return null;
}
@Nullable
public void setCommand(int line, String command)
{

View File

@@ -15,9 +15,9 @@ import exopandora.worldhandler.builder.component.impl.ComponentTag;
import exopandora.worldhandler.builder.impl.abstr.EnumAttributes;
import exopandora.worldhandler.builder.impl.abstr.EnumAttributes.Applyable;
import exopandora.worldhandler.builder.types.Coordinate.CoordinateType;
import exopandora.worldhandler.text.MutableStringTextComponent;
import exopandora.worldhandler.builder.types.CoordinateDouble;
import exopandora.worldhandler.builder.types.Type;
import exopandora.worldhandler.format.text.ColoredString;
import net.minecraft.block.Block;
import net.minecraft.block.Blocks;
import net.minecraft.item.Item;
@@ -27,8 +27,6 @@ import net.minecraft.nbt.ListNBT;
import net.minecraft.nbt.StringNBT;
import net.minecraft.potion.Effect;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.text.ITextComponent;
import net.minecraft.util.text.StringTextComponent;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
@@ -36,7 +34,7 @@ import net.minecraftforge.api.distmarker.OnlyIn;
public class BuilderSummon extends CommandBuilderNBT
{
private final ComponentAttributeMob attribute;
private final ComponentTag<ColoredString> customName;
private final ComponentTag<MutableStringTextComponent> customName;
private final ComponentTag<ListNBT> passengers;
private final ComponentTag<ListNBT> armorItems;
private final ComponentTag<ListNBT> handItems;
@@ -48,7 +46,7 @@ public class BuilderSummon extends CommandBuilderNBT
public BuilderSummon()
{
this.attribute = this.registerNBTComponent(new ComponentAttributeMob(attribute -> attribute.getApplyable().equals(Applyable.BOTH) || attribute.getApplyable().equals(Applyable.MOB)));
this.customName = this.registerNBTComponent(new ComponentTag<ColoredString>("CustomName", new ColoredString(), this::colorStringSerializer));
this.customName = this.registerNBTComponent(new ComponentTag<MutableStringTextComponent>("CustomName", new MutableStringTextComponent(), this::textComponentSerializer));
this.passengers = this.registerNBTComponent(new ComponentTag<ListNBT>("Passengers"));
this.armorItems = this.registerNBTComponent(new ComponentTag<ListNBT>("ArmorItems", this::itemListSerializer));
this.handItems = this.registerNBTComponent(new ComponentTag<ListNBT>("HandItems", this::itemListSerializer));
@@ -124,7 +122,7 @@ public class BuilderSummon extends CommandBuilderNBT
return this.attribute.getAttributes();
}
public void setCustomName(ColoredString name)
public void setCustomName(MutableStringTextComponent name)
{
this.customName.setValue(name);
}
@@ -135,7 +133,7 @@ public class BuilderSummon extends CommandBuilderNBT
}
@Nonnull
public ColoredString getCustomName()
public MutableStringTextComponent getCustomName()
{
if(this.customName.getValue() != null)
{
@@ -348,11 +346,11 @@ public class BuilderSummon extends CommandBuilderNBT
return null;
}
private INBT colorStringSerializer(ColoredString string)
private INBT textComponentSerializer(MutableStringTextComponent string)
{
if(string.getText() != null && !string.getText().isEmpty())
if(string.getUnformattedComponentText() != null && !string.getUnformattedComponentText().isEmpty())
{
return new StringNBT(ITextComponent.Serializer.toJson(new StringTextComponent(string.toString())));
return new StringNBT(string.serialize());
}
return null;

View File

@@ -1,125 +0,0 @@
package exopandora.worldhandler.format.text;
import exopandora.worldhandler.format.EnumColor;
import net.minecraft.util.text.TextFormatting;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
/**
* XXX To be replaced with ITextComponent
*/
@Deprecated
@OnlyIn(Dist.CLIENT)
public class ColoredString extends FormattedString
{
private EnumColor color = EnumColor.DEFAULT;
private static final String EMPTY_STRING = "(\u00A7[a-f0-9k-or]?)*";
public ColoredString(String string)
{
this.text = string;
}
public ColoredString()
{
this("");
}
public void setText(String string)
{
this.text = TextFormatting.getTextWithoutFormattingCodes(string);
}
public EnumColor getColor()
{
return this.color;
}
public void setColor(EnumColor color)
{
this.color = color;
}
public void setColor(int color)
{
this.color = EnumColor.getColorFromId(color);
}
public boolean isSpecial()
{
if(this.text != null && !this.text.isEmpty())
{
return this.toString().contains("\u00A7");
}
return false;
}
private String getFormattedString(String string)
{
String result = string;
if(result != null)
{
if(this.italic)
{
result = TextFormatting.ITALIC + result;
}
if(this.underlined)
{
result = TextFormatting.UNDERLINE + result;
}
if(this.strikethrough)
{
result = TextFormatting.STRIKETHROUGH + result;
}
if(this.bold)
{
result = TextFormatting.BOLD + result;
}
if(this.obfuscated)
{
result = TextFormatting.OBFUSCATED + result;
}
if(this.color != null && !this.color.equals(EnumColor.DEFAULT))
{
result = this.color + result;
}
}
return result;
}
@Override
public String toString()
{
if(this.text != null)
{
String result = super.getPreformattedString(this.text);
if(!result.matches(EMPTY_STRING))
{
result = this.getFormattedString(result);
}
if(result.contains("\u00A7"))
{
result += TextFormatting.RESET;
}
return result;
}
return null;
}
public String textFormatter(String string, Integer integer)
{
return this.getFormattedString(string);
}
}

View File

@@ -1,86 +0,0 @@
package exopandora.worldhandler.format.text;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
@OnlyIn(Dist.CLIENT)
public abstract class FormattedString
{
protected String text;
protected boolean underlined;
protected boolean bold;
protected boolean italic;
protected boolean strikethrough;
protected boolean obfuscated;
public String getText()
{
return this.text;
}
public void setText(String string)
{
this.text = string;
}
public boolean isUnderlined()
{
return this.underlined;
}
public void setUnderlined(boolean underlined)
{
this.underlined = underlined;
}
public boolean isBold()
{
return this.bold;
}
public void setBold(boolean bold)
{
this.bold = bold;
}
public boolean isItalic()
{
return this.italic;
}
public void setItalic(boolean italic)
{
this.italic = italic;
}
public boolean isStriked()
{
return this.strikethrough;
}
public void setStriked(boolean striked)
{
this.strikethrough = striked;
}
public boolean isObfuscated()
{
return this.obfuscated;
}
public void setObfuscated(boolean obfuscated)
{
this.obfuscated = obfuscated;
}
public static String getPreformattedString(String string)
{
if(string != null)
{
return string.replaceAll("\u0026", "\u00A7").replaceAll("\u00A7\u00A7", "\u0026");
}
return null;
}
}

View File

@@ -1,42 +0,0 @@
package exopandora.worldhandler.format.text;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
@OnlyIn(Dist.CLIENT)
public class JsonClickEvent
{
private String action;
private String value;
public JsonClickEvent()
{
this(null, null);
}
public JsonClickEvent(String action, String value)
{
this.action = action;
this.value = value;
}
public String getAction()
{
return this.action;
}
public void setAction(String action)
{
this.action = action;
}
public String getValue()
{
return this.value;
}
public void setValue(String value)
{
this.value = value;
}
}

View File

@@ -1,47 +0,0 @@
package exopandora.worldhandler.format.text;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
@OnlyIn(Dist.CLIENT)
public class JsonSignLine extends FormattedString
{
private String color;
private JsonClickEvent clickEvent;
public JsonSignLine()
{
}
public JsonSignLine(ColoredString string)
{
this.text = super.getPreformattedString(string.getText());
this.color = string.getColor().getName();
this.bold = string.isBold();
this.strikethrough = string.isStriked();
this.underlined = string.isUnderlined();
this.italic = string.isItalic();
this.obfuscated = string.isObfuscated();
}
public JsonClickEvent getClickEvent()
{
return this.clickEvent;
}
public void setClickEvent(JsonClickEvent clickEvent)
{
this.clickEvent = clickEvent;
}
public String getColor()
{
return this.color;
}
public void setColor(String color)
{
this.color = color;
}
}

View File

@@ -1,63 +0,0 @@
package exopandora.worldhandler.format.text;
import java.lang.reflect.Type;
import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;
import exopandora.worldhandler.format.EnumColor;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
@OnlyIn(Dist.CLIENT)
public class JsonSignLineSerializer implements JsonSerializer<JsonSignLine>
{
@Override
public JsonElement serialize(JsonSignLine src, Type typeOfSrc, JsonSerializationContext context)
{
JsonObject object = (JsonObject) new Gson().toJsonTree(src);
if(src.getClickEvent() == null)
{
object.remove("clickEvent");
}
if(!src.isBold())
{
object.remove("bold");
}
if(!src.isStriked())
{
object.remove("strikethrough");
}
if(!src.isUnderlined())
{
object.remove("underlined");
}
if(!src.isItalic())
{
object.remove("italic");
}
if(!src.isObfuscated())
{
object.remove("obfuscated");
}
if(src.getColor() != null)
{
if(src.getColor().equals(EnumColor.DEFAULT.getName()))
{
object.remove("color");
}
}
return object;
}
}

View File

@@ -3,8 +3,6 @@ package exopandora.worldhandler.gui.content.element.impl;
import java.util.ArrayList;
import java.util.List;
import exopandora.worldhandler.format.EnumColor;
import exopandora.worldhandler.format.text.ColoredString;
import exopandora.worldhandler.gui.button.GuiButtonBase;
import exopandora.worldhandler.gui.button.GuiButtonList;
import exopandora.worldhandler.gui.button.GuiTextFieldTooltip;
@@ -12,6 +10,7 @@ import exopandora.worldhandler.gui.container.Container;
import exopandora.worldhandler.gui.content.element.Element;
import exopandora.worldhandler.gui.logic.ILogicColorMenu;
import exopandora.worldhandler.gui.logic.ILogicMapped;
import exopandora.worldhandler.text.MutableStringTextComponent;
import net.minecraft.client.resources.I18n;
import net.minecraft.util.text.TextFormatting;
import net.minecraftforge.api.distmarker.Dist;
@@ -20,40 +19,40 @@ import net.minecraftforge.api.distmarker.OnlyIn;
@OnlyIn(Dist.CLIENT)
public class ElementColorMenu extends Element
{
private static final List<EnumColor> COLORS = new ArrayList<EnumColor>();
private static final List<TextFormatting> COLORS = new ArrayList<TextFormatting>();
static
{
COLORS.add(EnumColor.DEFAULT);
COLORS.add(EnumColor.YELLOW);
COLORS.add(EnumColor.GOLD);
COLORS.add(EnumColor.DARK_RED);
COLORS.add(EnumColor.RED);
COLORS.add(EnumColor.LIGHT_PURPLE);
COLORS.add(EnumColor.DARK_PURPLE);
COLORS.add(EnumColor.BLUE);
COLORS.add(EnumColor.DARK_BLUE);
COLORS.add(EnumColor.DARK_AQUA);
COLORS.add(EnumColor.AQUA);
COLORS.add(EnumColor.GREEN);
COLORS.add(EnumColor.DARK_GREEN);
COLORS.add(EnumColor.BLACK);
COLORS.add(EnumColor.DARK_GRAY);
COLORS.add(EnumColor.GRAY);
COLORS.add(EnumColor.WHITE);
COLORS.add(TextFormatting.RESET);
COLORS.add(TextFormatting.YELLOW);
COLORS.add(TextFormatting.GOLD);
COLORS.add(TextFormatting.DARK_RED);
COLORS.add(TextFormatting.RED);
COLORS.add(TextFormatting.LIGHT_PURPLE);
COLORS.add(TextFormatting.DARK_PURPLE);
COLORS.add(TextFormatting.BLUE);
COLORS.add(TextFormatting.DARK_BLUE);
COLORS.add(TextFormatting.DARK_AQUA);
COLORS.add(TextFormatting.AQUA);
COLORS.add(TextFormatting.GREEN);
COLORS.add(TextFormatting.DARK_GREEN);
COLORS.add(TextFormatting.BLACK);
COLORS.add(TextFormatting.DARK_GRAY);
COLORS.add(TextFormatting.GRAY);
COLORS.add(TextFormatting.WHITE);
}
private GuiTextFieldTooltip textField;
private final ColoredString string;
private final MutableStringTextComponent string;
private final ILogicColorMenu logic;
private final String translationKey;
public ElementColorMenu(int x, int y, String translationKey, ColoredString string)
public ElementColorMenu(int x, int y, String translationKey, MutableStringTextComponent string)
{
this(x, y, translationKey, string, new ILogicColorMenu(){});
}
public ElementColorMenu(int x, int y, String translationKey, ColoredString string, ILogicColorMenu logic)
public ElementColorMenu(int x, int y, String translationKey, MutableStringTextComponent string, ILogicColorMenu logic)
{
super(x, y);
this.translationKey = translationKey;
@@ -66,7 +65,7 @@ public class ElementColorMenu extends Element
{
this.textField = new GuiTextFieldTooltip(this.x + 118, this.y, 114, 20, I18n.format(this.translationKey));
this.textField.setValidator(this.logic::validate);
this.textField.setTextFormatter(this.string::textFormatter);
this.textField.setTextFormatter(this.string::formatter);
this.textField.setText(this.string.getText());
this.textField.func_212954_a(text ->
{
@@ -81,30 +80,30 @@ public class ElementColorMenu extends Element
if(this.logic.doDrawButtons())
{
container.add(new GuiButtonList<EnumColor>(this.x + 118, this.y + 24, COLORS, 114, 20, container, new ILogicMapped<EnumColor>()
container.add(new GuiButtonList<TextFormatting>(this.x + 118, this.y + 24, COLORS, 114, 20, container, new ILogicMapped<TextFormatting>()
{
@Override
public String translate(EnumColor item)
public String translate(TextFormatting item)
{
return item + I18n.format("gui.worldhandler.color") + ": " + I18n.format("gui.worldhandler.color." + item.getName());
return item + I18n.format("gui.worldhandler.color") + ": " + I18n.format("gui.worldhandler.color." + item.getFriendlyName());
}
@Override
public String toTooltip(EnumColor item)
public String toTooltip(TextFormatting item)
{
return null;
}
@Override
public String formatTooltip(EnumColor item, int index, int max)
public String formatTooltip(TextFormatting item, int index, int max)
{
return null;
}
@Override
public void onClick(EnumColor item)
public void onClick(TextFormatting item)
{
ElementColorMenu.this.string.setColor(item);
ElementColorMenu.this.string.getStyle().setColor(item);
}
@Override
@@ -114,29 +113,29 @@ public class ElementColorMenu extends Element
}
}));
container.add(new GuiButtonBase(this.x + 118, this.y + 48, 20, 20, (this.string.isItalic() ? TextFormatting.ITALIC : TextFormatting.RESET) + "I", () ->
container.add(new GuiButtonBase(this.x + 118, this.y + 48, 20, 20, (this.string.getStyle().getItalic() ? TextFormatting.ITALIC : TextFormatting.RESET) + "I", () ->
{
this.string.setItalic(!this.string.isItalic());
this.string.getStyle().setItalic(!this.string.getStyle().getItalic());
container.init();
}));
container.add(new GuiButtonBase(this.x + 118 + 24 - 1, this.y + 48, 20, 20, (this.string.isBold() ? TextFormatting.BOLD : TextFormatting.RESET) + "B", () ->
container.add(new GuiButtonBase(this.x + 118 + 24 - 1, this.y + 48, 20, 20, (this.string.getStyle().getBold() ? TextFormatting.BOLD : TextFormatting.RESET) + "B", () ->
{
this.string.setBold(!this.string.isBold());
this.string.getStyle().setBold(!this.string.getStyle().getBold());
container.init();
}));
container.add(new GuiButtonBase(this.x + 118 + 24 * 2 - 1, this.y + 48, 20, 20, (this.string.isUnderlined() ? TextFormatting.UNDERLINE : TextFormatting.RESET) + "U", () ->
container.add(new GuiButtonBase(this.x + 118 + 24 * 2 - 1, this.y + 48, 20, 20, (this.string.getStyle().getUnderlined() ? TextFormatting.UNDERLINE : TextFormatting.RESET) + "U", () ->
{
this.string.setUnderlined(!this.string.isUnderlined());
this.string.getStyle().setUnderlined(!this.string.getStyle().getUnderlined());
container.init();
}));
container.add(new GuiButtonBase(this.x + 118 + 24 * 3 - 1, this.y + 48, 20, 20, (this.string.isStriked() ? TextFormatting.STRIKETHROUGH : TextFormatting.RESET) + "S", () ->
container.add(new GuiButtonBase(this.x + 118 + 24 * 3 - 1, this.y + 48, 20, 20, (this.string.getStyle().getStrikethrough() ? TextFormatting.STRIKETHROUGH : TextFormatting.RESET) + "S", () ->
{
this.string.setStriked(!this.string.isStriked());
this.string.getStyle().setStrikethrough(!this.string.getStyle().getStrikethrough());
container.init();
}));
container.add(new GuiButtonBase(this.x + 118 + 24 * 4 - 2, this.y + 48, 20, 20, (this.string.isObfuscated() ? TextFormatting.OBFUSCATED : TextFormatting.RESET) + "O", () ->
container.add(new GuiButtonBase(this.x + 118 + 24 * 4 - 2, this.y + 48, 20, 20, (this.string.getStyle().getObfuscated() ? TextFormatting.OBFUSCATED : TextFormatting.RESET) + "O", () ->
{
this.string.setObfuscated(!this.string.isObfuscated());
this.string.getStyle().setObfuscated(!this.string.getStyle().getObfuscated());
container.init();
}));
}

View File

@@ -153,9 +153,9 @@ public class ContentSignEditor extends Content
@Override
public void tick(Container container)
{
if(this.editColor)
if(!this.editColor)
{
this.commandField.tick();
}
}

View File

@@ -0,0 +1,107 @@
package exopandora.worldhandler.text;
import net.minecraft.util.text.ITextComponent;
import net.minecraft.util.text.StringTextComponent;
import net.minecraft.util.text.TextFormatting;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
@OnlyIn(Dist.CLIENT)
public class MutableStringTextComponent extends StringTextComponent
{
private String text;
public MutableStringTextComponent()
{
super(null);
this.text = "";
}
public MutableStringTextComponent(String text)
{
super(null);
this.text = text;
}
public void setText(String text)
{
this.text = text;
}
public String getText()
{
return this.text;
}
@Override
public String getUnformattedComponentText()
{
return this.text;
}
@Override
public String getFormattedText()
{
String formatted = super.getFormattedText();
if(this.isSpecial())
{
return MutableStringTextComponent.getSpecialFormattedText(formatted);
}
return formatted;
}
public boolean isSpecial()
{
return this.text != null && !this.text.isEmpty() && MutableStringTextComponent.getSpecialFormattedText(this.text).contains("\u00A7");
}
public static String getSpecialFormattedText(String text)
{
String result = text.replaceAll("\u0026", "\u00A7").replaceAll("\u00A7\u00A7", "\u0026");
if(result.contains("\u00A7"))
{
result += TextFormatting.RESET;
}
return result;
}
public String formatter(String string, Integer index)
{
return this.getStyle().getFormattingCode() + string;
}
public String serialize()
{
MutableStringTextComponent serial = (MutableStringTextComponent) this.deepCopy();
serial.setText(MutableStringTextComponent.getSpecialFormattedText(this.getUnformattedComponentText()));
return ITextComponent.Serializer.toJson(serial);
}
@Override
public MutableStringTextComponent shallowCopy()
{
return new MutableStringTextComponent(this.text);
}
@Override
public boolean equals(Object object)
{
if(this == object)
{
return true;
}
else if(!(object instanceof MutableStringTextComponent))
{
return false;
}
else
{
MutableStringTextComponent stringtextcomponent = (MutableStringTextComponent) object;
return this.text.equals(stringtextcomponent.getText()) && super.equals(object);
}
}
}

View File

@@ -1,18 +1,16 @@
package exopandora.worldhandler.format.text;
package exopandora.worldhandler.text;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import javax.annotation.Nullable;
import net.minecraft.util.text.event.ClickEvent;
import net.minecraft.util.text.event.ClickEvent.Action;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
@OnlyIn(Dist.CLIENT)
public class SignText
{
private static final Gson GSON = new GsonBuilder().registerTypeAdapter(JsonSignLine.class, new JsonSignLineSerializer()).create();
private ColoredString text = new ColoredString();
private String command;
private MutableStringTextComponent text = new MutableStringTextComponent();
private final int line;
public SignText(int line)
@@ -25,46 +23,57 @@ public class SignText
return this.line;
}
public ColoredString getColoredString()
public MutableStringTextComponent getString()
{
return this.text;
}
public void setColoredString(ColoredString coloredString)
public void setString(MutableStringTextComponent string)
{
this.text = coloredString;
}
public String getCommand()
{
return this.command;
this.text = string;
}
public void setCommand(String command)
{
this.command = command;
if(command != null && !command.isEmpty())
{
this.text.getStyle().setClickEvent(new ClickEvent(Action.RUN_COMMAND, command));
}
else
{
this.text.getStyle().setClickEvent(null);
}
}
@Nullable
public String getCommand()
{
if(this.hasCommand())
{
return this.text.getStyle().getClickEvent().getValue();
}
return null;
}
public boolean hasCommand()
{
return this.command != null && !this.command.isEmpty();
return this.text.getStyle().getClickEvent() != null && this.text.getStyle().getClickEvent().getAction() == Action.RUN_COMMAND && this.text.getStyle().getClickEvent().getValue() != null;
}
@Override
public String toString()
{
if(!this.text.isSpecial() && !this.hasCommand())
if(this.text.getUnformattedComponentText().isEmpty())
{
return this.text.getText();
return this.text.getUnformattedComponentText();
}
JsonSignLine line = new JsonSignLine(this.text);
if(this.hasCommand())
if(this.text.getStyle().isEmpty() && !this.hasCommand())
{
line.setClickEvent(new JsonClickEvent("run_command", FormattedString.getPreformattedString(this.command)));
return this.text.getFormattedText();
}
return GSON.toJson(line);
return this.text.serialize();
}
}