001/*
002 * PlotSquared, a land and world management plugin for Minecraft.
003 * Copyright (C) IntellectualSites <https://intellectualsites.com>
004 * Copyright (C) IntellectualSites team and contributors
005 *
006 * This program is free software: you can redistribute it and/or modify
007 * it under the terms of the GNU General Public License as published by
008 * the Free Software Foundation, either version 3 of the License, or
009 * (at your option) any later version.
010 *
011 * This program is distributed in the hope that it will be useful,
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
014 * GNU General Public License for more details.
015 *
016 * You should have received a copy of the GNU General Public License
017 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
018 */
019package com.plotsquared.bukkit.schematic;
020
021import com.destroystokyo.paper.profile.PlayerProfile;
022import com.destroystokyo.paper.profile.ProfileProperty;
023import com.plotsquared.bukkit.util.BukkitUtil;
024import com.sk89q.jnbt.ByteTag;
025import com.sk89q.jnbt.CompoundTag;
026import com.sk89q.jnbt.ListTag;
027import com.sk89q.jnbt.ShortTag;
028import com.sk89q.jnbt.StringTag;
029import com.sk89q.jnbt.Tag;
030import com.sk89q.worldedit.blocks.BaseItemStack;
031import com.sk89q.worldedit.bukkit.BukkitAdapter;
032import com.sk89q.worldedit.world.item.ItemType;
033import io.papermc.lib.PaperLib;
034import org.apache.logging.log4j.LogManager;
035import org.apache.logging.log4j.Logger;
036import org.bukkit.Bukkit;
037import org.bukkit.ChatColor;
038import org.bukkit.DyeColor;
039import org.bukkit.World;
040import org.bukkit.block.Banner;
041import org.bukkit.block.Block;
042import org.bukkit.block.Container;
043import org.bukkit.block.Sign;
044import org.bukkit.block.Skull;
045import org.bukkit.block.banner.Pattern;
046import org.bukkit.block.banner.PatternType;
047import org.bukkit.enchantments.Enchantment;
048import org.bukkit.inventory.Inventory;
049import org.bukkit.inventory.ItemStack;
050import org.checkerframework.checker.nullness.qual.NonNull;
051
052import java.util.ArrayList;
053import java.util.HashMap;
054import java.util.List;
055import java.util.Map;
056import java.util.Map.Entry;
057import java.util.Objects;
058import java.util.UUID;
059
060public class StateWrapper {
061
062    public CompoundTag tag;
063
064    private boolean paperErrorTextureSent = false;
065    private static final Logger LOGGER = LogManager.getLogger("PlotSquared/" + StateWrapper.class.getSimpleName());
066
067    public StateWrapper(CompoundTag tag) {
068        this.tag = tag;
069    }
070
071    public static String jsonToColourCode(String str) {
072        str = str.replace("{\"extra\":", "").replace("],\"text\":\"\"}", "]")
073                .replace("[{\"color\":\"black\",\"text\":\"", "&0")
074                .replace("[{\"color\":\"dark_blue\",\"text\":\"", "&1")
075                .replace("[{\"color\":\"dark_green\",\"text\":\"", "&2")
076                .replace("[{\"color\":\"dark_aqua\",\"text\":\"", "&3")
077                .replace("[{\"color\":\"dark_red\",\"text\":\"", "&4")
078                .replace("[{\"color\":\"dark_purple\",\"text\":\"", "&5")
079                .replace("[{\"color\":\"gold\",\"text\":\"", "&6")
080                .replace("[{\"color\":\"gray\",\"text\":\"", "&7")
081                .replace("[{\"color\":\"dark_gray\",\"text\":\"", "&8")
082                .replace("[{\"color\":\"blue\",\"text\":\"", "&9")
083                .replace("[{\"color\":\"green\",\"text\":\"", "&a")
084                .replace("[{\"color\":\"aqua\",\"text\":\"", "&b")
085                .replace("[{\"color\":\"red\",\"text\":\"", "&c")
086                .replace("[{\"color\":\"light_purple\",\"text\":\"", "&d")
087                .replace("[{\"color\":\"yellow\",\"text\":\"", "&e")
088                .replace("[{\"color\":\"white\",\"text\":\"", "&f")
089                .replace("[{\"obfuscated\":true,\"text\":\"", "&k")
090                .replace("[{\"bold\":true,\"text\":\"", "&l")
091                .replace("[{\"strikethrough\":true,\"text\":\"", "&m")
092                .replace("[{\"underlined\":true,\"text\":\"", "&n")
093                .replace("[{\"italic\":true,\"text\":\"", "&o").replace("[{\"color\":\"black\",", "&0")
094                .replace("[{\"color\":\"dark_blue\",", "&1")
095                .replace("[{\"color\":\"dark_green\",", "&2")
096                .replace("[{\"color\":\"dark_aqua\",", "&3").replace("[{\"color\":\"dark_red\",", "&4")
097                .replace("[{\"color\":\"dark_purple\",", "&5").replace("[{\"color\":\"gold\",", "&6")
098                .replace("[{\"color\":\"gray\",", "&7").replace("[{\"color\":\"dark_gray\",", "&8")
099                .replace("[{\"color\":\"blue\",", "&9").replace("[{\"color\":\"green\",", "&a")
100                .replace("[{\"color\":\"aqua\",", "&b").replace("[{\"color\":\"red\",", "&c")
101                .replace("[{\"color\":\"light_purple\",", "&d").replace("[{\"color\":\"yellow\",", "&e")
102                .replace("[{\"color\":\"white\",", "&f").replace("[{\"obfuscated\":true,", "&k")
103                .replace("[{\"bold\":true,", "&l").replace("[{\"strikethrough\":true,", "&m")
104                .replace("[{\"underlined\":true,", "&n").replace("[{\"italic\":true,", "&o")
105                .replace("{\"color\":\"black\",\"text\":\"", "&0")
106                .replace("{\"color\":\"dark_blue\",\"text\":\"", "&1")
107                .replace("{\"color\":\"dark_green\",\"text\":\"", "&2")
108                .replace("{\"color\":\"dark_aqua\",\"text\":\"", "&3")
109                .replace("{\"color\":\"dark_red\",\"text\":\"", "&4")
110                .replace("{\"color\":\"dark_purple\",\"text\":\"", "&5")
111                .replace("{\"color\":\"gold\",\"text\":\"", "&6")
112                .replace("{\"color\":\"gray\",\"text\":\"", "&7")
113                .replace("{\"color\":\"dark_gray\",\"text\":\"", "&8")
114                .replace("{\"color\":\"blue\",\"text\":\"", "&9")
115                .replace("{\"color\":\"green\",\"text\":\"", "&a")
116                .replace("{\"color\":\"aqua\",\"text\":\"", "&b")
117                .replace("{\"color\":\"red\",\"text\":\"", "&c")
118                .replace("{\"color\":\"light_purple\",\"text\":\"", "&d")
119                .replace("{\"color\":\"yellow\",\"text\":\"", "&e")
120                .replace("{\"color\":\"white\",\"text\":\"", "&f")
121                .replace("{\"obfuscated\":true,\"text\":\"", "&k")
122                .replace("{\"bold\":true,\"text\":\"", "&l")
123                .replace("{\"strikethrough\":true,\"text\":\"", "&m")
124                .replace("{\"underlined\":true,\"text\":\"", "&n")
125                .replace("{\"italic\":true,\"text\":\"", "&o").replace("{\"color\":\"black\",", "&0")
126                .replace("{\"color\":\"dark_blue\",", "&1").replace("{\"color\":\"dark_green\",", "&2")
127                .replace("{\"color\":\"dark_aqua\",", "&3").replace("{\"color\":\"dark_red\",", "&4")
128                .replace("{\"color\":\"dark_purple\",", "&5").replace("{\"color\":\"gold\",", "&6")
129                .replace("{\"color\":\"gray\",", "&7").replace("{\"color\":\"dark_gray\",", "&8")
130                .replace("{\"color\":\"blue\",", "&9").replace("{\"color\":\"green\",", "&a")
131                .replace("{\"color\":\"aqua\",", "&b").replace("{\"color\":\"red\",", "&c")
132                .replace("{\"color\":\"light_purple\",", "&d").replace("{\"color\":\"yellow\",", "&e")
133                .replace("{\"color\":\"white\",", "&f").replace("{\"obfuscated\":true,", "&k")
134                .replace("{\"bold\":true,", "&l").replace("{\"strikethrough\":true,", "&m")
135                .replace("{\"underlined\":true,", "&n").replace("{\"italic\":true,", "&o")
136                .replace("\"color\":\"black\",\"text\":\"", "&0")
137                .replace("\"color\":\"dark_blue\",\"text\":\"", "&1")
138                .replace("\"color\":\"dark_green\",\"text\":\"", "&2")
139                .replace("\"color\":\"dark_aqua\",\"text\":\"", "&3")
140                .replace("\"color\":\"dark_red\",\"text\":\"", "&4")
141                .replace("\"color\":\"dark_purple\",\"text\":\"", "&5")
142                .replace("\"color\":\"gold\",\"text\":\"", "&6")
143                .replace("\"color\":\"gray\",\"text\":\"", "&7")
144                .replace("\"color\":\"dark_gray\",\"text\":\"", "&8")
145                .replace("\"color\":\"blue\",\"text\":\"", "&9")
146                .replace("\"color\":\"green\",\"text\":\"", "&a")
147                .replace("\"color\":\"aqua\",\"text\":\"", "&b")
148                .replace("\"color\":\"red\",\"text\":\"", "&c")
149                .replace("\"color\":\"light_purple\",\"text\":\"", "&d")
150                .replace("\"color\":\"yellow\",\"text\":\"", "&e")
151                .replace("\"color\":\"white\",\"text\":\"", "&f")
152                .replace("\"obfuscated\":true,\"text\":\"", "&k")
153                .replace("\"bold\":true,\"text\":\"", "&l")
154                .replace("\"strikethrough\":true,\"text\":\"", "&m")
155                .replace("\"underlined\":true,\"text\":\"", "&n")
156                .replace("\"italic\":true,\"text\":\"", "&o").replace("\"color\":\"black\",", "&0")
157                .replace("\"color\":\"dark_blue\",", "&1").replace("\"color\":\"dark_green\",", "&2")
158                .replace("\"color\":\"dark_aqua\",", "&3").replace("\"color\":\"dark_red\",", "&4")
159                .replace("\"color\":\"dark_purple\",", "&5").replace("\"color\":\"gold\",", "&6")
160                .replace("\"color\":\"gray\",", "&7").replace("\"color\":\"dark_gray\",", "&8")
161                .replace("\"color\":\"blue\",", "&9").replace("\"color\":\"green\",", "&a")
162                .replace("\"color\":\"aqua\",", "&b").replace("\"color\":\"red\",", "&c")
163                .replace("\"color\":\"light_purple\",", "&d").replace("\"color\":\"yellow\",", "&e")
164                .replace("\"color\":\"white\",", "&f").replace("\"obfuscated\":true,", "&k")
165                .replace("\"bold\":true,", "&l").replace("\"strikethrough\":true,", "&m")
166                .replace("\"underlined\":true,", "&n").replace("\"italic\":true,", "&o")
167                .replace("[{\"text\":\"", "&0").replace("{\"text\":\"", "&0").replace("\"},", "")
168                .replace("\"}]", "").replace("\"}", "");
169        str = ChatColor.translateAlternateColorCodes('&', str);
170        return str;
171    }
172
173    /**
174     * Restore the TileEntity data to the given world at the given coordinates.
175     *
176     * @param worldName World name
177     * @param x         x position
178     * @param y         y position
179     * @param z         z position
180     * @return true if successful
181     */
182    public boolean restoreTag(String worldName, int x, int y, int z) {
183        World world = BukkitUtil.getWorld(worldName);
184        if (world == null) {
185            return false;
186        }
187        return restoreTag(world.getBlockAt(x, y, z));
188    }
189
190    /**
191     * Restore the TileEntity data to the given block
192     *
193     * @param block Block to restore to
194     * @return true if successful
195     */
196    @SuppressWarnings("deprecation") // #setLine is needed for Spigot compatibility
197    public boolean restoreTag(@NonNull Block block) {
198        if (this.tag == null) {
199            return false;
200        }
201        org.bukkit.block.BlockState state = block.getState();
202        switch (getId()) {
203            case "chest", "beacon", "brewingstand", "dispenser", "dropper", "furnace", "hopper", "shulkerbox" -> {
204                if (!(state instanceof Container container)) {
205                    return false;
206                }
207                List<Tag> itemsTag = this.tag.getListTag("Items").getValue();
208                Inventory inv = container.getSnapshotInventory();
209                for (Tag itemTag : itemsTag) {
210                    CompoundTag itemComp = (CompoundTag) itemTag;
211                    ItemType type = ItemType.REGISTRY.get(itemComp.getString("id").toLowerCase());
212                    if (type == null) {
213                        continue;
214                    }
215                    // At some point they used lowercase "count" as IntTag; older format uses "Count" as ByteTag
216                    int count = itemComp.getValue().containsKey("count")
217                            ? itemComp.getInt("count")
218                            : itemComp.getByte("Count");
219                    int slot = itemComp.getByte("Slot");
220                    CompoundTag tag = (CompoundTag) itemComp.getValue().get("tag");
221                    BaseItemStack baseItemStack = new BaseItemStack(type, tag, count);
222                    ItemStack itemStack = BukkitAdapter.adapt(baseItemStack);
223                    inv.setItem(slot, itemStack);
224                }
225                container.update(true, false);
226                return true;
227            }
228            case "sign" -> {
229                if (state instanceof Sign sign) {
230                    sign.setLine(0, jsonToColourCode(tag.getString("Text1")));
231                    sign.setLine(1, jsonToColourCode(tag.getString("Text2")));
232                    sign.setLine(2, jsonToColourCode(tag.getString("Text3")));
233                    sign.setLine(3, jsonToColourCode(tag.getString("Text4")));
234                    state.update(true);
235                    return true;
236                }
237                return false;
238            }
239            case "skull" -> {
240                if (state instanceof Skull skull) {
241                    CompoundTag skullOwner = ((CompoundTag) this.tag.getValue().get("SkullOwner"));
242                    if (skullOwner == null) {
243                        return true;
244                    }
245                    String player = skullOwner.getString("Name");
246
247                    if (player != null && !player.isEmpty()) {
248                        try {
249                            skull.setOwningPlayer(Bukkit.getOfflinePlayer(player));
250                            skull.update(true);
251                        } catch (Exception e) {
252                            e.printStackTrace();
253                        }
254                        return true;
255                    }
256
257                    final CompoundTag properties = (CompoundTag) skullOwner.getValue().get("Properties");
258                    if (properties == null) {
259                        return false;
260                    }
261                    final ListTag textures = properties.getListTag("textures");
262                    if (textures.getValue().isEmpty()) {
263                        return false;
264                    }
265                    final CompoundTag textureCompound = (CompoundTag) textures.getValue().get(0);
266                    if (textureCompound == null) {
267                        return false;
268                    }
269                    String textureValue = textureCompound.getString("Value");
270                    if (textureValue == null) {
271                        return false;
272                    }
273                    if (!PaperLib.isPaper()) {
274                        if (!paperErrorTextureSent) {
275                            paperErrorTextureSent = true;
276                            LOGGER.error("Failed to populate skull data in your road schematic - This is a Spigot limitation.");
277                        }
278                        return false;
279                    }
280                    final PlayerProfile profile = Bukkit.createProfile(UUID.randomUUID());
281                    profile.setProperty(new ProfileProperty("textures", textureValue));
282                    skull.setPlayerProfile(profile);
283                    skull.update(true);
284                    return true;
285
286                }
287                return false;
288            }
289            case "banner" -> {
290                if (state instanceof Banner banner) {
291                    List<Tag> patterns = this.tag.getListTag("Patterns").getValue();
292                    if (patterns == null || patterns.isEmpty()) {
293                        return false;
294                    }
295                    banner.setPatterns(patterns.stream().map(t -> (CompoundTag) t).map(compoundTag -> {
296                        DyeColor color = DyeColor.getByWoolData((byte) compoundTag.getInt("Color"));
297                        PatternType patternType = PatternType.getByIdentifier(compoundTag.getString("Pattern"));
298                        if (color == null || patternType == null) {
299                            return null;
300                        }
301                        return new Pattern(color, patternType);
302                    }).filter(Objects::nonNull).toList());
303                    banner.update(true);
304                    return true;
305                }
306                return false;
307            }
308        }
309        return false;
310    }
311
312    public String getId() {
313        String tileid = this.tag.getString("id").toLowerCase();
314        if (tileid.startsWith("minecraft:")) {
315            tileid = tileid.replace("minecraft:", "");
316        }
317        return tileid;
318    }
319
320    public List<CompoundTag> serializeInventory(ItemStack[] items) {
321        List<CompoundTag> tags = new ArrayList<>();
322        for (int i = 0; i < items.length; ++i) {
323            if (items[i] != null) {
324                Map<String, Tag> tagData = serializeItem(items[i]);
325                tagData.put("Slot", new ByteTag((byte) i));
326                tags.add(new CompoundTag(tagData));
327            }
328        }
329        return tags;
330    }
331
332    public Map<String, Tag> serializeItem(ItemStack item) {
333        Map<String, Tag> data = new HashMap<>();
334        data.put("id", new StringTag(item.getType().name()));
335        data.put("Damage", new ShortTag(item.getDurability()));
336        data.put("Count", new ByteTag((byte) item.getAmount()));
337        if (!item.getEnchantments().isEmpty()) {
338            List<CompoundTag> enchantmentList = new ArrayList<>();
339            for (Entry<Enchantment, Integer> entry : item.getEnchantments().entrySet()) {
340                Map<String, Tag> enchantment = new HashMap<>();
341                enchantment.put("id", new StringTag(entry.getKey().toString()));
342                enchantment.put("lvl", new ShortTag(entry.getValue().shortValue()));
343                enchantmentList.add(new CompoundTag(enchantment));
344            }
345            Map<String, Tag> auxData = new HashMap<>();
346            auxData.put("ench", new ListTag(CompoundTag.class, enchantmentList));
347            data.put("tag", new CompoundTag(auxData));
348        }
349        return data;
350    }
351
352}