1
0
mirror of https://github.com/SquidDev-CC/CC-Tweaked synced 2024-06-25 22:53:22 +00:00

Fix Pocket API working outside of player inventory

This makes Pocket API not equip/unequip upgrades when the pocket
computer is outside of the player inventory (e.g. dragging,
dropped, placed in a chest).
This commit is contained in:
Iunius118 2017-06-21 22:31:27 +09:00 committed by SquidDev
parent 1290a4402c
commit ba823bae13

View File

@ -12,6 +12,7 @@
import dan200.computercraft.api.pocket.IPocketUpgrade;
import dan200.computercraft.shared.PocketUpgrades;
import dan200.computercraft.shared.pocket.core.PocketServerComputer;
import dan200.computercraft.shared.pocket.items.ItemPocketComputer;
import dan200.computercraft.shared.util.InventoryUtil;
import dan200.computercraft.shared.util.WorldUtil;
import net.minecraft.entity.player.EntityPlayer;
@ -66,6 +67,12 @@ public Object[] callMethod( @Nonnull ILuaContext context, int method, @Nonnull O
EntityPlayer player = (EntityPlayer) m_computer.getEntity();
InventoryPlayer inventory = player.inventory;
int computerID = m_computer.getID();
if ( !pocketComputerExists( inventory.mainInventory, computerID ) && !pocketComputerExists( inventory.offHandInventory, computerID ) )
{
throw new LuaException( "Cannot find pocket computer" );
}
IPocketUpgrade previousUpgrade = m_computer.getUpgrade();
// Attempt to find the upgrade, starting in the main segment, and then looking in the opposite
@ -109,6 +116,12 @@ public Object[] callMethod( @Nonnull ILuaContext context, int method, @Nonnull O
EntityPlayer player = (EntityPlayer) m_computer.getEntity();
InventoryPlayer inventory = player.inventory;
int computerID = m_computer.getID();
if ( !pocketComputerExists( inventory.mainInventory, computerID ) && !pocketComputerExists( inventory.offHandInventory, computerID ) )
{
throw new LuaException( "Cannot find pocket computer" );
}
IPocketUpgrade previousUpgrade = m_computer.getUpgrade();
if( previousUpgrade == null ) throw new LuaException( "Nothing to unequip" );
@ -155,4 +168,20 @@ private static IPocketUpgrade findUpgrade( NonNullList<ItemStack> inv, int start
return null;
}
private static boolean pocketComputerExists( NonNullList<ItemStack> inv, int computerID )
{
for( ItemStack invStack : inv )
{
if( !invStack.isEmpty() && invStack.getItem() instanceof ItemPocketComputer )
{
if( ComputerCraft.Items.pocketComputer.getComputerID( invStack ) == computerID )
{
return true;
}
}
}
return false;
}
}