From 6b8ae6b988ff8d38c60ce9c4b076d3a417a61bfd Mon Sep 17 00:00:00 2001 From: Wyatt Childers Date: Tue, 4 Jul 2017 14:45:14 -0400 Subject: [PATCH] Provide compatibility for legacy schematics that use the old sign NBT format --- .../extent/clipboard/io/SchematicReader.java | 17 ++++++ .../legacycompat/NBTCompatibilityHandler.java | 30 +++++++++ .../SignCompatibilityHandler.java | 61 +++++++++++++++++++ 3 files changed, 108 insertions(+) create mode 100644 worldedit-core/src/main/java/com/sk89q/worldedit/extent/clipboard/io/legacycompat/NBTCompatibilityHandler.java create mode 100644 worldedit-core/src/main/java/com/sk89q/worldedit/extent/clipboard/io/legacycompat/SignCompatibilityHandler.java diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/extent/clipboard/io/SchematicReader.java b/worldedit-core/src/main/java/com/sk89q/worldedit/extent/clipboard/io/SchematicReader.java index 771d850cf..a4f4fbaa6 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/extent/clipboard/io/SchematicReader.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/extent/clipboard/io/SchematicReader.java @@ -35,6 +35,8 @@ import com.sk89q.worldedit.entity.BaseEntity; import com.sk89q.worldedit.extent.clipboard.BlockArrayClipboard; import com.sk89q.worldedit.extent.clipboard.Clipboard; +import com.sk89q.worldedit.extent.clipboard.io.legacycompat.NBTCompatibilityHandler; +import com.sk89q.worldedit.extent.clipboard.io.legacycompat.SignCompatibilityHandler; import com.sk89q.worldedit.regions.CuboidRegion; import com.sk89q.worldedit.regions.Region; import com.sk89q.worldedit.util.Location; @@ -43,6 +45,7 @@ import javax.annotation.Nullable; import java.io.IOException; +import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -56,6 +59,12 @@ */ public class SchematicReader implements ClipboardReader { + private static final List COMPATIBILITY_HANDLERS = new ArrayList<>(); + + static { + COMPATIBILITY_HANDLERS.add(new SignCompatibilityHandler()); + } + private static final Logger log = Logger.getLogger(SchematicReader.class.getCanonicalName()); private final NBTInputStream inputStream; @@ -181,6 +190,14 @@ public Clipboard read(WorldData data) throws IOException { values.put(entry.getKey(), entry.getValue()); } + int index = y * width * length + z * width + x; + BaseBlock block = new BaseBlock(blocks[index], blockData[index]); + for (NBTCompatibilityHandler handler : COMPATIBILITY_HANDLERS) { + if (handler.isAffectedBlock(block)) { + handler.updateNBT(block, values); + } + } + BlockVector vec = new BlockVector(x, y, z); tileEntitiesMap.put(vec, values); } diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/extent/clipboard/io/legacycompat/NBTCompatibilityHandler.java b/worldedit-core/src/main/java/com/sk89q/worldedit/extent/clipboard/io/legacycompat/NBTCompatibilityHandler.java new file mode 100644 index 000000000..83f981a6f --- /dev/null +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/extent/clipboard/io/legacycompat/NBTCompatibilityHandler.java @@ -0,0 +1,30 @@ +/* + * 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 Lesser 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 Lesser General Public License + * for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program. If not, see . + */ + +package com.sk89q.worldedit.extent.clipboard.io.legacycompat; + +import com.sk89q.jnbt.Tag; +import com.sk89q.worldedit.blocks.BaseBlock; + +import java.util.Map; + +public interface NBTCompatibilityHandler { + boolean isAffectedBlock(BaseBlock block); + void updateNBT(BaseBlock block, Map values); +} diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/extent/clipboard/io/legacycompat/SignCompatibilityHandler.java b/worldedit-core/src/main/java/com/sk89q/worldedit/extent/clipboard/io/legacycompat/SignCompatibilityHandler.java new file mode 100644 index 000000000..4ca4a1486 --- /dev/null +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/extent/clipboard/io/legacycompat/SignCompatibilityHandler.java @@ -0,0 +1,61 @@ +/* + * 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 Lesser 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 Lesser General Public License + * for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program. If not, see . + */ + +package com.sk89q.worldedit.extent.clipboard.io.legacycompat; + +import com.google.gson.JsonElement; +import com.google.gson.JsonObject; +import com.google.gson.JsonParser; +import com.google.gson.JsonPrimitive; +import com.sk89q.jnbt.StringTag; +import com.sk89q.jnbt.Tag; +import com.sk89q.worldedit.blocks.BaseBlock; +import com.sk89q.worldedit.blocks.BlockID; + +import java.util.Map; + +public class SignCompatibilityHandler implements NBTCompatibilityHandler { + @Override + public boolean isAffectedBlock(BaseBlock block) { + return block.getType() == BlockID.SIGN_POST || block.getType() == BlockID.WALL_SIGN; + } + + @Override + public void updateNBT(BaseBlock block, Map values) { + for (int i = 0; i < 4; ++i) { + String key = "Text" + (i + 1); + Tag value = values.get(key); + if (value instanceof StringTag) { + String storedString = ((StringTag) value).getValue(); + JsonElement jsonElement = new JsonParser().parse(storedString); + if (jsonElement.isJsonObject()) { + continue; + } + + if (jsonElement.isJsonNull()) { + jsonElement = new JsonPrimitive(""); + } + + JsonObject jsonTextObject = new JsonObject(); + jsonTextObject.add("text", jsonElement); + values.put("Text" + (i + 1), new StringTag(jsonTextObject.toString())); + } + } + } +}