-
Notifications
You must be signed in to change notification settings - Fork 7
refactor Drums, #55
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
refactor Drums, #55
Changes from all commits
9244473
506f295
f6ae2c7
b3d0fb4
9387f06
4e998fc
9bb2f50
07168c4
a375e4f
4f300a2
a0ac1f3
cebe461
8e67beb
2479af4
8b936a8
8a03f54
5745589
a5a0641
89550df
56ce020
50a59c6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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 { | ||
|
|
||
|
|
@@ -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, | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This method is HUGE and should be divided into smaller parts. @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; | ||
Spicierspace153 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| // 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
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
|
||
| 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())); | ||
Spicierspace153 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| 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) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
|
|
||
| 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; | ||
|
|
||
| } |

There was a problem hiding this comment.
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