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

@@ -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();
}
}