From 7b77fb08531ca3858ecfeaa296d6e983a0e943ec Mon Sep 17 00:00:00 2001 From: altrisi Date: Mon, 14 Dec 2020 18:36:24 +0100 Subject: [PATCH 1/3] Add FormattedText addition --- .../carpet/script/value/FormattedTextValue.java | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/main/java/carpet/script/value/FormattedTextValue.java b/src/main/java/carpet/script/value/FormattedTextValue.java index 218575ca..529c340a 100644 --- a/src/main/java/carpet/script/value/FormattedTextValue.java +++ b/src/main/java/carpet/script/value/FormattedTextValue.java @@ -48,10 +48,25 @@ public class FormattedTextValue extends StringValue return StringTag.of(Text.Serializer.toJson(text)); } + @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); + } + } + public String serialize() { return Text.Serializer.toJson(text); } + public static FormattedTextValue deserialize(String serialized) { return new FormattedTextValue(Text.Serializer.fromJson(serialized)); From 6dfd64027d78dd9affd9502ac1e217ed203ab936 Mon Sep 17 00:00:00 2001 From: altrisi Date: Mon, 14 Dec 2020 19:20:21 +0100 Subject: [PATCH 2/3] Add FormattedText functionality to the right of the operand --- .../java/carpet/script/value/FormattedTextValue.java | 2 +- src/main/java/carpet/script/value/Value.java | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/src/main/java/carpet/script/value/FormattedTextValue.java b/src/main/java/carpet/script/value/FormattedTextValue.java index 529c340a..70546d71 100644 --- a/src/main/java/carpet/script/value/FormattedTextValue.java +++ b/src/main/java/carpet/script/value/FormattedTextValue.java @@ -27,7 +27,7 @@ public class FormattedTextValue extends StringValue @Override public Value clone() { - return new FormattedTextValue(text); + return new FormattedTextValue(text.shallowCopy()); } @Override diff --git a/src/main/java/carpet/script/value/Value.java b/src/main/java/carpet/script/value/Value.java index 679a9263..93e32183 100644 --- a/src/main/java/carpet/script/value/Value.java +++ b/src/main/java/carpet/script/value/Value.java @@ -4,6 +4,8 @@ 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; @@ -79,6 +81,14 @@ public abstract class Value implements Comparable, Cloneable public Value add(Value o) { 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); + } if (lstr == null) // null return new StringValue(o.getString()); String rstr = o.getString(); From a81b549360bbab7fb23a47dcf252e081d19328e1 Mon Sep 17 00:00:00 2001 From: altrisi Date: Tue, 15 Dec 2020 10:00:09 +0100 Subject: [PATCH 3/3] Move FormattedText addition into a combined function --- .../script/value/FormattedTextValue.java | 41 ++++++++++++++----- src/main/java/carpet/script/value/Value.java | 8 +--- 2 files changed, 31 insertions(+), 18 deletions(-) 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());