net.minecraft.nbt.ListNBT Java Examples

The following examples show how to use net.minecraft.nbt.ListNBT. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: UpgradeTools.java    From MiningGadgets with MIT License 6 votes vote down vote up
public static void updateUpgrade(ItemStack tool, Upgrade upgrade) {
    CompoundNBT tagCompound = tool.getOrCreateTag();
    ListNBT list = tagCompound.getList(KEY_UPGRADES, Constants.NBT.TAG_COMPOUND);

    list.forEach( e -> {
        CompoundNBT compound = (CompoundNBT) e;
        String name = compound.getString(KEY_UPGRADE);
        boolean enabled = compound.getBoolean(KEY_ENABLED);

        if( (name.contains(Upgrade.FORTUNE_1.getBaseName()) && enabled && upgrade.lazyIs(Upgrade.SILK) )
                        || (name.equals(Upgrade.SILK.getBaseName()) && enabled && upgrade.lazyIs(Upgrade.FORTUNE_1) ))
            compound.putBoolean(KEY_ENABLED, false);

        if( name.equals(upgrade.getName()) )
            compound.putBoolean(KEY_ENABLED, !compound.getBoolean(KEY_ENABLED));
    });
}
 
Example #2
Source File: UpgradeTools.java    From MiningGadgets with MIT License 6 votes vote down vote up
public static List<Upgrade> getUpgradesFromTag(CompoundNBT tagCompound) {
    ListNBT upgrades = tagCompound.getList(KEY_UPGRADES, Constants.NBT.TAG_COMPOUND);

    List<Upgrade> functionalUpgrades = new ArrayList<>();
    if (upgrades.isEmpty())
        return functionalUpgrades;

    for (int i = 0; i < upgrades.size(); i++) {
        CompoundNBT tag = upgrades.getCompound(i);

        // Skip unknowns
        Upgrade type = getUpgradeByName(tag.getString(KEY_UPGRADE));
        if( type == null )
            continue;

        type.setEnabled(!tag.contains(KEY_ENABLED) || tag.getBoolean(KEY_ENABLED));
        functionalUpgrades.add(type);
    }

    return functionalUpgrades;
}
 
Example #3
Source File: UpgradeTools.java    From MiningGadgets with MIT License 6 votes vote down vote up
public static List<Upgrade> getActiveUpgradesFromTag(CompoundNBT tagCompound) {
    ListNBT upgrades = tagCompound.getList(KEY_UPGRADES, Constants.NBT.TAG_COMPOUND);

    List<Upgrade> functionalUpgrades = new ArrayList<>();
    if (upgrades.isEmpty())
        return functionalUpgrades;

    for (int i = 0; i < upgrades.size(); i++) {
        CompoundNBT tag = upgrades.getCompound(i);

        Upgrade type = getUpgradeByName(tag.getString(KEY_UPGRADE));
        if (type == null)
            continue;

        type.setEnabled(!tag.contains(KEY_ENABLED) || tag.getBoolean(KEY_ENABLED));
        if (type.isEnabled())
            functionalUpgrades.add(type);
    }

    return functionalUpgrades;
}
 
Example #4
Source File: MiningProperties.java    From MiningGadgets with MIT License 6 votes vote down vote up
public static CompoundNBT serializeItemStackList(List<ItemStack> stacks) {
    ListNBT nbtTagList = new ListNBT();
    for (int i = 0; i < stacks.size(); i++)
    {
        if (stacks.get(i).isEmpty())
            continue;

        CompoundNBT itemTag = new CompoundNBT();
        stacks.get(i).write(itemTag);
        nbtTagList.add(itemTag);
    }

    CompoundNBT nbt = new CompoundNBT();
    nbt.put("Items", nbtTagList);
    return nbt;
}
 
Example #5
Source File: InventoryUtils.java    From CodeChickenLib with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * NBT item saving function with support for stack sizes > 32K
 */
public static ListNBT writeItemStacksToTag(ItemStack[] items, int maxQuantity) {
    ListNBT tagList = new ListNBT();
    for (int i = 0; i < items.length; i++) {
        CompoundNBT tag = new CompoundNBT();
        tag.putShort("Slot", (short) i);
        items[i].write(tag);

        if (maxQuantity > Short.MAX_VALUE) {
            tag.putInt("Quantity", items[i].getCount());
        } else if (maxQuantity > Byte.MAX_VALUE) {
            tag.putShort("Quantity", (short) items[i].getCount());
        }

        tagList.add(tag);
    }
    return tagList;
}
 
Example #6
Source File: CmdEnchant.java    From bleachhack-1.14 with GNU General Public License v3.0 5 votes vote down vote up
@SuppressWarnings("deprecation")
public void enchant(ItemStack item, Enchantment e, int level) {
	if (item.getTag() == null) item.setTag(new CompoundNBT());
	if (!item.getTag().contains("Enchantments", 9)) {
		item.getTag().put("Enchantments", new ListNBT());
    }

    ListNBT listnbt = item.getTag().getList("Enchantments", 10);
    CompoundNBT compoundnbt = new CompoundNBT();
    compoundnbt.putString("id", String.valueOf(Registry.ENCHANTMENT.getKey(e)));
    compoundnbt.putInt("lvl", level);
    listnbt.add(compoundnbt);
}
 
Example #7
Source File: PacketDurabilitySync.java    From MiningGadgets with MIT License 5 votes vote down vote up
public static void encode(PacketDurabilitySync msg, PacketBuffer buffer) {
    List<Tuple<BlockPos, Integer>> thisList = msg.updateList;
    CompoundNBT tag = new CompoundNBT();
    ListNBT nbtList = new ListNBT();
    for (int i = 0; i < thisList.size(); i++) {
        CompoundNBT nbt = new CompoundNBT();
        nbt.put("pos", NBTUtil.writeBlockPos(thisList.get(i).getA()));
        nbt.putInt("dur", thisList.get(i).getB());
        nbtList.add(i, nbt);
    }
    tag.put("list", nbtList);
    buffer.writeCompoundTag(tag);
}
 
