return trainInvs;
}
}
public static int depositInFurnace(Inventory from, Furnace toFurnace, ItemParser parser, boolean isFuelPreferred) {
final Inventory to = toFurnace.getInventory();
List<ItemParser> heatables = new ArrayList<ItemParser>();
List<ItemParser> fuels = new ArrayList<ItemParser>();
if (!parser.hasType()) {
// Add all heatables and fuels
for (ItemParser p : TrainCarts.plugin.getParsers("heatable", 1)) {
if (p == null || !p.hasType()) {
heatables.clear();
break;
} else {
heatables.add(p);
}
}
for (ItemParser p : TrainCarts.plugin.getParsers("fuel", 1)) {
if (p == null || !p.hasType()) {
fuels.clear();
break;
} else {
fuels.add(p);
}
}
if (heatables.isEmpty() && fuels.isEmpty()) {
return 0;
}
} else {
// Is the parser fuel or heatable?
boolean heatable = RecipeUtil.isHeatableItem(parser.getItemStack(1));
boolean fuel = RecipeUtil.isFuelItem(parser.getTypeId());
if (heatable && fuel) {
if (isFuelPreferred) {
fuels.add(parser);
} else {
heatables.add(parser);
}
} else if (heatable) {
heatables.add(parser);
} else if (fuel) {
fuels.add(parser);
} else {
return 0;
}
}
final int startAmount = parser.hasAmount() ? parser.getAmount() : Integer.MAX_VALUE;
int amountToTransfer = startAmount;
// Transfer heatable items
for (ItemParser p : heatables) {
ItemStack item = to.getItem(0);
if (item == null) {
item = ItemUtil.emptyItem();
}
amountToTransfer -= ItemUtil.transfer(from, item, p, amountToTransfer);
to.setItem(0, item);
}
// Transfer fuel (requires manual limiting if no amount is set)
for (ItemParser p : fuels) {
if (p == null) {
continue;
}
if (amountToTransfer == 0) {
break;
}
int transferCount = amountToTransfer;
ItemStack fuel = to.getItem(1);
if (fuel == null) {
fuel = ItemUtil.emptyItem();
}
if (!p.hasAmount()) {
// Fill the minimal amount needed to burn all the heatables in the furnace
ItemStack cookeditem = to.getItem(0);
if (cookeditem == null || cookeditem.getType() == Material.AIR) continue;
int fuelNeeded = cookeditem.getAmount() * 200;
if (fuelNeeded == 0) continue; //nothing to cook
//===================================================
fuelNeeded -= toFurnace.getCookTime();
if (fuelNeeded <= 0) continue; //we got enough
//===================================================
int fuelPerItem = 0;
if (fuel.getType() == Material.AIR) {
fuelPerItem = RecipeUtil.getFuelTime(p.getItemStack(1));
} else {
fuelPerItem = RecipeUtil.getFuelTime(fuel);
}
//====================================================
if (fuelPerItem == 0) continue;
fuelNeeded -= fuelPerItem * fuel.getAmount();
if (fuelNeeded <= 0) continue;
//====================================================
transferCount = Math.min(amountToTransfer, (int) Math.ceil((double) fuelNeeded / (double) fuelPerItem));
}
amountToTransfer -= ItemUtil.transfer(from, fuel, p, transferCount);
to.setItem(1, fuel);
}
return startAmount - amountToTransfer;
}