Avoid deprecated CompoundTag in API (#2883)

* Avoid deprecated CompoundTag in API

* use javax annotations
This commit is contained in:
Hannes Greule
2024-09-14 10:47:37 +02:00
committed by GitHub
parent 19370a3549
commit 1e8778b528
68 changed files with 1096 additions and 612 deletions

View File

@ -1,11 +1,8 @@
package com.sk89q.worldedit.bukkit.adapter.impl.fawe.v1_21_R1;
import com.google.common.base.Suppliers;
import com.sk89q.jnbt.CompoundTag;
import com.sk89q.worldedit.bukkit.adapter.impl.fawe.v1_21_R1.nbt.PaperweightLazyCompoundTag;
import com.fastasyncworldedit.core.nbt.FaweCompoundTag;
import com.sk89q.worldedit.world.registry.BlockMaterial;
import net.minecraft.core.BlockPos;
import net.minecraft.server.dedicated.DedicatedServer;
import net.minecraft.world.level.EmptyBlockGetter;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.EntityBlock;
@ -15,6 +12,8 @@ import net.minecraft.world.level.material.Fluids;
import net.minecraft.world.level.material.PushReaction;
import org.bukkit.craftbukkit.block.data.CraftBlockData;
import javax.annotation.Nullable;
public class PaperweightBlockMaterial implements BlockMaterial {
private final Block block;
@ -22,7 +21,7 @@ public class PaperweightBlockMaterial implements BlockMaterial {
private final CraftBlockData craftBlockData;
private final org.bukkit.Material craftMaterial;
private final int opacity;
private final CompoundTag tile;
private final FaweCompoundTag tile;
public PaperweightBlockMaterial(Block block) {
this(block, block.defaultBlockState());
@ -38,9 +37,9 @@ public class PaperweightBlockMaterial implements BlockMaterial {
BlockPos.ZERO,
blockState
);
tile = tileEntity == null ? null : new PaperweightLazyCompoundTag(
Suppliers.memoize(() -> tileEntity.saveWithId(DedicatedServer.getServer().registryAccess()))
);
tile = tileEntity == null
? null
: PaperweightGetBlocks.NMS_TO_TILE.apply(tileEntity);
}
public Block getBlock() {
@ -164,7 +163,7 @@ public class PaperweightBlockMaterial implements BlockMaterial {
}
@Override
public CompoundTag getDefaultTile() {
public @Nullable FaweCompoundTag defaultTile() {
return tile;
}

View File

@ -5,6 +5,7 @@ import com.fastasyncworldedit.bukkit.adapter.NMSRelighterFactory;
import com.fastasyncworldedit.core.FaweCache;
import com.fastasyncworldedit.core.entity.LazyBaseEntity;
import com.fastasyncworldedit.core.extent.processor.lighting.RelighterFactory;
import com.fastasyncworldedit.core.nbt.FaweCompoundTag;
import com.fastasyncworldedit.core.queue.IBatchProcessor;
import com.fastasyncworldedit.core.queue.IChunkGet;
import com.fastasyncworldedit.core.queue.implementation.packet.ChunkPacket;
@ -102,6 +103,7 @@ import java.util.Map;
import java.util.Objects;
import java.util.OptionalInt;
import java.util.Set;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import java.util.stream.Stream;
@ -137,6 +139,12 @@ public final class PaperweightFaweAdapter extends FaweAdapter<net.minecraft.nbt.
this.parent = new com.sk89q.worldedit.bukkit.adapter.ext.fawe.v1_21_R1.PaperweightAdapter();
}
public Function<BlockEntity, FaweCompoundTag> blockEntityToCompoundTag() {
return blockEntity -> FaweCompoundTag.of(
() -> (LinCompoundTag) toNativeLin(blockEntity.saveWithId(DedicatedServer.getServer().registryAccess()))
);
}
@Nullable
private static String getEntityId(Entity entity) {
ResourceLocation resourceLocation = net.minecraft.world.entity.EntityType.getKey(entity.getType());

View File

@ -7,20 +7,16 @@ import com.fastasyncworldedit.core.FaweCache;
import com.fastasyncworldedit.core.configuration.Settings;
import com.fastasyncworldedit.core.extent.processor.heightmap.HeightMapType;
import com.fastasyncworldedit.core.math.BitArrayUnstretched;
import com.fastasyncworldedit.core.nbt.FaweCompoundTag;
import com.fastasyncworldedit.core.queue.IChunkGet;
import com.fastasyncworldedit.core.queue.IChunkSet;
import com.fastasyncworldedit.core.queue.implementation.QueueHandler;
import com.fastasyncworldedit.core.queue.implementation.blocks.CharGetBlocks;
import com.fastasyncworldedit.core.util.MathMan;
import com.fastasyncworldedit.core.util.NbtUtils;
import com.fastasyncworldedit.core.util.collection.AdaptedMap;
import com.google.common.base.Suppliers;
import com.sk89q.jnbt.CompoundTag;
import com.sk89q.jnbt.ListTag;
import com.sk89q.jnbt.StringTag;
import com.sk89q.jnbt.Tag;
import com.sk89q.worldedit.bukkit.BukkitAdapter;
import com.sk89q.worldedit.bukkit.WorldEditPlugin;
import com.sk89q.worldedit.bukkit.adapter.impl.fawe.v1_21_R1.nbt.PaperweightLazyCompoundTag;
import com.sk89q.worldedit.internal.Constants;
import com.sk89q.worldedit.internal.util.LogManagerCompat;
import com.sk89q.worldedit.math.BlockVector3;
@ -34,6 +30,7 @@ import net.minecraft.core.Holder;
import net.minecraft.core.IdMap;
import net.minecraft.core.Registry;
import net.minecraft.core.SectionPos;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.IntTag;
import net.minecraft.server.dedicated.DedicatedServer;
import net.minecraft.server.level.ServerLevel;
@ -62,11 +59,19 @@ import org.bukkit.World;
import org.bukkit.craftbukkit.CraftWorld;
import org.bukkit.craftbukkit.block.CraftBlock;
import org.bukkit.event.entity.CreatureSpawnEvent;
import org.enginehub.linbus.tree.LinCompoundTag;
import org.enginehub.linbus.tree.LinDoubleTag;
import org.enginehub.linbus.tree.LinFloatTag;
import org.enginehub.linbus.tree.LinListTag;
import org.enginehub.linbus.tree.LinStringTag;
import org.enginehub.linbus.tree.LinTagType;
import javax.annotation.Nonnull;
import java.util.AbstractSet;
import javax.annotation.Nullable;
import java.util.AbstractCollection;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
@ -83,7 +88,6 @@ import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
import java.util.function.Function;
import java.util.stream.Collectors;
import static net.minecraft.core.registries.Registries.BIOME;
@ -92,9 +96,9 @@ public class PaperweightGetBlocks extends CharGetBlocks implements BukkitGetBloc
private static final Logger LOGGER = LogManagerCompat.getLogger();
private static final Function<BlockPos, BlockVector3> posNms2We = v -> BlockVector3.at(v.getX(), v.getY(), v.getZ());
private static final Function<BlockEntity, CompoundTag> nmsTile2We = tileEntity -> new PaperweightLazyCompoundTag(
Suppliers.memoize(() -> tileEntity.saveWithId(DedicatedServer.getServer().registryAccess()))
);
public static final Function<BlockEntity, FaweCompoundTag> NMS_TO_TILE = ((PaperweightFaweAdapter) WorldEditPlugin
.getInstance()
.getBukkitImplAdapter()).blockEntityToCompoundTag();
private final PaperweightFaweAdapter adapter = ((PaperweightFaweAdapter) WorldEditPlugin
.getInstance()
.getBukkitImplAdapter());
@ -258,23 +262,24 @@ public class PaperweightGetBlocks extends CharGetBlocks implements BukkitGetBloc
}
@Override
public CompoundTag getTile(int x, int y, int z) {
public FaweCompoundTag tile(final int x, final int y, final int z) {
BlockEntity blockEntity = getChunk().getBlockEntity(new BlockPos((x & 15) + (
chunkX << 4), y, (z & 15) + (
chunkZ << 4)));
if (blockEntity == null) {
return null;
}
return new PaperweightLazyCompoundTag(Suppliers.memoize(() -> blockEntity.saveWithId(DedicatedServer.getServer().registryAccess())));
return NMS_TO_TILE.apply(blockEntity);
}
@Override
public Map<BlockVector3, CompoundTag> getTiles() {
public Map<BlockVector3, FaweCompoundTag> tiles() {
Map<BlockPos, BlockEntity> nmsTiles = getChunk().getBlockEntities();
if (nmsTiles.isEmpty()) {
return Collections.emptyMap();
}
return AdaptedMap.immutable(nmsTiles, posNms2We, nmsTile2We);
return AdaptedMap.immutable(nmsTiles, posNms2We, NMS_TO_TILE);
}
@Override
@ -337,7 +342,7 @@ public class PaperweightGetBlocks extends CharGetBlocks implements BukkitGetBloc
}
@Override
public CompoundTag getEntity(UUID uuid) {
public @Nullable FaweCompoundTag entity(final UUID uuid) {
ensureLoaded(serverLevel, chunkX, chunkZ);
List<Entity> entities = PaperweightPlatformAdapter.getEntities(getChunk());
Entity entity = null;
@ -349,10 +354,10 @@ public class PaperweightGetBlocks extends CharGetBlocks implements BukkitGetBloc
}
if (entity != null) {
org.bukkit.entity.Entity bukkitEnt = entity.getBukkitEntity();
return BukkitAdapter.adapt(bukkitEnt).getState().getNbtData();
return FaweCompoundTag.of(BukkitAdapter.adapt(bukkitEnt).getState().getNbt());
}
for (CompoundTag tag : getEntities()) {
if (uuid.equals(tag.getUUID())) {
for (FaweCompoundTag tag : entities()) {
if (uuid.equals(NbtUtils.uuid(tag))) {
return tag;
}
}
@ -360,14 +365,14 @@ public class PaperweightGetBlocks extends CharGetBlocks implements BukkitGetBloc
}
@Override
public Set<CompoundTag> getEntities() {
public Collection<FaweCompoundTag> entities() {
ensureLoaded(serverLevel, chunkX, chunkZ);
List<Entity> entities = PaperweightPlatformAdapter.getEntities(getChunk());
if (entities.isEmpty()) {
return Collections.emptySet();
return Collections.emptyList();
}
int size = entities.size();
return new AbstractSet<>() {
return new AbstractCollection<>() {
@Override
public int size() {
return size;
@ -380,10 +385,10 @@ public class PaperweightGetBlocks extends CharGetBlocks implements BukkitGetBloc
@Override
public boolean contains(Object get) {
if (!(get instanceof CompoundTag getTag)) {
if (!(get instanceof FaweCompoundTag getTag)) {
return false;
}
UUID getUUID = getTag.getUUID();
UUID getUUID = NbtUtils.uuid(getTag);
for (Entity entity : entities) {
UUID uuid = entity.getUUID();
if (uuid.equals(getUUID)) {
@ -395,15 +400,16 @@ public class PaperweightGetBlocks extends CharGetBlocks implements BukkitGetBloc
@Nonnull
@Override
public Iterator<CompoundTag> iterator() {
Iterable<CompoundTag> result = entities.stream().map(input -> {
net.minecraft.nbt.CompoundTag tag = new net.minecraft.nbt.CompoundTag();
public Iterator<FaweCompoundTag> iterator() {
Iterable<FaweCompoundTag> result = entities.stream().map(input -> {
CompoundTag tag = new CompoundTag();
input.save(tag);
return (CompoundTag) adapter.toNative(tag);
}).collect(Collectors.toList());
return FaweCompoundTag.of((LinCompoundTag) adapter.toNativeLin(tag));
})::iterator;
return result.iterator();
}
};
}
private void removeEntity(Entity entity) {
@ -722,43 +728,42 @@ public class PaperweightGetBlocks extends CharGetBlocks implements BukkitGetBloc
};
}
Set<CompoundTag> entities = set.getEntities();
Collection<FaweCompoundTag> entities = set.entities();
if (entities != null && !entities.isEmpty()) {
if (syncTasks == null) {
syncTasks = new Runnable[2];
}
syncTasks[1] = () -> {
Iterator<CompoundTag> iterator = entities.iterator();
Iterator<FaweCompoundTag> iterator = entities.iterator();
while (iterator.hasNext()) {
final CompoundTag nativeTag = iterator.next();
final Map<String, Tag<?, ?>> entityTagMap = nativeTag.getValue();
final StringTag idTag = (StringTag) entityTagMap.get("Id");
final ListTag posTag = (ListTag) entityTagMap.get("Pos");
final ListTag rotTag = (ListTag) entityTagMap.get("Rotation");
final FaweCompoundTag nativeTag = iterator.next();
final LinCompoundTag linTag = nativeTag.linTag();
final LinStringTag idTag = linTag.findTag("Id", LinTagType.stringTag());
final LinListTag<LinDoubleTag> posTag = linTag.findListTag("Pos", LinTagType.doubleTag());
final LinListTag<LinFloatTag> rotTag = linTag.findListTag("Rotation", LinTagType.floatTag());
if (idTag == null || posTag == null || rotTag == null) {
LOGGER.error("Unknown entity tag: {}", nativeTag);
continue;
}
final double x = posTag.getDouble(0);
final double y = posTag.getDouble(1);
final double z = posTag.getDouble(2);
final float yaw = rotTag.getFloat(0);
final float pitch = rotTag.getFloat(1);
final String id = idTag.getValue();
final double x = posTag.get(0).valueAsDouble();
final double y = posTag.get(1).valueAsDouble();
final double z = posTag.get(2).valueAsDouble();
final float yaw = rotTag.get(0).valueAsFloat();
final float pitch = rotTag.get(1).valueAsFloat();
final String id = idTag.value();
EntityType<?> type = EntityType.byString(id).orElse(null);
if (type != null) {
Entity entity = type.create(nmsWorld);
if (entity != null) {
final net.minecraft.nbt.CompoundTag tag = (net.minecraft.nbt.CompoundTag) adapter.fromNative(
nativeTag);
final CompoundTag tag = (CompoundTag) adapter.fromNativeLin(linTag);
for (final String name : Constants.NO_COPY_ENTITY_NBT_FIELDS) {
tag.remove(name);
}
entity.load(tag);
entity.absMoveTo(x, y, z, yaw, pitch);
entity.setUUID(nativeTag.getUUID());
entity.setUUID(NbtUtils.uuid(nativeTag));
if (!nmsWorld.addFreshEntity(entity, CreatureSpawnEvent.SpawnReason.CUSTOM)) {
LOGGER.warn(
"Error creating entity of type `{}` in world `{}` at location `{},{},{}`",
@ -778,15 +783,15 @@ public class PaperweightGetBlocks extends CharGetBlocks implements BukkitGetBloc
}
// set tiles
Map<BlockVector3, CompoundTag> tiles = set.getTiles();
Map<BlockVector3, FaweCompoundTag> tiles = set.tiles();
if (tiles != null && !tiles.isEmpty()) {
if (syncTasks == null) {
syncTasks = new Runnable[1];
}
syncTasks[0] = () -> {
for (final Map.Entry<BlockVector3, CompoundTag> entry : tiles.entrySet()) {
final CompoundTag nativeTag = entry.getValue();
for (final Map.Entry<BlockVector3, FaweCompoundTag> entry : tiles.entrySet()) {
final FaweCompoundTag nativeTag = entry.getValue();
final BlockVector3 blockHash = entry.getKey();
final int x = blockHash.x() + bx;
final int y = blockHash.y();
@ -800,8 +805,7 @@ public class PaperweightGetBlocks extends CharGetBlocks implements BukkitGetBloc
tileEntity = nmsWorld.getBlockEntity(pos);
}
if (tileEntity != null) {
final net.minecraft.nbt.CompoundTag tag = (net.minecraft.nbt.CompoundTag) adapter.fromNative(
nativeTag);
final CompoundTag tag = (CompoundTag) adapter.fromNativeLin(nativeTag.linTag());
tag.put("x", IntTag.valueOf(x));
tag.put("y", IntTag.valueOf(y));
tag.put("z", IntTag.valueOf(z));

View File

@ -1,14 +1,13 @@
package com.sk89q.worldedit.bukkit.adapter.impl.fawe.v1_21_R1;
import com.fastasyncworldedit.core.extent.processor.heightmap.HeightMapType;
import com.fastasyncworldedit.core.nbt.FaweCompoundTag;
import com.fastasyncworldedit.core.queue.IBlocks;
import com.fastasyncworldedit.core.queue.IChunkGet;
import com.fastasyncworldedit.core.queue.IChunkSet;
import com.google.common.base.Suppliers;
import com.sk89q.jnbt.CompoundTag;
import com.fastasyncworldedit.core.util.NbtUtils;
import com.sk89q.worldedit.bukkit.WorldEditPlugin;
import com.sk89q.worldedit.bukkit.adapter.BukkitImplAdapter;
import com.sk89q.worldedit.bukkit.adapter.impl.fawe.v1_21_R1.nbt.PaperweightLazyCompoundTag;
import com.sk89q.worldedit.internal.util.LogManagerCompat;
import com.sk89q.worldedit.math.BlockVector3;
import com.sk89q.worldedit.world.biome.BiomeType;
@ -17,6 +16,7 @@ import com.sk89q.worldedit.world.block.BlockState;
import com.sk89q.worldedit.world.block.BlockTypesCache;
import io.papermc.lib.PaperLib;
import net.minecraft.core.Holder;
import net.minecraft.nbt.Tag;
import net.minecraft.server.dedicated.DedicatedServer;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.entity.Entity;
@ -26,9 +26,11 @@ import net.minecraft.world.level.chunk.LevelChunk;
import net.minecraft.world.level.chunk.PalettedContainer;
import net.minecraft.world.level.chunk.PalettedContainerRO;
import org.apache.logging.log4j.Logger;
import org.enginehub.linbus.tree.LinCompoundTag;
import javax.annotation.Nullable;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
@ -40,8 +42,8 @@ public class PaperweightGetBlocks_Copy implements IChunkGet {
private static final Logger LOGGER = LogManagerCompat.getLogger();
private final Map<BlockVector3, CompoundTag> tiles = new HashMap<>();
private final Set<CompoundTag> entities = new HashSet<>();
private final Map<BlockVector3, FaweCompoundTag> tiles = new HashMap<>();
private final Set<FaweCompoundTag> entities = new HashSet<>();
private final char[][] blocks;
private final int minHeight;
private final int maxHeight;
@ -58,44 +60,37 @@ public class PaperweightGetBlocks_Copy implements IChunkGet {
}
protected void storeTile(BlockEntity blockEntity) {
@SuppressWarnings("unchecked")
BukkitImplAdapter<Tag> adapter = WorldEditPlugin.getInstance().getBukkitImplAdapter();
tiles.put(
BlockVector3.at(
blockEntity.getBlockPos().getX(),
blockEntity.getBlockPos().getY(),
blockEntity.getBlockPos().getZ()
),
new PaperweightLazyCompoundTag(Suppliers.memoize(() -> blockEntity.saveWithId(DedicatedServer.getServer().registryAccess())))
FaweCompoundTag.of((LinCompoundTag) adapter.toNativeLin(blockEntity.saveWithId(DedicatedServer
.getServer()
.registryAccess())))
);
}
@Override
public Map<BlockVector3, CompoundTag> getTiles() {
return tiles;
}
@Override
@Nullable
public CompoundTag getTile(int x, int y, int z) {
return tiles.get(BlockVector3.at(x, y, z));
}
@SuppressWarnings({"unchecked", "rawtypes"})
protected void storeEntity(Entity entity) {
BukkitImplAdapter adapter = WorldEditPlugin.getInstance().getBukkitImplAdapter();
@SuppressWarnings("unchecked")
BukkitImplAdapter<Tag> adapter = WorldEditPlugin.getInstance().getBukkitImplAdapter();
net.minecraft.nbt.CompoundTag compoundTag = new net.minecraft.nbt.CompoundTag();
entity.save(compoundTag);
entities.add((CompoundTag) adapter.toNative(compoundTag));
entities.add(FaweCompoundTag.of((LinCompoundTag) adapter.toNativeLin(compoundTag)));
}
@Override
public Set<CompoundTag> getEntities() {
public Collection<FaweCompoundTag> entities() {
return this.entities;
}
@Override
public CompoundTag getEntity(UUID uuid) {
for (CompoundTag tag : entities) {
if (uuid.equals(tag.getUUID())) {
public @Nullable FaweCompoundTag entity(final UUID uuid) {
for (FaweCompoundTag tag : entities) {
if (uuid.equals(NbtUtils.uuid(tag))) {
return tag;
}
}
@ -206,7 +201,7 @@ public class PaperweightGetBlocks_Copy implements IChunkGet {
@Override
public BaseBlock getFullBlock(int x, int y, int z) {
BlockState state = BlockTypesCache.states[get(x, y, z)];
return state.toBaseBlock(this, x, y, z);
return state.toBaseBlock((IBlocks) this, x, y, z);
}
@Override
@ -236,6 +231,16 @@ public class PaperweightGetBlocks_Copy implements IChunkGet {
return BlockTypesCache.states[get(x, y, z)];
}
@Override
public Map<BlockVector3, FaweCompoundTag> tiles() {
return tiles;
}
@Override
public @Nullable FaweCompoundTag tile(final int x, final int y, final int z) {
return tiles.get(BlockVector3.at(x, y, z));
}
@Override
public int getSkyLight(int x, int y, int z) {
return 0;