Permalink
Browse files

Implement the new entity API.

  • Loading branch information...
sk89q committed Jul 15, 2014
1 parent 8acdf29 commit 72ae498b1431efe0783e60cc77df3376934561ab
@@ -35,7 +35,11 @@
import com.sk89q.jnbt.Tag;
import com.sk89q.worldedit.blocks.BaseBlock;
import com.sk89q.worldedit.bukkit.adapter.BukkitImplAdapter;
import com.sk89q.worldedit.entity.BaseEntity;
import com.sk89q.worldedit.internal.Constants;
import net.minecraft.server.v1_7_R2.Block;
import net.minecraft.server.v1_7_R2.Entity;
import net.minecraft.server.v1_7_R2.EntityTypes;
import net.minecraft.server.v1_7_R2.NBTBase;
import net.minecraft.server.v1_7_R2.NBTTagByte;
import net.minecraft.server.v1_7_R2.NBTTagByteArray;
@@ -50,11 +54,16 @@
import net.minecraft.server.v1_7_R2.NBTTagShort;
import net.minecraft.server.v1_7_R2.NBTTagString;
import net.minecraft.server.v1_7_R2.TileEntity;
import net.minecraft.server.v1_7_R2.World;
import net.minecraft.server.v1_7_R2.WorldServer;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.craftbukkit.v1_7_R2.CraftServer;
import org.bukkit.craftbukkit.v1_7_R2.CraftWorld;
import org.bukkit.craftbukkit.v1_7_R2.entity.CraftEntity;
import org.bukkit.event.entity.CreatureSpawnEvent.SpawnReason;
import javax.annotation.Nullable;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
@@ -102,25 +111,68 @@ private static Block getBlockFromTypeId(int type) {
}
/**
* Read the given {@code tag} into the given tile entity.
* Read the given NBT data into the given tile entity.
*
* @param tileEntity the tile entity
* @param tag the tag
*/
private static void setTileEntityTag(TileEntity tileEntity, NBTTagCompound tag) {
private static void readTagIntoTileEntity(NBTTagCompound tag, TileEntity tileEntity) {
tileEntity.a(tag);
}
/**
* Read the given {@code tag} from the given tile entity.
* Write the tile entity's NBT data to the given tag.
*
* @param tileEntity the tile entity
* @param tag the tag
*/
private static void getTileEntityTag(TileEntity tileEntity, NBTTagCompound tag) {
private static void readTileEntityIntoTag(TileEntity tileEntity, NBTTagCompound tag) {
tileEntity.b(tag);
}
/**
* Get the ID string of the given entity.
*
* @param entity the entity
* @return the entity ID or null if one is not known
*/
@Nullable
private static String getEntityId(Entity entity) {
return EntityTypes.b(entity);
}
/**
* Create an entity using the given entity ID.
*
* @param id the entity ID
* @param world the world
* @return an entity or null
*/
@Nullable
private static Entity createEntityFromId(String id, World world) {
return EntityTypes.createEntityByName(id, world);
}
/**
* Write the given NBT data into the given entity.
*
* @param entity the entity
* @param tag the tag
*/
private static void readTagIntoEntity(NBTTagCompound tag, Entity entity) {
entity.f(tag);
}
/**
* Write the entity's NBT data to the given tag.
*
* @param entity the entity
* @param tag the tag
*/
private static void readEntityIntoTag(Entity entity, NBTTagCompound tag) {
entity.e(tag);
}
// ------------------------------------------------------------------------
// Code that is less likely to break
// ------------------------------------------------------------------------
@@ -142,7 +194,7 @@ public BaseBlock getBlock(Location location) {
TileEntity te = craftWorld.getHandle().getTileEntity(x, y, z);
if (te != null) {
NBTTagCompound tag = new NBTTagCompound();
getTileEntityTag(te, tag); // Load data
readTileEntityIntoTag(te, tag); // Load data
block.setNbtData((CompoundTag) toNative(tag));
}
@@ -173,7 +225,7 @@ public boolean setBlock(Location location, BaseBlock block, boolean notifyAndLig
tag.set("x", new NBTTagInt(x));
tag.set("y", new NBTTagInt(y));
tag.set("z", new NBTTagInt(z));
setTileEntityTag(tileEntity, tag); // Load data
readTagIntoTileEntity(tag, tileEntity); // Load data
}
}
@@ -189,6 +241,54 @@ public boolean setBlock(Location location, BaseBlock block, boolean notifyAndLig
return changed;
}
@Override
public BaseEntity getEntity(org.bukkit.entity.Entity entity) {
checkNotNull(entity);
CraftEntity craftEntity = ((CraftEntity) entity);
Entity mcEntity = craftEntity.getHandle();
String id = getEntityId(mcEntity);
if (id != null) {
NBTTagCompound tag = new NBTTagCompound();
readEntityIntoTag(mcEntity, tag);
return new BaseEntity(id, (CompoundTag) toNative(tag));
} else {
return null;
}
}
@Nullable
@Override
public org.bukkit.entity.Entity createEntity(Location location, BaseEntity state) {
checkNotNull(location);
checkNotNull(state);
CraftWorld craftWorld = ((CraftWorld) location.getWorld());
WorldServer worldServer = craftWorld.getHandle();
Entity createdEntity = createEntityFromId(state.getTypeId(), craftWorld.getHandle());
if (createdEntity != null) {
CompoundTag nativeTag = state.getNbtData();
if (nativeTag != null) {
NBTTagCompound tag = (NBTTagCompound) fromNative(nativeTag);
for (String name : Constants.NO_COPY_ENTITY_NBT_FIELDS) {
tag.remove(name);
}
readTagIntoEntity(tag, createdEntity);
}
createdEntity.setLocation(location.getX(), location.getY(), location.getZ(), location.getYaw(), location.getPitch());
worldServer.addEntity(createdEntity, SpawnReason.CUSTOM);
return createdEntity.getBukkitEntity();
} else {
return null;
}
}
/**
* Get the tag name, which is used for a workaround for WorldEdit's
* NBT library, JNBT.
@@ -35,7 +35,11 @@
import com.sk89q.jnbt.Tag;
import com.sk89q.worldedit.blocks.BaseBlock;
import com.sk89q.worldedit.bukkit.adapter.BukkitImplAdapter;
import com.sk89q.worldedit.entity.BaseEntity;
import com.sk89q.worldedit.internal.Constants;
import net.minecraft.server.v1_7_R3.Block;
import net.minecraft.server.v1_7_R3.Entity;
import net.minecraft.server.v1_7_R3.EntityTypes;
import net.minecraft.server.v1_7_R3.NBTBase;
import net.minecraft.server.v1_7_R3.NBTTagByte;
import net.minecraft.server.v1_7_R3.NBTTagByteArray;
@@ -50,11 +54,16 @@
import net.minecraft.server.v1_7_R3.NBTTagShort;
import net.minecraft.server.v1_7_R3.NBTTagString;
import net.minecraft.server.v1_7_R3.TileEntity;
import net.minecraft.server.v1_7_R3.World;
import net.minecraft.server.v1_7_R3.WorldServer;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.craftbukkit.v1_7_R3.CraftServer;
import org.bukkit.craftbukkit.v1_7_R3.CraftWorld;
import org.bukkit.craftbukkit.v1_7_R3.entity.CraftEntity;
import org.bukkit.event.entity.CreatureSpawnEvent.SpawnReason;
import javax.annotation.Nullable;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
@@ -102,25 +111,68 @@ private static Block getBlockFromTypeId(int type) {
}
/**
* Read the given {@code tag} into the given tile entity.
* Read the given NBT data into the given tile entity.
*
* @param tileEntity the tile entity
* @param tag the tag
*/
private static void setTileEntityTag(TileEntity tileEntity, NBTTagCompound tag) {
private static void readTagIntoTileEntity(NBTTagCompound tag, TileEntity tileEntity) {
tileEntity.a(tag);
}
/**
* Read the given {@code tag} from the given tile entity.
* Write the tile entity's NBT data to the given tag.
*
* @param tileEntity the tile entity
* @param tag the tag
*/
private static void getTileEntityTag(TileEntity tileEntity, NBTTagCompound tag) {
private static void readTileEntityIntoTag(TileEntity tileEntity, NBTTagCompound tag) {
tileEntity.b(tag);
}
/**
* Get the ID string of the given entity.
*
* @param entity the entity
* @return the entity ID or null if one is not known
*/
@Nullable
private static String getEntityId(Entity entity) {
return EntityTypes.b(entity);
}
/**
* Create an entity using the given entity ID.
*
* @param id the entity ID
* @param world the world
* @return an entity or null
*/
@Nullable
private static Entity createEntityFromId(String id, World world) {
return EntityTypes.createEntityByName(id, world);
}
/**
* Write the given NBT data into the given entity.
*
* @param entity the entity
* @param tag the tag
*/
private static void readTagIntoEntity(NBTTagCompound tag, Entity entity) {
entity.f(tag);
}
/**
* Write the entity's NBT data to the given tag.
*
* @param entity the entity
* @param tag the tag
*/
private static void readEntityIntoTag(Entity entity, NBTTagCompound tag) {
entity.e(tag);
}
// ------------------------------------------------------------------------
// Code that is less likely to break
// ------------------------------------------------------------------------
@@ -142,7 +194,7 @@ public BaseBlock getBlock(Location location) {
TileEntity te = craftWorld.getHandle().getTileEntity(x, y, z);
if (te != null) {
NBTTagCompound tag = new NBTTagCompound();
getTileEntityTag(te, tag); // Load data
readTileEntityIntoTag(te, tag); // Load data
block.setNbtData((CompoundTag) toNative(tag));
}
@@ -173,7 +225,7 @@ public boolean setBlock(Location location, BaseBlock block, boolean notifyAndLig
tag.set("x", new NBTTagInt(x));
tag.set("y", new NBTTagInt(y));
tag.set("z", new NBTTagInt(z));
setTileEntityTag(tileEntity, tag); // Load data
readTagIntoTileEntity(tag, tileEntity); // Load data
}
}
@@ -189,6 +241,54 @@ public boolean setBlock(Location location, BaseBlock block, boolean notifyAndLig
return changed;
}
@Override
public BaseEntity getEntity(org.bukkit.entity.Entity entity) {
checkNotNull(entity);
CraftEntity craftEntity = ((CraftEntity) entity);
Entity mcEntity = craftEntity.getHandle();
String id = getEntityId(mcEntity);
if (id != null) {
NBTTagCompound tag = new NBTTagCompound();
readEntityIntoTag(mcEntity, tag);
return new BaseEntity(id, (CompoundTag) toNative(tag));
} else {
return null;
}
}
@Nullable
@Override
public org.bukkit.entity.Entity createEntity(Location location, BaseEntity state) {
checkNotNull(location);
checkNotNull(state);
CraftWorld craftWorld = ((CraftWorld) location.getWorld());
WorldServer worldServer = craftWorld.getHandle();
Entity createdEntity = createEntityFromId(state.getTypeId(), craftWorld.getHandle());
if (createdEntity != null) {
CompoundTag nativeTag = state.getNbtData();
if (nativeTag != null) {
NBTTagCompound tag = (NBTTagCompound) fromNative(nativeTag);
for (String name : Constants.NO_COPY_ENTITY_NBT_FIELDS) {
tag.remove(name);
}
readTagIntoEntity(tag, createdEntity);
}
createdEntity.setLocation(location.getX(), location.getY(), location.getZ(), location.getYaw(), location.getPitch());
worldServer.addEntity(createdEntity, SpawnReason.CUSTOM);
return createdEntity.getBukkitEntity();
} else {
return null;
}
}
/**
* Get the tag name, which is used for a workaround for WorldEdit's
* NBT library, JNBT.
Oops, something went wrong.

0 comments on commit 72ae498

Please sign in to comment.