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-11-18 13:40:38 +00:00
|
|
|
import dan200.computercraft.api.lua.ILuaAPI;
|
2017-05-01 13:32:39 +00:00
|
|
|
import dan200.computercraft.api.lua.ILuaContext;
|
|
|
|
import dan200.computercraft.api.lua.LuaException;
|
2017-05-02 00:53:30 +00:00
|
|
|
import dan200.computercraft.api.pocket.IPocketUpgrade;
|
2018-12-10 23:46:50 +00:00
|
|
|
import dan200.computercraft.shared.PocketUpgrades;
|
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-11 00:08:26 +00:00
|
|
|
import net.minecraft.util.NonNullList;
|
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"
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
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-06-12 20:08:35 +00:00
|
|
|
return context.executeMainThreadTask( () ->
|
2017-05-02 00:53:30 +00:00
|
|
|
{
|
2017-06-12 20:08:35 +00:00
|
|
|
if( !(m_computer.getEntity() instanceof EntityPlayer) )
|
2017-05-05 01:07:11 +00:00
|
|
|
{
|
2017-06-12 20:08:35 +00:00
|
|
|
throw new LuaException( "Cannot find player" );
|
|
|
|
}
|
2017-05-02 00:53:30 +00:00
|
|
|
|
2017-06-12 20:08:35 +00:00
|
|
|
EntityPlayer player = (EntityPlayer) m_computer.getEntity();
|
|
|
|
InventoryPlayer inventory = player.inventory;
|
2017-05-02 00:53:30 +00:00
|
|
|
|
2017-06-12 20:08:35 +00:00
|
|
|
IPocketUpgrade previousUpgrade = m_computer.getUpgrade();
|
2017-05-02 00:53:30 +00:00
|
|
|
|
2017-06-12 20:08:35 +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 )
|
|
|
|
{
|
|
|
|
newUpgrade = findUpgrade( inventory.offHandInventory, 0, previousUpgrade );
|
|
|
|
}
|
|
|
|
if( newUpgrade == null ) throw new LuaException( "Cannot find a valid upgrade" );
|
2017-05-02 00:53:30 +00:00
|
|
|
|
2017-06-12 20:08:35 +00:00
|
|
|
// Remove the current upgrade
|
|
|
|
if( previousUpgrade != null )
|
|
|
|
{
|
|
|
|
ItemStack stack = previousUpgrade.getCraftingItem();
|
|
|
|
if( !stack.isEmpty() )
|
2017-05-05 01:07:11 +00:00
|
|
|
{
|
2017-06-12 20:08:35 +00:00
|
|
|
stack = InventoryUtil.storeItems( stack, new PlayerMainInvWrapper( inventory ), inventory.currentItem );
|
2017-05-11 00:08:26 +00:00
|
|
|
if( !stack.isEmpty() )
|
2017-05-05 01:07:11 +00:00
|
|
|
{
|
2017-06-12 20:08:35 +00:00
|
|
|
WorldUtil.dropItemStack( stack, player.getEntityWorld(), player.posX, player.posY, player.posZ );
|
2017-05-05 01:07:11 +00:00
|
|
|
}
|
|
|
|
}
|
2017-06-12 20:08:35 +00:00
|
|
|
}
|
2017-05-02 00:53:30 +00:00
|
|
|
|
2017-06-12 20:08:35 +00:00
|
|
|
// Set the new upgrade
|
|
|
|
m_computer.setUpgrade( newUpgrade );
|
2017-05-04 22:21:26 +00:00
|
|
|
|
2017-06-12 20:08:35 +00:00
|
|
|
return null;
|
2017-05-05 01:07:11 +00:00
|
|
|
} );
|
2017-05-02 00:53:30 +00:00
|
|
|
|
|
|
|
case 1:
|
2017-05-04 22:21:26 +00:00
|
|
|
// unequipBack
|
2017-06-12 20:08:35 +00:00
|
|
|
return context.executeMainThreadTask( () ->
|
2017-05-02 00:53:30 +00:00
|
|
|
{
|
2017-06-12 20:08:35 +00:00
|
|
|
if( !(m_computer.getEntity() instanceof EntityPlayer) )
|
2017-05-05 01:07:11 +00:00
|
|
|
{
|
2017-06-12 20:08:35 +00:00
|
|
|
throw new LuaException( "Cannot find player" );
|
|
|
|
}
|
2017-05-02 00:53:30 +00:00
|
|
|
|
2017-06-12 20:08:35 +00:00
|
|
|
EntityPlayer player = (EntityPlayer) m_computer.getEntity();
|
|
|
|
InventoryPlayer inventory = player.inventory;
|
2017-05-02 00:53:30 +00:00
|
|
|
|
2017-06-12 20:08:35 +00:00
|
|
|
IPocketUpgrade previousUpgrade = m_computer.getUpgrade();
|
2017-05-02 00:53:30 +00:00
|
|
|
|
2017-06-12 20:08:35 +00:00
|
|
|
if( previousUpgrade == null ) throw new LuaException( "Nothing to unequip" );
|
2017-05-02 00:53:30 +00:00
|
|
|
|
2017-06-12 20:08:35 +00:00
|
|
|
m_computer.setUpgrade( null );
|
2017-05-02 00:53:30 +00:00
|
|
|
|
2017-06-12 20:08:35 +00:00
|
|
|
ItemStack stack = previousUpgrade.getCraftingItem();
|
|
|
|
if( !stack.isEmpty() )
|
|
|
|
{
|
|
|
|
stack = InventoryUtil.storeItems( stack, new PlayerMainInvWrapper( inventory ), inventory.currentItem );
|
|
|
|
if( stack.isEmpty() )
|
2017-05-05 01:07:11 +00:00
|
|
|
{
|
2017-06-12 20:08:35 +00:00
|
|
|
WorldUtil.dropItemStack( stack, player.getEntityWorld(), player.posX, player.posY, player.posZ );
|
2017-05-05 01:07:11 +00:00
|
|
|
}
|
|
|
|
}
|
2017-06-12 20:08:35 +00:00
|
|
|
|
|
|
|
return null;
|
2017-05-05 01:07:11 +00:00
|
|
|
} );
|
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
|
|
|
|
2017-05-11 00:08:26 +00:00
|
|
|
private static IPocketUpgrade findUpgrade( NonNullList<ItemStack> inv, int start, IPocketUpgrade previous )
|
2017-05-04 22:21:26 +00:00
|
|
|
{
|
2017-05-11 00:08:26 +00:00
|
|
|
for( int i = 0; i < inv.size(); i++ )
|
2017-05-04 22:21:26 +00:00
|
|
|
{
|
2017-05-11 00:08:26 +00:00
|
|
|
ItemStack invStack = inv.get( (i + start) % inv.size() );
|
|
|
|
if( !invStack.isEmpty() )
|
2017-05-04 22:21:26 +00:00
|
|
|
{
|
2018-12-10 23:46:50 +00:00
|
|
|
IPocketUpgrade newUpgrade = PocketUpgrades.get( invStack );
|
2017-05-04 22:21:26 +00:00
|
|
|
|
|
|
|
if( newUpgrade != null && newUpgrade != previous )
|
|
|
|
{
|
|
|
|
// Consume an item from this stack and exit the loop
|
|
|
|
invStack = invStack.copy();
|
2017-05-11 00:08:26 +00:00
|
|
|
invStack.shrink( 1 );
|
|
|
|
inv.set( (i + start) % inv.size(), invStack.isEmpty() ? ItemStack.EMPTY : invStack );
|
2017-05-04 22:21:26 +00:00
|
|
|
|
|
|
|
return newUpgrade;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
2017-05-01 13:32:39 +00:00
|
|
|
}
|