2017-05-07 00:18:59 +00:00
|
|
|
/*
|
2017-05-01 13:32:39 +00:00
|
|
|
* This file is part of ComputerCraft - http://www.computercraft.info
|
2017-05-13 18:20:39 +00:00
|
|
|
* Copyright Daniel Ratcliffe, 2011-2017. Do not distribute without permission.
|
2017-05-01 13:32:39 +00:00
|
|
|
* Send enquiries to dratcliffe@gmail.com
|
|
|
|
*/
|
|
|
|
|
|
|
|
package dan200.computercraft.shared.pocket.apis;
|
|
|
|
|
2017-05-02 00:53:30 +00:00
|
|
|
import dan200.computercraft.ComputerCraft;
|
2017-05-01 13:32:39 +00:00
|
|
|
import dan200.computercraft.api.lua.ILuaContext;
|
2017-05-05 01:07:11 +00:00
|
|
|
import dan200.computercraft.api.lua.ILuaTask;
|
2017-05-01 13:32:39 +00:00
|
|
|
import dan200.computercraft.api.lua.LuaException;
|
2017-05-02 00:53:30 +00:00
|
|
|
import dan200.computercraft.api.pocket.IPocketUpgrade;
|
2017-05-01 13:32:39 +00:00
|
|
|
import dan200.computercraft.core.apis.ILuaAPI;
|
2017-05-02 00:53:30 +00:00
|
|
|
import dan200.computercraft.shared.pocket.core.PocketServerComputer;
|
|
|
|
import dan200.computercraft.shared.util.InventoryUtil;
|
|
|
|
import dan200.computercraft.shared.util.WorldUtil;
|
|
|
|
import net.minecraft.entity.player.EntityPlayer;
|
|
|
|
import net.minecraft.entity.player.InventoryPlayer;
|
|
|
|
import net.minecraft.item.ItemStack;
|
2017-05-08 23:41:20 +00:00
|
|
|
import net.minecraftforge.items.wrapper.PlayerMainInvWrapper;
|
2017-05-01 13:32:39 +00:00
|
|
|
|
2017-05-06 23:07:42 +00:00
|
|
|
import javax.annotation.Nonnull;
|
|
|
|
|
2017-05-01 13:32:39 +00:00
|
|
|
public class PocketAPI implements ILuaAPI
|
|
|
|
{
|
2017-05-02 00:53:30 +00:00
|
|
|
private final PocketServerComputer m_computer;
|
|
|
|
|
|
|
|
public PocketAPI( PocketServerComputer computer )
|
2017-05-01 13:32:39 +00:00
|
|
|
{
|
2017-05-02 00:53:30 +00:00
|
|
|
this.m_computer = computer;
|
2017-05-01 13:32:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String[] getNames()
|
|
|
|
{
|
|
|
|
return new String[] {
|
|
|
|
"pocket"
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void startup()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void advance( double dt )
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void shutdown()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2017-05-06 23:07:42 +00:00
|
|
|
@Nonnull
|
2017-05-01 13:32:39 +00:00
|
|
|
@Override
|
|
|
|
public String[] getMethodNames()
|
|
|
|
{
|
|
|
|
return new String[] {
|
2017-05-04 22:21:26 +00:00
|
|
|
"equipBack",
|
|
|
|
"unequipBack"
|
2017-05-01 13:32:39 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2017-05-06 23:07:42 +00:00
|
|
|
public Object[] callMethod( @Nonnull ILuaContext context, int method, @Nonnull Object[] arguments ) throws LuaException, InterruptedException
|
2017-05-01 13:32:39 +00:00
|
|
|
{
|
2017-05-02 00:53:30 +00:00
|
|
|
switch( method )
|
|
|
|
{
|
|
|
|
case 0:
|
2017-05-04 22:21:26 +00:00
|
|
|
// equipBack
|
2017-05-05 01:07:11 +00:00
|
|
|
return context.executeMainThreadTask( new ILuaTask()
|
2017-05-02 00:53:30 +00:00
|
|
|
{
|
2017-05-05 01:07:11 +00:00
|
|
|
@Override
|
|
|
|
public Object[] execute() throws LuaException
|
|
|
|
{
|
|
|
|
if( !(m_computer.getEntity() instanceof EntityPlayer) )
|
|
|
|
{
|
|
|
|
throw new LuaException( "Cannot find player" );
|
|
|
|
}
|
2017-05-02 00:53:30 +00:00
|
|
|
|
2017-05-05 01:07:11 +00:00
|
|
|
EntityPlayer player = (EntityPlayer) m_computer.getEntity();
|
|
|
|
InventoryPlayer inventory = player.inventory;
|
2017-05-02 00:53:30 +00:00
|
|
|
|
2017-05-05 01:07:11 +00:00
|
|
|
IPocketUpgrade previousUpgrade = m_computer.getUpgrade();
|
2017-05-02 00:53:30 +00:00
|
|
|
|
2017-05-05 01:07:11 +00:00
|
|
|
// 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 )
|
2017-05-02 00:53:30 +00:00
|
|
|
{
|
2017-05-05 01:07:11 +00:00
|
|
|
newUpgrade = findUpgrade( inventory.offHandInventory, 0, previousUpgrade );
|
2017-05-02 00:53:30 +00:00
|
|
|
}
|
2017-05-05 01:07:11 +00:00
|
|
|
if( newUpgrade == null ) throw new LuaException( "Cannot find a valid upgrade" );
|
2017-05-02 00:53:30 +00:00
|
|
|
|
2017-05-05 01:07:11 +00:00
|
|
|
// Remove the current upgrade
|
|
|
|
if( previousUpgrade != null )
|
|
|
|
{
|
|
|
|
ItemStack stack = previousUpgrade.getCraftingItem();
|
|
|
|
if( stack != null )
|
|
|
|
{
|
2017-05-08 23:41:20 +00:00
|
|
|
stack = InventoryUtil.storeItems( stack, new PlayerMainInvWrapper( inventory ), inventory.currentItem );
|
2017-05-05 01:07:11 +00:00
|
|
|
if( stack != null )
|
|
|
|
{
|
|
|
|
WorldUtil.dropItemStack( stack, player.worldObj, player.posX, player.posY, player.posZ );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-05-02 00:53:30 +00:00
|
|
|
|
2017-05-05 01:07:11 +00:00
|
|
|
// Set the new upgrade
|
|
|
|
m_computer.setUpgrade( newUpgrade );
|
2017-05-04 22:21:26 +00:00
|
|
|
|
2017-05-05 01:07:11 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
} );
|
2017-05-02 00:53:30 +00:00
|
|
|
|
|
|
|
case 1:
|
2017-05-04 22:21:26 +00:00
|
|
|
// unequipBack
|
2017-05-05 01:07:11 +00:00
|
|
|
return context.executeMainThreadTask( new ILuaTask()
|
2017-05-02 00:53:30 +00:00
|
|
|
{
|
2017-05-05 01:07:11 +00:00
|
|
|
@Override
|
|
|
|
public Object[] execute() throws LuaException
|
|
|
|
{
|
|
|
|
if( !(m_computer.getEntity() instanceof EntityPlayer) )
|
|
|
|
{
|
|
|
|
throw new LuaException( "Cannot find player" );
|
|
|
|
}
|
2017-05-02 00:53:30 +00:00
|
|
|
|
2017-05-05 01:07:11 +00:00
|
|
|
EntityPlayer player = (EntityPlayer) m_computer.getEntity();
|
|
|
|
InventoryPlayer inventory = player.inventory;
|
2017-05-02 00:53:30 +00:00
|
|
|
|
2017-05-05 01:07:11 +00:00
|
|
|
IPocketUpgrade previousUpgrade = m_computer.getUpgrade();
|
2017-05-02 00:53:30 +00:00
|
|
|
|
2017-05-05 01:07:11 +00:00
|
|
|
if( previousUpgrade == null ) throw new LuaException( "Nothing to unequip" );
|
2017-05-02 00:53:30 +00:00
|
|
|
|
2017-05-05 01:07:11 +00:00
|
|
|
m_computer.setUpgrade( null );
|
2017-05-02 00:53:30 +00:00
|
|
|
|
2017-05-05 01:07:11 +00:00
|
|
|
ItemStack stack = previousUpgrade.getCraftingItem();
|
|
|
|
if( stack != null )
|
|
|
|
{
|
2017-05-08 23:41:20 +00:00
|
|
|
stack = InventoryUtil.storeItems( stack, new PlayerMainInvWrapper( inventory ), inventory.currentItem );
|
2017-05-05 01:07:11 +00:00
|
|
|
if( stack != null )
|
|
|
|
{
|
|
|
|
WorldUtil.dropItemStack( stack, player.worldObj, player.posX, player.posY, player.posZ );
|
|
|
|
}
|
|
|
|
}
|
2017-05-02 00:53:30 +00:00
|
|
|
|
2017-05-05 01:07:11 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
} );
|
2017-05-02 00:53:30 +00:00
|
|
|
default:
|
|
|
|
return null;
|
|
|
|
}
|
2017-05-01 13:32:39 +00:00
|
|
|
}
|
2017-05-04 22:21:26 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
2017-05-01 13:32:39 +00:00
|
|
|
}
|