Example #8
Source File: PacketDurabilitySync.java    From MiningGadgets with MIT License 5 votes vote down vote up
public static PacketDurabilitySync decode(PacketBuffer buffer) {
    CompoundNBT tag = buffer.readCompoundTag();
    ListNBT nbtList = tag.getList("list", Constants.NBT.TAG_COMPOUND);
    List<Tuple<BlockPos, Integer>> thisList = new ArrayList<>();
    for (int i = 0; i < nbtList.size(); i++) {
        CompoundNBT nbt = nbtList.getCompound(i);
        thisList.add(new Tuple<>(NBTUtil.readBlockPos(nbt.getCompound("pos")), nbt.getInt("dur")));
    }
    return new PacketDurabilitySync(thisList);
}
 
Example #9
Source File: UpgradeTools.java    From MiningGadgets with MIT License 5 votes vote down vote up
/**
 * DO NOT USE UNLESS YOU KNOW WHAT YOU'RE DOING. This method does not, and for some reason
 * can not, validate the upgrade you are inserting to the item. Please be sure to always
 * use {@link MiningGadget#applyUpgrade(ItemStack, UpgradeCard)} unless you actually require this
 * kind of unchecked functionality
 */
private static void setUpgradeNBT(CompoundNBT nbt, UpgradeCard upgrade) {
    ListNBT list = nbt.getList(KEY_UPGRADES, Constants.NBT.TAG_COMPOUND);

    CompoundNBT compound = new CompoundNBT();
    compound.putString(KEY_UPGRADE, upgrade.getUpgrade().getName());
    compound.putBoolean(KEY_ENABLED, upgrade.getUpgrade().isEnabled());

    list.add(compound);
    nbt.put(KEY_UPGRADES, list);
}
 
Example #10
Source File: UpgradeTools.java    From MiningGadgets with MIT License 5 votes vote down vote up
public static CompoundNBT setUpgradesNBT(List<Upgrade> laserUpgrades) {
    CompoundNBT listCompound = new CompoundNBT();
    ListNBT list = new ListNBT();

    laserUpgrades.forEach( upgrade -> {
        CompoundNBT compound = new CompoundNBT();
        compound.putString(KEY_UPGRADE, upgrade.getName());
        compound.putBoolean(KEY_ENABLED, upgrade.isEnabled());
        list.add(compound);
    });

    listCompound.put(KEY_UPGRADES, list);
    return listCompound;
}
 
Example #11
Source File: UpgradeTools.java    From MiningGadgets with MIT License 5 votes vote down vote up
/**
 * @implNote note that this is the only instance we use getName for non-eval uses
 * as the gadget stores the full name and not it's base name
 */
public static void removeUpgrade(ItemStack tool, Upgrade upgrade) {
    CompoundNBT tagCompound = tool.getOrCreateTag();
    ListNBT upgrades = tagCompound.getList(KEY_UPGRADES, Constants.NBT.TAG_COMPOUND);

    // Slightly completed but basically it just makes a new list and collects that back to an ListNBT
    tagCompound.put(KEY_UPGRADES, upgrades.stream()
            .filter(e -> !((CompoundNBT) e).getString(KEY_UPGRADE).equals(upgrade.getName()))
            .collect(Collectors.toCollection(ListNBT::new)));
}
 
Example #12
Source File: MiningProperties.java    From MiningGadgets with MIT License 5 votes vote down vote up
public static List<ItemStack> deserializeItemStackList(CompoundNBT nbt) {
    List<ItemStack> stacks = new ArrayList<>();
    ListNBT tagList = nbt.getList("Items", Constants.NBT.TAG_COMPOUND);

    for (int i = 0; i < tagList.size(); i++) {
        CompoundNBT itemTags = tagList.getCompound(i);
        stacks.add(ItemStack.read(itemTags));
    }

    return stacks;
}
 
Example #13
Source File: InventoryUtils.java    From CodeChickenLib with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * NBT item loading function with support for stack sizes > 32K
 */
public static void readItemStacksFromTag(ItemStack[] items, ListNBT tagList) {
    for (int i = 0; i < tagList.size(); i++) {
        CompoundNBT tag = tagList.getCompound(i);
        int b = tag.getShort("Slot");
        items[b] = ItemStack.read(tag);
        INBT quant = tag.get("Quantity");
        if (quant instanceof NumberNBT) {
            items[b].setCount(((NumberNBT) quant).getInt());
        }
    }
}
 
Example #14
Source File: NBTStructureLoader.java    From BoundingBoxOutlineReloaded with MIT License 5 votes vote down vote up
SimpleStructureStart(CompoundNBT compound) {
    super(null,
            0,
            0,
            null,
            new MutableBoundingBox(compound.getIntArray("BB")),
            0,
            0);

    ListNBT children = compound.getList("Children", 10);
    for (int index = 0; index < children.size(); ++index) {
        CompoundNBT child = children.getCompound(index);
        if (child.contains("BB")) this.components.add(new SimpleStructurePiece(child));
    }
}
 
Example #15
Source File: InventoryUtils.java    From CodeChickenLib with GNU Lesser General Public License v2.1 4 votes vote down vote up
/**
 * NBT item saving function
 */
public static ListNBT writeItemStacksToTag(ItemStack[] items) {
    return writeItemStacksToTag(items, 64);
}