From bf94073935877287587b5bf7ff2094c925aaed57 Mon Sep 17 00:00:00 2001 From: nossr50 Date: Tue, 29 Oct 2019 19:00:16 -0700 Subject: [PATCH] Work on NBT adapters --- .../nossr50/core/adapters/NBTAdapter.java | 7 ++ .../adapters/NMS_114/BukkitNBTAdapter.java | 93 +++++++++++++++++++ .../NMS_114/BukkitPlatformAdapter.java | 17 ++++ .../core/adapters/PlatformAdapter.java | 15 +++ .../com/gmail/nossr50/core/nbt/NBTByte.java | 4 + .../{NBTTagCompound.java => NBTCompound.java} | 4 +- .../com/gmail/nossr50/core/nbt/NBTEnd.java | 8 ++ .../com/gmail/nossr50/core/nbt/NBTLong.java | 4 +- .../gmail/nossr50/util/nbt/NBTFactory.java | 69 ++++++++++++++ 9 files changed, 217 insertions(+), 4 deletions(-) create mode 100644 mcmmo-core/src/main/java/com/gmail/nossr50/core/adapters/NBTAdapter.java create mode 100644 mcmmo-core/src/main/java/com/gmail/nossr50/core/adapters/NMS_114/BukkitNBTAdapter.java create mode 100644 mcmmo-core/src/main/java/com/gmail/nossr50/core/adapters/NMS_114/BukkitPlatformAdapter.java create mode 100644 mcmmo-core/src/main/java/com/gmail/nossr50/core/adapters/PlatformAdapter.java rename mcmmo-core/src/main/java/com/gmail/nossr50/core/nbt/{NBTTagCompound.java => NBTCompound.java} (91%) create mode 100644 mcmmo-core/src/main/java/com/gmail/nossr50/core/nbt/NBTEnd.java create mode 100644 mcmmo-core/src/main/java/com/gmail/nossr50/util/nbt/NBTFactory.java diff --git a/mcmmo-core/src/main/java/com/gmail/nossr50/core/adapters/NBTAdapter.java b/mcmmo-core/src/main/java/com/gmail/nossr50/core/adapters/NBTAdapter.java new file mode 100644 index 000000000..8a6ee639f --- /dev/null +++ b/mcmmo-core/src/main/java/com/gmail/nossr50/core/adapters/NBTAdapter.java @@ -0,0 +1,7 @@ +package com.gmail.nossr50.core.adapters; + +import com.gmail.nossr50.core.nbt.NBTBase; + +public interface NBTAdapter { + Object asNative(NBTBase nbtBase); +} diff --git a/mcmmo-core/src/main/java/com/gmail/nossr50/core/adapters/NMS_114/BukkitNBTAdapter.java b/mcmmo-core/src/main/java/com/gmail/nossr50/core/adapters/NMS_114/BukkitNBTAdapter.java new file mode 100644 index 000000000..79cd5b4b9 --- /dev/null +++ b/mcmmo-core/src/main/java/com/gmail/nossr50/core/adapters/NMS_114/BukkitNBTAdapter.java @@ -0,0 +1,93 @@ +package com.gmail.nossr50.core.adapters.NMS_114; + +import com.gmail.nossr50.core.adapters.NBTAdapter; +import com.gmail.nossr50.core.nbt.*; +import com.gmail.nossr50.core.nbt.NBTBase; +import com.gmail.nossr50.core.nbt.NBTList; +import com.gmail.nossr50.core.nbt.NBTCompound; +import net.minecraft.server.v1_14_R1.*; + +public class BukkitNBTAdapter implements NBTAdapter { + + @Override + public Object asNative(NBTBase nbtBase) { + switch(nbtBase.getNBTType()) { + case END: + return new NBTTagEnd(); + case BYTE: + return asNativeNBTByte((NBTByte) nbtBase); + case SHORT: + return asNativeNBTShort((NBTShort) nbtBase); + case INT: + return asNativeNBTInt((NBTInteger) nbtBase); + case LONG: + return asNativeNBTLong((NBTLong) nbtBase); + case FLOAT: + return asNativeNBTFloat((NBTFloat) nbtBase); + case DOUBLE: + return asNativeNBTDouble((NBTDouble) nbtBase); + case BYTE_ARRAY: + return asNativeNBTByteArray((NBTByteArray) nbtBase); + case STRING: + return asNativeNBTString((NBTString) nbtBase); + case LIST: + return asNativeNBTList((NBTList) nbtBase); + case COMPOUND: + return ; + case INT_ARRAY: + break; + case LONG_ARRAY: + break; + } + + return null; + } + + /** + * Create a NBTTagByte (NMS Type) from our NBTByte representation + * @param nbtByte target NBTByte + * @return NBTTagByte copy of our NBTByte representation + */ + private NBTTagByte asNativeNBTByte(NBTByte nbtByte) { + return new NBTTagByte(nbtByte.getValue()); + } + + private NBTTagShort asNativeNBTShort(NBTShort nbtShort) { + return new NBTTagShort(nbtShort.getValue()); + } + + private NBTTagInt asNativeNBTInt(NBTInteger nbtInteger) { + return new NBTTagInt(nbtInteger.getValue()); + } + + private NBTTagLong asNativeNBTLong(NBTLong nbtLong) { + return new NBTTagLong(nbtLong.getValue()); + } + + private NBTTagFloat asNativeNBTFloat(NBTFloat nbtFloat) { + return new NBTTagFloat(nbtFloat.getValue()); + } + + private NBTTagDouble asNativeNBTDouble(NBTDouble nbtDouble) { + return new NBTTagDouble(nbtDouble.getValue()); + } + + private NBTTagByteArray asNativeNBTByteArray(NBTByteArray nbtByteArray) { + return new NBTTagByteArray(nbtByteArray.getValues()); + } + + private NBTTagString asNativeNBTString(NBTString nbtString) { + return new NBTTagString(nbtString.getValue()); + } + + private NBTTagList asNativeNBTList(NBTList nbtList) { + NBTTagList nbtTagList = new NBTTagList(); + nbtList.setValues(nbtList.getValues()); + return nbtTagList; + } + + private NBTTagCompound asNativeNBTCompound(NBTCompound nbtCompound) { + NBTTagCompound nbtTagCompound = new NBTTagCompound(); + nbtCompound + } +} diff --git a/mcmmo-core/src/main/java/com/gmail/nossr50/core/adapters/NMS_114/BukkitPlatformAdapter.java b/mcmmo-core/src/main/java/com/gmail/nossr50/core/adapters/NMS_114/BukkitPlatformAdapter.java new file mode 100644 index 000000000..c97df31bf --- /dev/null +++ b/mcmmo-core/src/main/java/com/gmail/nossr50/core/adapters/NMS_114/BukkitPlatformAdapter.java @@ -0,0 +1,17 @@ +package com.gmail.nossr50.core.adapters.NMS_114; + +import com.gmail.nossr50.core.adapters.NBTAdapter; +import com.gmail.nossr50.core.adapters.PlatformAdapter; +import com.gmail.nossr50.core.nbt.NBTBase; +import com.gmail.nossr50.core.nbt.NBTByte; +import net.minecraft.server.v1_14_R1.NBTTagByte; + +public class BukkitPlatformAdapter extends PlatformAdapter { + + public BukkitPlatformAdapter() { + super(new BukkitNBTAdapter()); + } + + + +} diff --git a/mcmmo-core/src/main/java/com/gmail/nossr50/core/adapters/PlatformAdapter.java b/mcmmo-core/src/main/java/com/gmail/nossr50/core/adapters/PlatformAdapter.java new file mode 100644 index 000000000..3a6853e45 --- /dev/null +++ b/mcmmo-core/src/main/java/com/gmail/nossr50/core/adapters/PlatformAdapter.java @@ -0,0 +1,15 @@ +package com.gmail.nossr50.core.adapters; + +public abstract class PlatformAdapter { + + private NBTAdapter nbtAdapter; //nbt + + public PlatformAdapter(NBTAdapter nbtAdapter) { + this.nbtAdapter = nbtAdapter; + } + + public NBTAdapter getNbtAdapter() { + return nbtAdapter; + } + +} diff --git a/mcmmo-core/src/main/java/com/gmail/nossr50/core/nbt/NBTByte.java b/mcmmo-core/src/main/java/com/gmail/nossr50/core/nbt/NBTByte.java index b01a15f21..93a3fab17 100644 --- a/mcmmo-core/src/main/java/com/gmail/nossr50/core/nbt/NBTByte.java +++ b/mcmmo-core/src/main/java/com/gmail/nossr50/core/nbt/NBTByte.java @@ -5,6 +5,10 @@ public class NBTByte implements NBTBase { private String key; private Byte value; + public NBTByte(Byte value) { + this.value = value; + } + @Override public NBTType getNBTType() { return NBTType.BYTE; diff --git a/mcmmo-core/src/main/java/com/gmail/nossr50/core/nbt/NBTTagCompound.java b/mcmmo-core/src/main/java/com/gmail/nossr50/core/nbt/NBTCompound.java similarity index 91% rename from mcmmo-core/src/main/java/com/gmail/nossr50/core/nbt/NBTTagCompound.java rename to mcmmo-core/src/main/java/com/gmail/nossr50/core/nbt/NBTCompound.java index d5ca3c542..70d6d544d 100644 --- a/mcmmo-core/src/main/java/com/gmail/nossr50/core/nbt/NBTTagCompound.java +++ b/mcmmo-core/src/main/java/com/gmail/nossr50/core/nbt/NBTCompound.java @@ -5,12 +5,12 @@ import java.util.LinkedHashMap; import java.util.Map; import java.util.Set; -public class NBTTagCompound implements NBTBase { +public class NBTCompound implements NBTBase { private String key; private Map tagMap; - public NBTTagCompound(String key) { + public NBTCompound(String key) { tagMap = new LinkedHashMap<>(); } diff --git a/mcmmo-core/src/main/java/com/gmail/nossr50/core/nbt/NBTEnd.java b/mcmmo-core/src/main/java/com/gmail/nossr50/core/nbt/NBTEnd.java new file mode 100644 index 000000000..71bf1d7ef --- /dev/null +++ b/mcmmo-core/src/main/java/com/gmail/nossr50/core/nbt/NBTEnd.java @@ -0,0 +1,8 @@ +package com.gmail.nossr50.core.nbt; + +public class NBTEnd implements NBTBase { + @Override + public NBTType getNBTType() { + return NBTType.END; + } +} diff --git a/mcmmo-core/src/main/java/com/gmail/nossr50/core/nbt/NBTLong.java b/mcmmo-core/src/main/java/com/gmail/nossr50/core/nbt/NBTLong.java index 630f748fa..20e6f50ea 100644 --- a/mcmmo-core/src/main/java/com/gmail/nossr50/core/nbt/NBTLong.java +++ b/mcmmo-core/src/main/java/com/gmail/nossr50/core/nbt/NBTLong.java @@ -2,8 +2,8 @@ package com.gmail.nossr50.core.nbt; public class NBTLong implements NBTBase { - public String key; - public long value; + private String key; + private long value; public String getKey() { return key; diff --git a/mcmmo-core/src/main/java/com/gmail/nossr50/util/nbt/NBTFactory.java b/mcmmo-core/src/main/java/com/gmail/nossr50/util/nbt/NBTFactory.java new file mode 100644 index 000000000..35529853f --- /dev/null +++ b/mcmmo-core/src/main/java/com/gmail/nossr50/util/nbt/NBTFactory.java @@ -0,0 +1,69 @@ +package com.gmail.nossr50.util.nbt; + +import com.gmail.nossr50.core.nbt.NBTByte; +import com.gmail.nossr50.core.nbt.NBTCompound; +import net.minecraft.server.v1_14_R1.NBTTagByte; + +public class NBTFactory { + /** + * Converts NMS NBT types into our own NBT type representation + * @param nmsNBT target NMS Compound + * @return NMS Representation of our NBT + */ + public NBTCompound asNBT(net.minecraft.server.v1_14_R1.NBTTagCompound nmsNBT) { + NBTCompound nbtCompound = new NBTCompound(""); + + //Traverse the NMS Map + for(String key : nmsNBT.getKeys()) { + + } + } + + /** + * Convert our NBT type into the NMS NBT Type equivalent + * @param nbtCompound target nbt compound + * @return NMS NBT copy of our NBT type + */ + public net.minecraft.server.v1_14_R1.NBTTagCompound asNMSCopy(NBTCompound nbtCompound) { + + } + + /** + * Create a new NMS NBT tag compound with only 1 tag compound named "tag" + * @return new NMS NBT tag compound + */ + private net.minecraft.server.v1_14_R1.NBTTagCompound makeNewNMSNBT() { + net.minecraft.server.v1_14_R1.NBTTagCompound nbtTagCompound = new net.minecraft.server.v1_14_R1.NBTTagCompound(); + + //Add the 'tag' compound where arbitrary data persists + nbtTagCompound.set("tag", new net.minecraft.server.v1_14_R1.NBTTagCompound()); + return nbtTagCompound; + } + + private NBTCompound deepCopy(NBTCompound target, String key, net.minecraft.server.v1_14_R1.NBTBase nbtBase) { + switch (nbtBase.getTypeId()) { + case 0: + return new NBTCompound(); + } + } + + /** + * Create a NBTByte representation of NBTTagByte (NMS Type) + * @param nmsNBTByte target NMS NBTTagByte + * @return NBTByte representation of the targeted NMS nbt-type + */ + private NBTByte asNBTByte(NBTTagByte nmsNBTByte) { + NBTByte nbtByte = new NBTByte(nmsNBTByte.asByte()); + return nbtByte; + } + + /** + * Create a NBTTagByte (NMS Type) from our NBTByte representation + * @param nbtByte target NBTByte + * @return NBTTagByte copy of our NBTByte representation + */ + private NBTTagByte asNBTTagByte(NBTByte nbtByte) { + NBTTagByte nbtTagByte = new NBTTagByte(nbtByte.getValue()); + return nbtTagByte; + } +}