From 17034afb944297a7ae624f6bd5eeeda744276ec9 Mon Sep 17 00:00:00 2001 From: Aurora Date: Tue, 18 Aug 2020 12:46:31 +0200 Subject: [PATCH 1/4] Adjust methods to support openJDK --- Core/pom.xml | 1 + .../songoda/core/utils/ReflectionUtils.java | 36 ++++++++++++------- 2 files changed, 25 insertions(+), 12 deletions(-) diff --git a/Core/pom.xml b/Core/pom.xml index d671f34a..d7831f88 100644 --- a/Core/pom.xml +++ b/Core/pom.xml @@ -46,6 +46,7 @@ 3.1.1 none + 1.8 diff --git a/Core/src/main/java/com/songoda/core/utils/ReflectionUtils.java b/Core/src/main/java/com/songoda/core/utils/ReflectionUtils.java index f36f25f7..8681a89f 100644 --- a/Core/src/main/java/com/songoda/core/utils/ReflectionUtils.java +++ b/Core/src/main/java/com/songoda/core/utils/ReflectionUtils.java @@ -64,17 +64,29 @@ public class ReflectionUtils { } // does not work in JRE 8+ - private static Method getStackTraceElementMethod; - private static Method getStackTraceDepthMethod; + private static Method j7getStackTraceElementMethod; + private static Method j7getStackTraceDepthMethod; + + // does not work in JRE != 8 + private static Method j8getJavaLangAccess; + private static Method j8getStackTraceElementMethod; static { try { - getStackTraceElementMethod = Throwable.class.getDeclaredMethod("getStackTraceElement", int.class); - getStackTraceElementMethod.setAccessible(true); - getStackTraceDepthMethod = Throwable.class.getDeclaredMethod("getStackTraceDepth"); - getStackTraceDepthMethod.setAccessible(true); + j7getStackTraceElementMethod = Throwable.class.getDeclaredMethod("getStackTraceElement", int.class); + j7getStackTraceElementMethod.setAccessible(true); + j7getStackTraceDepthMethod = Throwable.class.getDeclaredMethod("getStackTraceDepth"); + j7getStackTraceDepthMethod.setAccessible(true); } catch (Exception ex) { - getStackTraceElementMethod = getStackTraceDepthMethod = null; + j7getStackTraceElementMethod = j7getStackTraceDepthMethod = null; + } + try { + j8getJavaLangAccess = Class.forName("sun.misc.SharedSecrets").getDeclaredMethod("getStackTraceElement"); + j8getJavaLangAccess.setAccessible(true); + j8getStackTraceElementMethod = Class.forName("sun.misc.JavaLangAccess").getDeclaredMethod("getStackTraceDepth", Throwable.class, int.class); + j8getStackTraceElementMethod.setAccessible(true); + } catch (Exception ex) { + j8getJavaLangAccess = j8getStackTraceElementMethod = null; } } @@ -87,18 +99,18 @@ public class ReflectionUtils { try { Throwable dummy = new Throwable(); - if (JAVA_VERSION >= 8 && JAVA_VERSION < 9) { - return sun.misc.SharedSecrets.getJavaLangAccess().getStackTraceElement(dummy, index); + if (j8getStackTraceElementMethod != null) { + return (StackTraceElement) j8getStackTraceElementMethod.invoke(j8getJavaLangAccess.invoke(null), dummy, index); // } else if (JAVA_VERSION >= 9) { // return StackWalker.getInstance(Collections.emptySet(), index + 1) // .walk(s -> s.skip(index).findFirst()) // .orElse(null); - } else if (getStackTraceElementMethod == null) { + } else if (j7getStackTraceElementMethod == null) { // better than nothing, right? :/ return (new Throwable()).getStackTrace()[index]; } else { - if (index < (Integer) getStackTraceDepthMethod.invoke(dummy)) { - return (StackTraceElement) getStackTraceElementMethod.invoke(new Throwable(), index); + if (index < (Integer) j7getStackTraceDepthMethod.invoke(dummy)) { + return (StackTraceElement) j7getStackTraceElementMethod.invoke(new Throwable(), index); } else { return null; } From 28b2d99b049a98b8c5744b72bcc8aea1568a5c5c Mon Sep 17 00:00:00 2001 From: Brianna Date: Sat, 22 Aug 2020 16:07:37 -0500 Subject: [PATCH 2/4] Added the required ability to load data separately. --- Core/src/main/java/com/songoda/core/SongodaPlugin.java | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/Core/src/main/java/com/songoda/core/SongodaPlugin.java b/Core/src/main/java/com/songoda/core/SongodaPlugin.java index 34f94bc8..9054b546 100644 --- a/Core/src/main/java/com/songoda/core/SongodaPlugin.java +++ b/Core/src/main/java/com/songoda/core/SongodaPlugin.java @@ -21,6 +21,7 @@ public abstract class SongodaPlugin extends JavaPlugin { protected Locale locale; protected Config config = new Config(this); + protected long dataLoadDelay = 20L; protected ConsoleCommandSender console = Bukkit.getConsoleSender(); private boolean emergencyStop = false; @@ -31,6 +32,8 @@ public abstract class SongodaPlugin extends JavaPlugin { public abstract void onPluginDisable(); + public abstract void onDataLoad(); + /** * Called after reloadConfig​() is called */ @@ -95,6 +98,8 @@ public abstract class SongodaPlugin extends JavaPlugin { locale = Locale.loadDefaultLocale(this, "en_US"); // plugin setup onPluginEnable(); + // Load Data. + Bukkit.getScheduler().runTaskLater(this, this::onDataLoad, dataLoadDelay); if(emergencyStop) { console.sendMessage(ChatColor.RED + "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"); console.sendMessage(" "); From f24188d3bd541d80dfafc173f9cb7ded954c68e5 Mon Sep 17 00:00:00 2001 From: Brianna Date: Sat, 22 Aug 2020 16:09:21 -0500 Subject: [PATCH 3/4] Added Entity and serialization options to the NBT --- .../com/songoda/core/nms/nbt/NBTCompound.java | 6 ++ .../src/com/songoda/core/nms/nbt/NBTCore.java | 7 +- .../com/songoda/core/nms/nbt/NBTEntity.java | 12 ++++ .../nms/v1_10_R1/nbt/NBTCompoundImpl.java | 34 ++++++++- .../core/nms/v1_10_R1/nbt/NBTCoreImpl.java | 22 +++++- .../core/nms/v1_10_R1/nbt/NBTEntityImpl.java | 52 ++++++++++++++ .../core/nms/v1_10_R1/nbt/NBTItemImpl.java | 15 +++- .../nms/v1_11_R1/nbt/NBTCompoundImpl.java | 34 ++++++++- .../core/nms/v1_11_R1/nbt/NBTCoreImpl.java | 22 +++++- .../core/nms/v1_11_R1/nbt/NBTEntityImpl.java | 52 ++++++++++++++ .../core/nms/v1_11_R1/nbt/NBTItemImpl.java | 15 +++- .../nms/v1_12_R1/nbt/NBTCompoundImpl.java | 34 ++++++++- .../core/nms/v1_12_R1/nbt/NBTCoreImpl.java | 22 +++++- .../core/nms/v1_12_R1/nbt/NBTEntityImpl.java | 53 ++++++++++++++ .../core/nms/v1_12_R1/nbt/NBTItemImpl.java | 15 +++- .../nms/v1_13_R1/nbt/NBTCompoundImpl.java | 34 ++++++++- .../core/nms/v1_13_R1/nbt/NBTCoreImpl.java | 22 +++++- .../core/nms/v1_13_R1/nbt/NBTEntityImpl.java | 62 ++++++++++++++++ .../core/nms/v1_13_R1/nbt/NBTItemImpl.java | 15 +++- .../nms/v1_13_R2/nbt/NBTCompoundImpl.java | 34 ++++++++- .../core/nms/v1_13_R2/nbt/NBTCoreImpl.java | 22 +++++- .../core/nms/v1_13_R2/nbt/NBTEntityImpl.java | 62 ++++++++++++++++ .../core/nms/v1_13_R2/nbt/NBTItemImpl.java | 15 +++- .../nms/v1_14_R1/nbt/NBTCompoundImpl.java | 34 ++++++++- .../core/nms/v1_14_R1/nbt/NBTCoreImpl.java | 22 +++++- .../core/nms/v1_14_R1/nbt/NBTEntityImpl.java | 58 +++++++++++++++ .../core/nms/v1_14_R1/nbt/NBTItemImpl.java | 15 +++- .../nms/v1_15_R1/nbt/NBTCompoundImpl.java | 35 ++++++++- .../core/nms/v1_15_R1/nbt/NBTCoreImpl.java | 22 +++++- .../core/nms/v1_15_R1/nbt/NBTEntityImpl.java | 58 +++++++++++++++ .../core/nms/v1_15_R1/nbt/NBTItemImpl.java | 15 +++- .../nms/v1_16_R1/nbt/NBTCompoundImpl.java | 35 ++++++++- .../core/nms/v1_16_R1/nbt/NBTCoreImpl.java | 22 +++++- .../core/nms/v1_16_R1/nbt/NBTEntityImpl.java | 57 +++++++++++++++ .../core/nms/v1_16_R1/nbt/NBTItemImpl.java | 15 +++- .../nms/v1_16_R2/nbt/NBTCompoundImpl.java | 35 ++++++++- .../core/nms/v1_16_R2/nbt/NBTCoreImpl.java | 21 +++++- .../core/nms/v1_16_R2/nbt/NBTEntityImpl.java | 57 +++++++++++++++ .../core/nms/v1_16_R2/nbt/NBTItemImpl.java | 16 ++++- .../core/nms/v1_8_R1/nbt/NBTCompoundImpl.java | 34 ++++++++- .../core/nms/v1_8_R1/nbt/NBTCoreImpl.java | 22 +++++- .../core/nms/v1_8_R1/nbt/NBTEntityImpl.java | 71 +++++++++++++++++++ .../core/nms/v1_8_R1/nbt/NBTItemImpl.java | 15 +++- .../core/nms/v1_8_R2/nbt/NBTCompoundImpl.java | 34 ++++++++- .../core/nms/v1_8_R2/nbt/NBTCoreImpl.java | 22 +++++- .../core/nms/v1_8_R2/nbt/NBTEntityImpl.java | 71 +++++++++++++++++++ .../core/nms/v1_8_R2/nbt/NBTItemImpl.java | 15 +++- .../core/nms/v1_8_R3/nbt/NBTCompoundImpl.java | 34 ++++++++- .../core/nms/v1_8_R3/nbt/NBTCoreImpl.java | 22 +++++- .../core/nms/v1_8_R3/nbt/NBTEntityImpl.java | 71 +++++++++++++++++++ .../core/nms/v1_8_R3/nbt/NBTItemImpl.java | 15 +++- .../core/nms/v1_9_R1/nbt/NBTCompoundImpl.java | 34 ++++++++- .../core/nms/v1_9_R1/nbt/NBTCoreImpl.java | 22 +++++- .../core/nms/v1_9_R1/nbt/NBTEntityImpl.java | 52 ++++++++++++++ .../core/nms/v1_9_R1/nbt/NBTItemImpl.java | 15 +++- .../core/nms/v1_9_R2/nbt/NBTCompoundImpl.java | 34 ++++++++- .../core/nms/v1_9_R2/nbt/NBTCoreImpl.java | 22 +++++- .../core/nms/v1_9_R2/nbt/NBTEntityImpl.java | 52 ++++++++++++++ .../core/nms/v1_9_R2/nbt/NBTItemImpl.java | 15 +++- 59 files changed, 1762 insertions(+), 88 deletions(-) create mode 100644 NMS/NMS-API/src/com/songoda/core/nms/nbt/NBTEntity.java create mode 100644 NMS/NMS-v1_10_R1/src/com/songoda/core/nms/v1_10_R1/nbt/NBTEntityImpl.java create mode 100644 NMS/NMS-v1_11_R1/src/com/songoda/core/nms/v1_11_R1/nbt/NBTEntityImpl.java create mode 100644 NMS/NMS-v1_12_R1/src/com/songoda/core/nms/v1_12_R1/nbt/NBTEntityImpl.java create mode 100644 NMS/NMS-v1_13_R1/src/com/songoda/core/nms/v1_13_R1/nbt/NBTEntityImpl.java create mode 100644 NMS/NMS-v1_13_R2/src/com/songoda/core/nms/v1_13_R2/nbt/NBTEntityImpl.java create mode 100644 NMS/NMS-v1_14_R1/src/com/songoda/core/nms/v1_14_R1/nbt/NBTEntityImpl.java create mode 100644 NMS/NMS-v1_15_R1/src/com/songoda/core/nms/v1_15_R1/nbt/NBTEntityImpl.java create mode 100644 NMS/NMS-v1_16_R1/src/com/songoda/core/nms/v1_16_R1/nbt/NBTEntityImpl.java create mode 100644 NMS/NMS-v1_16_R2/src/com/songoda/core/nms/v1_16_R2/nbt/NBTEntityImpl.java create mode 100644 NMS/NMS-v1_8_R1/src/com/songoda/core/nms/v1_8_R1/nbt/NBTEntityImpl.java create mode 100644 NMS/NMS-v1_8_R2/src/com/songoda/core/nms/v1_8_R2/nbt/NBTEntityImpl.java create mode 100644 NMS/NMS-v1_8_R3/src/com/songoda/core/nms/v1_8_R3/nbt/NBTEntityImpl.java create mode 100644 NMS/NMS-v1_9_R1/src/com/songoda/core/nms/v1_9_R1/nbt/NBTEntityImpl.java create mode 100644 NMS/NMS-v1_9_R2/src/com/songoda/core/nms/v1_9_R2/nbt/NBTEntityImpl.java 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() { } } From 8c6c9b1daf52601ea76ab6371c5e798cdf4981e0 Mon Sep 17 00:00:00 2001 From: Brianna Date: Sat, 22 Aug 2020 16:12:02 -0500 Subject: [PATCH 4/4] version 2.4 --- .gitlab-ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index efce72e9..1deb4081 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -4,7 +4,7 @@ stages: variables: name: "SongodaCore" path: "/builds/$CI_PROJECT_PATH" - version: "2.3.48" + version: "2.4" build: stage: build