diff --git a/src/main/java/carpet/script/value/FormattedTextValue.java b/src/main/java/carpet/script/value/FormattedTextValue.java index 70546d71..25e45bf0 100644 --- a/src/main/java/carpet/script/value/FormattedTextValue.java +++ b/src/main/java/carpet/script/value/FormattedTextValue.java @@ -3,6 +3,7 @@ package carpet.script.value; import net.minecraft.nbt.StringTag; import net.minecraft.nbt.Tag; import net.minecraft.text.BaseText; +import net.minecraft.text.LiteralText; import net.minecraft.text.Text; public class FormattedTextValue extends StringValue @@ -14,6 +15,33 @@ public class FormattedTextValue extends StringValue this.text = text; } + public static Value combine(Value left, Value right) { + BaseText text; + if (left instanceof FormattedTextValue) + { + text = (BaseText) ((FormattedTextValue) left).getText().shallowCopy(); + } + else + { + if (left instanceof NullValue) + return right; + text = new LiteralText(left.getString()); + } + + if (right instanceof FormattedTextValue) + { + text.append(((FormattedTextValue) right).getText().shallowCopy()); + return new FormattedTextValue(text); + } + else + { + if (right instanceof NullValue) + return left; + text.append(right.getString()); + return new FormattedTextValue(text); + } + } + @Override public String getString() { return text.getString(); @@ -27,7 +55,7 @@ public class FormattedTextValue extends StringValue @Override public Value clone() { - return new FormattedTextValue(text.shallowCopy()); + return new FormattedTextValue(text); } @Override @@ -50,16 +78,7 @@ public class FormattedTextValue extends StringValue @Override public Value add(Value o) { - if (o instanceof FormattedTextValue) - { - Text mergedText = ((BaseText) text).shallowCopy().append(((FormattedTextValue) o).getText()); - return new FormattedTextValue(mergedText); - } - else - { - Text mergedText = ((BaseText) text).shallowCopy().append(o.getString()); - return new FormattedTextValue(mergedText); - } + return combine(this, o); } public String serialize() diff --git a/src/main/java/carpet/script/value/Value.java b/src/main/java/carpet/script/value/Value.java index 93e32183..c2f188e7 100644 --- a/src/main/java/carpet/script/value/Value.java +++ b/src/main/java/carpet/script/value/Value.java @@ -4,8 +4,6 @@ import carpet.script.exception.InternalExpressionException; import com.google.gson.JsonElement; import com.google.gson.JsonPrimitive; import net.minecraft.nbt.Tag; -import net.minecraft.text.BaseText; -import net.minecraft.text.LiteralText; import java.util.ArrayList; import java.util.List; @@ -83,11 +81,7 @@ public abstract class Value implements Comparable, Cloneable String lstr = this.getString(); if (o instanceof FormattedTextValue) { - if (lstr == null) - return ((FormattedTextValue) o).clone(); - BaseText newText = new LiteralText(lstr); - newText.append(((FormattedTextValue) o).getText().shallowCopy()); - return new FormattedTextValue(newText); + return FormattedTextValue.combine(this, o); } if (lstr == null) // null return new StringValue(o.getString());