Skip to content
Draft
Changes from 4 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know how to fix this, but there is a desync between the client and server here. The client doesn't get told how full the drum is when it's loaded. Not sure of how impactful this is but the onBlockActivated logic is wrong on the client because of it

Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,17 @@
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.item.EntityItem;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.Item;
import net.minecraft.item.ItemBlock;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.ChatComponentTranslation;
import net.minecraft.util.StatCollector;
import net.minecraft.world.World;
import net.minecraftforge.common.util.ForgeDirection;
import net.minecraftforge.fluids.FluidContainerRegistry;
import net.minecraftforge.fluids.FluidStack;
import net.minecraftforge.fluids.FluidTank;
import net.minecraftforge.fluids.IFluidContainerItem;

import com.cleanroommc.modularui.utils.NumberFormat;
Expand Down Expand Up @@ -51,48 +52,117 @@ public boolean hasTileEntity(int metadata) {
@Override
public boolean onBlockActivated(World world, int x, int y, int z, EntityPlayer player, int side, float hitX,
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method is HUGE and should be divided into smaller parts.
Here is how I would refactor the current code to improve readability (logic kept the same):

@Override
public boolean onBlockActivated(
    World world, int x, int y, int z,
    EntityPlayer player, int side,
    float hitX, float hitY, float hitZ
) {
    TileEntity te = world.getTileEntity(x, y, z);
    if (!(te instanceof TileEntityDrum drum)) {
        return false;
    }

    FluidTank tank = drum.tank;
    FluidStack fluid = tank.getFluid();
    ItemStack held = player.inventory.getCurrentItem();

    if (held == null) {
        return handleEmptyHand(world, player, tank);
    }

    Item item = held.getItem();

    return fluid == null
        ? handleEmptyDrum(world, x, y, z, player, tank, held, item)
        : handleFilledDrum(world, x, y, z, player, tank, fluid, held, item);
}

private boolean handleEmptyHand(World world, EntityPlayer player, FluidTank tank) {
    if (world.isRemote) {
        return false;
    }

    player.addChatComponentMessage(
        new ChatComponentTranslation(
            "%s %s",
            NumberFormat.DEFAULT.format(tank.getFluidAmount()),
            DrumConfig.unitToDisplay ? "mB" : "L"
        )
    );
    return true;
}

private boolean handleEmptyDrum(
    World world, int x, int y, int z,
    EntityPlayer player,
    FluidTank tank,
    ItemStack stack,
    Item item
) {
    if (item instanceof IFluidContainerItem container) {
        return fillDrumFromContainer(world, x, y, z, player, tank, stack, container);
    }

    if (item instanceof ItemBucket) {
        return fillDrumFromBucket(world, x, y, z, player, tank, stack);
    }

    return true;
}

private boolean fillDrumFromContainer(
    World world, int x, int y, int z,
    EntityPlayer player,
    FluidTank tank,
    ItemStack stack,
    IFluidContainerItem container
) {
    FluidStack fluid = container.getFluid(stack);
    if (fluid == null) {
        return true;
    }

    int fillAmount = Math.min(tank.getCapacity(), fluid.amount);
    FluidStack toFill = fluid.copy();
    toFill.amount = fillAmount;

    int filled = tank.fill(toFill, true);
    container.drain(stack, filled, true);

    if (!world.isRemote) {
        player.addChatComponentMessage(
            new ChatComponentTranslation(
                "message.drum.filled",
                filled,
                fluid.getLocalizedName()
            )
        );
    }

    world.markBlockForUpdate(x, y, z);
    return true;
}

private boolean fillDrumFromBucket(
    World world, int x, int y, int z,
    EntityPlayer player,
    FluidTank tank,
    ItemStack stack
) {
    FluidStack bucketFluid = FluidContainerRegistry.getFluidForFilledItem(stack);
    if (bucketFluid == null) {
        return false;
    }

    int filled = tank.fill(bucketFluid.copy(), true);
    if (filled <= 0) {
        return false;
    }

    if (!world.isRemote) {
        consumeItemStack(player, stack, new ItemStack(Items.bucket));
        player.addChatComponentMessage(
            new ChatComponentTranslation(
                "message.drum.filled",
                filled,
                bucketFluid.getLocalizedName()
            )
        );
    }

    world.markBlockForUpdate(x, y, z);
    return true;
}

private boolean handleFilledDrum(
    World world, int x, int y, int z,
    EntityPlayer player,
    FluidTank tank,
    FluidStack fluid,
    ItemStack stack,
    Item item
) {
    if (item instanceof IFluidContainerItem container) {
        return drainDrumIntoContainer(world, x, y, z, player, tank, fluid, stack, container);
    }

    if (item instanceof ItemBucket) {
        return handleBucketDrain(world, x, y, z, player, tank, fluid, stack);
    }

    return true;
}

float hitY, float hitZ) {
if (world.isRemote) {
return true;

ItemStack heldItem = player.getCurrentEquippedItem();
if (heldItem == null) {
return false;
}

TileEntity tile = world.getTileEntity(x, y, z);
if (tile instanceof TileEntityDrum drum) {
ItemStack heldItem = player.getCurrentEquippedItem();
FluidStack heldFluid = FluidContainerRegistry.getFluidForFilledItem(heldItem);
if (!(tile instanceof TileEntityDrum drum)) {
return false;
}

if (FluidContainerRegistry.isFilledContainer(heldItem)) {
FluidTank tank = drum.tank;

if (drum.tank.getFluid() == null) {
drum.setFluid(new FluidStack(heldFluid.getFluid(), 0));
}
if (FluidContainerRegistry.isEmptyContainer(heldItem)) {
FluidStack stored = tank.getFluid();
if (stored == null || stored.amount <= 0) {
return false;
}

FluidStack available = new FluidStack(stored.getFluid(), Math.min(1000, stored.amount));
ItemStack filledContainer = FluidContainerRegistry.fillFluidContainer(available, heldItem);
if (filledContainer == null) {
return false;
}

if (drum.fill(ForgeDirection.UP, heldFluid, true) == heldFluid.amount) {
FluidContainerRegistry.drainFluidContainer(heldItem);
ItemStack emptyContainer = FluidContainerRegistry.drainFluidContainer(heldItem);
emptyContainer.stackSize = 1;
FluidStack fluidInContainer = FluidContainerRegistry.getFluidForFilledItem(filledContainer);
if (fluidInContainer == null) return false;

tank.drain(fluidInContainer.amount, true);

if (!player.capabilities.isCreativeMode) {
if (heldItem.stackSize == 1) {
player.inventory.setInventorySlotContents(player.inventory.currentItem, filledContainer);
} else {
heldItem.stackSize--;
player.inventory.setInventorySlotContents(player.inventory.currentItem, heldItem);
player.inventory.addItemStackToInventory(emptyContainer);

player.addChatMessage(
new ChatComponentTranslation(
"tile.drum.chat.filled",
drum.tank.getFluid()
.getLocalizedName(),
NumberFormat.DEFAULT.format(drum.tank.getFluid().amount)));
if (!player.inventory.addItemStackToInventory(filledContainer)) {
player.dropPlayerItemWithRandomChoice(filledContainer, false);
}

}
if (!world.isRemote) {
if (drum.tank.getFluid() == null) {
player.addChatMessage(new ChatComponentTranslation("tile.drum.chat.empty"));
} else {
player.addChatMessage(
new ChatComponentTranslation(
"tile.drum.chat.filled",
drum.tank.getFluid()
.getLocalizedName(),
NumberFormat.DEFAULT.format(drum.tank.getFluid().amount)));
}
}
} else if (FluidContainerRegistry.isEmptyContainer(heldItem)) {
if (drum.tank.getFluid() != null) {
FluidStack drainedFluid = drum.drain(ForgeDirection.UP, 1000, true);

if (drainedFluid.amount == 1000) {
ItemStack filledContainer = FluidContainerRegistry.fillFluidContainer(drainedFluid, heldItem);
player.inventory.setInventorySlotContents(player.inventory.currentItem, filledContainer);
player.inventory.markDirty();
}

drum.markDirty();
world.markBlockForUpdate(x, y, z);
return true;
}

FluidStack fluid = null;
Item item = heldItem.getItem();

if (item instanceof IFluidContainerItem fluidContainer) {
fluid = fluidContainer.getFluid(heldItem);
} else {
fluid = FluidContainerRegistry.getFluidForFilledItem(heldItem);
}

if (fluid != null && fluid.getFluid() != null) {
int filled = tank.fill(fluid, true);
if (filled > 0) {
ItemStack emptyContainer = null;

if (item instanceof IFluidContainerItem fluidContainer) {
fluidContainer.drain(heldItem, filled, true);
emptyContainer = heldItem;
} else {
emptyContainer = FluidContainerRegistry.drainFluidContainer(heldItem);
}

if (!player.capabilities.isCreativeMode) {
if (heldItem.stackSize == 1) {
player.inventory.setInventorySlotContents(player.inventory.currentItem, emptyContainer);
} else {
heldItem.stackSize--;
if (emptyContainer != null && !player.inventory.addItemStackToInventory(emptyContainer)) {
player.dropPlayerItemWithRandomChoice(emptyContainer, false);
}

}
if (world.isRemote) {
player.addChatMessage(
new ChatComponentTranslation(
"tile.drum.chat.filled",
drum.tank.getFluid()
.getLocalizedName(),
NumberFormat.DEFAULT.format(drum.tank.getFluid().amount)));
}

player.inventory.markDirty();
}

drum.markDirty();
world.markBlockForUpdate(x, y, z);
return true;
}
}

return true;
return false;
}

@Override
Expand Down