From 689c7f62b866107333829b6480c3945da57e47eb Mon Sep 17 00:00:00 2001 From: Alexander Brandes Date: Fri, 9 Jun 2023 15:27:02 +0200 Subject: [PATCH] Fixed setting sign text on 1.20 --- .../com/sk89q/worldedit/blocks/SignBlock.java | 71 ++++++++++++------- 1 file changed, 47 insertions(+), 24 deletions(-) diff --git a/worldedit-core/src/legacy/java/com/sk89q/worldedit/blocks/SignBlock.java b/worldedit-core/src/legacy/java/com/sk89q/worldedit/blocks/SignBlock.java index 2817365ff..333c3336b 100644 --- a/worldedit-core/src/legacy/java/com/sk89q/worldedit/blocks/SignBlock.java +++ b/worldedit-core/src/legacy/java/com/sk89q/worldedit/blocks/SignBlock.java @@ -20,27 +20,29 @@ package com.sk89q.worldedit.blocks; import com.sk89q.jnbt.CompoundTag; +import com.sk89q.jnbt.ListTag; import com.sk89q.jnbt.StringTag; import com.sk89q.jnbt.Tag; +import com.sk89q.worldedit.WorldEdit; +import com.sk89q.worldedit.extension.platform.Capability; +import com.sk89q.worldedit.internal.Constants; import com.sk89q.worldedit.util.gson.GsonUtil; import com.sk89q.worldedit.world.block.BaseBlock; import com.sk89q.worldedit.world.block.BlockState; +import java.util.Arrays; import java.util.HashMap; import java.util.Map; +import java.util.stream.Collectors; /** * Represents a sign block. - * - * @deprecated WorldEdit does not handle interpreting NBT, - * deprecated for removal without replacement */ -@Deprecated(forRemoval = true) public class SignBlock extends BaseBlock { private String[] text; - private static String EMPTY = "{\"text\":\"\"}"; + private static final String EMPTY = "{\"text\":\"\"}"; /** * Construct the sign with text. @@ -64,6 +66,11 @@ public class SignBlock extends BaseBlock { this.text = text; } + private boolean isLegacy() { + int dataVersion = WorldEdit.getInstance().getPlatformManager().queryCapability(Capability.WORLD_EDITING).getDataVersion(); + return dataVersion < Constants.DATA_VERSION_MC_1_20; + } + /** * Get the text. * @@ -98,10 +105,17 @@ public class SignBlock extends BaseBlock { @Override public CompoundTag getNbtData() { Map values = new HashMap<>(); - values.put("Text1", new StringTag(text[0])); - values.put("Text2", new StringTag(text[1])); - values.put("Text3", new StringTag(text[2])); - values.put("Text4", new StringTag(text[3])); + if (isLegacy()) { + values.put("Text1", new StringTag(text[0])); + values.put("Text2", new StringTag(text[1])); + values.put("Text3", new StringTag(text[2])); + values.put("Text4", new StringTag(text[3])); + } else { + ListTag messages = new ListTag(StringTag.class, Arrays.stream(text).map(StringTag::new).collect(Collectors.toList())); + Map frontTextTag = new HashMap<>(); + frontTextTag.put("messages", messages); + values.put("front_text", new CompoundTag(frontTextTag)); + } return new CompoundTag(values); } @@ -122,24 +136,33 @@ public class SignBlock extends BaseBlock { throw new RuntimeException(String.format("'%s' tile entity expected", getNbtId())); } - t = values.get("Text1"); - if (t instanceof StringTag) { - text[0] = ((StringTag) t).getValue(); - } + if (isLegacy()) { + t = values.get("Text1"); + if (t instanceof StringTag) { + text[0] = ((StringTag) t).getValue(); + } - t = values.get("Text2"); - if (t instanceof StringTag) { - text[1] = ((StringTag) t).getValue(); - } + t = values.get("Text2"); + if (t instanceof StringTag) { + text[1] = ((StringTag) t).getValue(); + } - t = values.get("Text3"); - if (t instanceof StringTag) { - text[2] = ((StringTag) t).getValue(); - } + t = values.get("Text3"); + if (t instanceof StringTag) { + text[2] = ((StringTag) t).getValue(); + } - t = values.get("Text4"); - if (t instanceof StringTag) { - text[3] = ((StringTag) t).getValue(); + t = values.get("Text4"); + if (t instanceof StringTag) { + text[3] = ((StringTag) t).getValue(); + } + } else { + CompoundTag frontTextTag = (CompoundTag) values.get("front_text"); + ListTag messagesTag = frontTextTag.getListTag("messages"); + for (int i = 0; i < messagesTag.getValue().size(); i++) { + StringTag tag = (StringTag) messagesTag.getValue().get(i); + text[i] = tag.getValue(); + } } }