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,8 +288,7 @@ 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 {
@@ -295,7 +296,6 @@ public class TileGrid extends TileNode implements IGrid {
} }
} }
} }
}
onCraftingMatrixChanged(); onCraftingMatrixChanged();
} }
@@ -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) {
// Only if we are a crafting grid. Pattern grids can just be emptied.
if (getType() == EnumGridType.CRAFTING) { 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);
} }
} }
// Now let's fill the matrix
for (int i = 0; i < matrix.getSizeInventory(); ++i) { for (int i = 0; i < matrix.getSizeInventory(); ++i) {
if (recipe[i] != null) { if (recipe[i] != null) {
ItemStack[] possibilities = recipe[i]; ItemStack[] possibilities = recipe[i];
// If we are a crafting grid
if (getType() == EnumGridType.CRAFTING) { if (getType() == EnumGridType.CRAFTING) {
boolean found = false; 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,7 +403,9 @@ public class TileGrid extends TileNode implements IGrid {
break; break;
} }
} }
}
// If we haven't found anything in the network (or we are disconnected), go look in the player inventory
if (!found) { if (!found) {
for (ItemStack possibility : possibilities) { for (ItemStack possibility : possibilities) {
for (int j = 0; j < player.inventory.getSizeInventory(); ++j) { for (int j = 0; j < player.inventory.getSizeInventory(); ++j) {
@@ -411,12 +426,12 @@ public class TileGrid extends TileNode implements IGrid {
} }
} }
} else if (getType() == EnumGridType.PATTERN) { } else if (getType() == EnumGridType.PATTERN) {
// If we are a pattern grid we can just set the slot
matrix.setInventorySlotContents(i, possibilities[0]); matrix.setInventorySlotContents(i, possibilities[0]);
} }
} }
} }
} }
}
@Override @Override
public int getViewType() { public int getViewType() {