Search in sources :

Example 11 with NbtCompound

use of net.minecraft.nbt.NbtCompound in project Primeval by devs-immortal.

the class ClayMoldItem method insertFluid.

/*
     * Handle inserting fluid into a mold from a source
     * Return the amount of fluid inserted
     */
public static int insertFluid(Pair<FluidVariant, Integer> fluidPair, ItemStack mold) {
    NbtCompound nbt = mold.getOrCreateNbt();
    FluidVariant incomingFluid = fluidPair.getLeft();
    int incomingAmount = fluidPair.getRight();
    int currentStoredAmount = 0;
    if (nbt.contains("Fluid")) {
        // If already stored
        // get fluid nbt
        NbtCompound fluidNbt = nbt.getCompound("Fluid");
        FluidVariant stored = FluidVariant.fromNbt(fluidNbt);
        int amount = fluidNbt.getInt("Amount");
        if (amount != 0 && !stored.isBlank()) {
            // If not and not storing empty
            if (stored.getFluid() == incomingFluid.getFluid()) {
                currentStoredAmount = amount;
            } else {
                return 0;
            }
        }
    }
    // if not already storing, or we pass the tests above, insert 1000dp of fluid
    int remainingCapacity = CAPACITY - currentStoredAmount;
    int amountToInsert = Math.min(remainingCapacity, Math.min(incomingAmount, 1000));
    // build new nbt for mold item
    NbtCompound nbtF = fluidPair.getLeft().toNbt();
    nbtF.putInt("Amount", currentStoredAmount + amountToInsert);
    nbt.put("Fluid", nbtF);
    mold.setNbt(nbt);
    return amountToInsert;
}
Also used : NbtCompound(net.minecraft.nbt.NbtCompound) FluidVariant(net.fabricmc.fabric.api.transfer.v1.fluid.FluidVariant)

Example 12 with NbtCompound

use of net.minecraft.nbt.NbtCompound in project Primeval by devs-immortal.

the class ClayMoldItem method appendTooltip.

@Environment(EnvType.CLIENT)
public void appendTooltip(ItemStack stack, @Nullable World world, List<Text> tooltip, TooltipContext context) {
    NbtCompound nbt = stack.getOrCreateNbt();
    NbtCompound fluidNbt = nbt.getCompound("Fluid");
    int fluidAmount = fluidNbt.getInt("Amount");
    if (fluidAmount > 0) {
        tooltip.add((new TranslatableText("text.primeval.fluid.contains", fluidAmount, new TranslatableText("block." + fluidNbt.getString("fluid").replace(':', '.')))).formatted(Formatting.GRAY));
    }
    tooltip.add((new TranslatableText("⚖ ").append(this.weight.getText()).append(" ⤧ ").append(this.size.getText())).formatted(Formatting.GRAY));
}
Also used : TranslatableText(net.minecraft.text.TranslatableText) NbtCompound(net.minecraft.nbt.NbtCompound) Environment(net.fabricmc.api.Environment)

Example 13 with NbtCompound

use of net.minecraft.nbt.NbtCompound in project Primeval by devs-immortal.

the class VesselItem method onStackClicked.

