Move FormattedText addition into a combined function
This commit is contained in:
2 files changed
+31
-18
No files matched your search
@@ -3,6 +3,7 @@ package carpet.script.value;
|
|||||||
import net.minecraft.nbt.StringTag;
|
import net.minecraft.nbt.StringTag;
|
||||||
import net.minecraft.nbt.Tag;
|
import net.minecraft.nbt.Tag;
|
||||||
import net.minecraft.text.BaseText;
|
import net.minecraft.text.BaseText;
|
||||||
|
import net.minecraft.text.LiteralText;
|
||||||
import net.minecraft.text.Text;
|
import net.minecraft.text.Text;
|
||||||
|
|
||||||
public class FormattedTextValue extends StringValue
|
public class FormattedTextValue extends StringValue
|
||||||
@@ -14,6 +15,33 @@ public class FormattedTextValue extends StringValue
|
|||||||
this.text = text;
|
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
|
@Override
|
||||||
public String getString() {
|
public String getString() {
|
||||||
return text.getString();
|
return text.getString();
|
||||||
@@ -27,7 +55,7 @@ public class FormattedTextValue extends StringValue
|
|||||||
@Override
|
@Override
|
||||||
public Value clone()
|
public Value clone()
|
||||||
{
|
{
|
||||||
return new FormattedTextValue(text.shallowCopy());
|
return new FormattedTextValue(text);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -50,16 +78,7 @@ public class FormattedTextValue extends StringValue
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Value add(Value o) {
|
public Value add(Value o) {
|
||||||
if (o instanceof FormattedTextValue)
|
return combine(this, o);
|
||||||
{
|
|
||||||
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()
|
public String serialize()
|
||||||
|
|||||||
@@ -4,8 +4,6 @@ import carpet.script.exception.InternalExpressionException;
|
|||||||
import com.google.gson.JsonElement;
|
import com.google.gson.JsonElement;
|
||||||
import com.google.gson.JsonPrimitive;
|
import com.google.gson.JsonPrimitive;
|
||||||
import net.minecraft.nbt.Tag;
|
import net.minecraft.nbt.Tag;
|
||||||
import net.minecraft.text.BaseText;
|
|
||||||
import net.minecraft.text.LiteralText;
|
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@@ -83,11 +81,7 @@ public abstract class Value implements Comparable<Value>, Cloneable
|
|||||||
String lstr = this.getString();
|
String lstr = this.getString();
|
||||||
if (o instanceof FormattedTextValue)
|
if (o instanceof FormattedTextValue)
|
||||||
{
|
{
|
||||||
if (lstr == null)
|
return FormattedTextValue.combine(this, o);
|
||||||
return ((FormattedTextValue) o).clone();
|
|
||||||
BaseText newText = new LiteralText(lstr);
|
|
||||||
newText.append(((FormattedTextValue) o).getText().shallowCopy());
|
|
||||||
return new FormattedTextValue(newText);
|
|
||||||
}
|
}
|
||||||
if (lstr == null) // null
|
if (lstr == null) // null
|
||||||
return new StringValue(o.getString());
|
return new StringValue(o.getString());
|
||||||
|
|||||||
Reference in new issue
Block a user