1
0
mirror of https://github.com/SquidDev-CC/CC-Tweaked synced 2025-01-27 09:24:47 +00:00

Check the offhand inventory too when searching for upgrades

This commit is contained in:
SquidDev 2017-05-04 23:21:26 +01:00
parent 22631cfc63
commit 7f8100ae0f

View File

@ -55,8 +55,8 @@ public class PocketAPI implements ILuaAPI
public String[] getMethodNames() public String[] getMethodNames()
{ {
return new String[] { return new String[] {
"equip", "equipBack",
"unequip" "unequipBack"
}; };
} }
@ -67,7 +67,7 @@ public class PocketAPI implements ILuaAPI
{ {
case 0: case 0:
{ {
// equip // equipBack
if( !(m_computer.getEntity() instanceof EntityPlayer) ) if( !(m_computer.getEntity() instanceof EntityPlayer) )
{ {
throw new LuaException( "Cannot find player" ); throw new LuaException( "Cannot find player" );
@ -78,28 +78,11 @@ public class PocketAPI implements ILuaAPI
InventoryPlayer inventory = player.inventory; InventoryPlayer inventory = player.inventory;
IPocketUpgrade previousUpgrade = m_computer.getUpgrade(); IPocketUpgrade previousUpgrade = m_computer.getUpgrade();
IPocketUpgrade newUpgrade = null;
int size = inventory.getSizeInventory(), held = inventory.currentItem;
for (int i = 0; i < size; i++)
{
ItemStack invStack = inventory.getStackInSlot( (i + held) % size );
if( invStack != null )
{
newUpgrade = ComputerCraft.getPocketUpgrade( invStack );
if( newUpgrade != null && newUpgrade != previousUpgrade )
{
// Consume an item from this stack and exit the loop
invStack = invStack.copy();
invStack.stackSize--;
inventory.setInventorySlotContents( (i + held) % size, invStack.stackSize <= 0 ? null : invStack );
break;
}
}
}
// Attempt to find the upgrade, starting in the main segment, and then looking in the opposite
// one. We start from the position the item is currently in and loop round to the start.
IPocketUpgrade newUpgrade = findUpgrade( inventory.mainInventory, inventory.currentItem, previousUpgrade );
if( newUpgrade == null ) newUpgrade = findUpgrade( inventory.offHandInventory, 0, previousUpgrade );
if( newUpgrade == null ) throw new LuaException( "Cannot find a valid upgrade" ); if( newUpgrade == null ) throw new LuaException( "Cannot find a valid upgrade" );
// Remove the current upgrade // Remove the current upgrade
@ -120,12 +103,14 @@ public class PocketAPI implements ILuaAPI
ItemPocketComputer.setUpgrade( pocketStack, newUpgrade ); ItemPocketComputer.setUpgrade( pocketStack, newUpgrade );
m_computer.setUpgrade( newUpgrade ); m_computer.setUpgrade( newUpgrade );
inventory.markDirty();
return null; return null;
} }
case 1: case 1:
{ {
// unequip // unequipBack
if( !(m_computer.getEntity() instanceof EntityPlayer) ) if( !(m_computer.getEntity() instanceof EntityPlayer) )
{ {
throw new LuaException( "Cannot find player" ); throw new LuaException( "Cannot find player" );
@ -158,4 +143,28 @@ public class PocketAPI implements ILuaAPI
return null; return null;
} }
} }
private static IPocketUpgrade findUpgrade( ItemStack[] inv, int start, IPocketUpgrade previous )
{
for (int i = 0; i < inv.length; i++)
{
ItemStack invStack = inv[ (i + start) % inv.length ];
if( invStack != null )
{
IPocketUpgrade newUpgrade = ComputerCraft.getPocketUpgrade( invStack );
if( newUpgrade != null && newUpgrade != previous )
{
// Consume an item from this stack and exit the loop
invStack = invStack.copy();
invStack.stackSize--;
inv[ (i + start) % inv.length ] = invStack.stackSize <= 0 ? null : invStack;
return newUpgrade;
}
}
}
return null;
}
} }