Skip to content

Commit

Permalink
fix: Stored tank fluid is not shown/rendered correctly in item form
Browse files Browse the repository at this point in the history
Closes: GH-811
  • Loading branch information
Rover656 committed Sep 26, 2024
1 parent 068c56f commit 98d650c
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@
import net.minecraft.world.level.material.Fluid;
import net.minecraft.world.level.material.Fluids;
import net.minecraftforge.client.extensions.common.IClientFluidTypeExtensions;
import net.minecraftforge.common.capabilities.ForgeCapabilities;
import net.minecraftforge.common.util.LazyOptional;
import net.minecraftforge.fluids.FluidStack;
import net.minecraftforge.fluids.capability.IFluidHandlerItem;
import net.minecraftforge.registries.ForgeRegistries;

// TODO: No longer lights in the inventory/hand like other machines...
Expand All @@ -42,34 +46,28 @@ public void renderByItem(ItemStack stack, ItemDisplayContext transformType, Pose
// Render the main model
Minecraft.getInstance().getItemRenderer().renderModelLists(model, stack, packedLight, packedOverlay, poseStack, buffer.getBuffer(RenderType.cutout()));

// Read the fluid from the NBT, if it has fluid, then we render it.
CompoundTag nbt = stack.getTag();
if (nbt != null && nbt.contains(BlockItem.BLOCK_ENTITY_TAG)) {
CompoundTag blockEntityTag = nbt.getCompound(BlockItem.BLOCK_ENTITY_TAG);
if (blockEntityTag.contains(MachineNBTKeys.FLUID)) {
CompoundTag tank = blockEntityTag.getCompound(MachineNBTKeys.FLUID);
LazyOptional<IFluidHandlerItem> fluidHandlerItemOptional = stack.getCapability(ForgeCapabilities.FLUID_HANDLER_ITEM);
if (fluidHandlerItemOptional.isPresent()) {
IFluidHandlerItem fluidHandlerItem = fluidHandlerItemOptional.orElseThrow(IllegalStateException::new);

if (tank.contains("FluidName") && tank.contains("Amount")) {
Fluid fluid = ForgeRegistries.FLUIDS.getValue(new ResourceLocation(tank.getString("FluidName")));
int amount = tank.getInt("Amount");
FluidStack fluidStack = fluidHandlerItem.getFluidInTank(0);

if (fluid != null && fluid != Fluids.EMPTY && amount > 0) {
// Get the preferred render buffer
VertexConsumer fluidBuffer = buffer.getBuffer(Sheets.translucentCullBlockSheet());
if (!fluidStack.isEmpty()) {
// Get the preferred render buffer
VertexConsumer fluidBuffer = buffer.getBuffer(Sheets.translucentCullBlockSheet());

// Determine capacity.
int capacity = FluidTankBlockEntity.Standard.CAPACITY;
if (stack.is(MachineBlocks.PRESSURIZED_FLUID_TANK.get().asItem())) {
capacity = FluidTankBlockEntity.Enhanced.CAPACITY;
}

PoseStack.Pose pose = poseStack.last();
IClientFluidTypeExtensions props = IClientFluidTypeExtensions.of(fluid);
FluidTankBER.renderFluid(pose.pose(), pose.normal(), fluidBuffer, fluid, amount / (float) capacity, props.getTintColor(), packedLight);
}
// Determine capacity.
int capacity = FluidTankBlockEntity.Standard.CAPACITY;
if (stack.is(MachineBlocks.PRESSURIZED_FLUID_TANK.get().asItem())) {
capacity = FluidTankBlockEntity.Enhanced.CAPACITY;
}

PoseStack.Pose pose = poseStack.last();
IClientFluidTypeExtensions props = IClientFluidTypeExtensions.of(fluidStack.getFluid());
FluidTankBER.renderFluid(pose.pose(), pose.normal(), fluidBuffer, fluidStack.getFluid(), fluidStack.getAmount() / (float) capacity, props.getTintColor(), packedLight);
}
}

poseStack.popPose();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,13 @@
import com.enderio.machines.client.rendering.item.FluidTankBEWLR;
import com.enderio.machines.common.MachineNBTKeys;
import com.enderio.machines.common.block.MachineBlock;
import com.enderio.machines.common.blockentity.FluidTankBlockEntity;
import com.enderio.machines.common.io.fluid.MachineFluidHandler;
import com.enderio.machines.common.io.fluid.MachineFluidTank;
import net.minecraft.client.renderer.BlockEntityWithoutLevelRenderer;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.ListTag;
import net.minecraft.nbt.Tag;
import net.minecraft.network.chat.Component;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.BlockItem;
Expand Down Expand Up @@ -84,22 +89,73 @@ public FluidItemStack(@NotNull ItemStack container, int capacity) {

@NotNull
public FluidStack getFluid() {
Optional<CompoundTag> tagCompoundOptional = Optional.ofNullable(container.getTag());
return tagCompoundOptional
.map(tagCompound -> tagCompound.getCompound(BLOCK_ENTITY_TAG))
.map(blockEntityTag -> blockEntityTag.getCompound(MachineNBTKeys.FLUID))
.map(FluidStack::loadFluidStackFromNBT)
.orElse(FluidStack.EMPTY);
CompoundTag itemTag = container.getTag();
if (itemTag == null) {
return FluidStack.EMPTY;
}

if (!itemTag.contains(BLOCK_ENTITY_TAG)) {
return FluidStack.EMPTY;
}
CompoundTag blockEntityTag = itemTag.getCompound(BLOCK_ENTITY_TAG);

if (!blockEntityTag.contains(MachineNBTKeys.FLUID)) {
return FluidStack.EMPTY;
}
CompoundTag fluidContainerTag = blockEntityTag.getCompound(MachineNBTKeys.FLUID);

// Support old NBT type.
if (fluidContainerTag.contains(MachineFluidHandler.TANKS)) {
ListTag tankList = fluidContainerTag.getList(MachineFluidHandler.TANKS, Tag.TAG_COMPOUND);
if (tankList.isEmpty()) {
return FluidStack.EMPTY;
}

CompoundTag firstTank = tankList.getCompound(0);
return FluidStack.loadFluidStackFromNBT(firstTank);
} else {
return FluidStack.loadFluidStackFromNBT(blockEntityTag.getCompound(MachineNBTKeys.FLUID));
}
}

protected void setFluid(FluidStack fluid) {
CompoundTag mainTag = container.getOrCreateTag();
CompoundTag blockEntityTag = new CompoundTag();
mainTag.put(BLOCK_ENTITY_TAG, blockEntityTag);

CompoundTag fluidTag = new CompoundTag();
fluid.writeToNBT(fluidTag);//rewrites the old value
blockEntityTag.put(MachineNBTKeys.FLUID, fluidTag);
CompoundTag blockEntityTag;
if (mainTag.contains(BLOCK_ENTITY_TAG)) {
blockEntityTag = mainTag.getCompound(BLOCK_ENTITY_TAG);
} else {
blockEntityTag = new CompoundTag();
mainTag.put(BLOCK_ENTITY_TAG, blockEntityTag);
}

CompoundTag fluidContainerTag;
if (blockEntityTag.contains(MachineNBTKeys.FLUID)) {
fluidContainerTag = blockEntityTag.getCompound(MachineNBTKeys.FLUID);
} else {
fluidContainerTag = new CompoundTag();
fluidContainerTag.putInt(MachineFluidHandler.TANK_LIST_SIZE, 1);
blockEntityTag.put(MachineNBTKeys.FLUID, fluidContainerTag);
}

ListTag tankList;
if (fluidContainerTag.contains(MachineFluidHandler.TANKS)) {
tankList = fluidContainerTag.getList(MachineFluidHandler.TANKS, Tag.TAG_COMPOUND);
} else {
tankList = new ListTag();
fluidContainerTag.put(MachineFluidHandler.TANKS, tankList);
}

CompoundTag tankTag;
if (!tankList.isEmpty()) {
tankTag = tankList.getCompound(0);
} else {
tankTag = new CompoundTag();
tankTag.putInt(MachineFluidTank.Capacity, capacity);
tankList.add(tankTag);
}

fluid.writeToNBT(tankTag);
}

@Override
Expand Down

0 comments on commit 98d650c

Please sign in to comment.