From 15b173dca614d6b42b11f14af22eb0dd975dc141 Mon Sep 17 00:00:00 2001 From: sk89q Date: Sat, 19 Jul 2014 12:19:27 -0700 Subject: [PATCH] Remove old Bukkit entity adapter classes. (reverted from commit b461f44db809674123639378355f245d5d9c9344) CraftBook uses some of these classes. --- .../sk89q/worldedit/bukkit/BukkitUtil.java | 39 +++++-- .../worldedit/bukkit/entity/BukkitEntity.java | 51 +++++++++ .../worldedit/bukkit/entity/BukkitExpOrb.java | 50 +++++++++ .../worldedit/bukkit/entity/BukkitItem.java | 44 ++++++++ .../bukkit/entity/BukkitPainting.java | 105 ++++++++++++++++++ 5 files changed, 281 insertions(+), 8 deletions(-) create mode 100644 src/bukkit/java/com/sk89q/worldedit/bukkit/entity/BukkitEntity.java create mode 100644 src/bukkit/java/com/sk89q/worldedit/bukkit/entity/BukkitExpOrb.java create mode 100644 src/bukkit/java/com/sk89q/worldedit/bukkit/entity/BukkitItem.java create mode 100644 src/bukkit/java/com/sk89q/worldedit/bukkit/entity/BukkitPainting.java diff --git a/src/bukkit/java/com/sk89q/worldedit/bukkit/BukkitUtil.java b/src/bukkit/java/com/sk89q/worldedit/bukkit/BukkitUtil.java index 54db3d22f..d4cb72a6d 100644 --- a/src/bukkit/java/com/sk89q/worldedit/bukkit/BukkitUtil.java +++ b/src/bukkit/java/com/sk89q/worldedit/bukkit/BukkitUtil.java @@ -19,14 +19,10 @@ package com.sk89q.worldedit.bukkit; -import com.sk89q.worldedit.BlockVector; -import com.sk89q.worldedit.BlockWorldVector; -import com.sk89q.worldedit.LocalWorld; -import com.sk89q.worldedit.Location; +import java.util.List; + import com.sk89q.worldedit.NotABlockException; -import com.sk89q.worldedit.Vector; import com.sk89q.worldedit.WorldEditException; -import com.sk89q.worldedit.WorldVector; import com.sk89q.worldedit.blocks.BaseBlock; import com.sk89q.worldedit.blocks.BlockID; import com.sk89q.worldedit.blocks.BlockType; @@ -37,12 +33,25 @@ import org.bukkit.Server; import org.bukkit.World; import org.bukkit.block.Block; import org.bukkit.block.BlockFace; +import org.bukkit.entity.Entity; +import org.bukkit.entity.ExperienceOrb; +import org.bukkit.entity.Item; +import org.bukkit.entity.Painting; import org.bukkit.entity.Player; + +import com.sk89q.worldedit.BlockVector; +import com.sk89q.worldedit.BlockWorldVector; +import com.sk89q.worldedit.LocalWorld; +import com.sk89q.worldedit.Location; +import com.sk89q.worldedit.Vector; +import com.sk89q.worldedit.WorldVector; +import com.sk89q.worldedit.bukkit.entity.BukkitEntity; +import com.sk89q.worldedit.bukkit.entity.BukkitExpOrb; +import com.sk89q.worldedit.bukkit.entity.BukkitItem; +import com.sk89q.worldedit.bukkit.entity.BukkitPainting; import org.bukkit.inventory.ItemStack; import org.bukkit.material.Dye; -import java.util.List; - public final class BukkitUtil { private BukkitUtil() { @@ -142,6 +151,20 @@ public final class BukkitUtil { return ((BukkitWorld) world).getWorld(); } + public static BukkitEntity toLocalEntity(Entity e) { + switch (e.getType()) { + case EXPERIENCE_ORB: + return new BukkitExpOrb(toLocation(e.getLocation()), e.getUniqueId(), ((ExperienceOrb)e).getExperience()); + case PAINTING: + Painting paint = (Painting) e; + return new BukkitPainting(toLocation(e.getLocation()), paint.getArt(), paint.getFacing(), e.getUniqueId()); + case DROPPED_ITEM: + return new BukkitItem(toLocation(e.getLocation()), ((Item)e).getItemStack(), e.getUniqueId()); + default: + return new BukkitEntity(toLocation(e.getLocation()), e.getType(), e.getUniqueId()); + } + } + public static BaseBlock toBlock(LocalWorld world, ItemStack itemStack) throws WorldEditException { final int typeId = itemStack.getTypeId(); diff --git a/src/bukkit/java/com/sk89q/worldedit/bukkit/entity/BukkitEntity.java b/src/bukkit/java/com/sk89q/worldedit/bukkit/entity/BukkitEntity.java new file mode 100644 index 000000000..dd0adfb83 --- /dev/null +++ b/src/bukkit/java/com/sk89q/worldedit/bukkit/entity/BukkitEntity.java @@ -0,0 +1,51 @@ +/* + * WorldEdit, a Minecraft world manipulation toolkit + * Copyright (C) sk89q + * Copyright (C) WorldEdit team and contributors + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as published by the + * Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License + * for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program. If not, see . + */ + +package com.sk89q.worldedit.bukkit.entity; + +import com.sk89q.worldedit.LocalEntity; +import com.sk89q.worldedit.Location; +import com.sk89q.worldedit.bukkit.BukkitUtil; +import org.bukkit.entity.EntityType; + +import java.util.UUID; + +/** + * @author zml2008 + */ +public class BukkitEntity extends LocalEntity { + private final EntityType type; + private final UUID entityId; + + public BukkitEntity(Location loc, EntityType type, UUID entityId) { + super(loc); + this.type = type; + this.entityId = entityId; + } + + public UUID getEntityId() { + return entityId; + } + + @Override + public boolean spawn(Location weLoc) { + org.bukkit.Location loc = BukkitUtil.toLocation(weLoc); + return loc.getWorld().spawn(loc, type.getEntityClass()) != null; + } +} diff --git a/src/bukkit/java/com/sk89q/worldedit/bukkit/entity/BukkitExpOrb.java b/src/bukkit/java/com/sk89q/worldedit/bukkit/entity/BukkitExpOrb.java new file mode 100644 index 000000000..8c2d1be3f --- /dev/null +++ b/src/bukkit/java/com/sk89q/worldedit/bukkit/entity/BukkitExpOrb.java @@ -0,0 +1,50 @@ +/* + * WorldEdit, a Minecraft world manipulation toolkit + * Copyright (C) sk89q + * Copyright (C) WorldEdit team and contributors + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as published by the + * Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License + * for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program. If not, see . + */ + +package com.sk89q.worldedit.bukkit.entity; + +import com.sk89q.worldedit.Location; +import com.sk89q.worldedit.bukkit.BukkitUtil; +import org.bukkit.entity.EntityType; +import org.bukkit.entity.ExperienceOrb; + +import java.util.UUID; + +/** + * @author zml2008 + */ +public class BukkitExpOrb extends BukkitEntity { + private final int amount; + + public BukkitExpOrb(Location loc, UUID entityId, int amount) { + super(loc, EntityType.EXPERIENCE_ORB, entityId); + this.amount = amount; + } + + @Override + public boolean spawn(Location weLoc) { + org.bukkit.Location loc = BukkitUtil.toLocation(weLoc); + ExperienceOrb orb = loc.getWorld().spawn(loc, ExperienceOrb.class); + if (orb != null) { + orb.setExperience(amount); + return true; + } + return false; + } +} diff --git a/src/bukkit/java/com/sk89q/worldedit/bukkit/entity/BukkitItem.java b/src/bukkit/java/com/sk89q/worldedit/bukkit/entity/BukkitItem.java new file mode 100644 index 000000000..208db6933 --- /dev/null +++ b/src/bukkit/java/com/sk89q/worldedit/bukkit/entity/BukkitItem.java @@ -0,0 +1,44 @@ +/* + * WorldEdit, a Minecraft world manipulation toolkit + * Copyright (C) sk89q + * Copyright (C) WorldEdit team and contributors + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as published by the + * Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License + * for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program. If not, see . + */ + +package com.sk89q.worldedit.bukkit.entity; + +import com.sk89q.worldedit.Location; +import com.sk89q.worldedit.bukkit.BukkitUtil; +import org.bukkit.entity.EntityType; +import org.bukkit.inventory.ItemStack; + +import java.util.UUID; + +/** + * @author zml2008 + */ +public class BukkitItem extends BukkitEntity { + private final ItemStack stack; + public BukkitItem(Location loc, ItemStack stack, UUID entityId) { + super(loc, EntityType.DROPPED_ITEM, entityId); + this.stack = stack; + } + + @Override + public boolean spawn(Location weLoc) { + org.bukkit.Location loc = BukkitUtil.toLocation(weLoc); + return loc.getWorld().dropItem(loc, stack) != null; + } +} diff --git a/src/bukkit/java/com/sk89q/worldedit/bukkit/entity/BukkitPainting.java b/src/bukkit/java/com/sk89q/worldedit/bukkit/entity/BukkitPainting.java new file mode 100644 index 000000000..0db7c8304 --- /dev/null +++ b/src/bukkit/java/com/sk89q/worldedit/bukkit/entity/BukkitPainting.java @@ -0,0 +1,105 @@ +/* + * WorldEdit, a Minecraft world manipulation toolkit + * Copyright (C) sk89q + * Copyright (C) WorldEdit team and contributors + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as published by the + * Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License + * for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program. If not, see . + */ + +package com.sk89q.worldedit.bukkit.entity; + +import com.sk89q.worldedit.Location; +import com.sk89q.worldedit.bukkit.BukkitUtil; +import org.bukkit.Art; +import org.bukkit.Bukkit; +import org.bukkit.block.BlockFace; +import org.bukkit.entity.EntityType; +import org.bukkit.entity.Painting; + +import java.util.ArrayDeque; +import java.util.Deque; +import java.util.UUID; + +/** + * @author zml2008 + */ +public class BukkitPainting extends BukkitEntity { + private static int spawnTask = -1; + private static final Deque spawnQueue = new ArrayDeque(); + + private class QueuedPaintingSpawn { + private final Location weLoc; + + public QueuedPaintingSpawn(Location weLoc) { + this.weLoc = weLoc; + } + + public void spawn() { + spawnRaw(weLoc); + } + } + private static class PaintingSpawnRunnable implements Runnable { + @Override + public void run() { + synchronized (spawnQueue) { + QueuedPaintingSpawn spawn; + while ((spawn = spawnQueue.poll()) != null) { + try { + spawn.spawn(); + } catch (Throwable t) { + t.printStackTrace(); + continue; + } + } + spawnTask = -1; + } + } + } + + private final Art art; + private final BlockFace facingDirection; + public BukkitPainting(Location loc, Art art, BlockFace facingDirection, UUID entityId) { + super(loc, EntityType.PAINTING, entityId); + this.art = art; + this.facingDirection = facingDirection; + } + + /** + * Queue the painting to be spawned at the specified location. + * This operation is delayed so that the block changes that may be applied can be applied before the painting spawn is attempted. + * + * @param weLoc The WorldEdit location + * @return Whether the spawn as successful + */ + public boolean spawn(Location weLoc) { + synchronized (spawnQueue) { + spawnQueue.add(new QueuedPaintingSpawn(weLoc)); + if (spawnTask == -1) { + spawnTask = Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(Bukkit.getServer().getPluginManager().getPlugin("WorldEdit"), new PaintingSpawnRunnable(), 1L); + } + } + return true; + } + + public boolean spawnRaw(Location weLoc) { + org.bukkit.Location loc = BukkitUtil.toLocation(weLoc); + Painting paint = loc.getWorld().spawn(loc, Painting.class); + if (paint != null) { + paint.setFacingDirection(facingDirection, true); + paint.setArt(art, true); + return true; + } + return false; + } +}