Skip to content

Commit

Permalink
Avoid deprecated CompoundTag in API (#2883)
Browse files Browse the repository at this point in the history
* Avoid deprecated CompoundTag in API

* use javax annotations
  • Loading branch information
SirYwell authored Sep 14, 2024
1 parent 19370a3 commit 1e8778b
Show file tree
Hide file tree
Showing 68 changed files with 1,093 additions and 609 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package com.sk89q.worldedit.bukkit.adapter.impl.fawe.v1_20_R2;

import com.google.common.base.Suppliers;
import com.sk89q.jnbt.CompoundTag;
import com.fastasyncworldedit.core.nbt.FaweCompoundTag;
import com.sk89q.util.ReflectionUtil;
import com.sk89q.worldedit.bukkit.adapter.Refraction;
import com.sk89q.worldedit.bukkit.adapter.impl.fawe.v1_20_R2.nbt.PaperweightLazyCompoundTag;
import com.sk89q.worldedit.world.registry.BlockMaterial;
import net.minecraft.core.BlockPos;
import net.minecraft.world.level.EmptyBlockGetter;
Expand All @@ -17,6 +15,8 @@
import net.minecraft.world.level.material.PushReaction;
import org.bukkit.craftbukkit.v1_20_R2.block.data.CraftBlockData;

import javax.annotation.Nullable;

public class PaperweightBlockMaterial implements BlockMaterial {

private final Block block;
Expand All @@ -25,7 +25,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());
Expand All @@ -48,7 +48,7 @@ public PaperweightBlockMaterial(Block block, BlockState blockState) {
);
tile = tileEntity == null
? null
: new PaperweightLazyCompoundTag(Suppliers.memoize(tileEntity::saveWithId));
: PaperweightGetBlocks.NMS_TO_TILE.apply(tileEntity);
}

public Block getBlock() {
Expand Down Expand Up @@ -173,7 +173,7 @@ public boolean isTile() {
}

@Override
public CompoundTag getDefaultTile() {
public @Nullable FaweCompoundTag defaultTile() {
return tile;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
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;
Expand Down Expand Up @@ -97,6 +98,7 @@
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;
Expand Down Expand Up @@ -129,6 +131,12 @@ public PaperweightFaweAdapter() throws NoSuchFieldException, NoSuchMethodExcepti
this.parent = new com.sk89q.worldedit.bukkit.adapter.ext.fawe.v1_20_R2.PaperweightAdapter();
}

public Function<BlockEntity, FaweCompoundTag> blockEntityToCompoundTag() {
return blockEntity -> FaweCompoundTag.of(
() -> (LinCompoundTag) toNativeLin(blockEntity.saveWithId())
);
}

@Nullable
private static String getEntityId(Entity entity) {
ResourceLocation resourceLocation = net.minecraft.world.entity.EntityType.getKey(entity.getType());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,16 @@
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_20_R2.nbt.PaperweightLazyCompoundTag;
import com.sk89q.worldedit.internal.Constants;
import com.sk89q.worldedit.internal.util.LogManagerCompat;
import com.sk89q.worldedit.math.BlockVector3;
Expand All @@ -34,6 +30,7 @@
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.level.ServerLevel;
import net.minecraft.sounds.SoundEvents;
Expand Down Expand Up @@ -61,11 +58,19 @@
import org.bukkit.craftbukkit.v1_20_R2.CraftWorld;
import org.bukkit.craftbukkit.v1_20_R2.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;
Expand All @@ -82,7 +87,6 @@
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;

Expand All @@ -91,8 +95,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));
public static final Function<BlockEntity, FaweCompoundTag> NMS_TO_TILE = ((PaperweightFaweAdapter) WorldEditPlugin
.getInstance()
.getBukkitImplAdapter()).blockEntityToCompoundTag();
private final PaperweightFaweAdapter adapter = ((PaperweightFaweAdapter) WorldEditPlugin
.getInstance()
.getBukkitImplAdapter());
Expand Down Expand Up @@ -256,23 +261,24 @@ public void removeSectionLighting(int layer, boolean sky) {
}

@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));
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
Expand Down Expand Up @@ -335,7 +341,7 @@ public int[] getHeightMap(HeightMapType type) {
}

@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;
Expand All @@ -347,25 +353,25 @@ public CompoundTag getEntity(UUID uuid) {
}
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;
}
}
return null;
}

@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;
Expand All @@ -378,10 +384,10 @@ public boolean isEmpty() {

@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)) {
Expand All @@ -393,12 +399,12 @@ public boolean contains(Object get) {

@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();
}
};
Expand Down Expand Up @@ -728,43 +734,42 @@ public synchronized <T extends Future<T>> T call(IChunkSet set, Runnable finaliz
};
}

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 `{},{},{}`",
Expand All @@ -784,15 +789,15 @@ public synchronized <T extends Future<T>> T call(IChunkSet set, Runnable finaliz
}

// 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();
Expand All @@ -806,8 +811,7 @@ public synchronized <T extends Future<T>> T call(IChunkSet set, Runnable finaliz
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));
Expand Down
Loading

0 comments on commit 1e8778b

Please sign in to comment.