mirror of
				https://github.com/mcMMO-Dev/mcMMO.git
				synced 2025-11-04 11:03:43 +01:00 
			
		
		
		
	Work on NBT adapters
This commit is contained in:
		@@ -0,0 +1,7 @@
 | 
			
		||||
package com.gmail.nossr50.core.adapters;
 | 
			
		||||
 | 
			
		||||
import com.gmail.nossr50.core.nbt.NBTBase;
 | 
			
		||||
 | 
			
		||||
public interface NBTAdapter {
 | 
			
		||||
    Object asNative(NBTBase nbtBase);
 | 
			
		||||
}
 | 
			
		||||
@@ -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
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -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());
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
@@ -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;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
@@ -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;
 | 
			
		||||
 
 | 
			
		||||
@@ -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<String, NBTBase> tagMap;
 | 
			
		||||
 | 
			
		||||
    public NBTTagCompound(String key) {
 | 
			
		||||
    public NBTCompound(String key) {
 | 
			
		||||
        tagMap = new LinkedHashMap<>();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
@@ -0,0 +1,8 @@
 | 
			
		||||
package com.gmail.nossr50.core.nbt;
 | 
			
		||||
 | 
			
		||||
public class NBTEnd implements NBTBase {
 | 
			
		||||
    @Override
 | 
			
		||||
    public NBTType getNBTType() {
 | 
			
		||||
        return NBTType.END;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -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;
 | 
			
		||||
 
 | 
			
		||||
@@ -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;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user