From 94b1233d98e782e0fb609d6c068e8de0a7604a1b Mon Sep 17 00:00:00 2001 From: Aurora Date: Fri, 3 Jul 2020 19:52:55 +0200 Subject: [PATCH] implement entities in paste -e --- .../sk89q/worldedit/bukkit/BukkitWorld.java | 25 ----------------- .../beta/implementation/IChunkExtent.java | 27 +++++++++++++++++++ .../com/boydti/fawe/util/ReflectionUtils.java | 16 ++++++++++- .../java/com/sk89q/worldedit/EditSession.java | 13 ++++++++- .../extent/clipboard/BlockArrayClipboard.java | 5 ++++ 5 files changed, 59 insertions(+), 27 deletions(-) diff --git a/worldedit-bukkit/src/main/java/com/sk89q/worldedit/bukkit/BukkitWorld.java b/worldedit-bukkit/src/main/java/com/sk89q/worldedit/bukkit/BukkitWorld.java index 588071c34..0e6ca78aa 100644 --- a/worldedit-bukkit/src/main/java/com/sk89q/worldedit/bukkit/BukkitWorld.java +++ b/worldedit-bukkit/src/main/java/com/sk89q/worldedit/bukkit/BukkitWorld.java @@ -119,31 +119,6 @@ public class BukkitWorld extends AbstractWorld { return list; } - @Nullable - @Override - public com.sk89q.worldedit.entity.Entity createEntity(com.sk89q.worldedit.util.Location location, BaseEntity entity) { - BukkitImplAdapter adapter = WorldEditPlugin.getInstance().getBukkitImplAdapter(); - if (adapter != null) { - try { - Entity createdEntity = adapter.createEntity(BukkitAdapter.adapt(getWorld(), location), entity); - if (createdEntity != null) { - return new BukkitEntity(createdEntity); - } else { - return null; - } - } catch (Exception e) { - logger.warn("Corrupt entity found when creating: " + entity.getType().getId()); - if (entity.getNbtData() != null) { - logger.warn(entity.getNbtData().toString()); - } - e.printStackTrace(); - return null; - } - } else { - return null; - } - } - /** * Get the world handle. * 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 5ebffe8cc..b299783bb 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 @@ -1,13 +1,23 @@ package com.boydti.fawe.beta.implementation; 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.ListTag; +import com.sk89q.jnbt.StringTag; +import com.sk89q.jnbt.Tag; import com.sk89q.worldedit.WorldEditException; +import com.sk89q.worldedit.entity.BaseEntity; +import com.sk89q.worldedit.entity.Entity; import com.sk89q.worldedit.extent.Extent; +import com.sk89q.worldedit.util.Location; 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.List; +import java.util.Map; import org.jetbrains.annotations.Range; public interface IChunkExtent extends Extent { @@ -91,4 +101,21 @@ public interface IChunkExtent extends Extent { final IChunk chunk = getOrCreateChunk(x >> 4, z >> 4); return chunk.getOpacity(x & 15, y, z & 15); } + + @Override + default Entity createEntity(Location location, BaseEntity entity) { + final IChunk chunk = getOrCreateChunk(location.getBlockX() >> 4, location.getBlockZ() >> 4); + CompoundTag tag = entity.getNbtData(); + Map map = ReflectionUtils.getMap(tag.getValue()); + map.put("Id", new StringTag(entity.getType().getName())); + ListTag pos = (ListTag) map.get("Pos"); + 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)); + } + chunk.setEntity(tag); + return null; + } } diff --git a/worldedit-core/src/main/java/com/boydti/fawe/util/ReflectionUtils.java b/worldedit-core/src/main/java/com/boydti/fawe/util/ReflectionUtils.java index 97505fd85..dfd78f0aa 100644 --- a/worldedit-core/src/main/java/com/boydti/fawe/util/ReflectionUtils.java +++ b/worldedit-core/src/main/java/com/boydti/fawe/util/ReflectionUtils.java @@ -88,7 +88,21 @@ public class ReflectionUtils { return list; } } - + private static Class UNMODIFIABLE_MAP = Collections.unmodifiableMap(Collections.EMPTY_MAP).getClass(); + + public static Map getMap(Map map) { + try { + Class clazz = map.getClass(); + if (clazz != UNMODIFIABLE_MAP) return map; + Field m = clazz.getDeclaredField("m"); + m.setAccessible(true); + return (Map) m.get(map); + } catch (Throwable e) { + e.printStackTrace(); + return map; + } + } + public static Object getHandle(Object wrapper) { final Method getHandle = makeMethod(wrapper.getClass(), "getHandle"); return callMethod(getHandle, wrapper); diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/EditSession.java b/worldedit-core/src/main/java/com/sk89q/worldedit/EditSession.java index ccfaa0ccc..b8e2153a1 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/EditSession.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/EditSession.java @@ -46,6 +46,7 @@ import com.boydti.fawe.util.ExtentTraverser; import com.boydti.fawe.util.MaskTraverser; import com.boydti.fawe.util.MathMan; import com.boydti.fawe.util.TaskManager; +import com.sk89q.worldedit.entity.BaseEntity; import com.sk89q.worldedit.entity.Entity; import com.sk89q.worldedit.entity.Player; import com.sk89q.worldedit.event.extent.EditSessionEvent; @@ -118,6 +119,7 @@ import com.sk89q.worldedit.regions.shape.RegionShape; import com.sk89q.worldedit.regions.shape.WorldEditExpressionEnvironment; import com.sk89q.worldedit.util.Countable; import com.sk89q.worldedit.util.Direction; +import com.sk89q.worldedit.util.Location; import com.sk89q.worldedit.util.SideEffectSet; import com.sk89q.worldedit.util.TreeGenerator; import com.sk89q.worldedit.util.eventbus.EventBus; @@ -3059,5 +3061,14 @@ public class EditSession extends PassthroughExtent implements AutoCloseable { @Override public List getEntities(Region region) { return world.getEntities(region); - } + } + + @Override + public Entity createEntity(Location location, BaseEntity entity){ + try { + return this.getExtent().createEntity(location, entity); + } catch (WorldEditException e) { + throw new RuntimeException("Unexpected exception", e); + } + } } diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/extent/clipboard/BlockArrayClipboard.java b/worldedit-core/src/main/java/com/sk89q/worldedit/extent/clipboard/BlockArrayClipboard.java index 9817dd235..f1716311c 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/extent/clipboard/BlockArrayClipboard.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/extent/clipboard/BlockArrayClipboard.java @@ -200,6 +200,11 @@ public class BlockArrayClipboard implements Clipboard { return getParent().getEntities(region); } + @Override + public List getEntities() { + return getParent().getEntities(); + } + @Override @Nullable public Entity createEntity(Location location, BaseEntity entity) {