Search in sources :

Example 6 with NBTPrimitive

use of net.minecraft.nbt.NBTPrimitive in project BetterQuesting by Funwayguy.

the class ItemComparison method CompareNBTTag.

public static boolean CompareNBTTag(NBTBase tag1, NBTBase tag2, boolean partial) {
    if ((tag1 == null && tag2 != null) || (tag1 != null && tag2 == null)) {
        return false;
    } else if (tag1 == null && tag2 == null) {
        return true;
    }
    if (tag1 instanceof NBTTagCompound && tag2 instanceof NBTTagCompound) {
        return CompareNBTTagCompound((NBTTagCompound) tag1, (NBTTagCompound) tag2, partial);
    } else if (tag1 instanceof NBTTagList && tag2 instanceof NBTTagList) {
        NBTTagList list1 = (NBTTagList) tag1;
        NBTTagList list2 = (NBTTagList) tag2;
        if (list1.tagCount() > list2.tagCount() || (!partial && list1.tagCount() != list2.tagCount())) {
            // Sample is missing requested tags or is not exact
            return false;
        }
        topLoop: for (int i = 0; i < list1.tagCount(); i++) {
            NBTBase lt1 = list1.getCompoundTagAt(i);
            for (int j = 0; j < list2.tagCount(); j++) {
                if (CompareNBTTag(lt1, list2.getCompoundTagAt(j), partial)) {
                    continue topLoop;
                }
            }
            // Couldn't find requested tag in list
            return false;
        }
    } else if (tag1 instanceof NBTTagIntArray && tag2 instanceof NBTTagIntArray) {
        NBTTagIntArray list1 = (NBTTagIntArray) tag1;
        NBTTagIntArray list2 = (NBTTagIntArray) tag2;
        if (list1.getIntArray().length > list2.getIntArray().length || (!partial && list1.getIntArray().length != list2.getIntArray().length)) {
            // Sample is missing requested tags or is not exact
            return false;
        }
        topLoop: for (int i = 0; i < list1.getIntArray().length; i++) {
            for (int j = 0; j < list2.getIntArray().length; j++) {
                if (list1.getIntArray()[i] == list2.getIntArray()[j]) {
                    continue topLoop;
                }
            }
            // Couldn't find requested integer in list
            return false;
        }
        return false;
    } else if (tag1 instanceof NBTTagByteArray && tag2 instanceof NBTTagByteArray) {
        NBTTagByteArray list1 = (NBTTagByteArray) tag1;
        NBTTagByteArray list2 = (NBTTagByteArray) tag2;
        if (list1.getByteArray().length > list2.getByteArray().length || (!partial && list1.getByteArray().length != list2.getByteArray().length)) {
            // Sample is missing requested tags or is not exact
            return false;
        }
        // Duplicate control
        ArrayList<Integer> usedIdxs = new ArrayList<Integer>();
        topLoop: for (int i = 0; i < list1.getByteArray().length; i++) {
            for (int j = 0; j < list2.getByteArray().length; j++) {
                if (usedIdxs.contains(j)) {
                    continue;
                } else if (list1.getByteArray()[i] == list2.getByteArray()[j]) {
                    usedIdxs.add(j);
                    continue topLoop;
                }
            }
            // Couldn't find requested integer in list
            return false;
        }
        return false;
    } else if (tag1 instanceof NBTTagString && tag2 instanceof NBTTagString) {
        return tag1.equals(tag2);
    } else if (// Standardize numbers to not care about format
    tag1 instanceof NBTPrimitive && tag2 instanceof NBTPrimitive) {
        Number num1 = NBTConverter.getNumber(tag1);
        Number num2 = NBTConverter.getNumber(tag2);
        // Second number will be cast to the requested number format
        if (tag1 instanceof NBTTagFloat || tag1 instanceof NBTTagDouble) {
            return num1.doubleValue() == num2.doubleValue();
        } else {
            return num1.longValue() == num2.longValue();
        }
    } else {
        return tag1.equals(tag2);
    }
    return true;
}
Also used : NBTPrimitive(net.minecraft.nbt.NBTPrimitive) NBTTagFloat(net.minecraft.nbt.NBTTagFloat) NBTTagCompound(net.minecraft.nbt.NBTTagCompound) ArrayList(java.util.ArrayList) NBTTagIntArray(net.minecraft.nbt.NBTTagIntArray) NBTTagList(net.minecraft.nbt.NBTTagList) NBTBase(net.minecraft.nbt.NBTBase) NBTTagString(net.minecraft.nbt.NBTTagString) NBTTagByteArray(net.minecraft.nbt.NBTTagByteArray) NBTTagDouble(net.minecraft.nbt.NBTTagDouble)

Aggregations

NBTPrimitive (net.minecraft.nbt.NBTPrimitive)6 NBTTagCompound (net.minecraft.nbt.NBTTagCompound)4 NBTBase (net.minecraft.nbt.NBTBase)3 NBTTagList (net.minecraft.nbt.NBTTagList)3 NBTTagByteArray (net.minecraft.nbt.NBTTagByteArray)2 NBTTagString (net.minecraft.nbt.NBTTagString)2 EnumFacing (net.minecraft.util.EnumFacing)2 GuiBigTextField (betterquesting.api.client.gui.controls.GuiBigTextField)1 GuiButtonJson (betterquesting.api.client.gui.controls.GuiButtonJson)1 GuiButtonThemed (betterquesting.api.client.gui.controls.GuiButtonThemed)1 GuiNumberField (betterquesting.api.client.gui.controls.GuiNumberField)1 IQuest (betterquesting.api.questing.IQuest)1 TextCallbackJsonArray (betterquesting.client.gui.editors.json.TextCallbackJsonArray)1 TextCallbackJsonObject (betterquesting.client.gui.editors.json.TextCallbackJsonObject)1 ArrayList (java.util.ArrayList)1 BitSet (java.util.BitSet)1 EntityPlayer (net.minecraft.entity.player.EntityPlayer)1 NBTTagDouble (net.minecraft.nbt.NBTTagDouble)1 NBTTagFloat (net.minecraft.nbt.NBTTagFloat)1 NBTTagIntArray (net.minecraft.nbt.NBTTagIntArray)1