From 50971ee31147115ac8b188edb8193742397a0415 Mon Sep 17 00:00:00 2001 From: wea_ondara Date: Sun, 26 Jul 2020 15:52:51 +0200 Subject: [PATCH] fix entity undo --- .../adapter/mc1_14/BukkitGetBlocks_1_14.java | 16 ++----- .../mc1_15_2/BukkitGetBlocks_1_15_2.java | 17 +++---- .../mc1_16_1/BukkitGetBlocks_1_16_1.java | 16 ++----- .../beta/implementation/IChunkExtent.java | 46 +++++++++++++++++-- .../fawe/object/change/MutableTileChange.java | 7 --- .../extent/AbstractDelegateExtent.java | 6 +++ .../worldedit/extent/PassthroughExtent.java | 5 -- .../sk89q/worldedit/internal/Constants.java | 2 +- 8 files changed, 65 insertions(+), 50 deletions(-) diff --git a/worldedit-bukkit/src/main/java/com/boydti/fawe/bukkit/adapter/mc1_14/BukkitGetBlocks_1_14.java b/worldedit-bukkit/src/main/java/com/boydti/fawe/bukkit/adapter/mc1_14/BukkitGetBlocks_1_14.java index 0f5d33fe0..fd5902df1 100644 --- a/worldedit-bukkit/src/main/java/com/boydti/fawe/bukkit/adapter/mc1_14/BukkitGetBlocks_1_14.java +++ b/worldedit-bukkit/src/main/java/com/boydti/fawe/bukkit/adapter/mc1_14/BukkitGetBlocks_1_14.java @@ -269,7 +269,6 @@ public class BukkitGetBlocks_1_14 extends CharGetBlocks { private void removeEntity(Entity entity) { entity.die(); - entity.valid = false; } public Chunk ensureLoaded(net.minecraft.server.v1_14_R1.World nmsWorld, int X, int Z) { @@ -449,17 +448,12 @@ public class BukkitGetBlocks_1_14 extends CharGetBlocks { if (type != null) { Entity entity = type.a(nmsWorld); if (entity != null) { - UUID uuid = entity.getUniqueID(); - entityTagMap.put("UUIDMost", new LongTag(uuid.getMostSignificantBits())); - entityTagMap.put("UUIDLeast", new LongTag(uuid.getLeastSignificantBits())); - if (nativeTag != null) { - BukkitImplAdapter adapter = WorldEditPlugin.getInstance().getBukkitImplAdapter(); - final NBTTagCompound tag = (NBTTagCompound) adapter.fromNative(nativeTag); - for (final String name : Constants.NO_COPY_ENTITY_NBT_FIELDS) { - tag.remove(name); - } - entity.f(tag); + BukkitImplAdapter adapter = WorldEditPlugin.getInstance().getBukkitImplAdapter(); + final NBTTagCompound tag = (NBTTagCompound) adapter.fromNative(nativeTag); + for (final String name : Constants.NO_COPY_ENTITY_NBT_FIELDS) { + tag.remove(name); } + entity.f(tag); entity.setLocation(x, y, z, yaw, pitch); nmsWorld.addEntity(entity, CreatureSpawnEvent.SpawnReason.CUSTOM); } diff --git a/worldedit-bukkit/src/main/java/com/boydti/fawe/bukkit/adapter/mc1_15_2/BukkitGetBlocks_1_15_2.java b/worldedit-bukkit/src/main/java/com/boydti/fawe/bukkit/adapter/mc1_15_2/BukkitGetBlocks_1_15_2.java index 4518420a2..a48a7922f 100644 --- a/worldedit-bukkit/src/main/java/com/boydti/fawe/bukkit/adapter/mc1_15_2/BukkitGetBlocks_1_15_2.java +++ b/worldedit-bukkit/src/main/java/com/boydti/fawe/bukkit/adapter/mc1_15_2/BukkitGetBlocks_1_15_2.java @@ -277,7 +277,6 @@ public class BukkitGetBlocks_1_15_2 extends CharGetBlocks { private void removeEntity(Entity entity) { entity.die(); - entity.valid = false; } public Chunk ensureLoaded(net.minecraft.server.v1_15_R1.World nmsWorld, int X, int Z) { @@ -467,17 +466,13 @@ public class BukkitGetBlocks_1_15_2 extends CharGetBlocks { if (type != null) { Entity entity = type.a(nmsWorld); if (entity != null) { - UUID uuid = entity.getUniqueID(); - entityTagMap.put("UUIDMost", new LongTag(uuid.getMostSignificantBits())); - entityTagMap.put("UUIDLeast", new LongTag(uuid.getLeastSignificantBits())); - if (nativeTag != null) { - BukkitImplAdapter adapter = WorldEditPlugin.getInstance().getBukkitImplAdapter(); - final NBTTagCompound tag = (NBTTagCompound) adapter.fromNative(nativeTag); - for (final String name : Constants.NO_COPY_ENTITY_NBT_FIELDS) { - tag.remove(name); - } - entity.f(tag); + BukkitImplAdapter adapter = WorldEditPlugin.getInstance().getBukkitImplAdapter(); + final NBTTagCompound tag = (NBTTagCompound) adapter.fromNative(nativeTag); + for (final String name : Constants.NO_COPY_ENTITY_NBT_FIELDS) { + tag.remove(name); } + + entity.f(tag); entity.setLocation(x, y, z, yaw, pitch); nmsWorld.addEntity(entity, CreatureSpawnEvent.SpawnReason.CUSTOM); } diff --git a/worldedit-bukkit/src/main/java/com/boydti/fawe/bukkit/adapter/mc1_16_1/BukkitGetBlocks_1_16_1.java b/worldedit-bukkit/src/main/java/com/boydti/fawe/bukkit/adapter/mc1_16_1/BukkitGetBlocks_1_16_1.java index 2d5ace231..9433308c2 100644 --- a/worldedit-bukkit/src/main/java/com/boydti/fawe/bukkit/adapter/mc1_16_1/BukkitGetBlocks_1_16_1.java +++ b/worldedit-bukkit/src/main/java/com/boydti/fawe/bukkit/adapter/mc1_16_1/BukkitGetBlocks_1_16_1.java @@ -245,7 +245,6 @@ public class BukkitGetBlocks_1_16_1 extends CharGetBlocks { private void removeEntity(Entity entity) { entity.die(); - entity.valid = false; } public Chunk ensureLoaded(net.minecraft.server.v1_16_R1.World nmsWorld, int X, int Z) { @@ -438,17 +437,12 @@ public class BukkitGetBlocks_1_16_1 extends CharGetBlocks { if (type != null) { Entity entity = type.a(nmsWorld); if (entity != null) { - UUID uuid = entity.getUniqueID(); - entityTagMap.put("UUIDMost", new LongTag(uuid.getMostSignificantBits())); - entityTagMap.put("UUIDLeast", new LongTag(uuid.getLeastSignificantBits())); - if (nativeTag != null) { - BukkitImplAdapter adapter = WorldEditPlugin.getInstance().getBukkitImplAdapter(); - final NBTTagCompound tag = (NBTTagCompound) adapter.fromNative(nativeTag); - for (final String name : Constants.NO_COPY_ENTITY_NBT_FIELDS) { - tag.remove(name); - } - entity.save(tag); + BukkitImplAdapter adapter = WorldEditPlugin.getInstance().getBukkitImplAdapter(); + final NBTTagCompound tag = (NBTTagCompound) adapter.fromNative(nativeTag); + for (final String name : Constants.NO_COPY_ENTITY_NBT_FIELDS) { + tag.remove(name); } + entity.load(tag); entity.setLocation(x, y, z, yaw, pitch); nmsWorld.addEntity(entity, CreatureSpawnEvent.SpawnReason.CUSTOM); } diff --git a/worldedit-core/src/main/java/com/boydti/fawe/beta/implementation/IChunkExtent.java b/worldedit-core/src/main/java/com/boydti/fawe/beta/implementation/IChunkExtent.java index d651ef673..3cd6a9c31 100644 --- a/worldedit-core/src/main/java/com/boydti/fawe/beta/implementation/IChunkExtent.java +++ b/worldedit-core/src/main/java/com/boydti/fawe/beta/implementation/IChunkExtent.java @@ -4,7 +4,10 @@ import com.boydti.fawe.beta.IChunk; import com.boydti.fawe.util.ReflectionUtils; import com.sk89q.jnbt.CompoundTag; import com.sk89q.jnbt.DoubleTag; +import com.sk89q.jnbt.IntArrayTag; +import com.sk89q.jnbt.IntTag; import com.sk89q.jnbt.ListTag; +import com.sk89q.jnbt.LongTag; import com.sk89q.jnbt.StringTag; import com.sk89q.jnbt.Tag; import com.sk89q.worldedit.WorldEditException; @@ -16,8 +19,10 @@ import com.sk89q.worldedit.world.biome.BiomeType; import com.sk89q.worldedit.world.block.BaseBlock; import com.sk89q.worldedit.world.block.BlockState; import com.sk89q.worldedit.world.block.BlockStateHolder; +import java.util.ArrayList; import java.util.List; import java.util.Map; +import java.util.UUID; import org.jetbrains.annotations.Range; public interface IChunkExtent extends Extent { @@ -108,14 +113,47 @@ public interface IChunkExtent extends Extent { CompoundTag tag = entity.getNbtData(); Map map = ReflectionUtils.getMap(tag.getValue()); map.put("Id", new StringTag(entity.getType().getName())); + + //Set pos ListTag pos = (ListTag) map.get("Pos"); + List posList; if (pos != null) { - List posList = ReflectionUtils.getList(pos.getValue()); - posList.set(0, new DoubleTag(location.getX() + 0.5)); - posList.set(1, new DoubleTag(location.getY())); - posList.set(2, new DoubleTag(location.getZ() + 0.5)); + posList = ReflectionUtils.getList(pos.getValue()); + } else { + posList = new ArrayList<>(); + pos = new ListTag(DoubleTag.class, posList); + map.put("Pos", pos); } + posList.set(0, new DoubleTag(location.getX() + 0.5)); + posList.set(1, new DoubleTag(location.getY())); + posList.set(2, new DoubleTag(location.getZ() + 0.5)); + + //set new uuid + UUID newuuid = UUID.randomUUID(); + IntArrayTag uuid = (IntArrayTag) map.get("UUID"); + int[] uuidArray; + if (uuid != null) { + uuidArray = uuid.getValue(); + } else { + uuidArray = new int[4]; + uuid = new IntArrayTag(uuidArray); + map.put("UUID", uuid); + } + uuidArray[0] = (int) (newuuid.getMostSignificantBits() >> 32); + uuidArray[1] = (int) newuuid.getMostSignificantBits(); + uuidArray[2] = (int) (newuuid.getLeastSignificantBits() >> 32); + uuidArray[3] = (int) newuuid.getLeastSignificantBits(); + + map.put("UUIDMost", new LongTag(newuuid.getMostSignificantBits())); + map.put("UUIDLeast", new LongTag(newuuid.getLeastSignificantBits())); + chunk.setEntity(tag); return null; } + + @Override + default void removeEntity(int x, int y, int z, UUID uuid) { + final IChunk chunk = getOrCreateChunk(x >> 4, z >> 4); + chunk.removeEntity(uuid); + } } diff --git a/worldedit-core/src/main/java/com/boydti/fawe/object/change/MutableTileChange.java b/worldedit-core/src/main/java/com/boydti/fawe/object/change/MutableTileChange.java index b25e764d7..6c8c557fe 100644 --- a/worldedit-core/src/main/java/com/boydti/fawe/object/change/MutableTileChange.java +++ b/worldedit-core/src/main/java/com/boydti/fawe/object/change/MutableTileChange.java @@ -1,16 +1,9 @@ package com.boydti.fawe.object.change; -import com.boydti.fawe.Fawe; -import com.boydti.fawe.beta.IQueueExtent; -import com.boydti.fawe.util.ExtentTraverser; import com.sk89q.jnbt.CompoundTag; -import com.sk89q.jnbt.IntTag; -import com.sk89q.jnbt.Tag; import com.sk89q.worldedit.WorldEditException; -import com.sk89q.worldedit.extent.Extent; import com.sk89q.worldedit.history.UndoContext; import com.sk89q.worldedit.history.change.Change; -import java.util.Map; public class MutableTileChange implements Change { diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/extent/AbstractDelegateExtent.java b/worldedit-core/src/main/java/com/sk89q/worldedit/extent/AbstractDelegateExtent.java index aa0eb90a2..467de473d 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/extent/AbstractDelegateExtent.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/extent/AbstractDelegateExtent.java @@ -46,6 +46,7 @@ import com.sk89q.worldedit.world.block.BlockState; import com.sk89q.worldedit.world.block.BlockStateHolder; import java.util.List; +import java.util.UUID; import javax.annotation.Nullable; import org.jetbrains.annotations.Range; @@ -155,6 +156,11 @@ public class AbstractDelegateExtent implements Extent { return extent.createEntity(location, entity); } + @Override + public void removeEntity(int x, int y, int z, UUID uuid) { + extent.removeEntity(x, y, z, uuid); + } + @Override public List getEntities() { return extent.getEntities(); diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/extent/PassthroughExtent.java b/worldedit-core/src/main/java/com/sk89q/worldedit/extent/PassthroughExtent.java index 14d8ba7d0..d6b387d94 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/extent/PassthroughExtent.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/extent/PassthroughExtent.java @@ -36,11 +36,6 @@ public class PassthroughExtent extends AbstractDelegateExtent { super(extent); } - @Override - public void removeEntity(int x, int y, int z, UUID uuid) { - getExtent().removeEntity(x, y, z, uuid); - } - @Override public boolean regenerateChunk(int x, int z, @Nullable BiomeType type, @Nullable Long seed) { return getExtent().regenerateChunk(x, z, type, seed); diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/internal/Constants.java b/worldedit-core/src/main/java/com/sk89q/worldedit/internal/Constants.java index 9194866b4..996bdb0a6 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/internal/Constants.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/internal/Constants.java @@ -36,7 +36,7 @@ public final class Constants { static { NO_COPY_ENTITY_NBT_FIELDS = Collections.unmodifiableList(Arrays.asList( - "UUIDLeast", "UUIDMost", // Bukkit and Vanilla +// "UUIDLeast", "UUIDMost", // Bukkit and Vanilla "WorldUUIDLeast", "WorldUUIDMost", // Bukkit and Vanilla "PersistentIDMSB", "PersistentIDLSB" // Forge ));