Skip to content
This repository has been archived by the owner on Jul 1, 2018. It is now read-only.

Commit

Permalink
Fix several item handling issues in turtle refueling
Browse files Browse the repository at this point in the history
 - Adding more fuel than there are items
 - Potentially consuming fuel from other slots too.
 - Not returning the container if the inventory is full.

Fixes #157
  • Loading branch information
SquidDev committed May 3, 2017
1 parent 6b60738 commit 512235a
Showing 1 changed file with 9 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import dan200.computercraft.shared.turtle.blocks.ITurtleTile;
import dan200.computercraft.shared.turtle.core.TurtlePlayer;
import dan200.computercraft.shared.util.InventoryUtil;
import dan200.computercraft.shared.util.WorldUtil;
import net.minecraft.block.Block;
import net.minecraft.block.BlockLiquid;
import net.minecraft.item.ItemStack;
Expand Down Expand Up @@ -51,14 +52,20 @@ public boolean canRefuel(@Nonnull ITurtleAccess turtle, @Nonnull ItemStack stack

@Override
public int refuel(@Nonnull ITurtleAccess turtle, @Nonnull ItemStack stack, int limit) {
if (limit > stack.stackSize) limit = stack.stackSize;

int fuelToGive = TileEntityFurnace.getItemBurnTime(stack) * 5 / 100 * limit;
ItemStack replacementStack = stack.getItem().getContainerItem(stack);

// Remove 'n' items from the stack.
InventoryUtil.takeItems(limit, turtle.getInventory(), 0, turtle.getInventory().getSizeInventory(), turtle.getSelectedSlot());
int slot = turtle.getSelectedSlot();
InventoryUtil.takeItems(limit, turtle.getInventory(), slot, 1, slot);
if (replacementStack != null) {
// If item is empty (bucket) then add it back
InventoryUtil.storeItems(replacementStack, turtle.getInventory(), 0, turtle.getInventory().getSizeInventory(), turtle.getSelectedSlot());
replacementStack = InventoryUtil.storeItems(replacementStack, turtle.getInventory(), 0, turtle.getInventory().getSizeInventory(), slot);
if (replacementStack != null) {
WorldUtil.dropItemStack(replacementStack, turtle.getWorld(), turtle.getPosition(), turtle.getDirection().getOpposite());
}
}

return fuelToGive;
Expand Down

0 comments on commit 512235a

Please sign in to comment.