Object serialization implements INBTWritable

This commit is contained in:
Marcel Konrad
2020-05-24 20:12:40 +02:00
parent 1d011c005b
commit 556305708b
3 changed files with 24 additions and 6 deletions

View File

@@ -22,7 +22,7 @@ public class BuilderSignEditor extends BuilderData
for(int x = 0; x < 4; x++) for(int x = 0; x < 4; x++)
{ {
this.sign[x] = this.registerNBTComponent(new ComponentTag<SignText>("Text" + (x + 1), new SignText(x), SignText::toNBT)); this.sign[x] = this.registerNBTComponent(new ComponentTag<SignText>("Text" + (x + 1), new SignText(x), SignText::serialize));
} }
} }

View File

@@ -1,5 +1,8 @@
package exopandora.worldhandler.util; package exopandora.worldhandler.util;
import exopandora.worldhandler.builder.INBTWritable;
import net.minecraft.nbt.INBT;
import net.minecraft.nbt.StringNBT;
import net.minecraft.util.text.ITextComponent; import net.minecraft.util.text.ITextComponent;
import net.minecraft.util.text.StringTextComponent; import net.minecraft.util.text.StringTextComponent;
import net.minecraft.util.text.TextFormatting; import net.minecraft.util.text.TextFormatting;
@@ -7,7 +10,7 @@ import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn; import net.minecraftforge.api.distmarker.OnlyIn;
@OnlyIn(Dist.CLIENT) @OnlyIn(Dist.CLIENT)
public class MutableStringTextComponent extends StringTextComponent public class MutableStringTextComponent extends StringTextComponent implements INBTWritable
{ {
private String text; private String text;
@@ -74,7 +77,19 @@ public class MutableStringTextComponent extends StringTextComponent
return this.getStyle().getFormattingCode() + string; return this.getStyle().getFormattingCode() + string;
} }
public String serialize() @Override
public INBT serialize()
{
if(this.getUnformattedComponentText() != null && !this.getUnformattedComponentText().isEmpty())
{
return StringNBT.valueOf(this.toString());
}
return null;
}
@Override
public String toString()
{ {
MutableStringTextComponent serial = (MutableStringTextComponent) this.deepCopy(); MutableStringTextComponent serial = (MutableStringTextComponent) this.deepCopy();
serial.setText(MutableStringTextComponent.getSpecialFormattedText(this.getUnformattedComponentText())); serial.setText(MutableStringTextComponent.getSpecialFormattedText(this.getUnformattedComponentText()));

View File

@@ -2,6 +2,8 @@ package exopandora.worldhandler.util;
import javax.annotation.Nullable; import javax.annotation.Nullable;
import exopandora.worldhandler.builder.INBTWritable;
import net.minecraft.nbt.INBT;
import net.minecraft.nbt.StringNBT; import net.minecraft.nbt.StringNBT;
import net.minecraft.util.text.event.ClickEvent; import net.minecraft.util.text.event.ClickEvent;
import net.minecraft.util.text.event.ClickEvent.Action; import net.minecraft.util.text.event.ClickEvent.Action;
@@ -9,7 +11,7 @@ import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn; import net.minecraftforge.api.distmarker.OnlyIn;
@OnlyIn(Dist.CLIENT) @OnlyIn(Dist.CLIENT)
public class SignText public class SignText implements INBTWritable
{ {
private MutableStringTextComponent text = new MutableStringTextComponent(); private MutableStringTextComponent text = new MutableStringTextComponent();
private final int line; private final int line;
@@ -62,7 +64,8 @@ public class SignText
return this.text.getStyle().getClickEvent() != null && this.text.getStyle().getClickEvent().getAction() == Action.RUN_COMMAND && this.text.getStyle().getClickEvent().getValue() != null; return this.text.getStyle().getClickEvent() != null && this.text.getStyle().getClickEvent().getAction() == Action.RUN_COMMAND && this.text.getStyle().getClickEvent().getValue() != null;
} }
public StringNBT toNBT() @Override
public INBT serialize()
{ {
return StringNBT.valueOf(this.toString()); return StringNBT.valueOf(this.toString());
} }
@@ -80,6 +83,6 @@ public class SignText
return this.text.getFormattedText(); return this.text.getFormattedText();
} }
return this.text.serialize(); return this.text.toString();
} }
} }