2020-04-16 04:52:39 +02:00
|
|
|
/*
|
|
|
|
* _____ _ _ _____ _
|
|
|
|
* | __ \| | | | / ____| | |
|
|
|
|
* | |__) | | ___ | |_| (___ __ _ _ _ __ _ _ __ ___ __| |
|
|
|
|
* | ___/| |/ _ \| __|\___ \ / _` | | | |/ _` | '__/ _ \/ _` |
|
|
|
|
* | | | | (_) | |_ ____) | (_| | |_| | (_| | | | __/ (_| |
|
|
|
|
* |_| |_|\___/ \__|_____/ \__, |\__,_|\__,_|_| \___|\__,_|
|
|
|
|
* | |
|
|
|
|
* |_|
|
|
|
|
* PlotSquared plot management system for Minecraft
|
|
|
|
* Copyright (C) 2020 IntellectualSites
|
|
|
|
*
|
|
|
|
* 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 <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2020-04-15 21:26:54 +02:00
|
|
|
package com.plotsquared.bukkit.schematic;
|
2015-02-25 13:25:28 +01:00
|
|
|
|
2020-04-11 02:19:18 +02:00
|
|
|
import com.plotsquared.bukkit.util.BukkitUtil;
|
2020-04-15 21:26:54 +02:00
|
|
|
import com.plotsquared.core.PlotSquared;
|
2020-04-16 06:14:33 +02:00
|
|
|
import com.plotsquared.core.configuration.Captions;
|
2019-11-04 18:44:23 +01:00
|
|
|
import com.sk89q.jnbt.ByteTag;
|
|
|
|
import com.sk89q.jnbt.CompoundTag;
|
|
|
|
import com.sk89q.jnbt.ListTag;
|
|
|
|
import com.sk89q.jnbt.ShortTag;
|
|
|
|
import com.sk89q.jnbt.StringTag;
|
|
|
|
import com.sk89q.jnbt.Tag;
|
2018-12-20 00:18:57 +01:00
|
|
|
import org.bukkit.Material;
|
2016-03-31 19:23:26 +02:00
|
|
|
import org.bukkit.World;
|
|
|
|
import org.bukkit.block.Block;
|
2018-12-31 18:15:47 +01:00
|
|
|
import org.bukkit.block.Container;
|
2018-12-24 18:56:13 +01:00
|
|
|
import org.bukkit.block.Sign;
|
2016-03-31 19:23:26 +02:00
|
|
|
import org.bukkit.enchantments.Enchantment;
|
|
|
|
import org.bukkit.inventory.Inventory;
|
|
|
|
import org.bukkit.inventory.InventoryHolder;
|
|
|
|
import org.bukkit.inventory.ItemStack;
|
2015-07-30 16:25:16 +02:00
|
|
|
|
2016-04-02 07:30:26 +02:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Map;
|
|
|
|
import java.util.Map.Entry;
|
|
|
|
|
2015-09-13 06:04:31 +02:00
|
|
|
public class StateWrapper {
|
2016-03-23 18:09:13 +01:00
|
|
|
|
2019-11-04 20:55:55 +01:00
|
|
|
public org.bukkit.block.BlockState state = null;
|
2015-02-26 05:16:52 +01:00
|
|
|
public CompoundTag tag = null;
|
2016-03-23 18:09:13 +01:00
|
|
|
|
2019-11-04 20:55:55 +01:00
|
|
|
public StateWrapper(org.bukkit.block.BlockState state) {
|
2015-02-25 13:25:28 +01:00
|
|
|
this.state = state;
|
|
|
|
}
|
2016-03-23 18:09:13 +01:00
|
|
|
|
|
|
|
public StateWrapper(CompoundTag tag) {
|
2015-02-26 05:16:52 +01:00
|
|
|
this.tag = tag;
|
|
|
|
}
|
2016-03-23 18:09:13 +01:00
|
|
|
|
2018-12-24 18:56:13 +01:00
|
|
|
public static String jsonToColourCode(String str) {
|
|
|
|
str = str.replace("{\"extra\":", "").replace("],\"text\":\"\"}", "]")
|
|
|
|
.replace("[{\"color\":\"black\",\"text\":\"", "&0")
|
|
|
|
.replace("[{\"color\":\"dark_blue\",\"text\":\"", "&1")
|
|
|
|
.replace("[{\"color\":\"dark_green\",\"text\":\"", "&2")
|
|
|
|
.replace("[{\"color\":\"dark_aqua\",\"text\":\"", "&3")
|
|
|
|
.replace("[{\"color\":\"dark_red\",\"text\":\"", "&4")
|
|
|
|
.replace("[{\"color\":\"dark_purple\",\"text\":\"", "&5")
|
|
|
|
.replace("[{\"color\":\"gold\",\"text\":\"", "&6")
|
|
|
|
.replace("[{\"color\":\"gray\",\"text\":\"", "&7")
|
|
|
|
.replace("[{\"color\":\"dark_gray\",\"text\":\"", "&8")
|
|
|
|
.replace("[{\"color\":\"blue\",\"text\":\"", "&9")
|
|
|
|
.replace("[{\"color\":\"green\",\"text\":\"", "&a")
|
|
|
|
.replace("[{\"color\":\"aqua\",\"text\":\"", "&b")
|
|
|
|
.replace("[{\"color\":\"red\",\"text\":\"", "&c")
|
|
|
|
.replace("[{\"color\":\"light_purple\",\"text\":\"", "&d")
|
|
|
|
.replace("[{\"color\":\"yellow\",\"text\":\"", "&e")
|
|
|
|
.replace("[{\"color\":\"white\",\"text\":\"", "&f")
|
|
|
|
.replace("[{\"obfuscated\":true,\"text\":\"", "&k")
|
|
|
|
.replace("[{\"bold\":true,\"text\":\"", "&l")
|
|
|
|
.replace("[{\"strikethrough\":true,\"text\":\"", "&m")
|
|
|
|
.replace("[{\"underlined\":true,\"text\":\"", "&n")
|
|
|
|
.replace("[{\"italic\":true,\"text\":\"", "&o").replace("[{\"color\":\"black\",", "&0")
|
|
|
|
.replace("[{\"color\":\"dark_blue\",", "&1")
|
|
|
|
.replace("[{\"color\":\"dark_green\",", "&2")
|
|
|
|
.replace("[{\"color\":\"dark_aqua\",", "&3").replace("[{\"color\":\"dark_red\",", "&4")
|
|
|
|
.replace("[{\"color\":\"dark_purple\",", "&5").replace("[{\"color\":\"gold\",", "&6")
|
|
|
|
.replace("[{\"color\":\"gray\",", "&7").replace("[{\"color\":\"dark_gray\",", "&8")
|
|
|
|
.replace("[{\"color\":\"blue\",", "&9").replace("[{\"color\":\"green\",", "&a")
|
|
|
|
.replace("[{\"color\":\"aqua\",", "&b").replace("[{\"color\":\"red\",", "&c")
|
|
|
|
.replace("[{\"color\":\"light_purple\",", "&d").replace("[{\"color\":\"yellow\",", "&e")
|
|
|
|
.replace("[{\"color\":\"white\",", "&f").replace("[{\"obfuscated\":true,", "&k")
|
|
|
|
.replace("[{\"bold\":true,", "&l").replace("[{\"strikethrough\":true,", "&m")
|
|
|
|
.replace("[{\"underlined\":true,", "&n").replace("[{\"italic\":true,", "&o")
|
|
|
|
.replace("{\"color\":\"black\",\"text\":\"", "&0")
|
|
|
|
.replace("{\"color\":\"dark_blue\",\"text\":\"", "&1")
|
|
|
|
.replace("{\"color\":\"dark_green\",\"text\":\"", "&2")
|
|
|
|
.replace("{\"color\":\"dark_aqua\",\"text\":\"", "&3")
|
|
|
|
.replace("{\"color\":\"dark_red\",\"text\":\"", "&4")
|
|
|
|
.replace("{\"color\":\"dark_purple\",\"text\":\"", "&5")
|
|
|
|
.replace("{\"color\":\"gold\",\"text\":\"", "&6")
|
|
|
|
.replace("{\"color\":\"gray\",\"text\":\"", "&7")
|
|
|
|
.replace("{\"color\":\"dark_gray\",\"text\":\"", "&8")
|
|
|
|
.replace("{\"color\":\"blue\",\"text\":\"", "&9")
|
|
|
|
.replace("{\"color\":\"green\",\"text\":\"", "&a")
|
|
|
|
.replace("{\"color\":\"aqua\",\"text\":\"", "&b")
|
|
|
|
.replace("{\"color\":\"red\",\"text\":\"", "&c")
|
|
|
|
.replace("{\"color\":\"light_purple\",\"text\":\"", "&d")
|
|
|
|
.replace("{\"color\":\"yellow\",\"text\":\"", "&e")
|
|
|
|
.replace("{\"color\":\"white\",\"text\":\"", "&f")
|
|
|
|
.replace("{\"obfuscated\":true,\"text\":\"", "&k")
|
|
|
|
.replace("{\"bold\":true,\"text\":\"", "&l")
|
|
|
|
.replace("{\"strikethrough\":true,\"text\":\"", "&m")
|
|
|
|
.replace("{\"underlined\":true,\"text\":\"", "&n")
|
|
|
|
.replace("{\"italic\":true,\"text\":\"", "&o").replace("{\"color\":\"black\",", "&0")
|
|
|
|
.replace("{\"color\":\"dark_blue\",", "&1").replace("{\"color\":\"dark_green\",", "&2")
|
|
|
|
.replace("{\"color\":\"dark_aqua\",", "&3").replace("{\"color\":\"dark_red\",", "&4")
|
|
|
|
.replace("{\"color\":\"dark_purple\",", "&5").replace("{\"color\":\"gold\",", "&6")
|
|
|
|
.replace("{\"color\":\"gray\",", "&7").replace("{\"color\":\"dark_gray\",", "&8")
|
|
|
|
.replace("{\"color\":\"blue\",", "&9").replace("{\"color\":\"green\",", "&a")
|
|
|
|
.replace("{\"color\":\"aqua\",", "&b").replace("{\"color\":\"red\",", "&c")
|
|
|
|
.replace("{\"color\":\"light_purple\",", "&d").replace("{\"color\":\"yellow\",", "&e")
|
|
|
|
.replace("{\"color\":\"white\",", "&f").replace("{\"obfuscated\":true,", "&k")
|
|
|
|
.replace("{\"bold\":true,", "&l").replace("{\"strikethrough\":true,", "&m")
|
|
|
|
.replace("{\"underlined\":true,", "&n").replace("{\"italic\":true,", "&o")
|
|
|
|
.replace("\"color\":\"black\",\"text\":\"", "&0")
|
|
|
|
.replace("\"color\":\"dark_blue\",\"text\":\"", "&1")
|
|
|
|
.replace("\"color\":\"dark_green\",\"text\":\"", "&2")
|
|
|
|
.replace("\"color\":\"dark_aqua\",\"text\":\"", "&3")
|
|
|
|
.replace("\"color\":\"dark_red\",\"text\":\"", "&4")
|
|
|
|
.replace("\"color\":\"dark_purple\",\"text\":\"", "&5")
|
|
|
|
.replace("\"color\":\"gold\",\"text\":\"", "&6")
|
|
|
|
.replace("\"color\":\"gray\",\"text\":\"", "&7")
|
|
|
|
.replace("\"color\":\"dark_gray\",\"text\":\"", "&8")
|
|
|
|
.replace("\"color\":\"blue\",\"text\":\"", "&9")
|
|
|
|
.replace("\"color\":\"green\",\"text\":\"", "&a")
|
|
|
|
.replace("\"color\":\"aqua\",\"text\":\"", "&b")
|
|
|
|
.replace("\"color\":\"red\",\"text\":\"", "&c")
|
|
|
|
.replace("\"color\":\"light_purple\",\"text\":\"", "&d")
|
|
|
|
.replace("\"color\":\"yellow\",\"text\":\"", "&e")
|
|
|
|
.replace("\"color\":\"white\",\"text\":\"", "&f")
|
|
|
|
.replace("\"obfuscated\":true,\"text\":\"", "&k")
|
|
|
|
.replace("\"bold\":true,\"text\":\"", "&l")
|
|
|
|
.replace("\"strikethrough\":true,\"text\":\"", "&m")
|
|
|
|
.replace("\"underlined\":true,\"text\":\"", "&n")
|
|
|
|
.replace("\"italic\":true,\"text\":\"", "&o").replace("\"color\":\"black\",", "&0")
|
|
|
|
.replace("\"color\":\"dark_blue\",", "&1").replace("\"color\":\"dark_green\",", "&2")
|
|
|
|
.replace("\"color\":\"dark_aqua\",", "&3").replace("\"color\":\"dark_red\",", "&4")
|
|
|
|
.replace("\"color\":\"dark_purple\",", "&5").replace("\"color\":\"gold\",", "&6")
|
|
|
|
.replace("\"color\":\"gray\",", "&7").replace("\"color\":\"dark_gray\",", "&8")
|
|
|
|
.replace("\"color\":\"blue\",", "&9").replace("\"color\":\"green\",", "&a")
|
|
|
|
.replace("\"color\":\"aqua\",", "&b").replace("\"color\":\"red\",", "&c")
|
|
|
|
.replace("\"color\":\"light_purple\",", "&d").replace("\"color\":\"yellow\",", "&e")
|
|
|
|
.replace("\"color\":\"white\",", "&f").replace("\"obfuscated\":true,", "&k")
|
|
|
|
.replace("\"bold\":true,", "&l").replace("\"strikethrough\":true,", "&m")
|
|
|
|
.replace("\"underlined\":true,", "&n").replace("\"italic\":true,", "&o")
|
|
|
|
.replace("[{\"text\":\"", "&0").replace("{\"text\":\"", "&0").replace("\"},", "")
|
|
|
|
.replace("\"}]", "").replace("\"}", "");
|
2019-02-22 17:51:06 +01:00
|
|
|
for (Entry<String, String> entry : Captions.replacements.entrySet()) {
|
2018-12-24 18:56:13 +01:00
|
|
|
str = str.replace(entry.getKey(), entry.getValue());
|
|
|
|
}
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
2016-03-31 19:23:26 +02:00
|
|
|
public boolean restoreTag(String worldName, int x, int y, int z) {
|
2016-03-23 18:09:13 +01:00
|
|
|
if (this.tag == null) {
|
2015-09-13 06:04:31 +02:00
|
|
|
return false;
|
|
|
|
}
|
2018-12-24 18:56:13 +01:00
|
|
|
String tileid = this.tag.getString("id").toLowerCase();
|
|
|
|
if (tileid.startsWith("minecraft:")) {
|
|
|
|
tileid = tileid.replace("minecraft:", "");
|
|
|
|
}
|
|
|
|
World world = BukkitUtil.getWorld(worldName);
|
|
|
|
Block block = world.getBlockAt(x, y, z);
|
|
|
|
if (block == null) {
|
|
|
|
return false;
|
|
|
|
}
|
2019-11-04 20:55:55 +01:00
|
|
|
org.bukkit.block.BlockState state = block.getState();
|
2018-12-24 18:56:13 +01:00
|
|
|
switch (tileid) {
|
2016-04-02 07:30:26 +02:00
|
|
|
case "chest":
|
2018-12-31 18:15:47 +01:00
|
|
|
case "beacon":
|
|
|
|
case "brewingstand":
|
|
|
|
case "dispenser":
|
|
|
|
case "dropper":
|
|
|
|
case "furnace":
|
|
|
|
case "hopper":
|
|
|
|
case "shulkerbox":
|
2016-03-31 19:23:26 +02:00
|
|
|
List<Tag> itemsTag = this.tag.getListTag("Items").getValue();
|
|
|
|
int length = itemsTag.size();
|
2018-12-20 00:18:57 +01:00
|
|
|
String[] ids = new String[length];
|
2016-03-31 19:23:26 +02:00
|
|
|
byte[] amounts = new byte[length];
|
|
|
|
byte[] slots = new byte[length];
|
|
|
|
for (int i = 0; i < length; i++) {
|
|
|
|
Tag itemTag = itemsTag.get(i);
|
|
|
|
CompoundTag itemComp = (CompoundTag) itemTag;
|
2018-12-31 18:15:47 +01:00
|
|
|
String id = itemComp.getString("id");
|
|
|
|
if (id.startsWith("minecraft:")) {
|
|
|
|
id = id.replace("minecraft:", "");
|
|
|
|
}
|
2016-03-31 19:23:26 +02:00
|
|
|
ids[i] = id;
|
|
|
|
amounts[i] = itemComp.getByte("Count");
|
|
|
|
slots[i] = itemComp.getByte("Slot");
|
|
|
|
}
|
2018-12-31 18:15:47 +01:00
|
|
|
if (state instanceof Container) {
|
|
|
|
Container container = (Container) state;
|
|
|
|
Inventory inv = container.getSnapshotInventory();
|
2016-03-31 19:23:26 +02:00
|
|
|
for (int i = 0; i < ids.length; i++) {
|
2018-12-31 18:15:47 +01:00
|
|
|
Material mat = Material.getMaterial(ids[i].toUpperCase());
|
|
|
|
if (mat != null) {
|
|
|
|
ItemStack item = new ItemStack(mat, (int) amounts[i]);
|
|
|
|
inv.setItem(slots[i], item);
|
|
|
|
}
|
2016-03-31 19:23:26 +02:00
|
|
|
}
|
2018-12-31 18:15:47 +01:00
|
|
|
container.update(true, true);
|
2016-03-31 19:23:26 +02:00
|
|
|
return true;
|
|
|
|
}
|
2018-12-24 18:56:13 +01:00
|
|
|
return false;
|
|
|
|
case "sign":
|
|
|
|
if (state instanceof Sign) {
|
|
|
|
Sign sign = (Sign) state;
|
|
|
|
sign.setLine(0, jsonToColourCode(tag.getString("Text1")));
|
|
|
|
sign.setLine(1, jsonToColourCode(tag.getString("Text2")));
|
|
|
|
sign.setLine(2, jsonToColourCode(tag.getString("Text3")));
|
|
|
|
sign.setLine(3, jsonToColourCode(tag.getString("Text4")));
|
|
|
|
state.update(true);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2015-02-26 05:16:52 +01:00
|
|
|
}
|
2016-03-31 19:23:26 +02:00
|
|
|
return false;
|
2015-02-26 05:16:52 +01:00
|
|
|
}
|
2016-03-23 18:09:13 +01:00
|
|
|
|
2015-09-13 06:04:31 +02:00
|
|
|
public CompoundTag getTag() {
|
2016-03-23 18:09:13 +01:00
|
|
|
if (this.tag != null) {
|
|
|
|
return this.tag;
|
2015-09-13 06:04:31 +02:00
|
|
|
}
|
2016-03-23 18:09:13 +01:00
|
|
|
if (this.state instanceof InventoryHolder) {
|
|
|
|
InventoryHolder inv = (InventoryHolder) this.state;
|
|
|
|
ItemStack[] contents = inv.getInventory().getContents();
|
|
|
|
Map<String, Tag> values = new HashMap<>();
|
2018-12-20 00:18:57 +01:00
|
|
|
values.put("Items", new ListTag(CompoundTag.class, serializeInventory(contents)));
|
2015-02-25 13:25:28 +01:00
|
|
|
return new CompoundTag(values);
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
2016-03-23 18:09:13 +01:00
|
|
|
|
2015-09-13 06:04:31 +02:00
|
|
|
public String getId() {
|
2015-02-25 13:25:28 +01:00
|
|
|
return "Chest";
|
|
|
|
}
|
2016-03-23 18:09:13 +01:00
|
|
|
|
|
|
|
public List<CompoundTag> serializeInventory(ItemStack[] items) {
|
|
|
|
List<CompoundTag> tags = new ArrayList<>();
|
2015-09-13 06:04:31 +02:00
|
|
|
for (int i = 0; i < items.length; ++i) {
|
|
|
|
if (items[i] != null) {
|
2016-03-23 18:09:13 +01:00
|
|
|
Map<String, Tag> tagData = serializeItem(items[i]);
|
2018-12-20 00:18:57 +01:00
|
|
|
tagData.put("Slot", new ByteTag((byte) i));
|
2015-02-25 13:25:28 +01:00
|
|
|
tags.add(new CompoundTag(tagData));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return tags;
|
|
|
|
}
|
2016-01-20 12:40:04 +01:00
|
|
|
|
2016-03-23 18:09:13 +01:00
|
|
|
public Map<String, Tag> serializeItem(ItemStack item) {
|
|
|
|
Map<String, Tag> data = new HashMap<>();
|
2018-12-20 00:18:57 +01:00
|
|
|
data.put("id", new StringTag(item.getType().name()));
|
2019-02-04 16:18:50 +01:00
|
|
|
data.put("Damage", new ShortTag(item.getDurability()));
|
2018-12-20 00:18:57 +01:00
|
|
|
data.put("Count", new ByteTag((byte) item.getAmount()));
|
2015-09-13 06:04:31 +02:00
|
|
|
if (!item.getEnchantments().isEmpty()) {
|
2016-03-23 18:09:13 +01:00
|
|
|
List<CompoundTag> enchantmentList = new ArrayList<>();
|
|
|
|
for (Entry<Enchantment, Integer> entry : item.getEnchantments().entrySet()) {
|
|
|
|
Map<String, Tag> enchantment = new HashMap<>();
|
2018-12-20 00:18:57 +01:00
|
|
|
enchantment.put("id", new StringTag(entry.getKey().toString()));
|
|
|
|
enchantment.put("lvl", new ShortTag(entry.getValue().shortValue()));
|
2015-02-25 13:25:28 +01:00
|
|
|
enchantmentList.add(new CompoundTag(enchantment));
|
|
|
|
}
|
2016-03-23 18:09:13 +01:00
|
|
|
Map<String, Tag> auxData = new HashMap<>();
|
2018-12-20 00:18:57 +01:00
|
|
|
auxData.put("ench", new ListTag(CompoundTag.class, enchantmentList));
|
|
|
|
data.put("tag", new CompoundTag(auxData));
|
2015-02-25 13:25:28 +01:00
|
|
|
}
|
|
|
|
return data;
|
|
|
|
}
|
2018-12-24 18:56:13 +01:00
|
|
|
|
2015-02-25 13:25:28 +01:00
|
|
|
}
|