From f38859237a73ceddec102730cada3266741edc9a Mon Sep 17 00:00:00 2001 From: dordsor21 Date: Wed, 29 Dec 2021 16:00:49 +0000 Subject: [PATCH] Move to BinaryTags where appropriate in adapters --- .../adapter/ext/fawe/PaperweightAdapter.java | 36 +++++++++------- .../v1_17_R1_2/PaperweightFaweAdapter.java | 27 ++++++------ .../PaperweightPlatformAdapter.java | 6 +-- .../v1_17_R1_2/regen/PaperweightRegen.java | 32 +++++++------- .../ext/fawe/v1_18_R1/PaperweightAdapter.java | 43 ++++++++++--------- .../fawe/v1_18_R1/PaperweightFaweAdapter.java | 25 +++++------ .../v1_18_R1/PaperweightPlatformAdapter.java | 9 ++-- .../fawe/v1_18_R1/regen/PaperweightRegen.java | 20 ++++----- .../core/entity/LazyBaseEntity.java | 15 ++++--- 9 files changed, 111 insertions(+), 102 deletions(-) diff --git a/worldedit-bukkit/adapters/adapter-1_17_1/src/main/java/com/sk89q/worldedit/bukkit/adapter/ext/fawe/PaperweightAdapter.java b/worldedit-bukkit/adapters/adapter-1_17_1/src/main/java/com/sk89q/worldedit/bukkit/adapter/ext/fawe/PaperweightAdapter.java index ab2530fed..e3129e07b 100644 --- a/worldedit-bukkit/adapters/adapter-1_17_1/src/main/java/com/sk89q/worldedit/bukkit/adapter/ext/fawe/PaperweightAdapter.java +++ b/worldedit-bukkit/adapters/adapter-1_17_1/src/main/java/com/sk89q/worldedit/bukkit/adapter/ext/fawe/PaperweightAdapter.java @@ -56,6 +56,7 @@ import com.sk89q.worldedit.registry.state.IntegerProperty; import com.sk89q.worldedit.registry.state.Property; import com.sk89q.worldedit.util.Direction; import com.sk89q.worldedit.util.SideEffect; +import com.sk89q.worldedit.util.concurrency.LazyReference; import com.sk89q.worldedit.util.formatting.text.Component; import com.sk89q.worldedit.util.formatting.text.TranslatableComponent; import com.sk89q.worldedit.util.io.file.SafeFiles; @@ -171,11 +172,11 @@ import static com.google.common.base.Preconditions.checkState; public final class PaperweightAdapter implements BukkitImplAdapter { - private final Logger logger = Logger.getLogger(getClass().getCanonicalName()); + private final Logger LOGGER = Logger.getLogger(getClass().getCanonicalName()); - private final Field serverWorldsField; - private final Method getChunkFutureMethod; - private final Field chunkProviderExecutorField; + private final Field worldsField; + private final Method getChunkFutureMainThreadMethod; + private final Field mainThreadProcessorField; private final Watchdog watchdog; // ------------------------------------------------------------------------ @@ -191,18 +192,18 @@ public final class PaperweightAdapter implements BukkitImplAdapter (CompoundBinaryTag) toNativeBinary(tag)) + ); } @Nullable @@ -667,7 +671,7 @@ public final class PaperweightAdapter implements BukkitImplAdapter map = (Map) serverWorldsField.get(Bukkit.getServer()); + Map map = (Map) worldsField.get(Bukkit.getServer()); map.remove("worldeditregentempworld"); } catch (IllegalAccessException ignored) { } @@ -740,7 +744,7 @@ public final class PaperweightAdapter implements BukkitImplAdapter> chunkLoadings = submitChunkLoadTasks(region, serverWorld); BlockableEventLoop executor; try { - executor = (BlockableEventLoop) chunkProviderExecutorField.get(serverWorld.getChunkSource()); + executor = (BlockableEventLoop) mainThreadProcessorField.get(serverWorld.getChunkSource()); } catch (IllegalAccessException e) { throw new IllegalStateException("Couldn't get executor for chunk loading.", e); } @@ -801,7 +805,7 @@ public final class PaperweightAdapter implements BukkitImplAdapter>) - getChunkFutureMethod.invoke(chunkManager, chunk.getX(), chunk.getZ(), ChunkStatus.FEATURES, true)) + getChunkFutureMainThreadMethod.invoke(chunkManager, chunk.getX(), chunk.getZ(), ChunkStatus.FEATURES, true)) .thenApply(either -> either.left().orElse(null)) ); } catch (IllegalAccessException | InvocationTargetException e) { @@ -893,7 +897,7 @@ public final class PaperweightAdapter implements BukkitImplAdapter saveTag = () -> { + Supplier saveTag = () -> { final net.minecraft.nbt.CompoundTag minecraftTag = new net.minecraft.nbt.CompoundTag(); readEntityIntoTag(mcEntity, minecraftTag); //add Id for AbstractChangeSet to work - final CompoundTag tag = (CompoundTag) toNative(minecraftTag); - final Map tags = new HashMap<>(tag.getValue()); - tags.put("Id", new StringTag(id)); - return new CompoundTag(tags); + final CompoundBinaryTag tag = (CompoundBinaryTag) toNative(minecraftTag); + final Map tags = new HashMap<>(); + tag.keySet().forEach(key -> tags.put(key, tag.get(key))); + tags.put("Id", StringBinaryTag.of(id)); + return CompoundBinaryTag.from(tags); }; return new LazyBaseEntity(type, saveTag); } else { @@ -541,7 +542,7 @@ public final class PaperweightFaweAdapter extends CachedBukkitAdapter implements Registry.ITEM.get(ResourceLocation.tryParse(baseItemStack.getType().getId())), baseItemStack.getAmount() ); - stack.setTag(((net.minecraft.nbt.CompoundTag) fromNative(baseItemStack.getNbtData()))); + stack.setTag(((net.minecraft.nbt.CompoundTag) fromNativeBinary(baseItemStack.getNbt()))); return CraftItemStack.asCraftMirror(stack); } @@ -603,7 +604,7 @@ public final class PaperweightFaweAdapter extends CachedBukkitAdapter implements public BaseItemStack adapt(org.bukkit.inventory.ItemStack itemStack) { final ItemStack nmsStack = CraftItemStack.asNMSCopy(itemStack); final BaseItemStack weStack = new BaseItemStack(BukkitAdapter.asItemType(itemStack.getType()), itemStack.getAmount()); - weStack.setNbtData(((CompoundTag) toNative(nmsStack.getTag()))); + weStack.setNbt(((CompoundBinaryTag) toNativeBinary(nmsStack.getTag()))); return weStack; } diff --git a/worldedit-bukkit/adapters/adapter-1_17_1/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v1_17_R1_2/PaperweightPlatformAdapter.java b/worldedit-bukkit/adapters/adapter-1_17_1/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v1_17_R1_2/PaperweightPlatformAdapter.java index 1f7706b57..b8b791e88 100644 --- a/worldedit-bukkit/adapters/adapter-1_17_1/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v1_17_R1_2/PaperweightPlatformAdapter.java +++ b/worldedit-bukkit/adapters/adapter-1_17_1/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v1_17_R1_2/PaperweightPlatformAdapter.java @@ -86,7 +86,7 @@ public final class PaperweightPlatformAdapter extends NMSAdapter { private static final long fieldLockOffset; private static final Field fieldGameEventDispatcherSections; - private static final MethodHandle methodremoveTickingBlockEntity; + private static final MethodHandle methodremoveBlockEntityTicker; private static final Field fieldRemove; @@ -133,7 +133,7 @@ public final class PaperweightPlatformAdapter extends NMSAdapter { ), BlockPos.class ); removeBlockEntityTicker.setAccessible(true); - methodremoveTickingBlockEntity = MethodHandles.lookup().unreflect(removeBlockEntityTicker); + methodremoveBlockEntityTicker = MethodHandles.lookup().unreflect(removeBlockEntityTicker); fieldRemove = BlockEntity.class.getDeclaredField(Refraction.pickName("remove", "p")); fieldRemove.setAccessible(true); @@ -440,7 +440,7 @@ public final class PaperweightPlatformAdapter extends NMSAdapter { fieldRemove.set(beacon, true); } } - methodremoveTickingBlockEntity.invoke(levelChunk, beacon.getBlockPos()); + methodremoveBlockEntityTicker.invoke(levelChunk, beacon.getBlockPos()); } catch (Throwable throwable) { throwable.printStackTrace(); } diff --git a/worldedit-bukkit/adapters/adapter-1_17_1/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v1_17_R1_2/regen/PaperweightRegen.java b/worldedit-bukkit/adapters/adapter-1_17_1/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v1_17_R1_2/regen/PaperweightRegen.java index 6807c7a05..c7c1e708f 100644 --- a/worldedit-bukkit/adapters/adapter-1_17_1/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v1_17_R1_2/regen/PaperweightRegen.java +++ b/worldedit-bukkit/adapters/adapter-1_17_1/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v1_17_R1_2/regen/PaperweightRegen.java @@ -90,13 +90,13 @@ public class PaperweightRegen extends Regenerator chunkStati = new LinkedHashMap<>(); @@ -125,8 +125,8 @@ public class PaperweightRegen extends Regenerator { try { - Map map = (Map) serverWorldsField.get(Bukkit.getServer()); + Map map = (Map) worldsField.get(Bukkit.getServer()); map.remove("worldeditregentempworld"); } catch (IllegalAccessException e) { throw new RuntimeException(e); diff --git a/worldedit-bukkit/adapters/adapter-1_18/src/main/java/com/sk89q/worldedit/bukkit/adapter/ext/fawe/v1_18_R1/PaperweightAdapter.java b/worldedit-bukkit/adapters/adapter-1_18/src/main/java/com/sk89q/worldedit/bukkit/adapter/ext/fawe/v1_18_R1/PaperweightAdapter.java index c30dfe662..b0d8eefb4 100644 --- a/worldedit-bukkit/adapters/adapter-1_18/src/main/java/com/sk89q/worldedit/bukkit/adapter/ext/fawe/v1_18_R1/PaperweightAdapter.java +++ b/worldedit-bukkit/adapters/adapter-1_18/src/main/java/com/sk89q/worldedit/bukkit/adapter/ext/fawe/v1_18_R1/PaperweightAdapter.java @@ -30,7 +30,6 @@ import com.mojang.datafixers.util.Either; import com.mojang.serialization.Codec; import com.mojang.serialization.Dynamic; import com.mojang.serialization.Lifecycle; -import com.sk89q.jnbt.CompoundTag; import com.sk89q.worldedit.WorldEditException; import com.sk89q.worldedit.blocks.BaseItem; import com.sk89q.worldedit.blocks.BaseItemStack; @@ -55,6 +54,7 @@ import com.sk89q.worldedit.registry.state.IntegerProperty; import com.sk89q.worldedit.registry.state.Property; import com.sk89q.worldedit.util.Direction; import com.sk89q.worldedit.util.SideEffect; +import com.sk89q.worldedit.util.concurrency.LazyReference; import com.sk89q.worldedit.util.formatting.text.Component; import com.sk89q.worldedit.util.formatting.text.TranslatableComponent; import com.sk89q.worldedit.util.io.file.SafeFiles; @@ -172,11 +172,11 @@ import static com.google.common.base.Preconditions.checkState; public final class PaperweightAdapter implements BukkitImplAdapter { - private final Logger logger = Logger.getLogger(getClass().getCanonicalName()); + private final Logger LOGGER = Logger.getLogger(getClass().getCanonicalName()); - private final Field serverWorldsField; - private final Method getChunkFutureMethod; - private final Field chunkProviderExecutorField; + private final Field worldsField; + private final Method getChunkFutureMainThreadMethod; + private final Field mainThreadProcessorField; private final Watchdog watchdog; // ------------------------------------------------------------------------ @@ -192,19 +192,19 @@ public final class PaperweightAdapter implements BukkitImplAdapter (CompoundBinaryTag) toNativeBinary(tag)) + ); } @Nullable @@ -442,9 +445,9 @@ public final class PaperweightAdapter implements BukkitImplAdapter map = (Map) serverWorldsField.get(Bukkit.getServer()); + Map map = (Map) worldsField.get(Bukkit.getServer()); map.remove("worldeditregentempworld"); } catch (IllegalAccessException ignored) { } @@ -738,7 +741,7 @@ public final class PaperweightAdapter implements BukkitImplAdapter> chunkLoadings = submitChunkLoadTasks(region, serverWorld); BlockableEventLoop executor; try { - executor = (BlockableEventLoop) chunkProviderExecutorField.get(serverWorld.getChunkSource()); + executor = (BlockableEventLoop) mainThreadProcessorField.get(serverWorld.getChunkSource()); } catch (IllegalAccessException e) { throw new IllegalStateException("Couldn't get executor for chunk loading.", e); } @@ -797,7 +800,7 @@ public final class PaperweightAdapter implements BukkitImplAdapter>) - getChunkFutureMethod.invoke(chunkManager, chunk.getX(), chunk.getZ(), ChunkStatus.FEATURES, true)) + getChunkFutureMainThreadMethod.invoke(chunkManager, chunk.getX(), chunk.getZ(), ChunkStatus.FEATURES, true)) .thenApply(either -> either.left().orElse(null)) ); } catch (IllegalAccessException | InvocationTargetException e) { @@ -889,7 +892,7 @@ public final class PaperweightAdapter implements BukkitImplAdapter saveTag = () -> { + Supplier saveTag = () -> { final net.minecraft.nbt.CompoundTag minecraftTag = new net.minecraft.nbt.CompoundTag(); readEntityIntoTag(mcEntity, minecraftTag); //add Id for AbstractChangeSet to work - final CompoundTag tag = (CompoundTag) toNative(minecraftTag); - final Map tags = new HashMap<>(tag.getValue()); - tags.put("Id", new StringTag(id)); - return new CompoundTag(tags); + final CompoundBinaryTag tag = (CompoundBinaryTag) toNativeBinary(minecraftTag); + final Map tags = new HashMap<>(); + tag.keySet().forEach(key -> tags.put(key, tag.get(key))); + tags.put("Id", StringBinaryTag.of(id)); + return CompoundBinaryTag.from(tags); }; return new LazyBaseEntity(type, saveTag); } else { @@ -598,7 +599,7 @@ public final class PaperweightFaweAdapter extends CachedBukkitAdapter implements public BaseItemStack adapt(org.bukkit.inventory.ItemStack itemStack) { final ItemStack nmsStack = CraftItemStack.asNMSCopy(itemStack); final BaseItemStack weStack = new BaseItemStack(BukkitAdapter.asItemType(itemStack.getType()), itemStack.getAmount()); - weStack.setNbtData(((CompoundTag) toNative(nmsStack.getTag()))); + weStack.setNbt(((CompoundBinaryTag) toNativeBinary(nmsStack.getTag()))); return weStack; } diff --git a/worldedit-bukkit/adapters/adapter-1_18/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v1_18_R1/PaperweightPlatformAdapter.java b/worldedit-bukkit/adapters/adapter-1_18/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v1_18_R1/PaperweightPlatformAdapter.java index 78ad00245..27e22a05c 100644 --- a/worldedit-bukkit/adapters/adapter-1_18/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v1_18_R1/PaperweightPlatformAdapter.java +++ b/worldedit-bukkit/adapters/adapter-1_18/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v1_18_R1/PaperweightPlatformAdapter.java @@ -85,7 +85,7 @@ public final class PaperweightPlatformAdapter extends NMSAdapter { public static final Field fieldPalette; - public static final Field fieldTickingFluidContent; + public static final Field fieldTickingFluidCount; public static final Field fieldTickingBlockCount; public static final Field fieldNonEmptyBlockCount; @@ -114,14 +114,13 @@ public final class PaperweightPlatformAdapter extends NMSAdapter { dataConstructor = dataClazz.getDeclaredConstructors()[0]; dataConstructor.setAccessible(true); - //TODO FIXME 1.18 fieldStorage = dataClazz.getDeclaredField(Refraction.pickName("storage", "b")); fieldStorage.setAccessible(true); fieldPalette = dataClazz.getDeclaredField(Refraction.pickName("palette", "c")); fieldPalette.setAccessible(true); - fieldTickingFluidContent = LevelChunkSection.class.getDeclaredField(Refraction.pickName("tickingFluidCount", "h")); - fieldTickingFluidContent.setAccessible(true); + fieldTickingFluidCount = LevelChunkSection.class.getDeclaredField(Refraction.pickName("tickingFluidCount", "h")); + fieldTickingFluidCount.setAccessible(true); fieldTickingBlockCount = LevelChunkSection.class.getDeclaredField(Refraction.pickName("tickingBlockCount", "g")); fieldTickingBlockCount.setAccessible(true); fieldNonEmptyBlockCount = LevelChunkSection.class.getDeclaredField(Refraction.pickName("nonEmptyBlockCount", "f")); @@ -544,7 +543,7 @@ public final class PaperweightPlatformAdapter extends NMSAdapter { public static void setCount(final int tickingBlockCount, final int nonEmptyBlockCount, final LevelChunkSection section) throws IllegalAccessException { - fieldTickingFluidContent.setShort(section, (short) 0); // TODO FIXME + fieldTickingFluidCount.setShort(section, (short) 0); // TODO FIXME fieldTickingBlockCount.setShort(section, (short) tickingBlockCount); fieldNonEmptyBlockCount.setShort(section, (short) nonEmptyBlockCount); } diff --git a/worldedit-bukkit/adapters/adapter-1_18/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v1_18_R1/regen/PaperweightRegen.java b/worldedit-bukkit/adapters/adapter-1_18/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v1_18_R1/regen/PaperweightRegen.java index c2ce83d38..49285b665 100644 --- a/worldedit-bukkit/adapters/adapter-1_18/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v1_18_R1/regen/PaperweightRegen.java +++ b/worldedit-bukkit/adapters/adapter-1_18/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v1_18_R1/regen/PaperweightRegen.java @@ -74,12 +74,12 @@ public class PaperweightRegen extends Regenerator chunkStati = new LinkedHashMap<>(); @@ -123,7 +123,7 @@ public class PaperweightRegen extends Regenerator saveTag; + private Supplier saveTag; - public LazyBaseEntity(EntityType type, Supplier saveTag) { + public LazyBaseEntity(EntityType type, Supplier saveTag) { super(type); this.saveTag = saveTag; } @Nullable @Override - public CompoundTag getNbtData() { - Supplier tmp = saveTag; + public CompoundBinaryTag getNbt() { + Supplier tmp = saveTag; if (tmp != null) { saveTag = null; if (Fawe.isMainThread()) { - setNbtData(tmp.get()); + setNbt(tmp.get()); } else { - setNbtData(TaskManager.taskManager().sync(tmp)); + setNbt(TaskManager.taskManager().sync(tmp)); } } - return super.getNbtData(); + return super.getNbt(); } }