mirror of
https://github.com/IntellectualSites/PlotSquared.git
synced 2025-09-07 13:25:34 +02:00
feat: use NMS to populate tile entities
This commit is contained in:
@@ -74,7 +74,7 @@ tasks.named<ShadowJar>("shadowJar") {
|
|||||||
relocate("net.kyori.examination", "com.plotsquared.core.configuration.examination")
|
relocate("net.kyori.examination", "com.plotsquared.core.configuration.examination")
|
||||||
relocate("io.papermc.lib", "com.plotsquared.bukkit.paperlib")
|
relocate("io.papermc.lib", "com.plotsquared.bukkit.paperlib")
|
||||||
relocate("org.bstats", "com.plotsquared.metrics")
|
relocate("org.bstats", "com.plotsquared.metrics")
|
||||||
relocate("org.enginehub", "com.plotsquared.squirrelid")
|
relocate("org.enginehub.squirrelid", "com.plotsquared.squirrelid")
|
||||||
relocate("org.khelekore.prtree", "com.plotsquared.prtree")
|
relocate("org.khelekore.prtree", "com.plotsquared.prtree")
|
||||||
relocate("com.google.inject", "com.plotsquared.google")
|
relocate("com.google.inject", "com.plotsquared.google")
|
||||||
relocate("org.aopalliance", "com.plotsquared.core.aopalliance")
|
relocate("org.aopalliance", "com.plotsquared.core.aopalliance")
|
||||||
|
@@ -18,62 +18,39 @@
|
|||||||
*/
|
*/
|
||||||
package com.plotsquared.bukkit.schematic;
|
package com.plotsquared.bukkit.schematic;
|
||||||
|
|
||||||
import com.destroystokyo.paper.profile.PlayerProfile;
|
|
||||||
import com.destroystokyo.paper.profile.ProfileProperty;
|
|
||||||
import com.plotsquared.bukkit.util.BukkitUtil;
|
import com.plotsquared.bukkit.util.BukkitUtil;
|
||||||
import com.plotsquared.core.PlotSquared;
|
import com.plotsquared.core.util.ReflectionUtils;
|
||||||
import com.sk89q.jnbt.ByteTag;
|
|
||||||
import com.sk89q.jnbt.CompoundTag;
|
import com.sk89q.jnbt.CompoundTag;
|
||||||
import com.sk89q.jnbt.ListTag;
|
import com.sk89q.worldedit.bukkit.WorldEditPlugin;
|
||||||
import com.sk89q.jnbt.ShortTag;
|
import com.sk89q.worldedit.bukkit.adapter.BukkitImplAdapter;
|
||||||
import com.sk89q.jnbt.StringTag;
|
import com.sk89q.worldedit.extension.platform.NoCapablePlatformException;
|
||||||
import com.sk89q.jnbt.Tag;
|
|
||||||
import com.sk89q.worldedit.blocks.BaseItemStack;
|
|
||||||
import com.sk89q.worldedit.bukkit.BukkitAdapter;
|
|
||||||
import com.sk89q.worldedit.world.item.ItemType;
|
|
||||||
import io.papermc.lib.PaperLib;
|
|
||||||
import org.apache.logging.log4j.LogManager;
|
import org.apache.logging.log4j.LogManager;
|
||||||
import org.apache.logging.log4j.Logger;
|
import org.apache.logging.log4j.Logger;
|
||||||
import org.bukkit.Bukkit;
|
|
||||||
import org.bukkit.ChatColor;
|
|
||||||
import org.bukkit.DyeColor;
|
|
||||||
import org.bukkit.NamespacedKey;
|
|
||||||
import org.bukkit.Registry;
|
|
||||||
import org.bukkit.World;
|
import org.bukkit.World;
|
||||||
import org.bukkit.block.Banner;
|
|
||||||
import org.bukkit.block.Block;
|
import org.bukkit.block.Block;
|
||||||
import org.bukkit.block.Container;
|
import org.bukkit.block.BlockState;
|
||||||
import org.bukkit.block.Sign;
|
|
||||||
import org.bukkit.block.Skull;
|
|
||||||
import org.bukkit.block.banner.Pattern;
|
|
||||||
import org.bukkit.block.banner.PatternType;
|
|
||||||
import org.bukkit.block.sign.Side;
|
|
||||||
import org.bukkit.block.sign.SignSide;
|
|
||||||
import org.bukkit.enchantments.Enchantment;
|
|
||||||
import org.bukkit.inventory.Inventory;
|
|
||||||
import org.bukkit.inventory.ItemStack;
|
|
||||||
import org.checkerframework.checker.nullness.qual.NonNull;
|
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||||
import org.jetbrains.annotations.ApiStatus;
|
import org.jetbrains.annotations.ApiStatus;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.lang.invoke.MethodHandle;
|
||||||
import java.util.HashMap;
|
import java.lang.invoke.MethodHandles;
|
||||||
import java.util.List;
|
import java.lang.invoke.MethodType;
|
||||||
import java.util.Locale;
|
import java.lang.reflect.Method;
|
||||||
import java.util.Map;
|
|
||||||
import java.util.Map.Entry;
|
|
||||||
import java.util.Objects;
|
|
||||||
import java.util.UUID;
|
|
||||||
import java.util.function.BiConsumer;
|
|
||||||
|
|
||||||
// TODO: somehow unbreak this class so it doesn't fuck up the whole schematic population system due to MC updates
|
|
||||||
@ApiStatus.Internal
|
@ApiStatus.Internal
|
||||||
public class StateWrapper {
|
public class StateWrapper {
|
||||||
|
|
||||||
private static final Logger LOGGER = LogManager.getLogger("PlotSquared/" + StateWrapper.class.getSimpleName());
|
private static final Logger LOGGER = LogManager.getLogger("PlotSquared/" + StateWrapper.class.getSimpleName());
|
||||||
private static final boolean MODERN_SIGNS = PlotSquared.platform().serverVersion()[1] > 19;
|
|
||||||
private final Registry<PatternType> PATTERN_TYPE_REGISTRY = Objects.requireNonNull(Bukkit.getRegistry(PatternType.class));
|
|
||||||
|
|
||||||
private static boolean paperErrorTextureSent = false;
|
private static boolean FAILED_INITIALIZATION = false;
|
||||||
|
private static BukkitImplAdapter ADAPTER = null;
|
||||||
|
private static Class<?> LIN_TAG_CLASS = null;
|
||||||
|
private static Class<?> JNBT_TAG_CLASS = null;
|
||||||
|
private static Class<?> CRAFT_BLOCK_ENTITY_STATE_CLASS = null;
|
||||||
|
private static MethodHandle PAPERWEIGHT_ADAPTER_FROM_NATIVE = null;
|
||||||
|
private static MethodHandle CRAFT_BLOCK_ENTITY_STATE_LOAD_DATA = null;
|
||||||
|
private static MethodHandle CRAFT_BLOCK_ENTITY_STATE_UPDATE = null;
|
||||||
|
private static MethodHandle TO_LIN_TAG = null;
|
||||||
|
|
||||||
public CompoundTag tag;
|
public CompoundTag tag;
|
||||||
|
|
||||||
@@ -81,108 +58,6 @@ public class StateWrapper {
|
|||||||
this.tag = tag;
|
this.tag = tag;
|
||||||
}
|
}
|
||||||
|
|
||||||
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("\"}", "");
|
|
||||||
str = ChatColor.translateAlternateColorCodes('&', str);
|
|
||||||
return str;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Restore the TileEntity data to the given world at the given coordinates.
|
* Restore the TileEntity data to the given world at the given coordinates.
|
||||||
*
|
*
|
||||||
@@ -206,283 +81,125 @@ public class StateWrapper {
|
|||||||
* @param block Block to restore to
|
* @param block Block to restore to
|
||||||
* @return true if successful
|
* @return true if successful
|
||||||
*/
|
*/
|
||||||
@SuppressWarnings("deprecation") // #setLine is needed for Spigot compatibility
|
|
||||||
public boolean restoreTag(@NonNull Block block) {
|
public boolean restoreTag(@NonNull Block block) {
|
||||||
if (this.tag == null) {
|
if (this.tag == null || FAILED_INITIALIZATION) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
org.bukkit.block.BlockState state = block.getState();
|
if (ADAPTER == null) {
|
||||||
switch (getId()) {
|
try {
|
||||||
case "chest", "beacon", "brewingstand", "dispenser", "dropper", "furnace", "hopper", "shulkerbox" -> {
|
initializeNbtCompoundClassType();
|
||||||
if (!(state instanceof Container container)) {
|
ReflectionUtils.RefClass worldEditPluginRefClass = ReflectionUtils.getRefClass(WorldEditPlugin.class);
|
||||||
return false;
|
WorldEditPlugin worldEditPlugin = (WorldEditPlugin) worldEditPluginRefClass
|
||||||
}
|
.getMethod("getInstance")
|
||||||
List<Tag> itemsTag = this.tag.getListTag("Items").getValue();
|
.of(null)
|
||||||
Inventory inv = container.getSnapshotInventory();
|
.call();
|
||||||
for (Tag itemTag : itemsTag) {
|
ADAPTER = (BukkitImplAdapter) worldEditPluginRefClass
|
||||||
CompoundTag itemComp = (CompoundTag) itemTag;
|
.getMethod("getBukkitImplAdapter")
|
||||||
ItemType type = ItemType.REGISTRY.get(itemComp.getString("id").toLowerCase());
|
.of(worldEditPlugin)
|
||||||
if (type == null) {
|
.call();
|
||||||
continue;
|
PAPERWEIGHT_ADAPTER_FROM_NATIVE = findPaperweightAdapterFromNativeMethodHandle(ADAPTER.getClass());
|
||||||
}
|
TO_LIN_TAG = findToLinTagMethodHandle(LIN_TAG_CLASS);
|
||||||
int count = itemComp.containsKey("count") ? itemComp.getInt("count") : itemComp.getByte("Count");
|
} catch (NoSuchMethodException | ClassNotFoundException | IllegalAccessException | NoCapablePlatformException e) {
|
||||||
int slot = itemComp.getByte("Slot");
|
LOGGER.error(
|
||||||
CompoundTag tag = (CompoundTag) itemComp.getValue().get(itemComp.containsKey("tag") ? "tag" : "components");
|
"Failed to access required WorldEdit methods, which are required to populate block data from " +
|
||||||
BaseItemStack baseItemStack = new BaseItemStack(type, tag, count);
|
"schematics. Pasted blocks will not have their respective data", e
|
||||||
ItemStack itemStack = BukkitAdapter.adapt(baseItemStack);
|
);
|
||||||
inv.setItem(slot, itemStack);
|
FAILED_INITIALIZATION = true;
|
||||||
}
|
|
||||||
container.update(true, false);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
case "sign" -> {
|
|
||||||
if (state instanceof Sign sign && this.restoreSign(sign)) {
|
|
||||||
state.update(true, false);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
case "skull" -> {
|
try {
|
||||||
if (state instanceof Skull skull && this.restoreSkull(skull)) {
|
CRAFT_BLOCK_ENTITY_STATE_CLASS = findCraftBlockEntityStateClass();
|
||||||
skull.update(true, false);
|
CRAFT_BLOCK_ENTITY_STATE_LOAD_DATA = findCraftBlockEntityStateLoadDataMethodHandle(CRAFT_BLOCK_ENTITY_STATE_CLASS);
|
||||||
return true;
|
CRAFT_BLOCK_ENTITY_STATE_UPDATE = findCraftBlockEntityStateUpdateMethodHandle(CRAFT_BLOCK_ENTITY_STATE_CLASS);
|
||||||
}
|
} catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException e) {
|
||||||
|
LOGGER.error(
|
||||||
|
"Failed to initialize required method accessors for block state population.",
|
||||||
|
e
|
||||||
|
);
|
||||||
|
FAILED_INITIALIZATION = true;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
case "banner" -> {
|
}
|
||||||
if (state instanceof Banner banner) {
|
try {
|
||||||
List<CompoundTag> patterns;
|
final BlockState blockState = block.getState();
|
||||||
// "old" format
|
if (!CRAFT_BLOCK_ENTITY_STATE_CLASS.isAssignableFrom(blockState.getClass())) {
|
||||||
if ((patterns = this.tag.getList("Patterns", CompoundTag.class)) != null && !patterns.isEmpty()) {
|
|
||||||
banner.setPatterns(patterns.stream().map(compoundTag -> {
|
|
||||||
DyeColor color = DyeColor.getByWoolData((byte) compoundTag.getInt("Color"));
|
|
||||||
final PatternType patternType = PATTERN_TYPE_REGISTRY.get(Objects.requireNonNull(
|
|
||||||
NamespacedKey.fromString(compoundTag.getString("Pattern"))
|
|
||||||
));
|
|
||||||
if (color == null || patternType == null) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
return new Pattern(color, patternType);
|
|
||||||
}).filter(Objects::nonNull).toList());
|
|
||||||
banner.update(true, false);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
// "new" format - since 1.21.3-ish
|
|
||||||
if ((patterns = this.tag.getList("patterns", CompoundTag.class)) != null && !patterns.isEmpty()) {
|
|
||||||
for (final CompoundTag patternTag : patterns) {
|
|
||||||
final String color = patternTag.getString("color");
|
|
||||||
if (color.isEmpty()) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
final Tag pattern = patternTag.getValue().get("pattern");
|
|
||||||
if (pattern instanceof StringTag patternString && !patternString.getValue().isEmpty()) {
|
|
||||||
final PatternType patternType = PATTERN_TYPE_REGISTRY.get(Objects.requireNonNull(
|
|
||||||
NamespacedKey.fromString(patternString.getValue())
|
|
||||||
));
|
|
||||||
if (patternType == null) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
banner.addPattern(new Pattern(
|
|
||||||
DyeColor.legacyValueOf(color.toUpperCase(Locale.ROOT)),
|
|
||||||
patternType
|
|
||||||
));
|
|
||||||
}
|
|
||||||
// not supporting banner pattern definitions (no API available)
|
|
||||||
}
|
|
||||||
banner.update(true, false);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
// get native tag
|
||||||
|
Object nativeTag = PAPERWEIGHT_ADAPTER_FROM_NATIVE.invoke(
|
||||||
|
ADAPTER,
|
||||||
|
LIN_TAG_CLASS == null ? this.tag : TO_LIN_TAG.invoke(this.tag)
|
||||||
|
);
|
||||||
|
CRAFT_BLOCK_ENTITY_STATE_LOAD_DATA.invoke(blockState, nativeTag);
|
||||||
|
CRAFT_BLOCK_ENTITY_STATE_UPDATE.invoke(blockState, true, false);
|
||||||
|
} catch (Throwable e) {
|
||||||
|
LOGGER.error("Failed to update tile entity", e);
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getId() {
|
private static void initializeNbtCompoundClassType() throws ClassNotFoundException {
|
||||||
String tileid = this.tag.getString("id").toLowerCase();
|
try {
|
||||||
if (tileid.startsWith("minecraft:")) {
|
LIN_TAG_CLASS = Class.forName("org.enginehub.linbus.tree.LinTag");
|
||||||
tileid = tileid.replace("minecraft:", "");
|
} catch (ClassNotFoundException e) {
|
||||||
|
JNBT_TAG_CLASS = Class.forName("com.sk89q.jnbt.Tag");
|
||||||
}
|
}
|
||||||
return tileid;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<CompoundTag> serializeInventory(ItemStack[] items) {
|
private static MethodHandle findToLinTagMethodHandle(Class<?> LIN_TAG_CLASS)
|
||||||
List<CompoundTag> tags = new ArrayList<>();
|
throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException {
|
||||||
for (int i = 0; i < items.length; ++i) {
|
if (LIN_TAG_CLASS == null) {
|
||||||
if (items[i] != null) {
|
return null;
|
||||||
Map<String, Tag> tagData = serializeItem(items[i]);
|
|
||||||
tagData.put("Slot", new ByteTag((byte) i));
|
|
||||||
tags.add(new CompoundTag(tagData));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return tags;
|
return MethodHandles.lookup().findVirtual(
|
||||||
|
Class.forName("org.enginehub.linbus.tree.ToLinTag"),
|
||||||
|
"toLinTag",
|
||||||
|
MethodType.methodType(LIN_TAG_CLASS)
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Map<String, Tag> serializeItem(ItemStack item) {
|
private static MethodHandle findPaperweightAdapterFromNativeMethodHandle(Class<?> adapterClass)
|
||||||
Map<String, Tag> data = new HashMap<>();
|
throws IllegalAccessException, NoSuchMethodException {
|
||||||
data.put("id", new StringTag(item.getType().name()));
|
final MethodHandles.Lookup lookup = MethodHandles.privateLookupIn(adapterClass, MethodHandles.lookup());
|
||||||
data.put("Damage", new ShortTag(item.getDurability()));
|
if (JNBT_TAG_CLASS != null) {
|
||||||
data.put("Count", new ByteTag((byte) item.getAmount()));
|
// usage of JNBT = identical method signatures for WE and FAWE
|
||||||
if (!item.getEnchantments().isEmpty()) {
|
return lookup.findVirtual(adapterClass, "fromNative", MethodType.methodType(Object.class, JNBT_TAG_CLASS));
|
||||||
List<CompoundTag> enchantmentList = new ArrayList<>();
|
}
|
||||||
for (Entry<Enchantment, Integer> entry : item.getEnchantments().entrySet()) {
|
try {
|
||||||
Map<String, Tag> enchantment = new HashMap<>();
|
// FAWE
|
||||||
enchantment.put("id", new StringTag(entry.getKey().toString()));
|
return lookup.findVirtual(adapterClass, "fromNativeLin", MethodType.methodType(Object.class, LIN_TAG_CLASS));
|
||||||
enchantment.put("lvl", new ShortTag(entry.getValue().shortValue()));
|
} catch (NoSuchMethodException e) {
|
||||||
enchantmentList.add(new CompoundTag(enchantment));
|
// WE
|
||||||
}
|
return lookup.findVirtual(adapterClass, "fromNative", MethodType.methodType(Object.class, LIN_TAG_CLASS));
|
||||||
Map<String, Tag> auxData = new HashMap<>();
|
|
||||||
auxData.put("ench", new ListTag(CompoundTag.class, enchantmentList));
|
|
||||||
data.put("tag", new CompoundTag(auxData));
|
|
||||||
}
|
}
|
||||||
return data;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean restoreSkull(Skull skull) {
|
private static MethodHandle findCraftBlockEntityStateLoadDataMethodHandle(Class<?> craftBlockEntityStateClass)
|
||||||
boolean updated = false;
|
throws NoSuchMethodException, IllegalAccessException, ClassNotFoundException {
|
||||||
// can't support custom_name - Spigot does not provide any API for that
|
final Class<?> compoundTagClass = Class.forName("net.minecraft.nbt.CompoundTag");
|
||||||
if (this.tag.containsKey("note_block_sound")) {
|
for (final Method method : craftBlockEntityStateClass.getMethods()) {
|
||||||
skull.setNoteBlockSound(NamespacedKey.fromString(this.tag.getString("note_block_sound")));
|
if (method.getReturnType().equals(Void.TYPE) && method.getParameterCount() == 1
|
||||||
updated = true;
|
&& method.getParameterTypes()[0] == compoundTagClass) {
|
||||||
}
|
return MethodHandles.lookup().unreflect(method);
|
||||||
// modern format - MC 1.21.3-ish
|
|
||||||
if (this.tag.containsKey("profile")) {
|
|
||||||
final Tag profile = this.tag.getValue().get("profile");
|
|
||||||
if (profile instanceof StringTag stringTag) {
|
|
||||||
final String name = stringTag.getValue();
|
|
||||||
if (name != null && !name.isEmpty()) {
|
|
||||||
skull.setOwningPlayer(Bukkit.getOfflinePlayer(name));
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
return updated;
|
|
||||||
}
|
|
||||||
if (profile instanceof CompoundTag compoundTag) {
|
|
||||||
final List<Tag> properties = compoundTag.getList("properties");
|
|
||||||
if (properties != null && !properties.isEmpty()) {
|
|
||||||
if (!PaperLib.isPaper()) {
|
|
||||||
if (!paperErrorTextureSent) {
|
|
||||||
paperErrorTextureSent = true;
|
|
||||||
LOGGER.error("Failed to populate schematic skull data - this is a Spigot limitation.");
|
|
||||||
}
|
|
||||||
return updated;
|
|
||||||
}
|
|
||||||
for (final Tag propertyTag : properties) {
|
|
||||||
if (!(propertyTag instanceof CompoundTag property)) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
if (!property.getString("name").equals("textures")) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
final String value = property.getString("value");
|
|
||||||
final String signature = property.containsKey("signature") ? property.getString("signature") : null;
|
|
||||||
final PlayerProfile playerProfile = Bukkit.createProfile(UUID.randomUUID());
|
|
||||||
playerProfile.setProperty(new ProfileProperty("textures", value, signature));
|
|
||||||
skull.setPlayerProfile(playerProfile);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
return updated;
|
|
||||||
}
|
|
||||||
final int[] id = compoundTag.getIntArray("id");
|
|
||||||
if (id != null && id.length == 4) {
|
|
||||||
skull.setOwningPlayer(Bukkit.getOfflinePlayer(new UUID(
|
|
||||||
(long) id[0] << 32 | (id[1] & 0xFFFFFFFFL),
|
|
||||||
(long) id[2] << 32 | (id[3] & 0xFFFFFFFFL)
|
|
||||||
)));
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
final String name = compoundTag.getString("name");
|
|
||||||
if (name != null && !name.isEmpty()) {
|
|
||||||
skull.setOwningPlayer(Bukkit.getOfflinePlayer(name));
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
throw new NoSuchMethodException("Couldn't find method for #loadData(CompoundTag) in " + compoundTagClass.getName());
|
||||||
// "Old" MC format (idk when it got updated)
|
|
||||||
if (this.tag.getValue().get("SkullOwner") instanceof CompoundTag skullOwner) {
|
|
||||||
if (skullOwner.getValue().get("Name") instanceof StringTag ownerName && !ownerName.getValue().isEmpty()) {
|
|
||||||
skull.setOwningPlayer(Bukkit.getOfflinePlayer(ownerName.getValue()));
|
|
||||||
skull.update(true);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
if (skullOwner.getValue().get("Properties") instanceof CompoundTag properties) {
|
|
||||||
if (!paperErrorTextureSent) {
|
|
||||||
paperErrorTextureSent = true;
|
|
||||||
LOGGER.error("Failed to populate schematic skull data - this is a Spigot limitation.");
|
|
||||||
return updated;
|
|
||||||
}
|
|
||||||
final List<CompoundTag> textures = properties.getList("textures", CompoundTag.class);
|
|
||||||
if (textures.isEmpty()) {
|
|
||||||
return updated;
|
|
||||||
}
|
|
||||||
final String value = textures.get(0).getString("Value");
|
|
||||||
if (!value.isEmpty()) {
|
|
||||||
final PlayerProfile profile = Bukkit.createProfile(UUID.randomUUID());
|
|
||||||
profile.setProperty(new ProfileProperty("textures", value));
|
|
||||||
skull.setPlayerProfile(profile);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return updated;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean restoreSign(Sign sign) {
|
private static MethodHandle findCraftBlockEntityStateUpdateMethodHandle(Class<?> craftBlockEntityStateClass)
|
||||||
// "old" format (pre 1.20)
|
throws NoSuchMethodException, IllegalAccessException, ClassNotFoundException {
|
||||||
if (this.tag.containsKey("Text1") || this.tag.containsKey("Text2")
|
final Class<?> compoundTagClass = Class.forName("net.minecraft.nbt.CompoundTag");
|
||||||
|| this.tag.containsKey("Text3") || this.tag.containsKey("Text4")) {
|
for (final Method method : craftBlockEntityStateClass.getMethods()) {
|
||||||
if (!MODERN_SIGNS) {
|
if (method.getReturnType().equals(Boolean.TYPE) && method.getParameterCount() == 2
|
||||||
sign.setLine(0, jsonToColourCode(tag.getString("Text1")));
|
&& method.getParameterTypes()[0] == Boolean.TYPE && method.getParameterTypes()[1] == Boolean.TYPE) {
|
||||||
sign.setLine(1, jsonToColourCode(tag.getString("Text2")));
|
return MethodHandles.lookup().unreflect(method);
|
||||||
sign.setLine(2, jsonToColourCode(tag.getString("Text3")));
|
|
||||||
sign.setLine(3, jsonToColourCode(tag.getString("Text4")));
|
|
||||||
sign.setGlowingText(tag.getByte("GlowingText") == 1);
|
|
||||||
if (tag.getValue().get("Color") instanceof StringTag colorTag && !colorTag.getValue().isEmpty()) {
|
|
||||||
sign.setColor(DyeColor.legacyValueOf(colorTag.getValue()));
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
SignSide front = sign.getSide(Side.FRONT);
|
|
||||||
front.setLine(0, jsonToColourCode(tag.getString("Text1")));
|
|
||||||
front.setLine(1, jsonToColourCode(tag.getString("Text2")));
|
|
||||||
front.setLine(2, jsonToColourCode(tag.getString("Text3")));
|
|
||||||
front.setLine(3, jsonToColourCode(tag.getString("Text4")));
|
|
||||||
front.setGlowingText(tag.getByte("GlowingText") == 1);
|
|
||||||
if (tag.getValue().get("Color") instanceof StringTag colorTag && !colorTag.getValue().isEmpty()) {
|
|
||||||
front.setColor(DyeColor.legacyValueOf(colorTag.getValue()));
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
throw new NoSuchMethodException("Couldn't find method for #update(boolean, boolean) in " + compoundTagClass.getName());
|
||||||
|
}
|
||||||
|
|
||||||
// "modern" format
|
private static Class<?> findCraftBlockEntityStateClass() throws ClassNotFoundException {
|
||||||
if (this.tag.containsKey("front_text") || this.tag.containsKey("back_text") || this.tag.containsKey("is_waxed")) {
|
return Class.forName("org.bukkit.craftbukkit.block.CraftBlockEntityState");
|
||||||
// the new format on older servers shouldn't be possible, I hope?
|
|
||||||
sign.setWaxed(this.tag.getByte("is_waxed") == 1);
|
|
||||||
BiConsumer<SignSide, CompoundTag> sideSetter = (signSide, compoundTag) -> {
|
|
||||||
signSide.setGlowingText(compoundTag.getByte("has_glowing_text") == 1);
|
|
||||||
if (tag.getValue().get("color") instanceof StringTag colorTag && !colorTag.getValue().isEmpty()) {
|
|
||||||
signSide.setColor(DyeColor.legacyValueOf(colorTag.getValue()));
|
|
||||||
}
|
|
||||||
final List<Tag> lines = compoundTag.getList("messages");
|
|
||||||
for (int i = 0; i < Math.min(lines.size(), 4); i++) {
|
|
||||||
final Tag line = lines.get(i);
|
|
||||||
if (line instanceof StringTag stringLine) {
|
|
||||||
signSide.setLine(i, jsonToColourCode(stringLine.getValue()));
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
// TODO: how tf support list of components + components - utilize paper + adventure?
|
|
||||||
}
|
|
||||||
};
|
|
||||||
if (this.tag.getValue().get("front_text") instanceof CompoundTag frontText) {
|
|
||||||
sideSetter.accept(sign.getSide(Side.FRONT), frontText);
|
|
||||||
}
|
|
||||||
if (this.tag.getValue().get("back_text") instanceof CompoundTag backText) {
|
|
||||||
sideSetter.accept(sign.getSide(Side.BACK), backText);
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user