Merge branch 'development'

This commit is contained in:
Brianna 2020-08-22 16:12:59 -05:00
commit 6f457cb0e3
63 changed files with 1793 additions and 101 deletions

View File

@ -4,7 +4,7 @@ stages:
variables:
name: "SongodaCore"
path: "/builds/$CI_PROJECT_PATH"
version: "2.3.48"
version: "2.4"
build:
stage: build

View File

@ -46,6 +46,7 @@
<version>3.1.1</version>
<configuration>
<doclint>none</doclint>
<source>1.8</source>
</configuration>
<executions>
<execution>

View File

@ -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(" ");

View File

@ -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;
}

View File

@ -22,4 +22,10 @@ public interface NBTCompound {
NBTObject getNBTObject(String tag);
byte[] serialize(String... exclusions);
void deSerialize(byte[] serialized);
void addExtras();
}

View File

@ -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();
}

View File

@ -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);
}

View File

@ -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();
}
}
}

View File

@ -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);
}
}

View File

@ -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
}
}

View File

@ -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() {
}
}

View File

@ -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();
}
}
}

View File

@ -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);
}
}

View File

@ -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
}
}

View File

@ -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() {
}
}

View File

@ -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();
}
}
}

View File

@ -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);
}
}

View File

@ -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
}
}

View File

@ -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() {
}
}

View File

@ -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();
}
}
}

View File

@ -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);
}
}

View File

@ -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<EntityTypes<?>> 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
}
}

View File

@ -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() {
}
}

View File

@ -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();
}
}
}

View File

@ -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);
}
}

View File

@ -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<EntityTypes<?>> 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
}
}

View File

@ -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() {
}
}

View File

@ -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();
}
}
}

View File

@ -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);
}
}

View File

@ -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<EntityTypes<?>> 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());
}
}

View File

@ -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() {
}
}

View File

@ -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();
}
}
}

View File

@ -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);
}
}

View File

@ -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<EntityTypes<?>> 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());
}
}

View File

@ -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() {
}
}

View File

@ -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();
}
}
}

View File

@ -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);
}
}

View File

@ -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<EntityTypes<?>> 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());
}
}

View File

@ -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() {
}
}

View File

@ -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();
}
}
}

View File

@ -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);
}
}

View File

@ -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<EntityTypes<?>> 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());
}
}

View File

@ -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() {
}
}

View File

@ -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();
}
}
}

View File

@ -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);
}
}

View File

@ -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<String, Integer>) 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
}
}

View File

@ -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() {
}
}

View File

@ -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();
}
}
}

View File

@ -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);
}
}

View File

@ -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<String, Integer>) 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
}
}

View File

@ -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() {
}
}

View File

@ -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();
}
}
}

View File

@ -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);
}
}

View File

@ -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<String, Integer>) 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
}
}

View File

@ -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() {
}
}

View File

@ -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();
}
}
}

View File

@ -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);
}
}

View File

@ -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
}
}

View File

@ -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() {
}
}

View File

@ -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();
}
}
}

View File

@ -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);
}
}

View File

@ -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
}
}

View File

@ -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() {
}
}