diff --git a/NMS/NMS-API/src/com/songoda/core/nms/nbt/NBTCompound.java b/NMS/NMS-API/src/com/songoda/core/nms/nbt/NBTCompound.java index cccb4e11..b80f28fb 100644 --- a/NMS/NMS-API/src/com/songoda/core/nms/nbt/NBTCompound.java +++ b/NMS/NMS-API/src/com/songoda/core/nms/nbt/NBTCompound.java @@ -22,4 +22,10 @@ public interface NBTCompound { NBTObject getNBTObject(String tag); + byte[] serialize(String... exclusions); + + void deSerialize(byte[] serialized); + + void addExtras(); + } diff --git a/NMS/NMS-API/src/com/songoda/core/nms/nbt/NBTCore.java b/NMS/NMS-API/src/com/songoda/core/nms/nbt/NBTCore.java index 090c46e2..87ef0bb3 100644 --- a/NMS/NMS-API/src/com/songoda/core/nms/nbt/NBTCore.java +++ b/NMS/NMS-API/src/com/songoda/core/nms/nbt/NBTCore.java @@ -1,11 +1,16 @@ package com.songoda.core.nms.nbt; +import org.bukkit.entity.Entity; import org.bukkit.inventory.ItemStack; public interface NBTCore { NBTItem of(ItemStack item); - NBTCompound newCompound(); + NBTItem newItem(); + + NBTEntity of(Entity entity); + + NBTEntity newEntity(); } diff --git a/NMS/NMS-API/src/com/songoda/core/nms/nbt/NBTEntity.java b/NMS/NMS-API/src/com/songoda/core/nms/nbt/NBTEntity.java new file mode 100644 index 00000000..88a032e9 --- /dev/null +++ b/NMS/NMS-API/src/com/songoda/core/nms/nbt/NBTEntity.java @@ -0,0 +1,12 @@ +package com.songoda.core.nms.nbt; + +import org.bukkit.Location; +import org.bukkit.entity.Entity; + +public interface NBTEntity extends NBTCompound { + + Entity spawn(Location location); + + Entity reSpawn(Location location); + +} diff --git a/NMS/NMS-v1_10_R1/src/com/songoda/core/nms/v1_10_R1/nbt/NBTCompoundImpl.java b/NMS/NMS-v1_10_R1/src/com/songoda/core/nms/v1_10_R1/nbt/NBTCompoundImpl.java index 362c104a..feb6829c 100644 --- a/NMS/NMS-v1_10_R1/src/com/songoda/core/nms/v1_10_R1/nbt/NBTCompoundImpl.java +++ b/NMS/NMS-v1_10_R1/src/com/songoda/core/nms/v1_10_R1/nbt/NBTCompoundImpl.java @@ -2,9 +2,12 @@ package com.songoda.core.nms.v1_10_R1.nbt; import com.songoda.core.nms.nbt.NBTCompound; import com.songoda.core.nms.nbt.NBTObject; +import net.minecraft.server.v1_10_R1.NBTCompressedStreamTools; import net.minecraft.server.v1_10_R1.NBTTagCompound; -public class NBTCompoundImpl implements NBTCompound { +import java.io.*; + +public abstract class NBTCompoundImpl implements NBTCompound { protected NBTTagCompound compound; @@ -74,4 +77,33 @@ public class NBTCompoundImpl implements NBTCompound { return new NBTObjectImpl(compound, tag); } + @Override + public byte[] serialize(String... exclusions) { + try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); + ObjectOutputStream dataOutput = new ObjectOutputStream(outputStream)) { + addExtras(); + NBTTagCompound compound = this.compound.g(); // Changed in 1.12 + + for (String exclusion : exclusions) + compound.remove(exclusion); + + NBTCompressedStreamTools.a(compound, (OutputStream) dataOutput); + + return outputStream.toByteArray(); + } catch (Exception e) { + e.printStackTrace(); + } + return null; + } + + @Override + public void deSerialize(byte[] serialized) { + try (ByteArrayInputStream inputStream = new ByteArrayInputStream(serialized); + ObjectInputStream dataInput = new ObjectInputStream(inputStream)) { + compound = NBTCompressedStreamTools.a((InputStream) dataInput); + } catch (Exception e) { + e.printStackTrace(); + } + } + } diff --git a/NMS/NMS-v1_10_R1/src/com/songoda/core/nms/v1_10_R1/nbt/NBTCoreImpl.java b/NMS/NMS-v1_10_R1/src/com/songoda/core/nms/v1_10_R1/nbt/NBTCoreImpl.java index 21aab0f6..c167d743 100644 --- a/NMS/NMS-v1_10_R1/src/com/songoda/core/nms/v1_10_R1/nbt/NBTCoreImpl.java +++ b/NMS/NMS-v1_10_R1/src/com/songoda/core/nms/v1_10_R1/nbt/NBTCoreImpl.java @@ -1,9 +1,12 @@ package com.songoda.core.nms.v1_10_R1.nbt; -import com.songoda.core.nms.nbt.NBTCompound; import com.songoda.core.nms.nbt.NBTCore; +import com.songoda.core.nms.nbt.NBTEntity; import com.songoda.core.nms.nbt.NBTItem; +import net.minecraft.server.v1_10_R1.NBTTagCompound; +import org.bukkit.craftbukkit.v1_10_R1.entity.CraftEntity; import org.bukkit.craftbukkit.v1_10_R1.inventory.CraftItemStack; +import org.bukkit.entity.Entity; import org.bukkit.inventory.ItemStack; public class NBTCoreImpl implements NBTCore { @@ -14,8 +17,21 @@ public class NBTCoreImpl implements NBTCore { } @Override - public NBTCompound newCompound() { - return new NBTCompoundImpl(); + public NBTItem newItem() { + return new NBTItemImpl(null); + } + + @Override + public NBTEntity of(Entity entity) { + net.minecraft.server.v1_10_R1.Entity nmsEntity = ((CraftEntity) entity).getHandle(); + NBTTagCompound nbt = new NBTTagCompound(); + nmsEntity.e(nbt); // Method name changed in 1.11 + return new NBTEntityImpl(nbt, nmsEntity); + } + + @Override + public NBTEntity newEntity() { + return new NBTEntityImpl(new NBTTagCompound(), null); } } diff --git a/NMS/NMS-v1_10_R1/src/com/songoda/core/nms/v1_10_R1/nbt/NBTEntityImpl.java b/NMS/NMS-v1_10_R1/src/com/songoda/core/nms/v1_10_R1/nbt/NBTEntityImpl.java new file mode 100644 index 00000000..c2343100 --- /dev/null +++ b/NMS/NMS-v1_10_R1/src/com/songoda/core/nms/v1_10_R1/nbt/NBTEntityImpl.java @@ -0,0 +1,52 @@ +package com.songoda.core.nms.v1_10_R1.nbt; + +import com.songoda.core.nms.nbt.NBTEntity; +import net.minecraft.server.v1_10_R1.*; +import org.bukkit.Location; +import org.bukkit.craftbukkit.v1_10_R1.CraftWorld; +import org.bukkit.event.entity.CreatureSpawnEvent; + +public class NBTEntityImpl extends NBTCompoundImpl implements NBTEntity { + + private Entity nmsEntity; + + public NBTEntityImpl(NBTTagCompound entityNBT, Entity nmsEntity) { + super(entityNBT); + this.nmsEntity = nmsEntity; + } + + @Override + public org.bukkit.entity.Entity spawn(Location location) { + String entityType = getNBTObject("entity_type").asString(); + + Entity spawned = ItemMonsterEgg.spawnCreature( // Changed since 1.14 + ((CraftWorld) location.getWorld()).getHandle(), + entityType, // Parameter simplified in 1.10 + location.getBlockX(), location.getBlockY(), location.getBlockZ(), + CreatureSpawnEvent.SpawnReason.DEFAULT + ); + + if (spawned != null) { + spawned.f(compound); // This changed from 1.16.1 + org.bukkit.entity.Entity entity = spawned.getBukkitEntity(); + spawned.setLocation(location.getX(), location.getY(), location.getZ(), + location.getPitch(), location.getYaw()); + nmsEntity = spawned; + return entity; + } + return null; + } + + @Override + public org.bukkit.entity.Entity reSpawn(Location location) { + nmsEntity.dead = true; + return spawn(location); + } + + @Override + public void addExtras() { + String key = EntityTypes.b(nmsEntity); // Changed in 1.12 + if (key != null) + compound.setString("entity_type", key); // Changed in 1.13 + } +} diff --git a/NMS/NMS-v1_10_R1/src/com/songoda/core/nms/v1_10_R1/nbt/NBTItemImpl.java b/NMS/NMS-v1_10_R1/src/com/songoda/core/nms/v1_10_R1/nbt/NBTItemImpl.java index 64bab8a0..f68439e0 100644 --- a/NMS/NMS-v1_10_R1/src/com/songoda/core/nms/v1_10_R1/nbt/NBTItemImpl.java +++ b/NMS/NMS-v1_10_R1/src/com/songoda/core/nms/v1_10_R1/nbt/NBTItemImpl.java @@ -2,6 +2,7 @@ package com.songoda.core.nms.v1_10_R1.nbt; import com.songoda.core.nms.nbt.NBTItem; import net.minecraft.server.v1_10_R1.NBTTagCompound; +import org.bukkit.Material; import org.bukkit.craftbukkit.v1_10_R1.inventory.CraftItemStack; import org.bukkit.inventory.ItemStack; @@ -10,11 +11,21 @@ public class NBTItemImpl extends NBTCompoundImpl implements NBTItem { private net.minecraft.server.v1_10_R1.ItemStack nmsItem; public NBTItemImpl(net.minecraft.server.v1_10_R1.ItemStack nmsItem) { - super(nmsItem.hasTag() ? nmsItem.getTag() : new NBTTagCompound()); + super(nmsItem != null && nmsItem.hasTag() ? nmsItem.getTag() : new NBTTagCompound()); this.nmsItem = nmsItem; } public ItemStack finish() { - return CraftItemStack.asBukkitCopy(nmsItem); + if (nmsItem == null) { + net.minecraft.server.v1_10_R1.ItemStack itemStack = CraftItemStack.asNMSCopy(new ItemStack(Material.STONE)); + itemStack.setTag(compound); + return CraftItemStack.asBukkitCopy(nmsItem); + } else { + return CraftItemStack.asBukkitCopy(nmsItem); + } + } + + @Override + public void addExtras() { } } diff --git a/NMS/NMS-v1_11_R1/src/com/songoda/core/nms/v1_11_R1/nbt/NBTCompoundImpl.java b/NMS/NMS-v1_11_R1/src/com/songoda/core/nms/v1_11_R1/nbt/NBTCompoundImpl.java index 0a88e0ce..051fac0b 100644 --- a/NMS/NMS-v1_11_R1/src/com/songoda/core/nms/v1_11_R1/nbt/NBTCompoundImpl.java +++ b/NMS/NMS-v1_11_R1/src/com/songoda/core/nms/v1_11_R1/nbt/NBTCompoundImpl.java @@ -2,9 +2,12 @@ package com.songoda.core.nms.v1_11_R1.nbt; import com.songoda.core.nms.nbt.NBTCompound; import com.songoda.core.nms.nbt.NBTObject; +import net.minecraft.server.v1_11_R1.NBTCompressedStreamTools; import net.minecraft.server.v1_11_R1.NBTTagCompound; -public class NBTCompoundImpl implements NBTCompound { +import java.io.*; + +public abstract class NBTCompoundImpl implements NBTCompound { protected NBTTagCompound compound; @@ -74,4 +77,33 @@ public class NBTCompoundImpl implements NBTCompound { return new NBTObjectImpl(compound, tag); } + @Override + public byte[] serialize(String... exclusions) { + try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); + ObjectOutputStream dataOutput = new ObjectOutputStream(outputStream)) { + addExtras(); + NBTTagCompound compound = this.compound.g(); // Changed in 1.12 + + for (String exclusion : exclusions) + compound.remove(exclusion); + + NBTCompressedStreamTools.a(compound, (OutputStream) dataOutput); + + return outputStream.toByteArray(); + } catch (Exception e) { + e.printStackTrace(); + } + return null; + } + + @Override + public void deSerialize(byte[] serialized) { + try (ByteArrayInputStream inputStream = new ByteArrayInputStream(serialized); + ObjectInputStream dataInput = new ObjectInputStream(inputStream)) { + compound = NBTCompressedStreamTools.a((InputStream) dataInput); + } catch (Exception e) { + e.printStackTrace(); + } + } + } diff --git a/NMS/NMS-v1_11_R1/src/com/songoda/core/nms/v1_11_R1/nbt/NBTCoreImpl.java b/NMS/NMS-v1_11_R1/src/com/songoda/core/nms/v1_11_R1/nbt/NBTCoreImpl.java index 1269a30e..af2d2bce 100644 --- a/NMS/NMS-v1_11_R1/src/com/songoda/core/nms/v1_11_R1/nbt/NBTCoreImpl.java +++ b/NMS/NMS-v1_11_R1/src/com/songoda/core/nms/v1_11_R1/nbt/NBTCoreImpl.java @@ -1,9 +1,12 @@ package com.songoda.core.nms.v1_11_R1.nbt; -import com.songoda.core.nms.nbt.NBTCompound; import com.songoda.core.nms.nbt.NBTCore; +import com.songoda.core.nms.nbt.NBTEntity; import com.songoda.core.nms.nbt.NBTItem; +import net.minecraft.server.v1_11_R1.NBTTagCompound; +import org.bukkit.craftbukkit.v1_11_R1.entity.CraftEntity; import org.bukkit.craftbukkit.v1_11_R1.inventory.CraftItemStack; +import org.bukkit.entity.Entity; import org.bukkit.inventory.ItemStack; public class NBTCoreImpl implements NBTCore { @@ -14,8 +17,21 @@ public class NBTCoreImpl implements NBTCore { } @Override - public NBTCompound newCompound() { - return new NBTCompoundImpl(); + public NBTItem newItem() { + return new NBTItemImpl(null); + } + + @Override + public NBTEntity of(Entity entity) { + net.minecraft.server.v1_11_R1.Entity nmsEntity = ((CraftEntity) entity).getHandle(); + NBTTagCompound nbt = new NBTTagCompound(); + nmsEntity.e(nbt); // Method name changed in 1.11 + return new NBTEntityImpl(nbt, nmsEntity); + } + + @Override + public NBTEntity newEntity() { + return new NBTEntityImpl(new NBTTagCompound(), null); } } diff --git a/NMS/NMS-v1_11_R1/src/com/songoda/core/nms/v1_11_R1/nbt/NBTEntityImpl.java b/NMS/NMS-v1_11_R1/src/com/songoda/core/nms/v1_11_R1/nbt/NBTEntityImpl.java new file mode 100644 index 00000000..fb07af67 --- /dev/null +++ b/NMS/NMS-v1_11_R1/src/com/songoda/core/nms/v1_11_R1/nbt/NBTEntityImpl.java @@ -0,0 +1,52 @@ +package com.songoda.core.nms.v1_11_R1.nbt; + +import com.songoda.core.nms.nbt.NBTEntity; +import net.minecraft.server.v1_11_R1.*; +import org.bukkit.Location; +import org.bukkit.craftbukkit.v1_11_R1.CraftWorld; +import org.bukkit.event.entity.CreatureSpawnEvent; + +public class NBTEntityImpl extends NBTCompoundImpl implements NBTEntity { + + private Entity nmsEntity; + + public NBTEntityImpl(NBTTagCompound entityNBT, Entity nmsEntity) { + super(entityNBT); + this.nmsEntity = nmsEntity; + } + + @Override + public org.bukkit.entity.Entity spawn(Location location) { + String entityType = getNBTObject("entity_type").asString(); + + Entity spawned = ItemMonsterEgg.spawnCreature( // Changed since 1.14 + ((CraftWorld) location.getWorld()).getHandle(), + new MinecraftKey(entityType), + location.getBlockX(), location.getBlockY(), location.getBlockZ(), + CreatureSpawnEvent.SpawnReason.DEFAULT + ); + + if (spawned != null) { + spawned.f(compound); // This changed from 1.16.1 + org.bukkit.entity.Entity entity = spawned.getBukkitEntity(); + spawned.setLocation(location.getX(), location.getY(), location.getZ(), + location.getPitch(), location.getYaw()); + nmsEntity = spawned; + return entity; + } + return null; + } + + @Override + public org.bukkit.entity.Entity reSpawn(Location location) { + nmsEntity.dead = true; + return spawn(location); + } + + @Override + public void addExtras() { + String key = EntityTypes.b(nmsEntity); // Changed in 1.12 + if (key != null) + compound.setString("entity_type", key); // Changed in 1.13 + } +} diff --git a/NMS/NMS-v1_11_R1/src/com/songoda/core/nms/v1_11_R1/nbt/NBTItemImpl.java b/NMS/NMS-v1_11_R1/src/com/songoda/core/nms/v1_11_R1/nbt/NBTItemImpl.java index e7be3924..f3efa771 100644 --- a/NMS/NMS-v1_11_R1/src/com/songoda/core/nms/v1_11_R1/nbt/NBTItemImpl.java +++ b/NMS/NMS-v1_11_R1/src/com/songoda/core/nms/v1_11_R1/nbt/NBTItemImpl.java @@ -2,6 +2,7 @@ package com.songoda.core.nms.v1_11_R1.nbt; import com.songoda.core.nms.nbt.NBTItem; import net.minecraft.server.v1_11_R1.NBTTagCompound; +import org.bukkit.Material; import org.bukkit.craftbukkit.v1_11_R1.inventory.CraftItemStack; import org.bukkit.inventory.ItemStack; @@ -10,11 +11,21 @@ public class NBTItemImpl extends NBTCompoundImpl implements NBTItem { private net.minecraft.server.v1_11_R1.ItemStack nmsItem; public NBTItemImpl(net.minecraft.server.v1_11_R1.ItemStack nmsItem) { - super(nmsItem.hasTag() ? nmsItem.getTag() : new NBTTagCompound()); + super(nmsItem != null && nmsItem.hasTag() ? nmsItem.getTag() : new NBTTagCompound()); this.nmsItem = nmsItem; } public ItemStack finish() { - return CraftItemStack.asBukkitCopy(nmsItem); + if (nmsItem == null) { + net.minecraft.server.v1_11_R1.ItemStack itemStack = CraftItemStack.asNMSCopy(new ItemStack(Material.STONE)); + itemStack.setTag(compound); + return CraftItemStack.asBukkitCopy(nmsItem); + } else { + return CraftItemStack.asBukkitCopy(nmsItem); + } + } + + @Override + public void addExtras() { } } diff --git a/NMS/NMS-v1_12_R1/src/com/songoda/core/nms/v1_12_R1/nbt/NBTCompoundImpl.java b/NMS/NMS-v1_12_R1/src/com/songoda/core/nms/v1_12_R1/nbt/NBTCompoundImpl.java index d6e9b5fd..13743bbc 100644 --- a/NMS/NMS-v1_12_R1/src/com/songoda/core/nms/v1_12_R1/nbt/NBTCompoundImpl.java +++ b/NMS/NMS-v1_12_R1/src/com/songoda/core/nms/v1_12_R1/nbt/NBTCompoundImpl.java @@ -2,9 +2,12 @@ package com.songoda.core.nms.v1_12_R1.nbt; import com.songoda.core.nms.nbt.NBTCompound; import com.songoda.core.nms.nbt.NBTObject; +import net.minecraft.server.v1_12_R1.NBTCompressedStreamTools; import net.minecraft.server.v1_12_R1.NBTTagCompound; -public class NBTCompoundImpl implements NBTCompound { +import java.io.*; + +public abstract class NBTCompoundImpl implements NBTCompound { protected NBTTagCompound compound; @@ -74,4 +77,33 @@ public class NBTCompoundImpl implements NBTCompound { return new NBTObjectImpl(compound, tag); } + @Override + public byte[] serialize(String... exclusions) { + try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); + ObjectOutputStream dataOutput = new ObjectOutputStream(outputStream)) { + addExtras(); + NBTTagCompound compound = this.compound.g(); // Changed in 1.12 + + for (String exclusion : exclusions) + compound.remove(exclusion); + + NBTCompressedStreamTools.a(compound, (OutputStream) dataOutput); + + return outputStream.toByteArray(); + } catch (Exception e) { + e.printStackTrace(); + } + return null; + } + + @Override + public void deSerialize(byte[] serialized) { + try (ByteArrayInputStream inputStream = new ByteArrayInputStream(serialized); + ObjectInputStream dataInput = new ObjectInputStream(inputStream)) { + compound = NBTCompressedStreamTools.a((InputStream) dataInput); + } catch (Exception e) { + e.printStackTrace(); + } + } + } diff --git a/NMS/NMS-v1_12_R1/src/com/songoda/core/nms/v1_12_R1/nbt/NBTCoreImpl.java b/NMS/NMS-v1_12_R1/src/com/songoda/core/nms/v1_12_R1/nbt/NBTCoreImpl.java index 2abb9594..18b44729 100644 --- a/NMS/NMS-v1_12_R1/src/com/songoda/core/nms/v1_12_R1/nbt/NBTCoreImpl.java +++ b/NMS/NMS-v1_12_R1/src/com/songoda/core/nms/v1_12_R1/nbt/NBTCoreImpl.java @@ -1,9 +1,12 @@ package com.songoda.core.nms.v1_12_R1.nbt; -import com.songoda.core.nms.nbt.NBTCompound; import com.songoda.core.nms.nbt.NBTCore; +import com.songoda.core.nms.nbt.NBTEntity; import com.songoda.core.nms.nbt.NBTItem; +import net.minecraft.server.v1_12_R1.NBTTagCompound; +import org.bukkit.craftbukkit.v1_12_R1.entity.CraftEntity; import org.bukkit.craftbukkit.v1_12_R1.inventory.CraftItemStack; +import org.bukkit.entity.Entity; import org.bukkit.inventory.ItemStack; public class NBTCoreImpl implements NBTCore { @@ -14,8 +17,21 @@ public class NBTCoreImpl implements NBTCore { } @Override - public NBTCompound newCompound() { - return new NBTCompoundImpl(); + public NBTItem newItem() { + return new NBTItemImpl(null); + } + + @Override + public NBTEntity of(Entity entity) { + net.minecraft.server.v1_12_R1.Entity nmsEntity = ((CraftEntity) entity).getHandle(); + NBTTagCompound nbt = new NBTTagCompound(); + nmsEntity.save(nbt); + return new NBTEntityImpl(nbt, nmsEntity); + } + + @Override + public NBTEntity newEntity() { + return new NBTEntityImpl(new NBTTagCompound(), null); } } diff --git a/NMS/NMS-v1_12_R1/src/com/songoda/core/nms/v1_12_R1/nbt/NBTEntityImpl.java b/NMS/NMS-v1_12_R1/src/com/songoda/core/nms/v1_12_R1/nbt/NBTEntityImpl.java new file mode 100644 index 00000000..e27af061 --- /dev/null +++ b/NMS/NMS-v1_12_R1/src/com/songoda/core/nms/v1_12_R1/nbt/NBTEntityImpl.java @@ -0,0 +1,53 @@ +package com.songoda.core.nms.v1_12_R1.nbt; + +import com.songoda.core.nms.nbt.NBTEntity; +import net.minecraft.server.v1_12_R1.*; +import org.bukkit.Location; +import org.bukkit.craftbukkit.v1_12_R1.CraftWorld; +import org.bukkit.entity.EntityType; +import org.bukkit.event.entity.CreatureSpawnEvent; + +public class NBTEntityImpl extends NBTCompoundImpl implements NBTEntity { + + private Entity nmsEntity; + + public NBTEntityImpl(NBTTagCompound entityNBT, Entity nmsEntity) { + super(entityNBT); + this.nmsEntity = nmsEntity; + } + + @Override + public org.bukkit.entity.Entity spawn(Location location) { + String entityType = getNBTObject("entity_type").asString(); + + Entity spawned = ItemMonsterEgg.spawnCreature( // Changed since 1.14 + ((CraftWorld) location.getWorld()).getHandle(), + new MinecraftKey(entityType), + location.getBlockX(), location.getBlockY(), location.getBlockZ(), + CreatureSpawnEvent.SpawnReason.DEFAULT + ); + + if (spawned != null) { + spawned.f(compound); // This changed from 1.16.1 + org.bukkit.entity.Entity entity = spawned.getBukkitEntity(); + spawned.setLocation(location.getX(), location.getY(), location.getZ(), + location.getPitch(), location.getYaw()); + nmsEntity = spawned; + return entity; + } + return null; + } + + @Override + public org.bukkit.entity.Entity reSpawn(Location location) { + nmsEntity.dead = true; + return spawn(location); + } + + @Override + public void addExtras() { + String key = EntityTypes.b(nmsEntity); // Changed in 1.12 + if (key != null) + compound.setString("entity_type", key); // Changed in 1.13 + } +} diff --git a/NMS/NMS-v1_12_R1/src/com/songoda/core/nms/v1_12_R1/nbt/NBTItemImpl.java b/NMS/NMS-v1_12_R1/src/com/songoda/core/nms/v1_12_R1/nbt/NBTItemImpl.java index e5e62c0c..ce2503a2 100644 --- a/NMS/NMS-v1_12_R1/src/com/songoda/core/nms/v1_12_R1/nbt/NBTItemImpl.java +++ b/NMS/NMS-v1_12_R1/src/com/songoda/core/nms/v1_12_R1/nbt/NBTItemImpl.java @@ -2,6 +2,7 @@ package com.songoda.core.nms.v1_12_R1.nbt; import com.songoda.core.nms.nbt.NBTItem; import net.minecraft.server.v1_12_R1.NBTTagCompound; +import org.bukkit.Material; import org.bukkit.craftbukkit.v1_12_R1.inventory.CraftItemStack; import org.bukkit.inventory.ItemStack; @@ -10,11 +11,21 @@ public class NBTItemImpl extends NBTCompoundImpl implements NBTItem { private net.minecraft.server.v1_12_R1.ItemStack nmsItem; public NBTItemImpl(net.minecraft.server.v1_12_R1.ItemStack nmsItem) { - super(nmsItem.hasTag() ? nmsItem.getTag() : new NBTTagCompound()); + super(nmsItem != null && nmsItem.hasTag() ? nmsItem.getTag() : new NBTTagCompound()); this.nmsItem = nmsItem; } public ItemStack finish() { - return CraftItemStack.asBukkitCopy(nmsItem); + if (nmsItem == null) { + net.minecraft.server.v1_12_R1.ItemStack itemStack = CraftItemStack.asNMSCopy(new ItemStack(Material.STONE)); + itemStack.setTag(compound); + return CraftItemStack.asBukkitCopy(nmsItem); + } else { + return CraftItemStack.asBukkitCopy(nmsItem); + } + } + + @Override + public void addExtras() { } } diff --git a/NMS/NMS-v1_13_R1/src/com/songoda/core/nms/v1_13_R1/nbt/NBTCompoundImpl.java b/NMS/NMS-v1_13_R1/src/com/songoda/core/nms/v1_13_R1/nbt/NBTCompoundImpl.java index 5dd8cf81..74ae71d3 100644 --- a/NMS/NMS-v1_13_R1/src/com/songoda/core/nms/v1_13_R1/nbt/NBTCompoundImpl.java +++ b/NMS/NMS-v1_13_R1/src/com/songoda/core/nms/v1_13_R1/nbt/NBTCompoundImpl.java @@ -2,9 +2,12 @@ package com.songoda.core.nms.v1_13_R1.nbt; import com.songoda.core.nms.nbt.NBTCompound; import com.songoda.core.nms.nbt.NBTObject; +import net.minecraft.server.v1_13_R1.NBTCompressedStreamTools; import net.minecraft.server.v1_13_R1.NBTTagCompound; -public class NBTCompoundImpl implements NBTCompound { +import java.io.*; + +public abstract class NBTCompoundImpl implements NBTCompound { protected NBTTagCompound compound; @@ -74,4 +77,33 @@ public class NBTCompoundImpl implements NBTCompound { return new NBTObjectImpl(compound, tag); } + @Override + public byte[] serialize(String... exclusions) { + try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); + ObjectOutputStream dataOutput = new ObjectOutputStream(outputStream)) { + addExtras(); + NBTTagCompound compound = this.compound.clone(); + + for (String exclusion : exclusions) + compound.remove(exclusion); + + NBTCompressedStreamTools.a(compound, (OutputStream) dataOutput); + + return outputStream.toByteArray(); + } catch (Exception e) { + e.printStackTrace(); + } + return null; + } + + @Override + public void deSerialize(byte[] serialized) { + try (ByteArrayInputStream inputStream = new ByteArrayInputStream(serialized); + ObjectInputStream dataInput = new ObjectInputStream(inputStream)) { + compound = NBTCompressedStreamTools.a((InputStream) dataInput); + } catch (Exception e) { + e.printStackTrace(); + } + } + } diff --git a/NMS/NMS-v1_13_R1/src/com/songoda/core/nms/v1_13_R1/nbt/NBTCoreImpl.java b/NMS/NMS-v1_13_R1/src/com/songoda/core/nms/v1_13_R1/nbt/NBTCoreImpl.java index b8d331a9..ccefa036 100644 --- a/NMS/NMS-v1_13_R1/src/com/songoda/core/nms/v1_13_R1/nbt/NBTCoreImpl.java +++ b/NMS/NMS-v1_13_R1/src/com/songoda/core/nms/v1_13_R1/nbt/NBTCoreImpl.java @@ -1,9 +1,12 @@ package com.songoda.core.nms.v1_13_R1.nbt; -import com.songoda.core.nms.nbt.NBTCompound; import com.songoda.core.nms.nbt.NBTCore; +import com.songoda.core.nms.nbt.NBTEntity; import com.songoda.core.nms.nbt.NBTItem; +import net.minecraft.server.v1_13_R1.NBTTagCompound; +import org.bukkit.craftbukkit.v1_13_R1.entity.CraftEntity; import org.bukkit.craftbukkit.v1_13_R1.inventory.CraftItemStack; +import org.bukkit.entity.Entity; import org.bukkit.inventory.ItemStack; public class NBTCoreImpl implements NBTCore { @@ -14,8 +17,21 @@ public class NBTCoreImpl implements NBTCore { } @Override - public NBTCompound newCompound() { - return new NBTCompoundImpl(); + public NBTItem newItem() { + return new NBTItemImpl(null); + } + + @Override + public NBTEntity of(Entity entity) { + net.minecraft.server.v1_13_R1.Entity nmsEntity = ((CraftEntity) entity).getHandle(); + NBTTagCompound nbt = new NBTTagCompound(); + nmsEntity.save(nbt); + return new NBTEntityImpl(nbt, nmsEntity); + } + + @Override + public NBTEntity newEntity() { + return new NBTEntityImpl(new NBTTagCompound(), null); } } diff --git a/NMS/NMS-v1_13_R1/src/com/songoda/core/nms/v1_13_R1/nbt/NBTEntityImpl.java b/NMS/NMS-v1_13_R1/src/com/songoda/core/nms/v1_13_R1/nbt/NBTEntityImpl.java new file mode 100644 index 00000000..2d90f197 --- /dev/null +++ b/NMS/NMS-v1_13_R1/src/com/songoda/core/nms/v1_13_R1/nbt/NBTEntityImpl.java @@ -0,0 +1,62 @@ +package com.songoda.core.nms.v1_13_R1.nbt; + +import com.songoda.core.nms.nbt.NBTEntity; +import net.minecraft.server.v1_13_R1.*; +import org.bukkit.Location; +import org.bukkit.craftbukkit.v1_13_R1.CraftWorld; +import org.bukkit.event.entity.CreatureSpawnEvent; + +import java.util.Optional; + +public class NBTEntityImpl extends NBTCompoundImpl implements NBTEntity { + + private Entity nmsEntity; + + public NBTEntityImpl(NBTTagCompound entityNBT, Entity nmsEntity) { + super(entityNBT); + this.nmsEntity = nmsEntity; + } + + @Override + public org.bukkit.entity.Entity spawn(Location location) { + String entityType = getNBTObject("entity_type").asString(); + + Optional> optionalEntity = Optional.ofNullable(EntityTypes.a(entityType)); // Changed since 1.13.2 + if (optionalEntity.isPresent()) { + Entity spawned = optionalEntity.get().spawnCreature( + ((CraftWorld) location.getWorld()).getHandle(), + compound, + null, + null, + new BlockPosition(location.getBlockX(), location.getBlockY(), location.getBlockZ()), + //EnumMobSpawn.COMMAND, // Changed since 1.14 + true, + false, + CreatureSpawnEvent.SpawnReason.DEFAULT + ); + + if (spawned != null) { + spawned.f(compound); // This changed from 1.16.1 + org.bukkit.entity.Entity entity = spawned.getBukkitEntity(); + spawned.setLocation(location.getX(), location.getY(), location.getZ(), + location.getPitch(), location.getYaw()); + nmsEntity = spawned; + return entity; + } + } + return null; + } + + @Override + public org.bukkit.entity.Entity reSpawn(Location location) { + nmsEntity.dead = true; + return spawn(location); + } + + @Override + public void addExtras() { + MinecraftKey key = EntityTypes.REGISTRY.b(nmsEntity.getEntityType()); + if (key != null) + compound.setString("entity_type", key.toString()); // Changed in 1.13 + } +} diff --git a/NMS/NMS-v1_13_R1/src/com/songoda/core/nms/v1_13_R1/nbt/NBTItemImpl.java b/NMS/NMS-v1_13_R1/src/com/songoda/core/nms/v1_13_R1/nbt/NBTItemImpl.java index d67ba96e..ab4adab6 100644 --- a/NMS/NMS-v1_13_R1/src/com/songoda/core/nms/v1_13_R1/nbt/NBTItemImpl.java +++ b/NMS/NMS-v1_13_R1/src/com/songoda/core/nms/v1_13_R1/nbt/NBTItemImpl.java @@ -2,6 +2,7 @@ package com.songoda.core.nms.v1_13_R1.nbt; import com.songoda.core.nms.nbt.NBTItem; import net.minecraft.server.v1_13_R1.NBTTagCompound; +import org.bukkit.Material; import org.bukkit.craftbukkit.v1_13_R1.inventory.CraftItemStack; import org.bukkit.inventory.ItemStack; @@ -10,11 +11,21 @@ public class NBTItemImpl extends NBTCompoundImpl implements NBTItem { private net.minecraft.server.v1_13_R1.ItemStack nmsItem; public NBTItemImpl(net.minecraft.server.v1_13_R1.ItemStack nmsItem) { - super(nmsItem.hasTag() ? nmsItem.getTag() : new NBTTagCompound()); + super(nmsItem != null && nmsItem.hasTag() ? nmsItem.getTag() : new NBTTagCompound()); this.nmsItem = nmsItem; } public ItemStack finish() { - return CraftItemStack.asBukkitCopy(nmsItem); + if (nmsItem == null) { + net.minecraft.server.v1_13_R1.ItemStack itemStack = CraftItemStack.asNMSCopy(new ItemStack(Material.STONE)); + itemStack.setTag(compound); + return CraftItemStack.asBukkitCopy(nmsItem); + } else { + return CraftItemStack.asBukkitCopy(nmsItem); + } + } + + @Override + public void addExtras() { } } diff --git a/NMS/NMS-v1_13_R2/src/com/songoda/core/nms/v1_13_R2/nbt/NBTCompoundImpl.java b/NMS/NMS-v1_13_R2/src/com/songoda/core/nms/v1_13_R2/nbt/NBTCompoundImpl.java index e75fef9c..7b00acc4 100644 --- a/NMS/NMS-v1_13_R2/src/com/songoda/core/nms/v1_13_R2/nbt/NBTCompoundImpl.java +++ b/NMS/NMS-v1_13_R2/src/com/songoda/core/nms/v1_13_R2/nbt/NBTCompoundImpl.java @@ -2,9 +2,12 @@ package com.songoda.core.nms.v1_13_R2.nbt; import com.songoda.core.nms.nbt.NBTCompound; import com.songoda.core.nms.nbt.NBTObject; +import net.minecraft.server.v1_13_R2.NBTCompressedStreamTools; import net.minecraft.server.v1_13_R2.NBTTagCompound; -public class NBTCompoundImpl implements NBTCompound { +import java.io.*; + +public abstract class NBTCompoundImpl implements NBTCompound { protected NBTTagCompound compound; @@ -74,4 +77,33 @@ public class NBTCompoundImpl implements NBTCompound { return new NBTObjectImpl(compound, tag); } + @Override + public byte[] serialize(String... exclusions) { + try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); + ObjectOutputStream dataOutput = new ObjectOutputStream(outputStream)) { + addExtras(); + NBTTagCompound compound = this.compound.clone(); + + for (String exclusion : exclusions) + compound.remove(exclusion); + + NBTCompressedStreamTools.a(compound, (OutputStream) dataOutput); + + return outputStream.toByteArray(); + } catch (Exception e) { + e.printStackTrace(); + } + return null; + } + + @Override + public void deSerialize(byte[] serialized) { + try (ByteArrayInputStream inputStream = new ByteArrayInputStream(serialized); + ObjectInputStream dataInput = new ObjectInputStream(inputStream)) { + compound = NBTCompressedStreamTools.a((InputStream) dataInput); + } catch (Exception e) { + e.printStackTrace(); + } + } + } diff --git a/NMS/NMS-v1_13_R2/src/com/songoda/core/nms/v1_13_R2/nbt/NBTCoreImpl.java b/NMS/NMS-v1_13_R2/src/com/songoda/core/nms/v1_13_R2/nbt/NBTCoreImpl.java index 90d38263..c78b4fc2 100644 --- a/NMS/NMS-v1_13_R2/src/com/songoda/core/nms/v1_13_R2/nbt/NBTCoreImpl.java +++ b/NMS/NMS-v1_13_R2/src/com/songoda/core/nms/v1_13_R2/nbt/NBTCoreImpl.java @@ -1,9 +1,12 @@ package com.songoda.core.nms.v1_13_R2.nbt; -import com.songoda.core.nms.nbt.NBTCompound; import com.songoda.core.nms.nbt.NBTCore; +import com.songoda.core.nms.nbt.NBTEntity; import com.songoda.core.nms.nbt.NBTItem; +import net.minecraft.server.v1_13_R2.NBTTagCompound; +import org.bukkit.craftbukkit.v1_13_R2.entity.CraftEntity; import org.bukkit.craftbukkit.v1_13_R2.inventory.CraftItemStack; +import org.bukkit.entity.Entity; import org.bukkit.inventory.ItemStack; public class NBTCoreImpl implements NBTCore { @@ -14,8 +17,21 @@ public class NBTCoreImpl implements NBTCore { } @Override - public NBTCompound newCompound() { - return new NBTCompoundImpl(); + public NBTItem newItem() { + return new NBTItemImpl(null); + } + + @Override + public NBTEntity of(Entity entity) { + net.minecraft.server.v1_13_R2.Entity nmsEntity = ((CraftEntity) entity).getHandle(); + NBTTagCompound nbt = new NBTTagCompound(); + nmsEntity.save(nbt); + return new NBTEntityImpl(nbt, nmsEntity); + } + + @Override + public NBTEntity newEntity() { + return new NBTEntityImpl(new NBTTagCompound(), null); } } diff --git a/NMS/NMS-v1_13_R2/src/com/songoda/core/nms/v1_13_R2/nbt/NBTEntityImpl.java b/NMS/NMS-v1_13_R2/src/com/songoda/core/nms/v1_13_R2/nbt/NBTEntityImpl.java new file mode 100644 index 00000000..337c0b31 --- /dev/null +++ b/NMS/NMS-v1_13_R2/src/com/songoda/core/nms/v1_13_R2/nbt/NBTEntityImpl.java @@ -0,0 +1,62 @@ +package com.songoda.core.nms.v1_13_R2.nbt; + +import com.songoda.core.nms.nbt.NBTEntity; +import net.minecraft.server.v1_13_R2.*; +import org.bukkit.Location; +import org.bukkit.craftbukkit.v1_13_R2.CraftWorld; +import org.bukkit.event.entity.CreatureSpawnEvent; + +import java.util.Optional; + +public class NBTEntityImpl extends NBTCompoundImpl implements NBTEntity { + + private Entity nmsEntity; + + public NBTEntityImpl(NBTTagCompound entityNBT, Entity nmsEntity) { + super(entityNBT); + this.nmsEntity = nmsEntity; + } + + @Override + public org.bukkit.entity.Entity spawn(Location location) { + String entityType = getNBTObject("entity_type").asString(); + + Optional> optionalEntity = Optional.ofNullable(EntityTypes.a(entityType)); // Changed since 1.13.2 + if (optionalEntity.isPresent()) { + Entity spawned = optionalEntity.get().spawnCreature( + ((CraftWorld) location.getWorld()).getHandle(), + compound, + null, + null, + new BlockPosition(location.getBlockX(), location.getBlockY(), location.getBlockZ()), + //EnumMobSpawn.COMMAND, // Changed since 1.13.2 + true, + false, + CreatureSpawnEvent.SpawnReason.DEFAULT + ); + + if (spawned != null) { + spawned.f(compound); // This changed from 1.16.1 + org.bukkit.entity.Entity entity = spawned.getBukkitEntity(); + spawned.setLocation(location.getX(), location.getY(), location.getZ(), + location.getPitch(), location.getYaw()); + nmsEntity = spawned; + return entity; + } + } + return null; + } + + @Override + public org.bukkit.entity.Entity reSpawn(Location location) { + nmsEntity.dead = true; + return spawn(location); + } + + @Override + public void addExtras() { + MinecraftKey key = IRegistry.ENTITY_TYPE.getKey(nmsEntity.P()); + if (key != null) + compound.setString("entity_type", key.toString()); // Changed in 1.13 + } +} diff --git a/NMS/NMS-v1_13_R2/src/com/songoda/core/nms/v1_13_R2/nbt/NBTItemImpl.java b/NMS/NMS-v1_13_R2/src/com/songoda/core/nms/v1_13_R2/nbt/NBTItemImpl.java index 9d33d858..ae3ce86a 100644 --- a/NMS/NMS-v1_13_R2/src/com/songoda/core/nms/v1_13_R2/nbt/NBTItemImpl.java +++ b/NMS/NMS-v1_13_R2/src/com/songoda/core/nms/v1_13_R2/nbt/NBTItemImpl.java @@ -2,6 +2,7 @@ package com.songoda.core.nms.v1_13_R2.nbt; import com.songoda.core.nms.nbt.NBTItem; import net.minecraft.server.v1_13_R2.NBTTagCompound; +import org.bukkit.Material; import org.bukkit.craftbukkit.v1_13_R2.inventory.CraftItemStack; import org.bukkit.inventory.ItemStack; @@ -10,11 +11,21 @@ public class NBTItemImpl extends NBTCompoundImpl implements NBTItem { private net.minecraft.server.v1_13_R2.ItemStack nmsItem; public NBTItemImpl(net.minecraft.server.v1_13_R2.ItemStack nmsItem) { - super(nmsItem.hasTag() ? nmsItem.getTag() : new NBTTagCompound()); + super(nmsItem != null && nmsItem.hasTag() ? nmsItem.getTag() : new NBTTagCompound()); this.nmsItem = nmsItem; } public ItemStack finish() { - return CraftItemStack.asBukkitCopy(nmsItem); + if (nmsItem == null) { + net.minecraft.server.v1_13_R2.ItemStack itemStack = CraftItemStack.asNMSCopy(new ItemStack(Material.STONE)); + itemStack.setTag(compound); + return CraftItemStack.asBukkitCopy(nmsItem); + } else { + return CraftItemStack.asBukkitCopy(nmsItem); + } + } + + @Override + public void addExtras() { } } diff --git a/NMS/NMS-v1_14_R1/src/com/songoda/core/nms/v1_14_R1/nbt/NBTCompoundImpl.java b/NMS/NMS-v1_14_R1/src/com/songoda/core/nms/v1_14_R1/nbt/NBTCompoundImpl.java index 0b2e4cf3..1b1c3d93 100644 --- a/NMS/NMS-v1_14_R1/src/com/songoda/core/nms/v1_14_R1/nbt/NBTCompoundImpl.java +++ b/NMS/NMS-v1_14_R1/src/com/songoda/core/nms/v1_14_R1/nbt/NBTCompoundImpl.java @@ -2,9 +2,12 @@ package com.songoda.core.nms.v1_14_R1.nbt; import com.songoda.core.nms.nbt.NBTCompound; import com.songoda.core.nms.nbt.NBTObject; +import net.minecraft.server.v1_14_R1.NBTCompressedStreamTools; import net.minecraft.server.v1_14_R1.NBTTagCompound; -public class NBTCompoundImpl implements NBTCompound { +import java.io.*; + +public abstract class NBTCompoundImpl implements NBTCompound { protected NBTTagCompound compound; @@ -74,4 +77,33 @@ public class NBTCompoundImpl implements NBTCompound { return new NBTObjectImpl(compound, tag); } + @Override + public byte[] serialize(String... exclusions) { + try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); + ObjectOutputStream dataOutput = new ObjectOutputStream(outputStream)) { + addExtras(); + NBTTagCompound compound = this.compound.clone(); + + for (String exclusion : exclusions) + compound.remove(exclusion); + + NBTCompressedStreamTools.a(compound, (OutputStream) dataOutput); + + return outputStream.toByteArray(); + } catch (Exception e) { + e.printStackTrace(); + } + return null; + } + + @Override + public void deSerialize(byte[] serialized) { + try (ByteArrayInputStream inputStream = new ByteArrayInputStream(serialized); + ObjectInputStream dataInput = new ObjectInputStream(inputStream)) { + compound = NBTCompressedStreamTools.a((InputStream) dataInput); + } catch (Exception e) { + e.printStackTrace(); + } + } + } diff --git a/NMS/NMS-v1_14_R1/src/com/songoda/core/nms/v1_14_R1/nbt/NBTCoreImpl.java b/NMS/NMS-v1_14_R1/src/com/songoda/core/nms/v1_14_R1/nbt/NBTCoreImpl.java index 0f80ce91..8c123e1f 100644 --- a/NMS/NMS-v1_14_R1/src/com/songoda/core/nms/v1_14_R1/nbt/NBTCoreImpl.java +++ b/NMS/NMS-v1_14_R1/src/com/songoda/core/nms/v1_14_R1/nbt/NBTCoreImpl.java @@ -1,9 +1,12 @@ package com.songoda.core.nms.v1_14_R1.nbt; -import com.songoda.core.nms.nbt.NBTCompound; import com.songoda.core.nms.nbt.NBTCore; +import com.songoda.core.nms.nbt.NBTEntity; import com.songoda.core.nms.nbt.NBTItem; +import net.minecraft.server.v1_14_R1.NBTTagCompound; +import org.bukkit.craftbukkit.v1_14_R1.entity.CraftEntity; import org.bukkit.craftbukkit.v1_14_R1.inventory.CraftItemStack; +import org.bukkit.entity.Entity; import org.bukkit.inventory.ItemStack; public class NBTCoreImpl implements NBTCore { @@ -14,8 +17,21 @@ public class NBTCoreImpl implements NBTCore { } @Override - public NBTCompound newCompound() { - return new NBTCompoundImpl(); + public NBTItem newItem() { + return new NBTItemImpl(null); + } + + @Override + public NBTEntity of(Entity entity) { + net.minecraft.server.v1_14_R1.Entity nmsEntity = ((CraftEntity) entity).getHandle(); + NBTTagCompound nbt = new NBTTagCompound(); + nmsEntity.save(nbt); + return new NBTEntityImpl(nbt, nmsEntity); + } + + @Override + public NBTEntity newEntity() { + return new NBTEntityImpl(new NBTTagCompound(), null); } } diff --git a/NMS/NMS-v1_14_R1/src/com/songoda/core/nms/v1_14_R1/nbt/NBTEntityImpl.java b/NMS/NMS-v1_14_R1/src/com/songoda/core/nms/v1_14_R1/nbt/NBTEntityImpl.java new file mode 100644 index 00000000..9d2caac4 --- /dev/null +++ b/NMS/NMS-v1_14_R1/src/com/songoda/core/nms/v1_14_R1/nbt/NBTEntityImpl.java @@ -0,0 +1,58 @@ +package com.songoda.core.nms.v1_14_R1.nbt; + +import com.songoda.core.nms.nbt.NBTEntity; +import net.minecraft.server.v1_14_R1.*; +import org.bukkit.Location; +import org.bukkit.craftbukkit.v1_14_R1.CraftWorld; + +import java.util.Optional; + +public class NBTEntityImpl extends NBTCompoundImpl implements NBTEntity { + + private Entity nmsEntity; + + public NBTEntityImpl(NBTTagCompound entityNBT, Entity nmsEntity) { + super(entityNBT); + this.nmsEntity = nmsEntity; + } + + @Override + public org.bukkit.entity.Entity spawn(Location location) { + String entityType = getNBTObject("entity_type").asString(); + + Optional> optionalEntity = EntityTypes.a(entityType); + if (optionalEntity.isPresent()) { + Entity spawned = optionalEntity.get().spawnCreature( + ((CraftWorld) location.getWorld()).getHandle(), + compound, + null, + null, + new BlockPosition(location.getBlockX(), location.getBlockY(), location.getBlockZ()), + EnumMobSpawn.COMMAND, + true, + false + ); + + if (spawned != null) { + spawned.f(compound); // This changed from 1.16.1 + org.bukkit.entity.Entity entity = spawned.getBukkitEntity(); + spawned.setLocation(location.getX(), location.getY(), location.getZ(), + location.getPitch(), location.getYaw()); + nmsEntity = spawned; + return entity; + } + } + return null; + } + + @Override + public org.bukkit.entity.Entity reSpawn(Location location) { + nmsEntity.dead = true; + return spawn(location); + } + + @Override + public void addExtras() { + compound.setString("entity_type", IRegistry.ENTITY_TYPE.getKey(nmsEntity.getEntityType()).toString()); + } +} diff --git a/NMS/NMS-v1_14_R1/src/com/songoda/core/nms/v1_14_R1/nbt/NBTItemImpl.java b/NMS/NMS-v1_14_R1/src/com/songoda/core/nms/v1_14_R1/nbt/NBTItemImpl.java index 8a915f26..94908643 100644 --- a/NMS/NMS-v1_14_R1/src/com/songoda/core/nms/v1_14_R1/nbt/NBTItemImpl.java +++ b/NMS/NMS-v1_14_R1/src/com/songoda/core/nms/v1_14_R1/nbt/NBTItemImpl.java @@ -2,6 +2,7 @@ package com.songoda.core.nms.v1_14_R1.nbt; import com.songoda.core.nms.nbt.NBTItem; import net.minecraft.server.v1_14_R1.NBTTagCompound; +import org.bukkit.Material; import org.bukkit.craftbukkit.v1_14_R1.inventory.CraftItemStack; import org.bukkit.inventory.ItemStack; @@ -10,11 +11,21 @@ public class NBTItemImpl extends NBTCompoundImpl implements NBTItem { private net.minecraft.server.v1_14_R1.ItemStack nmsItem; public NBTItemImpl(net.minecraft.server.v1_14_R1.ItemStack nmsItem) { - super(nmsItem.hasTag() ? nmsItem.getTag() : new NBTTagCompound()); + super(nmsItem != null && nmsItem.hasTag() ? nmsItem.getTag() : new NBTTagCompound()); this.nmsItem = nmsItem; } public ItemStack finish() { - return CraftItemStack.asBukkitCopy(nmsItem); + if (nmsItem == null) { + net.minecraft.server.v1_14_R1.ItemStack itemStack = CraftItemStack.asNMSCopy(new ItemStack(Material.STONE)); + itemStack.setTag(compound); + return CraftItemStack.asBukkitCopy(nmsItem); + } else { + return CraftItemStack.asBukkitCopy(nmsItem); + } + } + + @Override + public void addExtras() { } } diff --git a/NMS/NMS-v1_15_R1/src/com/songoda/core/nms/v1_15_R1/nbt/NBTCompoundImpl.java b/NMS/NMS-v1_15_R1/src/com/songoda/core/nms/v1_15_R1/nbt/NBTCompoundImpl.java index 58dfff2b..cdbd2215 100644 --- a/NMS/NMS-v1_15_R1/src/com/songoda/core/nms/v1_15_R1/nbt/NBTCompoundImpl.java +++ b/NMS/NMS-v1_15_R1/src/com/songoda/core/nms/v1_15_R1/nbt/NBTCompoundImpl.java @@ -2,12 +2,12 @@ package com.songoda.core.nms.v1_15_R1.nbt; import com.songoda.core.nms.nbt.NBTCompound; import com.songoda.core.nms.nbt.NBTObject; +import net.minecraft.server.v1_15_R1.NBTCompressedStreamTools; import net.minecraft.server.v1_15_R1.NBTTagCompound; -import net.minecraft.server.v1_15_R1.NBTTagList; -import java.util.List; +import java.io.*; -public class NBTCompoundImpl implements NBTCompound { +public abstract class NBTCompoundImpl implements NBTCompound { protected NBTTagCompound compound; @@ -77,4 +77,33 @@ public class NBTCompoundImpl implements NBTCompound { return new NBTObjectImpl(compound, tag); } + @Override + public byte[] serialize(String... exclusions) { + try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); + ObjectOutputStream dataOutput = new ObjectOutputStream(outputStream)) { + addExtras(); + NBTTagCompound compound = this.compound.clone(); + + for (String exclusion : exclusions) + compound.remove(exclusion); + + NBTCompressedStreamTools.a(compound, (OutputStream) dataOutput); + + return outputStream.toByteArray(); + } catch (Exception e) { + e.printStackTrace(); + } + return null; + } + + @Override + public void deSerialize(byte[] serialized) { + try (ByteArrayInputStream inputStream = new ByteArrayInputStream(serialized); + ObjectInputStream dataInput = new ObjectInputStream(inputStream)) { + compound = NBTCompressedStreamTools.a((InputStream) dataInput); + } catch (Exception e) { + e.printStackTrace(); + } + } + } diff --git a/NMS/NMS-v1_15_R1/src/com/songoda/core/nms/v1_15_R1/nbt/NBTCoreImpl.java b/NMS/NMS-v1_15_R1/src/com/songoda/core/nms/v1_15_R1/nbt/NBTCoreImpl.java index 41bc07b4..1b69a08c 100644 --- a/NMS/NMS-v1_15_R1/src/com/songoda/core/nms/v1_15_R1/nbt/NBTCoreImpl.java +++ b/NMS/NMS-v1_15_R1/src/com/songoda/core/nms/v1_15_R1/nbt/NBTCoreImpl.java @@ -1,9 +1,12 @@ package com.songoda.core.nms.v1_15_R1.nbt; -import com.songoda.core.nms.nbt.NBTCompound; import com.songoda.core.nms.nbt.NBTCore; +import com.songoda.core.nms.nbt.NBTEntity; import com.songoda.core.nms.nbt.NBTItem; +import net.minecraft.server.v1_15_R1.NBTTagCompound; +import org.bukkit.craftbukkit.v1_15_R1.entity.CraftEntity; import org.bukkit.craftbukkit.v1_15_R1.inventory.CraftItemStack; +import org.bukkit.entity.Entity; import org.bukkit.inventory.ItemStack; public class NBTCoreImpl implements NBTCore { @@ -14,8 +17,21 @@ public class NBTCoreImpl implements NBTCore { } @Override - public NBTCompound newCompound() { - return new NBTCompoundImpl(); + public NBTItem newItem() { + return new NBTItemImpl(null); + } + + @Override + public NBTEntity of(Entity entity) { + net.minecraft.server.v1_15_R1.Entity nmsEntity = ((CraftEntity) entity).getHandle(); + NBTTagCompound nbt = new NBTTagCompound(); + nmsEntity.save(nbt); + return new NBTEntityImpl(nbt, nmsEntity); + } + + @Override + public NBTEntity newEntity() { + return new NBTEntityImpl(new NBTTagCompound(), null); } } diff --git a/NMS/NMS-v1_15_R1/src/com/songoda/core/nms/v1_15_R1/nbt/NBTEntityImpl.java b/NMS/NMS-v1_15_R1/src/com/songoda/core/nms/v1_15_R1/nbt/NBTEntityImpl.java new file mode 100644 index 00000000..16af315e --- /dev/null +++ b/NMS/NMS-v1_15_R1/src/com/songoda/core/nms/v1_15_R1/nbt/NBTEntityImpl.java @@ -0,0 +1,58 @@ +package com.songoda.core.nms.v1_15_R1.nbt; + +import com.songoda.core.nms.nbt.NBTEntity; +import net.minecraft.server.v1_15_R1.*; +import org.bukkit.Location; +import org.bukkit.craftbukkit.v1_15_R1.CraftWorld; + +import java.util.Optional; + +public class NBTEntityImpl extends NBTCompoundImpl implements NBTEntity { + + private Entity nmsEntity; + + public NBTEntityImpl(NBTTagCompound entityNBT, Entity nmsEntity) { + super(entityNBT); + this.nmsEntity = nmsEntity; + } + + @Override + public org.bukkit.entity.Entity spawn(Location location) { + String entityType = getNBTObject("entity_type").asString(); + + Optional> optionalEntity = EntityTypes.a(entityType); + if (optionalEntity.isPresent()) { + Entity spawned = optionalEntity.get().spawnCreature( + ((CraftWorld) location.getWorld()).getHandle(), + compound, + null, + null, + new BlockPosition(location.getBlockX(), location.getBlockY(), location.getBlockZ()), + EnumMobSpawn.COMMAND, + true, + false + ); + + if (spawned != null) { + spawned.f(compound); // This changed from 1.16.1 + org.bukkit.entity.Entity entity = spawned.getBukkitEntity(); + spawned.setLocation(location.getX(), location.getY(), location.getZ(), + location.getPitch(), location.getYaw()); + nmsEntity = spawned; + return entity; + } + } + return null; + } + + @Override + public org.bukkit.entity.Entity reSpawn(Location location) { + nmsEntity.dead = true; + return spawn(location); + } + + @Override + public void addExtras() { + compound.setString("entity_type", IRegistry.ENTITY_TYPE.getKey(nmsEntity.getEntityType()).toString()); + } +} diff --git a/NMS/NMS-v1_15_R1/src/com/songoda/core/nms/v1_15_R1/nbt/NBTItemImpl.java b/NMS/NMS-v1_15_R1/src/com/songoda/core/nms/v1_15_R1/nbt/NBTItemImpl.java index 63b1203c..3bf879bb 100644 --- a/NMS/NMS-v1_15_R1/src/com/songoda/core/nms/v1_15_R1/nbt/NBTItemImpl.java +++ b/NMS/NMS-v1_15_R1/src/com/songoda/core/nms/v1_15_R1/nbt/NBTItemImpl.java @@ -2,6 +2,7 @@ package com.songoda.core.nms.v1_15_R1.nbt; import com.songoda.core.nms.nbt.NBTItem; import net.minecraft.server.v1_15_R1.NBTTagCompound; +import org.bukkit.Material; import org.bukkit.craftbukkit.v1_15_R1.inventory.CraftItemStack; import org.bukkit.inventory.ItemStack; @@ -10,11 +11,21 @@ public class NBTItemImpl extends NBTCompoundImpl implements NBTItem { private net.minecraft.server.v1_15_R1.ItemStack nmsItem; public NBTItemImpl(net.minecraft.server.v1_15_R1.ItemStack nmsItem) { - super(nmsItem.hasTag() ? nmsItem.getTag() : new NBTTagCompound()); + super(nmsItem != null && nmsItem.hasTag() ? nmsItem.getTag() : new NBTTagCompound()); this.nmsItem = nmsItem; } public ItemStack finish() { - return CraftItemStack.asBukkitCopy(nmsItem); + if (nmsItem == null) { + net.minecraft.server.v1_15_R1.ItemStack itemStack = CraftItemStack.asNMSCopy(new ItemStack(Material.STONE)); + itemStack.setTag(compound); + return CraftItemStack.asBukkitCopy(nmsItem); + } else { + return CraftItemStack.asBukkitCopy(nmsItem); + } + } + + @Override + public void addExtras() { } } diff --git a/NMS/NMS-v1_16_R1/src/com/songoda/core/nms/v1_16_R1/nbt/NBTCompoundImpl.java b/NMS/NMS-v1_16_R1/src/com/songoda/core/nms/v1_16_R1/nbt/NBTCompoundImpl.java index 0cdb5e82..6727d77a 100644 --- a/NMS/NMS-v1_16_R1/src/com/songoda/core/nms/v1_16_R1/nbt/NBTCompoundImpl.java +++ b/NMS/NMS-v1_16_R1/src/com/songoda/core/nms/v1_16_R1/nbt/NBTCompoundImpl.java @@ -2,10 +2,12 @@ package com.songoda.core.nms.v1_16_R1.nbt; import com.songoda.core.nms.nbt.NBTCompound; import com.songoda.core.nms.nbt.NBTObject; +import net.minecraft.server.v1_16_R1.NBTCompressedStreamTools; import net.minecraft.server.v1_16_R1.NBTTagCompound; -import net.minecraft.server.v1_16_R1.NBTTagList; -public class NBTCompoundImpl implements NBTCompound { +import java.io.*; + +public abstract class NBTCompoundImpl implements NBTCompound { protected NBTTagCompound compound; @@ -75,4 +77,33 @@ public class NBTCompoundImpl implements NBTCompound { return new NBTObjectImpl(compound, tag); } + @Override + public byte[] serialize(String... exclusions) { + try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); + ObjectOutputStream dataOutput = new ObjectOutputStream(outputStream)) { + addExtras(); + NBTTagCompound compound = this.compound.clone(); + + for (String exclusion : exclusions) + compound.remove(exclusion); + + NBTCompressedStreamTools.a(compound, (OutputStream) dataOutput); + + return outputStream.toByteArray(); + } catch (Exception e) { + e.printStackTrace(); + } + return null; + } + + @Override + public void deSerialize(byte[] serialized) { + try (ByteArrayInputStream inputStream = new ByteArrayInputStream(serialized); + ObjectInputStream dataInput = new ObjectInputStream(inputStream)) { + compound = NBTCompressedStreamTools.a((InputStream) dataInput); + } catch (Exception e) { + e.printStackTrace(); + } + } + } diff --git a/NMS/NMS-v1_16_R1/src/com/songoda/core/nms/v1_16_R1/nbt/NBTCoreImpl.java b/NMS/NMS-v1_16_R1/src/com/songoda/core/nms/v1_16_R1/nbt/NBTCoreImpl.java index c0e5ba3d..24aaa35c 100644 --- a/NMS/NMS-v1_16_R1/src/com/songoda/core/nms/v1_16_R1/nbt/NBTCoreImpl.java +++ b/NMS/NMS-v1_16_R1/src/com/songoda/core/nms/v1_16_R1/nbt/NBTCoreImpl.java @@ -1,9 +1,12 @@ package com.songoda.core.nms.v1_16_R1.nbt; -import com.songoda.core.nms.nbt.NBTCompound; import com.songoda.core.nms.nbt.NBTCore; +import com.songoda.core.nms.nbt.NBTEntity; import com.songoda.core.nms.nbt.NBTItem; +import net.minecraft.server.v1_16_R1.NBTTagCompound; +import org.bukkit.craftbukkit.v1_16_R1.entity.CraftEntity; import org.bukkit.craftbukkit.v1_16_R1.inventory.CraftItemStack; +import org.bukkit.entity.Entity; import org.bukkit.inventory.ItemStack; public class NBTCoreImpl implements NBTCore { @@ -14,8 +17,21 @@ public class NBTCoreImpl implements NBTCore { } @Override - public NBTCompound newCompound() { - return new NBTCompoundImpl(); + public NBTItem newItem() { + return new NBTItemImpl(null); + } + + @Override + public NBTEntity of(Entity entity) { + net.minecraft.server.v1_16_R1.Entity nmsEntity = ((CraftEntity) entity).getHandle(); + NBTTagCompound nbt = new NBTTagCompound(); + nmsEntity.save(nbt); + return new NBTEntityImpl(nbt, nmsEntity); + } + + @Override + public NBTEntity newEntity() { + return new NBTEntityImpl(new NBTTagCompound(), null); } } diff --git a/NMS/NMS-v1_16_R1/src/com/songoda/core/nms/v1_16_R1/nbt/NBTEntityImpl.java b/NMS/NMS-v1_16_R1/src/com/songoda/core/nms/v1_16_R1/nbt/NBTEntityImpl.java new file mode 100644 index 00000000..c3e6d53e --- /dev/null +++ b/NMS/NMS-v1_16_R1/src/com/songoda/core/nms/v1_16_R1/nbt/NBTEntityImpl.java @@ -0,0 +1,57 @@ +package com.songoda.core.nms.v1_16_R1.nbt; + +import com.songoda.core.nms.nbt.NBTEntity; +import net.minecraft.server.v1_16_R1.*; +import org.bukkit.Location; +import org.bukkit.craftbukkit.v1_16_R1.CraftWorld; + +import java.util.Optional; + +public class NBTEntityImpl extends NBTCompoundImpl implements NBTEntity { + + private Entity nmsEntity; + + public NBTEntityImpl(NBTTagCompound entityNBT, Entity nmsEntity) { + super(entityNBT); + this.nmsEntity = nmsEntity; + } + + @Override + public org.bukkit.entity.Entity spawn(Location location) { + String entityType = getNBTObject("entity_type").asString(); + + Optional> optionalEntity = EntityTypes.a(entityType); + if (optionalEntity.isPresent()) { + Entity spawned = optionalEntity.get().spawnCreature( + ((CraftWorld) location.getWorld()).getHandle(), + compound, + null, + null, + new BlockPosition(location.getBlockX(), location.getBlockY(), location.getBlockZ()), + EnumMobSpawn.COMMAND, + true, + false + ); + + if (spawned != null) { + spawned.load(compound); + org.bukkit.entity.Entity entity = spawned.getBukkitEntity(); + entity.teleport(location); + nmsEntity = spawned; + return entity; + } + } + return null; + } + + @Override + public org.bukkit.entity.Entity reSpawn(Location location) { + nmsEntity.dead = true; + return spawn(location); + } + + @Override + public void addExtras() { + compound.setString("entity_type", IRegistry.ENTITY_TYPE.getKey(nmsEntity.getEntityType()).toString()); + } +} diff --git a/NMS/NMS-v1_16_R1/src/com/songoda/core/nms/v1_16_R1/nbt/NBTItemImpl.java b/NMS/NMS-v1_16_R1/src/com/songoda/core/nms/v1_16_R1/nbt/NBTItemImpl.java index 2e9f368f..fdafe52e 100644 --- a/NMS/NMS-v1_16_R1/src/com/songoda/core/nms/v1_16_R1/nbt/NBTItemImpl.java +++ b/NMS/NMS-v1_16_R1/src/com/songoda/core/nms/v1_16_R1/nbt/NBTItemImpl.java @@ -2,6 +2,7 @@ package com.songoda.core.nms.v1_16_R1.nbt; import com.songoda.core.nms.nbt.NBTItem; import net.minecraft.server.v1_16_R1.NBTTagCompound; +import org.bukkit.Material; import org.bukkit.craftbukkit.v1_16_R1.inventory.CraftItemStack; import org.bukkit.inventory.ItemStack; @@ -10,11 +11,21 @@ public class NBTItemImpl extends NBTCompoundImpl implements NBTItem { private net.minecraft.server.v1_16_R1.ItemStack nmsItem; public NBTItemImpl(net.minecraft.server.v1_16_R1.ItemStack nmsItem) { - super(nmsItem.hasTag() ? nmsItem.getTag() : new NBTTagCompound()); + super(nmsItem != null && nmsItem.hasTag() ? nmsItem.getTag() : new NBTTagCompound()); this.nmsItem = nmsItem; } public ItemStack finish() { - return CraftItemStack.asBukkitCopy(nmsItem); + if (nmsItem == null) { + net.minecraft.server.v1_16_R1.ItemStack itemStack = CraftItemStack.asNMSCopy(new ItemStack(Material.STONE)); + itemStack.setTag(compound); + return CraftItemStack.asBukkitCopy(nmsItem); + } else { + return CraftItemStack.asBukkitCopy(nmsItem); + } + } + + @Override + public void addExtras() { } } diff --git a/NMS/NMS-v1_16_R2/src/com/songoda/core/nms/v1_16_R2/nbt/NBTCompoundImpl.java b/NMS/NMS-v1_16_R2/src/com/songoda/core/nms/v1_16_R2/nbt/NBTCompoundImpl.java index cef42ab9..8e5bb32f 100644 --- a/NMS/NMS-v1_16_R2/src/com/songoda/core/nms/v1_16_R2/nbt/NBTCompoundImpl.java +++ b/NMS/NMS-v1_16_R2/src/com/songoda/core/nms/v1_16_R2/nbt/NBTCompoundImpl.java @@ -2,10 +2,12 @@ package com.songoda.core.nms.v1_16_R2.nbt; import com.songoda.core.nms.nbt.NBTCompound; import com.songoda.core.nms.nbt.NBTObject; +import net.minecraft.server.v1_16_R2.NBTCompressedStreamTools; import net.minecraft.server.v1_16_R2.NBTTagCompound; -import net.minecraft.server.v1_16_R2.NBTTagList; -public class NBTCompoundImpl implements NBTCompound { +import java.io.*; + +public abstract class NBTCompoundImpl implements NBTCompound { protected NBTTagCompound compound; @@ -75,4 +77,33 @@ public class NBTCompoundImpl implements NBTCompound { return new NBTObjectImpl(compound, tag); } + @Override + public byte[] serialize(String... exclusions) { + try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); + ObjectOutputStream dataOutput = new ObjectOutputStream(outputStream)) { + addExtras(); + NBTTagCompound compound = this.compound.clone(); + + for (String exclusion : exclusions) + compound.remove(exclusion); + + NBTCompressedStreamTools.a(compound, (OutputStream) dataOutput); + + return outputStream.toByteArray(); + } catch (Exception e) { + e.printStackTrace(); + } + return null; + } + + @Override + public void deSerialize(byte[] serialized) { + try (ByteArrayInputStream inputStream = new ByteArrayInputStream(serialized); + ObjectInputStream dataInput = new ObjectInputStream(inputStream)) { + compound = NBTCompressedStreamTools.a((InputStream) dataInput); + } catch (Exception e) { + e.printStackTrace(); + } + } + } diff --git a/NMS/NMS-v1_16_R2/src/com/songoda/core/nms/v1_16_R2/nbt/NBTCoreImpl.java b/NMS/NMS-v1_16_R2/src/com/songoda/core/nms/v1_16_R2/nbt/NBTCoreImpl.java index 289e40c4..c717800b 100644 --- a/NMS/NMS-v1_16_R2/src/com/songoda/core/nms/v1_16_R2/nbt/NBTCoreImpl.java +++ b/NMS/NMS-v1_16_R2/src/com/songoda/core/nms/v1_16_R2/nbt/NBTCoreImpl.java @@ -2,8 +2,12 @@ package com.songoda.core.nms.v1_16_R2.nbt; import com.songoda.core.nms.nbt.NBTCompound; import com.songoda.core.nms.nbt.NBTCore; +import com.songoda.core.nms.nbt.NBTEntity; import com.songoda.core.nms.nbt.NBTItem; +import net.minecraft.server.v1_16_R2.NBTTagCompound; +import org.bukkit.craftbukkit.v1_16_R2.entity.CraftEntity; import org.bukkit.craftbukkit.v1_16_R2.inventory.CraftItemStack; +import org.bukkit.entity.Entity; import org.bukkit.inventory.ItemStack; public class NBTCoreImpl implements NBTCore { @@ -14,8 +18,21 @@ public class NBTCoreImpl implements NBTCore { } @Override - public NBTCompound newCompound() { - return new NBTCompoundImpl(); + public NBTItem newItem() { + return new NBTItemImpl(null); + } + + @Override + public NBTEntity of(Entity entity) { + net.minecraft.server.v1_16_R2.Entity nmsEntity = ((CraftEntity) entity).getHandle(); + NBTTagCompound nbt = new NBTTagCompound(); + nmsEntity.save(nbt); + return new NBTEntityImpl(nbt, nmsEntity); + } + + @Override + public NBTEntity newEntity() { + return new NBTEntityImpl(new NBTTagCompound(), null); } } diff --git a/NMS/NMS-v1_16_R2/src/com/songoda/core/nms/v1_16_R2/nbt/NBTEntityImpl.java b/NMS/NMS-v1_16_R2/src/com/songoda/core/nms/v1_16_R2/nbt/NBTEntityImpl.java new file mode 100644 index 00000000..fbcc3de7 --- /dev/null +++ b/NMS/NMS-v1_16_R2/src/com/songoda/core/nms/v1_16_R2/nbt/NBTEntityImpl.java @@ -0,0 +1,57 @@ +package com.songoda.core.nms.v1_16_R2.nbt; + +import com.songoda.core.nms.nbt.NBTEntity; +import net.minecraft.server.v1_16_R2.*; +import org.bukkit.Location; +import org.bukkit.craftbukkit.v1_16_R2.CraftWorld; + +import java.util.Optional; + +public class NBTEntityImpl extends NBTCompoundImpl implements NBTEntity { + + private Entity nmsEntity; + + public NBTEntityImpl(NBTTagCompound entityNBT, Entity nmsEntity) { + super(entityNBT); + this.nmsEntity = nmsEntity; + } + + @Override + public org.bukkit.entity.Entity spawn(Location location) { + String entityType = getNBTObject("entity_type").asString(); + + Optional> optionalEntity = EntityTypes.a(entityType); + if (optionalEntity.isPresent()) { + Entity spawned = optionalEntity.get().spawnCreature( + ((CraftWorld) location.getWorld()).getHandle(), + compound, + null, + null, + new BlockPosition(location.getBlockX(), location.getBlockY(), location.getBlockZ()), + EnumMobSpawn.COMMAND, + true, + false + ); + + if (spawned != null) { + spawned.load(compound); + org.bukkit.entity.Entity entity = spawned.getBukkitEntity(); + entity.teleport(location); + nmsEntity = spawned; + return entity; + } + } + return null; + } + + @Override + public org.bukkit.entity.Entity reSpawn(Location location) { + nmsEntity.dead = true; + return spawn(location); + } + + @Override + public void addExtras() { + compound.setString("entity_type", IRegistry.ENTITY_TYPE.getKey(nmsEntity.getEntityType()).toString()); + } +} diff --git a/NMS/NMS-v1_16_R2/src/com/songoda/core/nms/v1_16_R2/nbt/NBTItemImpl.java b/NMS/NMS-v1_16_R2/src/com/songoda/core/nms/v1_16_R2/nbt/NBTItemImpl.java index d00837e4..c5905716 100644 --- a/NMS/NMS-v1_16_R2/src/com/songoda/core/nms/v1_16_R2/nbt/NBTItemImpl.java +++ b/NMS/NMS-v1_16_R2/src/com/songoda/core/nms/v1_16_R2/nbt/NBTItemImpl.java @@ -1,7 +1,9 @@ package com.songoda.core.nms.v1_16_R2.nbt; import com.songoda.core.nms.nbt.NBTItem; +import net.minecraft.server.v1_16_R2.IRegistry; import net.minecraft.server.v1_16_R2.NBTTagCompound; +import org.bukkit.Material; import org.bukkit.craftbukkit.v1_16_R2.inventory.CraftItemStack; import org.bukkit.inventory.ItemStack; @@ -10,11 +12,21 @@ public class NBTItemImpl extends NBTCompoundImpl implements NBTItem { private net.minecraft.server.v1_16_R2.ItemStack nmsItem; public NBTItemImpl(net.minecraft.server.v1_16_R2.ItemStack nmsItem) { - super(nmsItem.hasTag() ? nmsItem.getTag() : new NBTTagCompound()); + super(nmsItem != null && nmsItem.hasTag() ? nmsItem.getTag() : new NBTTagCompound()); this.nmsItem = nmsItem; } public ItemStack finish() { - return CraftItemStack.asBukkitCopy(nmsItem); + if (nmsItem == null) { + net.minecraft.server.v1_16_R2.ItemStack itemStack = CraftItemStack.asNMSCopy(new ItemStack(Material.STONE)); + itemStack.setTag(compound); + return CraftItemStack.asBukkitCopy(nmsItem); + } else { + return CraftItemStack.asBukkitCopy(nmsItem); + } + } + + @Override + public void addExtras() { } } diff --git a/NMS/NMS-v1_8_R1/src/com/songoda/core/nms/v1_8_R1/nbt/NBTCompoundImpl.java b/NMS/NMS-v1_8_R1/src/com/songoda/core/nms/v1_8_R1/nbt/NBTCompoundImpl.java index 06343a37..4782ee49 100644 --- a/NMS/NMS-v1_8_R1/src/com/songoda/core/nms/v1_8_R1/nbt/NBTCompoundImpl.java +++ b/NMS/NMS-v1_8_R1/src/com/songoda/core/nms/v1_8_R1/nbt/NBTCompoundImpl.java @@ -2,9 +2,12 @@ package com.songoda.core.nms.v1_8_R1.nbt; import com.songoda.core.nms.nbt.NBTCompound; import com.songoda.core.nms.nbt.NBTObject; +import net.minecraft.server.v1_8_R1.NBTCompressedStreamTools; import net.minecraft.server.v1_8_R1.NBTTagCompound; -public class NBTCompoundImpl implements NBTCompound { +import java.io.*; + +public abstract class NBTCompoundImpl implements NBTCompound { protected NBTTagCompound compound; @@ -74,4 +77,33 @@ public class NBTCompoundImpl implements NBTCompound { return new NBTObjectImpl(compound, tag); } + @Override + public byte[] serialize(String... exclusions) { + try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); + ObjectOutputStream dataOutput = new ObjectOutputStream(outputStream)) { + addExtras(); + NBTTagCompound compound = (NBTTagCompound)this.compound.clone(); // Changed in 1.12 // Changed in 1.9.4 + + for (String exclusion : exclusions) + compound.remove(exclusion); + + NBTCompressedStreamTools.a(compound, (OutputStream) dataOutput); + + return outputStream.toByteArray(); + } catch (Exception e) { + e.printStackTrace(); + } + return null; + } + + @Override + public void deSerialize(byte[] serialized) { + try (ByteArrayInputStream inputStream = new ByteArrayInputStream(serialized); + ObjectInputStream dataInput = new ObjectInputStream(inputStream)) { + compound = NBTCompressedStreamTools.a((InputStream) dataInput); + } catch (Exception e) { + e.printStackTrace(); + } + } + } diff --git a/NMS/NMS-v1_8_R1/src/com/songoda/core/nms/v1_8_R1/nbt/NBTCoreImpl.java b/NMS/NMS-v1_8_R1/src/com/songoda/core/nms/v1_8_R1/nbt/NBTCoreImpl.java index b2e7fc37..572c8b2c 100644 --- a/NMS/NMS-v1_8_R1/src/com/songoda/core/nms/v1_8_R1/nbt/NBTCoreImpl.java +++ b/NMS/NMS-v1_8_R1/src/com/songoda/core/nms/v1_8_R1/nbt/NBTCoreImpl.java @@ -1,9 +1,12 @@ package com.songoda.core.nms.v1_8_R1.nbt; -import com.songoda.core.nms.nbt.NBTCompound; import com.songoda.core.nms.nbt.NBTCore; +import com.songoda.core.nms.nbt.NBTEntity; import com.songoda.core.nms.nbt.NBTItem; +import net.minecraft.server.v1_8_R1.NBTTagCompound; +import org.bukkit.craftbukkit.v1_8_R1.entity.CraftEntity; import org.bukkit.craftbukkit.v1_8_R1.inventory.CraftItemStack; +import org.bukkit.entity.Entity; import org.bukkit.inventory.ItemStack; public class NBTCoreImpl implements NBTCore { @@ -14,8 +17,21 @@ public class NBTCoreImpl implements NBTCore { } @Override - public NBTCompound newCompound() { - return new NBTCompoundImpl(); + public NBTItem newItem() { + return new NBTItemImpl(null); + } + + @Override + public NBTEntity of(Entity entity) { + net.minecraft.server.v1_8_R1.Entity nmsEntity = ((CraftEntity) entity).getHandle(); + NBTTagCompound nbt = new NBTTagCompound(); + nmsEntity.e(nbt); // Method name changed in 1.11 + return new NBTEntityImpl(nbt, nmsEntity); + } + + @Override + public NBTEntity newEntity() { + return new NBTEntityImpl(new NBTTagCompound(), null); } } diff --git a/NMS/NMS-v1_8_R1/src/com/songoda/core/nms/v1_8_R1/nbt/NBTEntityImpl.java b/NMS/NMS-v1_8_R1/src/com/songoda/core/nms/v1_8_R1/nbt/NBTEntityImpl.java new file mode 100644 index 00000000..d19b43c2 --- /dev/null +++ b/NMS/NMS-v1_8_R1/src/com/songoda/core/nms/v1_8_R1/nbt/NBTEntityImpl.java @@ -0,0 +1,71 @@ +package com.songoda.core.nms.v1_8_R1.nbt; + +import com.songoda.core.nms.nbt.NBTEntity; +import net.minecraft.server.v1_8_R1.*; +import org.bukkit.Location; +import org.bukkit.craftbukkit.v1_8_R1.CraftWorld; +import org.bukkit.event.entity.CreatureSpawnEvent; + +import java.lang.reflect.Field; +import java.util.Map; + +public class NBTEntityImpl extends NBTCompoundImpl implements NBTEntity { + + private static Field fieldG; + + private Entity nmsEntity; + + public NBTEntityImpl(NBTTagCompound entityNBT, Entity nmsEntity) { + super(entityNBT); + this.nmsEntity = nmsEntity; + } + + static { + try { + fieldG = EntityTypes.class.getDeclaredField("g"); + fieldG.setAccessible(true); + } catch (NoSuchFieldException e) { + e.printStackTrace(); + } + + } + + @Override + public org.bukkit.entity.Entity spawn(Location location) { + String entityType = getNBTObject("entity_type").asString(); + + try { + Entity spawned = ItemMonsterEgg.spawnCreature( // Changed since 1.14 + ((CraftWorld) location.getWorld()).getHandle(), + ((Map) fieldG.get(null)).get(entityType), // Parameter simplified in 1.10 + location.getBlockX(), location.getBlockY(), location.getBlockZ(), + CreatureSpawnEvent.SpawnReason.DEFAULT + ); + + if (spawned != null) { + spawned.f(compound); // This changed from 1.16.1 + org.bukkit.entity.Entity entity = spawned.getBukkitEntity(); + spawned.setLocation(location.getX(), location.getY(), location.getZ(), + location.getPitch(), location.getYaw()); + nmsEntity = spawned; + return entity; + } + } catch (IllegalAccessException e) { + e.printStackTrace(); + } + return null; + } + + @Override + public org.bukkit.entity.Entity reSpawn(Location location) { + nmsEntity.dead = true; + return spawn(location); + } + + @Override + public void addExtras() { + String key = EntityTypes.b(nmsEntity); // Changed in 1.12 + if (key != null) + compound.setString("entity_type", key); // Changed in 1.13 + } +} diff --git a/NMS/NMS-v1_8_R1/src/com/songoda/core/nms/v1_8_R1/nbt/NBTItemImpl.java b/NMS/NMS-v1_8_R1/src/com/songoda/core/nms/v1_8_R1/nbt/NBTItemImpl.java index b81f251d..f12be81e 100644 --- a/NMS/NMS-v1_8_R1/src/com/songoda/core/nms/v1_8_R1/nbt/NBTItemImpl.java +++ b/NMS/NMS-v1_8_R1/src/com/songoda/core/nms/v1_8_R1/nbt/NBTItemImpl.java @@ -2,6 +2,7 @@ package com.songoda.core.nms.v1_8_R1.nbt; import com.songoda.core.nms.nbt.NBTItem; import net.minecraft.server.v1_8_R1.NBTTagCompound; +import org.bukkit.Material; import org.bukkit.craftbukkit.v1_8_R1.inventory.CraftItemStack; import org.bukkit.inventory.ItemStack; @@ -10,11 +11,21 @@ public class NBTItemImpl extends NBTCompoundImpl implements NBTItem { private net.minecraft.server.v1_8_R1.ItemStack nmsItem; public NBTItemImpl(net.minecraft.server.v1_8_R1.ItemStack nmsItem) { - super(nmsItem.hasTag() ? nmsItem.getTag() : new NBTTagCompound()); + super(nmsItem != null && nmsItem.hasTag() ? nmsItem.getTag() : new NBTTagCompound()); this.nmsItem = nmsItem; } public ItemStack finish() { - return CraftItemStack.asBukkitCopy(nmsItem); + if (nmsItem == null) { + net.minecraft.server.v1_8_R1.ItemStack itemStack = CraftItemStack.asNMSCopy(new ItemStack(Material.STONE)); + itemStack.setTag(compound); + return CraftItemStack.asBukkitCopy(nmsItem); + } else { + return CraftItemStack.asBukkitCopy(nmsItem); + } + } + + @Override + public void addExtras() { } } diff --git a/NMS/NMS-v1_8_R2/src/com/songoda/core/nms/v1_8_R2/nbt/NBTCompoundImpl.java b/NMS/NMS-v1_8_R2/src/com/songoda/core/nms/v1_8_R2/nbt/NBTCompoundImpl.java index 70e21780..9984b805 100644 --- a/NMS/NMS-v1_8_R2/src/com/songoda/core/nms/v1_8_R2/nbt/NBTCompoundImpl.java +++ b/NMS/NMS-v1_8_R2/src/com/songoda/core/nms/v1_8_R2/nbt/NBTCompoundImpl.java @@ -2,9 +2,12 @@ package com.songoda.core.nms.v1_8_R2.nbt; import com.songoda.core.nms.nbt.NBTCompound; import com.songoda.core.nms.nbt.NBTObject; +import net.minecraft.server.v1_8_R2.NBTCompressedStreamTools; import net.minecraft.server.v1_8_R2.NBTTagCompound; -public class NBTCompoundImpl implements NBTCompound { +import java.io.*; + +public abstract class NBTCompoundImpl implements NBTCompound { protected NBTTagCompound compound; @@ -74,4 +77,33 @@ public class NBTCompoundImpl implements NBTCompound { return new NBTObjectImpl(compound, tag); } + @Override + public byte[] serialize(String... exclusions) { + try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); + ObjectOutputStream dataOutput = new ObjectOutputStream(outputStream)) { + addExtras(); + NBTTagCompound compound = (NBTTagCompound)this.compound.clone(); // Changed in 1.12 // Changed in 1.9.4 + + for (String exclusion : exclusions) + compound.remove(exclusion); + + NBTCompressedStreamTools.a(compound, (OutputStream) dataOutput); + + return outputStream.toByteArray(); + } catch (Exception e) { + e.printStackTrace(); + } + return null; + } + + @Override + public void deSerialize(byte[] serialized) { + try (ByteArrayInputStream inputStream = new ByteArrayInputStream(serialized); + ObjectInputStream dataInput = new ObjectInputStream(inputStream)) { + compound = NBTCompressedStreamTools.a((InputStream) dataInput); + } catch (Exception e) { + e.printStackTrace(); + } + } + } diff --git a/NMS/NMS-v1_8_R2/src/com/songoda/core/nms/v1_8_R2/nbt/NBTCoreImpl.java b/NMS/NMS-v1_8_R2/src/com/songoda/core/nms/v1_8_R2/nbt/NBTCoreImpl.java index 3d3447f1..644c708c 100644 --- a/NMS/NMS-v1_8_R2/src/com/songoda/core/nms/v1_8_R2/nbt/NBTCoreImpl.java +++ b/NMS/NMS-v1_8_R2/src/com/songoda/core/nms/v1_8_R2/nbt/NBTCoreImpl.java @@ -1,9 +1,12 @@ package com.songoda.core.nms.v1_8_R2.nbt; -import com.songoda.core.nms.nbt.NBTCompound; import com.songoda.core.nms.nbt.NBTCore; +import com.songoda.core.nms.nbt.NBTEntity; import com.songoda.core.nms.nbt.NBTItem; +import net.minecraft.server.v1_8_R2.NBTTagCompound; +import org.bukkit.craftbukkit.v1_8_R2.entity.CraftEntity; import org.bukkit.craftbukkit.v1_8_R2.inventory.CraftItemStack; +import org.bukkit.entity.Entity; import org.bukkit.inventory.ItemStack; public class NBTCoreImpl implements NBTCore { @@ -14,8 +17,21 @@ public class NBTCoreImpl implements NBTCore { } @Override - public NBTCompound newCompound() { - return new NBTCompoundImpl(); + public NBTItem newItem() { + return new NBTItemImpl(null); + } + + @Override + public NBTEntity of(Entity entity) { + net.minecraft.server.v1_8_R2.Entity nmsEntity = ((CraftEntity) entity).getHandle(); + NBTTagCompound nbt = new NBTTagCompound(); + nmsEntity.e(nbt); // Method name changed in 1.11 + return new NBTEntityImpl(nbt, nmsEntity); + } + + @Override + public NBTEntity newEntity() { + return new NBTEntityImpl(new NBTTagCompound(), null); } } diff --git a/NMS/NMS-v1_8_R2/src/com/songoda/core/nms/v1_8_R2/nbt/NBTEntityImpl.java b/NMS/NMS-v1_8_R2/src/com/songoda/core/nms/v1_8_R2/nbt/NBTEntityImpl.java new file mode 100644 index 00000000..d2450e81 --- /dev/null +++ b/NMS/NMS-v1_8_R2/src/com/songoda/core/nms/v1_8_R2/nbt/NBTEntityImpl.java @@ -0,0 +1,71 @@ +package com.songoda.core.nms.v1_8_R2.nbt; + +import com.songoda.core.nms.nbt.NBTEntity; +import net.minecraft.server.v1_8_R2.*; +import org.bukkit.Location; +import org.bukkit.craftbukkit.v1_8_R2.CraftWorld; +import org.bukkit.event.entity.CreatureSpawnEvent; + +import java.lang.reflect.Field; +import java.util.Map; + +public class NBTEntityImpl extends NBTCompoundImpl implements NBTEntity { + + private static Field fieldG; + + private Entity nmsEntity; + + public NBTEntityImpl(NBTTagCompound entityNBT, Entity nmsEntity) { + super(entityNBT); + this.nmsEntity = nmsEntity; + } + + static { + try { + fieldG = EntityTypes.class.getDeclaredField("g"); + fieldG.setAccessible(true); + } catch (NoSuchFieldException e) { + e.printStackTrace(); + } + + } + + @Override + public org.bukkit.entity.Entity spawn(Location location) { + String entityType = getNBTObject("entity_type").asString(); + + try { + Entity spawned = ItemMonsterEgg.spawnCreature( // Changed since 1.14 + ((CraftWorld) location.getWorld()).getHandle(), + ((Map) fieldG.get(null)).get(entityType), // Parameter simplified in 1.10 + location.getBlockX(), location.getBlockY(), location.getBlockZ(), + CreatureSpawnEvent.SpawnReason.DEFAULT + ); + + if (spawned != null) { + spawned.f(compound); // This changed from 1.16.1 + org.bukkit.entity.Entity entity = spawned.getBukkitEntity(); + spawned.setLocation(location.getX(), location.getY(), location.getZ(), + location.getPitch(), location.getYaw()); + nmsEntity = spawned; + return entity; + } + } catch (IllegalAccessException e) { + e.printStackTrace(); + } + return null; + } + + @Override + public org.bukkit.entity.Entity reSpawn(Location location) { + nmsEntity.dead = true; + return spawn(location); + } + + @Override + public void addExtras() { + String key = EntityTypes.b(nmsEntity); // Changed in 1.12 + if (key != null) + compound.setString("entity_type", key); // Changed in 1.13 + } +} diff --git a/NMS/NMS-v1_8_R2/src/com/songoda/core/nms/v1_8_R2/nbt/NBTItemImpl.java b/NMS/NMS-v1_8_R2/src/com/songoda/core/nms/v1_8_R2/nbt/NBTItemImpl.java index cee025b6..0fac0531 100644 --- a/NMS/NMS-v1_8_R2/src/com/songoda/core/nms/v1_8_R2/nbt/NBTItemImpl.java +++ b/NMS/NMS-v1_8_R2/src/com/songoda/core/nms/v1_8_R2/nbt/NBTItemImpl.java @@ -2,6 +2,7 @@ package com.songoda.core.nms.v1_8_R2.nbt; import com.songoda.core.nms.nbt.NBTItem; import net.minecraft.server.v1_8_R2.NBTTagCompound; +import org.bukkit.Material; import org.bukkit.craftbukkit.v1_8_R2.inventory.CraftItemStack; import org.bukkit.inventory.ItemStack; @@ -10,11 +11,21 @@ public class NBTItemImpl extends NBTCompoundImpl implements NBTItem { private net.minecraft.server.v1_8_R2.ItemStack nmsItem; public NBTItemImpl(net.minecraft.server.v1_8_R2.ItemStack nmsItem) { - super(nmsItem.hasTag() ? nmsItem.getTag() : new NBTTagCompound()); + super(nmsItem != null && nmsItem.hasTag() ? nmsItem.getTag() : new NBTTagCompound()); this.nmsItem = nmsItem; } public ItemStack finish() { - return CraftItemStack.asBukkitCopy(nmsItem); + if (nmsItem == null) { + net.minecraft.server.v1_8_R2.ItemStack itemStack = CraftItemStack.asNMSCopy(new ItemStack(Material.STONE)); + itemStack.setTag(compound); + return CraftItemStack.asBukkitCopy(nmsItem); + } else { + return CraftItemStack.asBukkitCopy(nmsItem); + } + } + + @Override + public void addExtras() { } } diff --git a/NMS/NMS-v1_8_R3/src/com/songoda/core/nms/v1_8_R3/nbt/NBTCompoundImpl.java b/NMS/NMS-v1_8_R3/src/com/songoda/core/nms/v1_8_R3/nbt/NBTCompoundImpl.java index 9b2d16f2..f0a8f794 100644 --- a/NMS/NMS-v1_8_R3/src/com/songoda/core/nms/v1_8_R3/nbt/NBTCompoundImpl.java +++ b/NMS/NMS-v1_8_R3/src/com/songoda/core/nms/v1_8_R3/nbt/NBTCompoundImpl.java @@ -2,9 +2,12 @@ package com.songoda.core.nms.v1_8_R3.nbt; import com.songoda.core.nms.nbt.NBTCompound; import com.songoda.core.nms.nbt.NBTObject; +import net.minecraft.server.v1_8_R3.NBTCompressedStreamTools; import net.minecraft.server.v1_8_R3.NBTTagCompound; -public class NBTCompoundImpl implements NBTCompound { +import java.io.*; + +public abstract class NBTCompoundImpl implements NBTCompound { protected NBTTagCompound compound; @@ -74,4 +77,33 @@ public class NBTCompoundImpl implements NBTCompound { return new NBTObjectImpl(compound, tag); } + @Override + public byte[] serialize(String... exclusions) { + try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); + ObjectOutputStream dataOutput = new ObjectOutputStream(outputStream)) { + addExtras(); + NBTTagCompound compound = (NBTTagCompound)this.compound.clone(); // Changed in 1.12 // Changed in 1.9.4 + + for (String exclusion : exclusions) + compound.remove(exclusion); + + NBTCompressedStreamTools.a(compound, (OutputStream) dataOutput); + + return outputStream.toByteArray(); + } catch (Exception e) { + e.printStackTrace(); + } + return null; + } + + @Override + public void deSerialize(byte[] serialized) { + try (ByteArrayInputStream inputStream = new ByteArrayInputStream(serialized); + ObjectInputStream dataInput = new ObjectInputStream(inputStream)) { + compound = NBTCompressedStreamTools.a((InputStream) dataInput); + } catch (Exception e) { + e.printStackTrace(); + } + } + } diff --git a/NMS/NMS-v1_8_R3/src/com/songoda/core/nms/v1_8_R3/nbt/NBTCoreImpl.java b/NMS/NMS-v1_8_R3/src/com/songoda/core/nms/v1_8_R3/nbt/NBTCoreImpl.java index 3cb35f44..d3ab7aae 100644 --- a/NMS/NMS-v1_8_R3/src/com/songoda/core/nms/v1_8_R3/nbt/NBTCoreImpl.java +++ b/NMS/NMS-v1_8_R3/src/com/songoda/core/nms/v1_8_R3/nbt/NBTCoreImpl.java @@ -1,9 +1,12 @@ package com.songoda.core.nms.v1_8_R3.nbt; -import com.songoda.core.nms.nbt.NBTCompound; import com.songoda.core.nms.nbt.NBTCore; +import com.songoda.core.nms.nbt.NBTEntity; import com.songoda.core.nms.nbt.NBTItem; +import net.minecraft.server.v1_8_R3.NBTTagCompound; +import org.bukkit.craftbukkit.v1_8_R3.entity.CraftEntity; import org.bukkit.craftbukkit.v1_8_R3.inventory.CraftItemStack; +import org.bukkit.entity.Entity; import org.bukkit.inventory.ItemStack; public class NBTCoreImpl implements NBTCore { @@ -14,8 +17,21 @@ public class NBTCoreImpl implements NBTCore { } @Override - public NBTCompound newCompound() { - return new NBTCompoundImpl(); + public NBTItem newItem() { + return new NBTItemImpl(null); + } + + @Override + public NBTEntity of(Entity entity) { + net.minecraft.server.v1_8_R3.Entity nmsEntity = ((CraftEntity) entity).getHandle(); + NBTTagCompound nbt = new NBTTagCompound(); + nmsEntity.e(nbt); // Method name changed in 1.11 + return new NBTEntityImpl(nbt, nmsEntity); + } + + @Override + public NBTEntity newEntity() { + return new NBTEntityImpl(new NBTTagCompound(), null); } } diff --git a/NMS/NMS-v1_8_R3/src/com/songoda/core/nms/v1_8_R3/nbt/NBTEntityImpl.java b/NMS/NMS-v1_8_R3/src/com/songoda/core/nms/v1_8_R3/nbt/NBTEntityImpl.java new file mode 100644 index 00000000..d08a143d --- /dev/null +++ b/NMS/NMS-v1_8_R3/src/com/songoda/core/nms/v1_8_R3/nbt/NBTEntityImpl.java @@ -0,0 +1,71 @@ +package com.songoda.core.nms.v1_8_R3.nbt; + +import com.songoda.core.nms.nbt.NBTEntity; +import net.minecraft.server.v1_8_R3.*; +import org.bukkit.Location; +import org.bukkit.craftbukkit.v1_8_R3.CraftWorld; +import org.bukkit.event.entity.CreatureSpawnEvent; + +import java.lang.reflect.Field; +import java.util.Map; + +public class NBTEntityImpl extends NBTCompoundImpl implements NBTEntity { + + private static Field fieldG; + + private Entity nmsEntity; + + public NBTEntityImpl(NBTTagCompound entityNBT, Entity nmsEntity) { + super(entityNBT); + this.nmsEntity = nmsEntity; + } + + static { + try { + fieldG = EntityTypes.class.getDeclaredField("g"); + fieldG.setAccessible(true); + } catch (NoSuchFieldException e) { + e.printStackTrace(); + } + + } + + @Override + public org.bukkit.entity.Entity spawn(Location location) { + String entityType = getNBTObject("entity_type").asString(); + + try { + Entity spawned = ItemMonsterEgg.spawnCreature( // Changed since 1.14 + ((CraftWorld) location.getWorld()).getHandle(), + ((Map) fieldG.get(null)).get(entityType), // Parameter simplified in 1.10 + location.getBlockX(), location.getBlockY(), location.getBlockZ(), + CreatureSpawnEvent.SpawnReason.DEFAULT + ); + + if (spawned != null) { + spawned.f(compound); // This changed from 1.16.1 + org.bukkit.entity.Entity entity = spawned.getBukkitEntity(); + spawned.setLocation(location.getX(), location.getY(), location.getZ(), + location.getPitch(), location.getYaw()); + nmsEntity = spawned; + return entity; + } + } catch (IllegalAccessException e) { + e.printStackTrace(); + } + return null; + } + + @Override + public org.bukkit.entity.Entity reSpawn(Location location) { + nmsEntity.dead = true; + return spawn(location); + } + + @Override + public void addExtras() { + String key = EntityTypes.b(nmsEntity); // Changed in 1.12 + if (key != null) + compound.setString("entity_type", key); // Changed in 1.13 + } +} diff --git a/NMS/NMS-v1_8_R3/src/com/songoda/core/nms/v1_8_R3/nbt/NBTItemImpl.java b/NMS/NMS-v1_8_R3/src/com/songoda/core/nms/v1_8_R3/nbt/NBTItemImpl.java index 3b038314..5a1aa849 100644 --- a/NMS/NMS-v1_8_R3/src/com/songoda/core/nms/v1_8_R3/nbt/NBTItemImpl.java +++ b/NMS/NMS-v1_8_R3/src/com/songoda/core/nms/v1_8_R3/nbt/NBTItemImpl.java @@ -2,6 +2,7 @@ package com.songoda.core.nms.v1_8_R3.nbt; import com.songoda.core.nms.nbt.NBTItem; import net.minecraft.server.v1_8_R3.NBTTagCompound; +import org.bukkit.Material; import org.bukkit.craftbukkit.v1_8_R3.inventory.CraftItemStack; import org.bukkit.inventory.ItemStack; @@ -10,11 +11,21 @@ public class NBTItemImpl extends NBTCompoundImpl implements NBTItem { private net.minecraft.server.v1_8_R3.ItemStack nmsItem; public NBTItemImpl(net.minecraft.server.v1_8_R3.ItemStack nmsItem) { - super(nmsItem.hasTag() ? nmsItem.getTag() : new NBTTagCompound()); + super(nmsItem != null && nmsItem.hasTag() ? nmsItem.getTag() : new NBTTagCompound()); this.nmsItem = nmsItem; } public ItemStack finish() { - return CraftItemStack.asBukkitCopy(nmsItem); + if (nmsItem == null) { + net.minecraft.server.v1_8_R3.ItemStack itemStack = CraftItemStack.asNMSCopy(new ItemStack(Material.STONE)); + itemStack.setTag(compound); + return CraftItemStack.asBukkitCopy(nmsItem); + } else { + return CraftItemStack.asBukkitCopy(nmsItem); + } + } + + @Override + public void addExtras() { } } diff --git a/NMS/NMS-v1_9_R1/src/com/songoda/core/nms/v1_9_R1/nbt/NBTCompoundImpl.java b/NMS/NMS-v1_9_R1/src/com/songoda/core/nms/v1_9_R1/nbt/NBTCompoundImpl.java index 31e9519d..2ae9994a 100644 --- a/NMS/NMS-v1_9_R1/src/com/songoda/core/nms/v1_9_R1/nbt/NBTCompoundImpl.java +++ b/NMS/NMS-v1_9_R1/src/com/songoda/core/nms/v1_9_R1/nbt/NBTCompoundImpl.java @@ -2,9 +2,12 @@ package com.songoda.core.nms.v1_9_R1.nbt; import com.songoda.core.nms.nbt.NBTCompound; import com.songoda.core.nms.nbt.NBTObject; +import net.minecraft.server.v1_9_R1.NBTCompressedStreamTools; import net.minecraft.server.v1_9_R1.NBTTagCompound; -public class NBTCompoundImpl implements NBTCompound { +import java.io.*; + +public abstract class NBTCompoundImpl implements NBTCompound { protected NBTTagCompound compound; @@ -74,4 +77,33 @@ public class NBTCompoundImpl implements NBTCompound { return new NBTObjectImpl(compound, tag); } + @Override + public byte[] serialize(String... exclusions) { + try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); + ObjectOutputStream dataOutput = new ObjectOutputStream(outputStream)) { + addExtras(); + NBTTagCompound compound = (NBTTagCompound)this.compound.clone(); // Changed in 1.12 // Changed in 1.9.4 + + for (String exclusion : exclusions) + compound.remove(exclusion); + + NBTCompressedStreamTools.a(compound, (OutputStream) dataOutput); + + return outputStream.toByteArray(); + } catch (Exception e) { + e.printStackTrace(); + } + return null; + } + + @Override + public void deSerialize(byte[] serialized) { + try (ByteArrayInputStream inputStream = new ByteArrayInputStream(serialized); + ObjectInputStream dataInput = new ObjectInputStream(inputStream)) { + compound = NBTCompressedStreamTools.a((InputStream) dataInput); + } catch (Exception e) { + e.printStackTrace(); + } + } + } diff --git a/NMS/NMS-v1_9_R1/src/com/songoda/core/nms/v1_9_R1/nbt/NBTCoreImpl.java b/NMS/NMS-v1_9_R1/src/com/songoda/core/nms/v1_9_R1/nbt/NBTCoreImpl.java index 23dd3a45..17f85624 100644 --- a/NMS/NMS-v1_9_R1/src/com/songoda/core/nms/v1_9_R1/nbt/NBTCoreImpl.java +++ b/NMS/NMS-v1_9_R1/src/com/songoda/core/nms/v1_9_R1/nbt/NBTCoreImpl.java @@ -1,9 +1,12 @@ package com.songoda.core.nms.v1_9_R1.nbt; -import com.songoda.core.nms.nbt.NBTCompound; import com.songoda.core.nms.nbt.NBTCore; +import com.songoda.core.nms.nbt.NBTEntity; import com.songoda.core.nms.nbt.NBTItem; +import net.minecraft.server.v1_9_R1.NBTTagCompound; +import org.bukkit.craftbukkit.v1_9_R1.entity.CraftEntity; import org.bukkit.craftbukkit.v1_9_R1.inventory.CraftItemStack; +import org.bukkit.entity.Entity; import org.bukkit.inventory.ItemStack; public class NBTCoreImpl implements NBTCore { @@ -14,8 +17,21 @@ public class NBTCoreImpl implements NBTCore { } @Override - public NBTCompound newCompound() { - return new NBTCompoundImpl(); + public NBTItem newItem() { + return new NBTItemImpl(null); + } + + @Override + public NBTEntity of(Entity entity) { + net.minecraft.server.v1_9_R1.Entity nmsEntity = ((CraftEntity) entity).getHandle(); + NBTTagCompound nbt = new NBTTagCompound(); + nmsEntity.e(nbt); // Method name changed in 1.11 + return new NBTEntityImpl(nbt, nmsEntity); + } + + @Override + public NBTEntity newEntity() { + return new NBTEntityImpl(new NBTTagCompound(), null); } } diff --git a/NMS/NMS-v1_9_R1/src/com/songoda/core/nms/v1_9_R1/nbt/NBTEntityImpl.java b/NMS/NMS-v1_9_R1/src/com/songoda/core/nms/v1_9_R1/nbt/NBTEntityImpl.java new file mode 100644 index 00000000..915db37e --- /dev/null +++ b/NMS/NMS-v1_9_R1/src/com/songoda/core/nms/v1_9_R1/nbt/NBTEntityImpl.java @@ -0,0 +1,52 @@ +package com.songoda.core.nms.v1_9_R1.nbt; + +import com.songoda.core.nms.nbt.NBTEntity; +import net.minecraft.server.v1_9_R1.*; +import org.bukkit.Location; +import org.bukkit.craftbukkit.v1_9_R1.CraftWorld; +import org.bukkit.event.entity.CreatureSpawnEvent; + +public class NBTEntityImpl extends NBTCompoundImpl implements NBTEntity { + + private Entity nmsEntity; + + public NBTEntityImpl(NBTTagCompound entityNBT, Entity nmsEntity) { + super(entityNBT); + this.nmsEntity = nmsEntity; + } + + @Override + public org.bukkit.entity.Entity spawn(Location location) { + String entityType = getNBTObject("entity_type").asString(); + + Entity spawned = ItemMonsterEgg.spawnCreature( // Changed since 1.14 + ((CraftWorld) location.getWorld()).getHandle(), + entityType, // Parameter simplified in 1.10 + location.getBlockX(), location.getBlockY(), location.getBlockZ(), + CreatureSpawnEvent.SpawnReason.DEFAULT + ); + + if (spawned != null) { + spawned.f(compound); // This changed from 1.16.1 + org.bukkit.entity.Entity entity = spawned.getBukkitEntity(); + spawned.setLocation(location.getX(), location.getY(), location.getZ(), + location.getPitch(), location.getYaw()); + nmsEntity = spawned; + return entity; + } + return null; + } + + @Override + public org.bukkit.entity.Entity reSpawn(Location location) { + nmsEntity.dead = true; + return spawn(location); + } + + @Override + public void addExtras() { + String key = EntityTypes.b(nmsEntity); // Changed in 1.12 + if (key != null) + compound.setString("entity_type", key); // Changed in 1.13 + } +} diff --git a/NMS/NMS-v1_9_R1/src/com/songoda/core/nms/v1_9_R1/nbt/NBTItemImpl.java b/NMS/NMS-v1_9_R1/src/com/songoda/core/nms/v1_9_R1/nbt/NBTItemImpl.java index 2ae70a70..abadb7e5 100644 --- a/NMS/NMS-v1_9_R1/src/com/songoda/core/nms/v1_9_R1/nbt/NBTItemImpl.java +++ b/NMS/NMS-v1_9_R1/src/com/songoda/core/nms/v1_9_R1/nbt/NBTItemImpl.java @@ -2,6 +2,7 @@ package com.songoda.core.nms.v1_9_R1.nbt; import com.songoda.core.nms.nbt.NBTItem; import net.minecraft.server.v1_9_R1.NBTTagCompound; +import org.bukkit.Material; import org.bukkit.craftbukkit.v1_9_R1.inventory.CraftItemStack; import org.bukkit.inventory.ItemStack; @@ -10,11 +11,21 @@ public class NBTItemImpl extends NBTCompoundImpl implements NBTItem { private net.minecraft.server.v1_9_R1.ItemStack nmsItem; public NBTItemImpl(net.minecraft.server.v1_9_R1.ItemStack nmsItem) { - super(nmsItem.hasTag() ? nmsItem.getTag() : new NBTTagCompound()); + super(nmsItem != null && nmsItem.hasTag() ? nmsItem.getTag() : new NBTTagCompound()); this.nmsItem = nmsItem; } public ItemStack finish() { - return CraftItemStack.asBukkitCopy(nmsItem); + if (nmsItem == null) { + net.minecraft.server.v1_9_R1.ItemStack itemStack = CraftItemStack.asNMSCopy(new ItemStack(Material.STONE)); + itemStack.setTag(compound); + return CraftItemStack.asBukkitCopy(nmsItem); + } else { + return CraftItemStack.asBukkitCopy(nmsItem); + } + } + + @Override + public void addExtras() { } } diff --git a/NMS/NMS-v1_9_R2/src/com/songoda/core/nms/v1_9_R2/nbt/NBTCompoundImpl.java b/NMS/NMS-v1_9_R2/src/com/songoda/core/nms/v1_9_R2/nbt/NBTCompoundImpl.java index 8b007fe3..ea9677d3 100644 --- a/NMS/NMS-v1_9_R2/src/com/songoda/core/nms/v1_9_R2/nbt/NBTCompoundImpl.java +++ b/NMS/NMS-v1_9_R2/src/com/songoda/core/nms/v1_9_R2/nbt/NBTCompoundImpl.java @@ -2,9 +2,12 @@ package com.songoda.core.nms.v1_9_R2.nbt; import com.songoda.core.nms.nbt.NBTCompound; import com.songoda.core.nms.nbt.NBTObject; +import net.minecraft.server.v1_9_R2.NBTCompressedStreamTools; import net.minecraft.server.v1_9_R2.NBTTagCompound; -public class NBTCompoundImpl implements NBTCompound { +import java.io.*; + +public abstract class NBTCompoundImpl implements NBTCompound { protected NBTTagCompound compound; @@ -74,4 +77,33 @@ public class NBTCompoundImpl implements NBTCompound { return new NBTObjectImpl(compound, tag); } + @Override + public byte[] serialize(String... exclusions) { + try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); + ObjectOutputStream dataOutput = new ObjectOutputStream(outputStream)) { + addExtras(); + NBTTagCompound compound = (NBTTagCompound)this.compound.clone(); // Changed in 1.12 // Changed in 1.9.4 + + for (String exclusion : exclusions) + compound.remove(exclusion); + + NBTCompressedStreamTools.a(compound, (OutputStream) dataOutput); + + return outputStream.toByteArray(); + } catch (Exception e) { + e.printStackTrace(); + } + return null; + } + + @Override + public void deSerialize(byte[] serialized) { + try (ByteArrayInputStream inputStream = new ByteArrayInputStream(serialized); + ObjectInputStream dataInput = new ObjectInputStream(inputStream)) { + compound = NBTCompressedStreamTools.a((InputStream) dataInput); + } catch (Exception e) { + e.printStackTrace(); + } + } + } diff --git a/NMS/NMS-v1_9_R2/src/com/songoda/core/nms/v1_9_R2/nbt/NBTCoreImpl.java b/NMS/NMS-v1_9_R2/src/com/songoda/core/nms/v1_9_R2/nbt/NBTCoreImpl.java index 900a3fd6..85a0e919 100644 --- a/NMS/NMS-v1_9_R2/src/com/songoda/core/nms/v1_9_R2/nbt/NBTCoreImpl.java +++ b/NMS/NMS-v1_9_R2/src/com/songoda/core/nms/v1_9_R2/nbt/NBTCoreImpl.java @@ -1,9 +1,12 @@ package com.songoda.core.nms.v1_9_R2.nbt; -import com.songoda.core.nms.nbt.NBTCompound; import com.songoda.core.nms.nbt.NBTCore; +import com.songoda.core.nms.nbt.NBTEntity; import com.songoda.core.nms.nbt.NBTItem; +import net.minecraft.server.v1_9_R2.NBTTagCompound; +import org.bukkit.craftbukkit.v1_9_R2.entity.CraftEntity; import org.bukkit.craftbukkit.v1_9_R2.inventory.CraftItemStack; +import org.bukkit.entity.Entity; import org.bukkit.inventory.ItemStack; public class NBTCoreImpl implements NBTCore { @@ -14,8 +17,21 @@ public class NBTCoreImpl implements NBTCore { } @Override - public NBTCompound newCompound() { - return new NBTCompoundImpl(); + public NBTItem newItem() { + return new NBTItemImpl(null); + } + + @Override + public NBTEntity of(Entity entity) { + net.minecraft.server.v1_9_R2.Entity nmsEntity = ((CraftEntity) entity).getHandle(); + NBTTagCompound nbt = new NBTTagCompound(); + nmsEntity.e(nbt); // Method name changed in 1.11 + return new NBTEntityImpl(nbt, nmsEntity); + } + + @Override + public NBTEntity newEntity() { + return new NBTEntityImpl(new NBTTagCompound(), null); } } diff --git a/NMS/NMS-v1_9_R2/src/com/songoda/core/nms/v1_9_R2/nbt/NBTEntityImpl.java b/NMS/NMS-v1_9_R2/src/com/songoda/core/nms/v1_9_R2/nbt/NBTEntityImpl.java new file mode 100644 index 00000000..3e4f6ed3 --- /dev/null +++ b/NMS/NMS-v1_9_R2/src/com/songoda/core/nms/v1_9_R2/nbt/NBTEntityImpl.java @@ -0,0 +1,52 @@ +package com.songoda.core.nms.v1_9_R2.nbt; + +import com.songoda.core.nms.nbt.NBTEntity; +import net.minecraft.server.v1_9_R2.*; +import org.bukkit.Location; +import org.bukkit.craftbukkit.v1_9_R2.CraftWorld; +import org.bukkit.event.entity.CreatureSpawnEvent; + +public class NBTEntityImpl extends NBTCompoundImpl implements NBTEntity { + + private Entity nmsEntity; + + public NBTEntityImpl(NBTTagCompound entityNBT, Entity nmsEntity) { + super(entityNBT); + this.nmsEntity = nmsEntity; + } + + @Override + public org.bukkit.entity.Entity spawn(Location location) { + String entityType = getNBTObject("entity_type").asString(); + + Entity spawned = ItemMonsterEgg.spawnCreature( // Changed since 1.14 + ((CraftWorld) location.getWorld()).getHandle(), + entityType, // Parameter simplified in 1.10 + location.getBlockX(), location.getBlockY(), location.getBlockZ(), + CreatureSpawnEvent.SpawnReason.DEFAULT + ); + + if (spawned != null) { + spawned.f(compound); // This changed from 1.16.1 + org.bukkit.entity.Entity entity = spawned.getBukkitEntity(); + spawned.setLocation(location.getX(), location.getY(), location.getZ(), + location.getPitch(), location.getYaw()); + nmsEntity = spawned; + return entity; + } + return null; + } + + @Override + public org.bukkit.entity.Entity reSpawn(Location location) { + nmsEntity.dead = true; + return spawn(location); + } + + @Override + public void addExtras() { + String key = EntityTypes.b(nmsEntity); // Changed in 1.12 + if (key != null) + compound.setString("entity_type", key); // Changed in 1.13 + } +} diff --git a/NMS/NMS-v1_9_R2/src/com/songoda/core/nms/v1_9_R2/nbt/NBTItemImpl.java b/NMS/NMS-v1_9_R2/src/com/songoda/core/nms/v1_9_R2/nbt/NBTItemImpl.java index 3ce737d5..34668c09 100644 --- a/NMS/NMS-v1_9_R2/src/com/songoda/core/nms/v1_9_R2/nbt/NBTItemImpl.java +++ b/NMS/NMS-v1_9_R2/src/com/songoda/core/nms/v1_9_R2/nbt/NBTItemImpl.java @@ -2,6 +2,7 @@ package com.songoda.core.nms.v1_9_R2.nbt; import com.songoda.core.nms.nbt.NBTItem; import net.minecraft.server.v1_9_R2.NBTTagCompound; +import org.bukkit.Material; import org.bukkit.craftbukkit.v1_9_R2.inventory.CraftItemStack; import org.bukkit.inventory.ItemStack; @@ -10,11 +11,21 @@ public class NBTItemImpl extends NBTCompoundImpl implements NBTItem { private net.minecraft.server.v1_9_R2.ItemStack nmsItem; public NBTItemImpl(net.minecraft.server.v1_9_R2.ItemStack nmsItem) { - super(nmsItem.hasTag() ? nmsItem.getTag() : new NBTTagCompound()); + super(nmsItem != null && nmsItem.hasTag() ? nmsItem.getTag() : new NBTTagCompound()); this.nmsItem = nmsItem; } public ItemStack finish() { - return CraftItemStack.asBukkitCopy(nmsItem); + if (nmsItem == null) { + net.minecraft.server.v1_9_R2.ItemStack itemStack = CraftItemStack.asNMSCopy(new ItemStack(Material.STONE)); + itemStack.setTag(compound); + return CraftItemStack.asBukkitCopy(nmsItem); + } else { + return CraftItemStack.asBukkitCopy(nmsItem); + } + } + + @Override + public void addExtras() { } }