From 28f1a2d7875d48b8d55051d712b160a63762f78a Mon Sep 17 00:00:00 2001 From: Maddy Miller Date: Sat, 15 Jul 2023 10:47:24 +0200 Subject: [PATCH] Fix legacy block wrappers with new NBT API (#2361) (cherry picked from commit 5b674745f17d8ede505857242e5240c6dbeebfd6) --- .../blocks/LegacyBaseBlockWrapper.java | 48 +++++++++++++++++++ .../worldedit/blocks/MobSpawnerBlock.java | 8 ++-- .../com/sk89q/worldedit/blocks/SignBlock.java | 13 +++-- .../sk89q/worldedit/blocks/SkullBlock.java | 6 ++- 4 files changed, 67 insertions(+), 8 deletions(-) create mode 100644 worldedit-core/src/legacy/java/com/sk89q/worldedit/blocks/LegacyBaseBlockWrapper.java diff --git a/worldedit-core/src/legacy/java/com/sk89q/worldedit/blocks/LegacyBaseBlockWrapper.java b/worldedit-core/src/legacy/java/com/sk89q/worldedit/blocks/LegacyBaseBlockWrapper.java new file mode 100644 index 0000000000..e22f267919 --- /dev/null +++ b/worldedit-core/src/legacy/java/com/sk89q/worldedit/blocks/LegacyBaseBlockWrapper.java @@ -0,0 +1,48 @@ +/* + * WorldEdit, a Minecraft world manipulation toolkit + * Copyright (C) sk89q + * Copyright (C) WorldEdit team and contributors + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package com.sk89q.worldedit.blocks; + +import com.sk89q.jnbt.CompoundTag; +import com.sk89q.worldedit.util.concurrency.LazyReference; +import com.sk89q.worldedit.world.block.BaseBlock; +import com.sk89q.worldedit.world.block.BlockState; +import org.enginehub.linbus.tree.LinCompoundTag; + +import javax.annotation.Nullable; + +@Deprecated +public class LegacyBaseBlockWrapper extends BaseBlock { + protected LegacyBaseBlockWrapper(BlockState blockState) { + super(blockState); + } + + // These two methods force the legacy blocks to use the old NBT methods. + @Nullable + @Override + public LazyReference getNbtReference() { + CompoundTag nbtData = getNbtData(); + return nbtData == null ? null : LazyReference.from(nbtData::toLinTag); + } + + @Override + public void setNbtReference(@Nullable LazyReference nbtData) { + setNbtData(nbtData == null ? null : new CompoundTag(nbtData.getValue())); + } +} diff --git a/worldedit-core/src/legacy/java/com/sk89q/worldedit/blocks/MobSpawnerBlock.java b/worldedit-core/src/legacy/java/com/sk89q/worldedit/blocks/MobSpawnerBlock.java index 1f0efe7a16..d408310fda 100644 --- a/worldedit-core/src/legacy/java/com/sk89q/worldedit/blocks/MobSpawnerBlock.java +++ b/worldedit-core/src/legacy/java/com/sk89q/worldedit/blocks/MobSpawnerBlock.java @@ -28,7 +28,6 @@ import com.sk89q.jnbt.ShortTag; import com.sk89q.jnbt.StringTag; import com.sk89q.jnbt.Tag; -import com.sk89q.worldedit.world.block.BaseBlock; import com.sk89q.worldedit.world.block.BlockState; import com.sk89q.worldedit.world.storage.InvalidFormatException; @@ -42,7 +41,7 @@ * deprecated for removal without replacement */ @Deprecated(forRemoval = true) -public class MobSpawnerBlock extends BaseBlock { +public class MobSpawnerBlock extends LegacyBaseBlockWrapper { private String mobType; private short delay = -1; @@ -114,6 +113,7 @@ public void setDelay(short delay) { } @Override + @Deprecated public boolean hasNbtData() { return true; } @@ -124,6 +124,7 @@ public String getNbtId() { } @Override + @Deprecated public CompoundTag getNbtData() { Map> values = new HashMap<>(); values.put("Delay", new ShortTag(delay)); @@ -165,6 +166,7 @@ public CompoundTag getNbtData() { } @Override + @Deprecated public void setNbtData(CompoundTag rootTag) { if (rootTag == null) { return; @@ -184,7 +186,7 @@ public void setNbtData(CompoundTag rootTag) { try { spawnDataTag = NBTUtils.getChildTag(values, "SpawnData", CompoundTag.class); mobType = spawnDataTag.getString("id"); - if (mobType.equals("")) { + if (mobType.isEmpty()) { throw new InvalidFormatException("No spawn id."); } this.mobType = mobType; 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 bf9adfa389..b84de8cf0a 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 @@ -37,8 +37,12 @@ /** * Represents a sign block. + * + * @deprecated WorldEdit does not handle interpreting NBT, + * deprecated for removal without replacement */ -public class SignBlock extends BaseBlock { +@Deprecated +public class SignBlock extends LegacyBaseBlockWrapper { private String[] text; @@ -93,6 +97,7 @@ public void setText(String[] text) { } @Override + @Deprecated public boolean hasNbtData() { return true; } @@ -103,6 +108,7 @@ public String getNbtId() { } @Override + @Deprecated public CompoundTag getNbtData() { Map> values = new HashMap<>(); if (isLegacy()) { @@ -111,7 +117,7 @@ public CompoundTag getNbtData() { 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())); + 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)); @@ -120,6 +126,7 @@ public CompoundTag getNbtData() { } @Override + @Deprecated public void setNbtData(CompoundTag rootTag) { if (rootTag == null) { return; @@ -158,7 +165,7 @@ public void setNbtData(CompoundTag rootTag) { } } else { CompoundTag frontTextTag = (CompoundTag) values.get("front_text"); - ListTag messagesTag = frontTextTag.getListTag("messages"); + 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(); diff --git a/worldedit-core/src/legacy/java/com/sk89q/worldedit/blocks/SkullBlock.java b/worldedit-core/src/legacy/java/com/sk89q/worldedit/blocks/SkullBlock.java index a5b405f47e..efd73a8db1 100644 --- a/worldedit-core/src/legacy/java/com/sk89q/worldedit/blocks/SkullBlock.java +++ b/worldedit-core/src/legacy/java/com/sk89q/worldedit/blocks/SkullBlock.java @@ -23,7 +23,6 @@ import com.sk89q.jnbt.StringTag; import com.sk89q.jnbt.Tag; import com.sk89q.worldedit.internal.util.DeprecationUtil; -import com.sk89q.worldedit.world.block.BaseBlock; import com.sk89q.worldedit.world.block.BlockState; import java.util.HashMap; @@ -36,7 +35,7 @@ * deprecated for removal without replacement */ @Deprecated(forRemoval = true) -public class SkullBlock extends BaseBlock { +public class SkullBlock extends LegacyBaseBlockWrapper { private String owner = ""; // notchian @@ -89,6 +88,7 @@ public String getOwner() { } @Override + @Deprecated public boolean hasNbtData() { return true; } @@ -99,6 +99,7 @@ public String getNbtId() { } @Override + @Deprecated public CompoundTag getNbtData() { Map> values = new HashMap<>(); Map> inner = new HashMap<>(); @@ -108,6 +109,7 @@ public CompoundTag getNbtData() { } @Override + @Deprecated public void setNbtData(CompoundTag rootTag) { if (rootTag == null) { return;