// 
// COPIED BUNDLE METHODS
// 
public boolean onStackClicked(ItemStack stack, Slot slot, ClickType clickType, PlayerEntity player) {
    if (clickType != ClickType.RIGHT) {
        return false;
    } else if (slot.getStack().getItem() instanceof ClayMoldItem) {
        ItemStack moldItemStack = slot.getStack();
        NbtCompound nbt = stack.getOrCreateNbt();
        NbtCompound fluidNbt = nbt.getCompound("Fluid");
        int fluidAmount = fluidNbt.getInt("Amount");
        Pair<FluidVariant, Integer> fluidPair = new Pair<>(FluidVariant.fromNbt(fluidNbt), fluidAmount);
        int amountInserted = ClayMoldItem.insertFluid(fluidPair, moldItemStack);
        fluidNbt.putInt("Amount", fluidAmount - amountInserted);
        nbt.put("Fluid", fluidNbt);
        stack.setNbt(nbt);
        return true;
    } else if (canAddItem(stack)) {
        ItemStack itemStack = slot.getStack();
        if (itemStack.isEmpty()) {
            this.playRemoveOneSound(player);
            removeFirstStack(stack).ifPresent((removedStack) -> addToBundle(stack, slot.insertStack(removedStack)));
        } else if (itemStack.getItem().canBeNested()) {
            int i = (64 - getBundleOccupancy(stack)) / getItemOccupancy(itemStack);
            int j = addToBundle(stack, slot.takeStackRange(itemStack.getCount(), i, player));
            if (j > 0) {
                this.playInsertSound(player);
            }
        }
        return true;
    }
    return false;
}
Also used : NbtCompound(net.minecraft.nbt.NbtCompound) ItemStack(net.minecraft.item.ItemStack) Pair(net.minecraft.util.Pair)

Example 14 with NbtCompound

use of net.minecraft.nbt.NbtCompound in project Primeval by devs-immortal.

the class VesselItem method removeFirstStack.

private static Optional<ItemStack> removeFirstStack(ItemStack stack) {
    NbtCompound nbtCompound = stack.getOrCreateNbt();
    if (!nbtCompound.contains("Items")) {
        return Optional.empty();
    } else {
        NbtList nbtList = nbtCompound.getList("Items", 10);
        if (nbtList.isEmpty()) {
            return Optional.empty();
        } else {
            NbtCompound nbtCompound2 = nbtList.getCompound(0);
            ItemStack itemStack = ItemStack.fromNbt(nbtCompound2);
            nbtList.remove(0);
            if (nbtList.isEmpty()) {
                stack.removeSubNbt("Items");
            }
            return Optional.of(itemStack);
        }
    }
}
Also used : NbtCompound(net.minecraft.nbt.NbtCompound) NbtList(net.minecraft.nbt.NbtList) ItemStack(net.minecraft.item.ItemStack)

Example 15 with NbtCompound

use of net.minecraft.nbt.NbtCompound in project Primeval by devs-immortal.

the class VesselItem method getBundledStacks.

private static Stream<ItemStack> getBundledStacks(ItemStack stack) {
    NbtCompound nbtCompound = stack.getNbt();
    if (nbtCompound == null) {
        return Stream.empty();
    } else {
        NbtList nbtList = nbtCompound.getList("Items", 10);
        Stream<NbtElement> var10000 = nbtList.stream();
        Objects.requireNonNull(NbtCompound.class);
        return var10000.map(NbtCompound.class::cast).map(ItemStack::fromNbt);
    }
}
Also used : NbtCompound(net.minecraft.nbt.NbtCompound) NbtList(net.minecraft.nbt.NbtList) NbtElement(net.minecraft.nbt.NbtElement) ItemStack(net.minecraft.item.ItemStack)

Aggregations

NbtCompound (net.minecraft.nbt.NbtCompound)282 NbtList (net.minecraft.nbt.NbtList)79 ItemStack (net.minecraft.item.ItemStack)58 LiteralText (net.minecraft.text.LiteralText)24 NbtElement (net.minecraft.nbt.NbtElement)23 Identifier (net.minecraft.util.Identifier)23 IOException (java.io.IOException)21 BlockPos (net.minecraft.util.math.BlockPos)19 Inject (org.spongepowered.asm.mixin.injection.Inject)18 NbtString (net.minecraft.nbt.NbtString)16 File (java.io.File)13 List (java.util.List)9 Text (net.minecraft.text.Text)9 Items (net.minecraft.item.Items)8 TranslatableText (net.minecraft.text.TranslatableText)8 BlockState (net.minecraft.block.BlockState)6 Item (net.minecraft.item.Item)6 NbtIo (net.minecraft.nbt.NbtIo)6 PacketByteBuf (net.minecraft.network.PacketByteBuf)6 Vec3d (net.minecraft.util.math.Vec3d)6