Improve TileGrid#onRecipeTransfer, we can now do recipe transfer if disconnected

This commit is contained in:
Raoul Van den Berge
2016-10-29 16:06:30 +02:00
parent 8478daac0a
commit c081d45723

View File

@@ -274,9 +274,11 @@ public class TileGrid extends TileNode implements IGrid {
ItemStack slot = matrix.getStackInSlot(i); ItemStack slot = matrix.getStackInSlot(i);
if (i < remainder.length && remainder[i] != null) { if (i < remainder.length && remainder[i] != null) {
// If there is no space for the remainder, dump it in the player inventory
if (slot != null && slot.stackSize > 1) { if (slot != null && slot.stackSize > 1) {
if (!player.inventory.addItemStackToInventory(remainder[i].copy())) { if (!player.inventory.addItemStackToInventory(remainder[i].copy())) {
ItemStack remainderStack = network.insertItem(remainder[i].copy(), remainder[i].stackSize, false); ItemStack remainderStack = network.insertItem(remainder[i].copy(), remainder[i].stackSize, false);
if (remainderStack != null) { if (remainderStack != null) {
InventoryHelper.spawnItemStack(player.worldObj, player.getPosition().getX(), player.getPosition().getY(), player.getPosition().getZ(), remainderStack); InventoryHelper.spawnItemStack(player.worldObj, player.getPosition().getX(), player.getPosition().getY(), player.getPosition().getZ(), remainderStack);
} }
@@ -286,13 +288,11 @@ public class TileGrid extends TileNode implements IGrid {
} else { } else {
matrix.setInventorySlotContents(i, remainder[i].copy()); matrix.setInventorySlotContents(i, remainder[i].copy());
} }
} else { } else if (slot != null) {
if (slot != null) { if (slot.stackSize == 1 && isConnected()) {
if (slot.stackSize == 1 && isConnected()) { matrix.setInventorySlotContents(i, network.extractItem(slot, 1));
matrix.setInventorySlotContents(i, network.extractItem(slot, 1)); } else {
} else { matrix.decrStackSize(i, 1);
matrix.decrStackSize(i, 1);
}
} }
} }
} }
@@ -355,30 +355,43 @@ public class TileGrid extends TileNode implements IGrid {
} }
public void onRecipeTransfer(EntityPlayer player, ItemStack[][] recipe) { public void onRecipeTransfer(EntityPlayer player, ItemStack[][] recipe) {
if (isConnected()) { // First try to empty the crafting matrix
for (int i = 0; i < matrix.getSizeInventory(); ++i) { for (int i = 0; i < matrix.getSizeInventory(); ++i) {
ItemStack slot = matrix.getStackInSlot(i); ItemStack slot = matrix.getStackInSlot(i);
if (slot != null) { if (slot != null) {
if (getType() == EnumGridType.CRAFTING) { // Only if we are a crafting grid. Pattern grids can just be emptied.
if (getType() == EnumGridType.CRAFTING) {
// If we are connected, try to insert into network. If it fails, stop.
if (isConnected()) {
if (network.insertItem(slot, slot.stackSize, true) != null) { if (network.insertItem(slot, slot.stackSize, true) != null) {
return; return;
} else { } else {
network.insertItem(slot, slot.stackSize, false); network.insertItem(slot, slot.stackSize, false);
} }
} else {
// If we aren't connected, try to insert into player inventory. If it fails, stop.
if (!player.inventory.addItemStackToInventory(slot.copy())) {
return;
}
} }
matrix.setInventorySlotContents(i, null);
} }
matrix.setInventorySlotContents(i, null);
} }
}
for (int i = 0; i < matrix.getSizeInventory(); ++i) { // Now let's fill the matrix
if (recipe[i] != null) { for (int i = 0; i < matrix.getSizeInventory(); ++i) {
ItemStack[] possibilities = recipe[i]; if (recipe[i] != null) {
ItemStack[] possibilities = recipe[i];
if (getType() == EnumGridType.CRAFTING) { // If we are a crafting grid
boolean found = false; if (getType() == EnumGridType.CRAFTING) {
boolean found = false;
// If we are connected, first try to get the possibilities from the network
if (isConnected()) {
for (ItemStack possibility : possibilities) { for (ItemStack possibility : possibilities) {
ItemStack took = network.extractItem(possibility, 1); ItemStack took = network.extractItem(possibility, 1);
@@ -390,29 +403,31 @@ public class TileGrid extends TileNode implements IGrid {
break; break;
} }
} }
}
if (!found) { // If we haven't found anything in the network (or we are disconnected), go look in the player inventory
for (ItemStack possibility : possibilities) { if (!found) {
for (int j = 0; j < player.inventory.getSizeInventory(); ++j) { for (ItemStack possibility : possibilities) {
if (API.instance().getComparer().isEqualNoQuantity(possibility, player.inventory.getStackInSlot(j))) { for (int j = 0; j < player.inventory.getSizeInventory(); ++j) {
matrix.setInventorySlotContents(i, ItemHandlerHelper.copyStackWithSize(player.inventory.getStackInSlot(j), 1)); if (API.instance().getComparer().isEqualNoQuantity(possibility, player.inventory.getStackInSlot(j))) {
matrix.setInventorySlotContents(i, ItemHandlerHelper.copyStackWithSize(player.inventory.getStackInSlot(j), 1));
player.inventory.decrStackSize(j, 1); player.inventory.decrStackSize(j, 1);
found = true; found = true;
break;
}
}
if (found) {
break; break;
} }
} }
if (found) {
break;
}
} }
} else if (getType() == EnumGridType.PATTERN) {
matrix.setInventorySlotContents(i, possibilities[0]);
} }
} else if (getType() == EnumGridType.PATTERN) {
// If we are a pattern grid we can just set the slot
matrix.setInventorySlotContents(i, possibilities[0]);
} }
} }
} }