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;
}
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));
}
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;
}
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);
}
}
}
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);
}
}
Aggregations