Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion dependencies.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
* For more details, see https://docs.gradle.org/8.0.1/userguide/java_library_plugin.html#sec:java_library_configurations_graph
*/
dependencies {
implementation("com.github.GTNewHorizons:GTNHLib:0.8.31:dev")
implementation("com.github.GTNewHorizons:GTNHLib:0.8.32:dev")
// TODO: remove MUI1 dep when the implicit dependency
// TODO: in IItemHandlerModifiable is removed
api("com.github.GTNewHorizons:ModularUI:1.3.1:dev")
Expand Down
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,20 +9,24 @@
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.item.EntityItem;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.Items;
import net.minecraft.item.Item;
import net.minecraft.item.ItemBlock;
import net.minecraft.item.ItemBucket;
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;
import com.fouristhenumber.utilitiesinexcess.common.tileentities.TileEntityDrum;
import com.fouristhenumber.utilitiesinexcess.config.blocks.DrumConfig;

public class BlockDrum extends BlockContainer {

Expand Down Expand Up @@ -51,50 +55,205 @@ 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) {

TileEntity te = world.getTileEntity(x, y, z);

// all the drum things in variables so i can just ref them later xd
if (!(te instanceof TileEntityDrum drum)) return false;
FluidTank drumTank = drum.tank;
FluidStack fluid = drumTank.getFluid();
int capacity = drumTank.getCapacity();

ItemStack itemStack = player.inventory.getCurrentItem();
if (itemStack == null) {
if (world.isRemote) {
return false;
}

// Print drum capacity to player chat
player.addChatComponentMessage(
new ChatComponentTranslation(
"%s %s",
NumberFormat.DEFAULT.format(drumTank.getFluidAmount()),
DrumConfig.unitToDisplay ? "mB" : "L"));
Comment on lines +74 to +78
Copy link
Contributor

Choose a reason for hiding this comment

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

Why ChatComponentTranslation? you aren't giving it a translation key...
either make it a translation key or replace with ChatComponentText

also the formatter puts a q after the number if it's 0 for some reaons
Image


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

if (FluidContainerRegistry.isFilledContainer(heldItem)) {
if (fluid == null) {
// empty drum cases
if (item instanceof IFluidContainerItem fluidTank) {
FluidStack playerFluid = fluidTank.getFluid(itemStack);
int playerCapacity = fluidTank.getCapacity(itemStack);

if (drum.tank.getFluid() == null) {
drum.setFluid(new FluidStack(heldFluid.getFluid(), 0));
if (playerFluid == null) {
// both the tank and the drum are false
return true;
} else {
// either the capacity of the drum, or the amount in the player hand
int fillAmount = Math.min(capacity, playerFluid.amount);
// copy the fluid add hte amount and fill the drum and empty the hand
FluidStack fillFluid = playerFluid.copy();
fillFluid.amount = fillAmount;
int filedAmount = drumTank.fill(fillFluid, true);
fluidTank.drain(itemStack, filedAmount, true);

// todo: pull this to its own helper method?
if (!world.isRemote) {
player.addChatComponentMessage(
new ChatComponentTranslation(
"message.drum.filled",
filedAmount,
playerFluid.getLocalizedName()));
}

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

if (drum.fill(ForgeDirection.UP, heldFluid, true) == heldFluid.amount) {
FluidContainerRegistry.drainFluidContainer(heldItem);
ItemStack emptyContainer = FluidContainerRegistry.drainFluidContainer(heldItem);
emptyContainer.stackSize = 1;
heldItem.stackSize--;
player.inventory.setInventorySlotContents(player.inventory.currentItem, heldItem);
player.inventory.addItemStackToInventory(emptyContainer);
}
// empty and full bucket, maybe can pull this out to a helper method as well?
else if (item instanceof ItemBucket) {

player.addChatMessage(
new ChatComponentTranslation(
"tile.drum.chat.filled",
drum.tank.getFluid()
.getLocalizedName(),
NumberFormat.DEFAULT.format(drum.tank.getFluid().amount)));
FluidStack bucketFluid = FluidContainerRegistry.getFluidForFilledItem(itemStack);
if (bucketFluid == null) {
return false;
}
} else if (FluidContainerRegistry.isEmptyContainer(heldItem)) {
if (drum.tank.getFluid() != null) {
FluidStack drainedFluid = drum.drain(ForgeDirection.UP, 1000, true);
// this SHOULD be 1000 i THINK
int fillAmount = Math.min(capacity, bucketFluid.amount);
FluidStack fillFluid = bucketFluid.copy();
fillFluid.amount = fillAmount;
int filedAmount = drumTank.fill(fillFluid, true);

// if the drum works?
if (filedAmount > 0) {

if (!world.isRemote) {

if (drainedFluid.amount == 1000) {
ItemStack filledContainer = FluidContainerRegistry.fillFluidContainer(drainedFluid, heldItem);
player.inventory.setInventorySlotContents(player.inventory.currentItem, filledContainer);
consumeItemStack(player, itemStack, new ItemStack(Items.bucket));

player.addChatComponentMessage(
new ChatComponentTranslation(
"message.drum.filled",
fillAmount,
fillFluid.getLocalizedName()));
}
world.markBlockForUpdate(x, y, z);

}

}
}
// if the drum has any fluid at all
else {
if (item instanceof IFluidContainerItem fluidItem) {

FluidStack itemFluid = fluidItem.getFluid(itemStack);

if (itemFluid != null && !itemFluid.isFluidEqual(fluid)) {
return false;
}

int space = fluidItem.getCapacity(itemStack) - (itemFluid == null ? 0 : itemFluid.amount);

if (space <= 0) return false;
int transfer = Math.min(space, fluid.amount);
FluidStack transferFluid = fluid.copy();
transferFluid.amount = transfer;
ItemStack single = itemStack.copy();
single.stackSize = 1;

int filled = fluidItem.fill(single, transferFluid, true);
if (filled <= 0) return false;

drumTank.drain(filled, true);

if (!world.isRemote) {
consumeItemStack(player, itemStack, single);

player.addChatComponentMessage(
new ChatComponentTranslation("message.drum.drained", filled, fluid.getLocalizedName()));
}

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

}

else if (item instanceof ItemBucket) {

FluidStack bucketFluid = FluidContainerRegistry.getFluidForFilledItem(itemStack);
if (bucketFluid != null) {
if (fluid != null && !fluid.isFluidEqual(bucketFluid)) {
return false;
}
int fill = drumTank.fill(bucketFluid.copy(), false);

if (fill < FluidContainerRegistry.BUCKET_VOLUME) {
return false;
}

drumTank.fill(bucketFluid.copy(), true);

if (!world.isRemote) {
player.inventory
.setInventorySlotContents(player.inventory.currentItem, new ItemStack(Items.bucket));

player.addChatComponentMessage(
new ChatComponentTranslation(
"message.drum.filled",
FluidContainerRegistry.BUCKET_VOLUME,
bucketFluid.getLocalizedName()));
}

world.markBlockForUpdate(x, y, z);
return true;
}
if (fluid == null || fluid.amount < FluidContainerRegistry.BUCKET_VOLUME) {
return false;
}

FluidStack take = fluid.copy();
take.amount = FluidContainerRegistry.BUCKET_VOLUME;

ItemStack filledBucket = FluidContainerRegistry.fillFluidContainer(take, itemStack);
if (filledBucket == null) return false;

if (!world.isRemote) {
consumeItemStack(player, itemStack, filledBucket);

player.addChatComponentMessage(
new ChatComponentTranslation(
"message.drum.drained",
FluidContainerRegistry.BUCKET_VOLUME,
fluid.getLocalizedName()));
}

drumTank.drain(FluidContainerRegistry.BUCKET_VOLUME, true);
world.markBlockForUpdate(x, y, z);
return true;

}

}
return true;
}

private static void consumeItemStack(EntityPlayer player, ItemStack hand, ItemStack result) {
Copy link

Choose a reason for hiding this comment

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

nesting could be reduced here

private static void consumeItemStack(
    EntityPlayer player,
    ItemStack hand,
    ItemStack result
) {
    InventoryPlayer inv = player.inventory;

    if (hand.stackSize == 1) {
        inv.setInventorySlotContents(inv.currentItem, result);
        finalizeInventoryChange(player);
        return;
    }

    hand.stackSize--;

    if (!inv.addItemStackToInventory(result)) {
        player.dropPlayerItemWithRandomChoice(result, false);
    }

    finalizeInventoryChange(player);
}

private static void finalizeInventoryChange(EntityPlayer player) {
    player.inventoryContainer.detectAndSendChanges();
    player.inventory.markDirty();
}

if (hand.stackSize == 1) {
player.inventory.setInventorySlotContents(player.inventory.currentItem, result);
} else {
hand.stackSize--;
if (!player.inventory.addItemStackToInventory(result)) {
player.dropPlayerItemWithRandomChoice(result, false);
}
}
player.inventoryContainer.detectAndSendChanges();
player.inventory.markDirty();

}

@Override
public void onBlockPlacedBy(World world, int x, int y, int z, EntityLivingBase placer, ItemStack stack) {
super.onBlockPlacedBy(world, x, y, z, placer, stack);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,19 @@
import net.minecraftforge.fluids.FluidTankInfo;
import net.minecraftforge.fluids.IFluidHandler;

import com.fouristhenumber.utilitiesinexcess.config.blocks.DrumConfig;

public class TileEntityDrum extends TileEntity implements IFluidHandler {

public final FluidTank tank;
public FluidTank tank = new FluidTank(0);

public TileEntityDrum() {
this(DrumConfig.drumSize);
}

public TileEntityDrum(int capacity) {
super();
this.tank = new FluidTank(capacity);
tank = new FluidTank(capacity);
}

public void setTank(FluidTank tank) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.fouristhenumber.utilitiesinexcess.config.blocks;

import com.fouristhenumber.utilitiesinexcess.UtilitiesInExcess;
import com.gtnewhorizon.gtnhlib.config.Config;

@Config(modid = UtilitiesInExcess.MODID, category = "blocks.drum")

public class DrumConfig {

@Config.DefaultBoolean(true)
@Config.Comment("Change it to MB vs L's")
public static boolean unitToDisplay;

@Config.RangeInt(min = 16000, max = 256000)
@Config.Comment("size of the the drum")
@Config.DefaultInt(16000)
public static int drumSize;

}