mirror of
https://github.com/SquidDev-CC/CC-Tweaked
synced 2025-10-26 03:17:38 +00:00
Add .equip and .unequip methods to pocket API.
This commit is contained in:
@@ -6,14 +6,26 @@
|
|||||||
|
|
||||||
package dan200.computercraft.shared.pocket.apis;
|
package dan200.computercraft.shared.pocket.apis;
|
||||||
|
|
||||||
|
import dan200.computercraft.ComputerCraft;
|
||||||
import dan200.computercraft.api.lua.ILuaContext;
|
import dan200.computercraft.api.lua.ILuaContext;
|
||||||
import dan200.computercraft.api.lua.LuaException;
|
import dan200.computercraft.api.lua.LuaException;
|
||||||
|
import dan200.computercraft.api.pocket.IPocketUpgrade;
|
||||||
import dan200.computercraft.core.apis.ILuaAPI;
|
import dan200.computercraft.core.apis.ILuaAPI;
|
||||||
|
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;
|
||||||
|
import net.minecraft.entity.player.InventoryPlayer;
|
||||||
|
import net.minecraft.item.ItemStack;
|
||||||
|
|
||||||
public class PocketAPI implements ILuaAPI
|
public class PocketAPI implements ILuaAPI
|
||||||
{
|
{
|
||||||
public PocketAPI()
|
private final PocketServerComputer m_computer;
|
||||||
|
|
||||||
|
public PocketAPI( PocketServerComputer computer )
|
||||||
{
|
{
|
||||||
|
this.m_computer = computer;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -43,14 +55,107 @@ public class PocketAPI implements ILuaAPI
|
|||||||
public String[] getMethodNames()
|
public String[] getMethodNames()
|
||||||
{
|
{
|
||||||
return new String[] {
|
return new String[] {
|
||||||
// TODO: Add some methods
|
"equip",
|
||||||
|
"unequip"
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Object[] callMethod( ILuaContext context, int method, Object[] arguments ) throws LuaException
|
public Object[] callMethod( ILuaContext context, int method, Object[] arguments ) throws LuaException
|
||||||
{
|
{
|
||||||
// TODO: Add some methods
|
switch( method )
|
||||||
|
{
|
||||||
|
case 0:
|
||||||
|
{
|
||||||
|
// equip
|
||||||
|
if( !(m_computer.getEntity() instanceof EntityPlayer) )
|
||||||
|
{
|
||||||
|
throw new LuaException( "Cannot find player" );
|
||||||
|
}
|
||||||
|
|
||||||
|
ItemStack pocketStack = m_computer.getStack();
|
||||||
|
EntityPlayer player = (EntityPlayer) m_computer.getEntity();
|
||||||
|
InventoryPlayer inventory = player.inventory;
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if( newUpgrade == null ) throw new LuaException( "Cannot find a valid upgrade" );
|
||||||
|
|
||||||
|
// Remove the current upgrade
|
||||||
|
if( previousUpgrade != null )
|
||||||
|
{
|
||||||
|
ItemStack stack = previousUpgrade.getCraftingItem();
|
||||||
|
if( stack != null )
|
||||||
|
{
|
||||||
|
stack = InventoryUtil.storeItems( stack, inventory, 0, 36, inventory.currentItem );
|
||||||
|
if( stack != null )
|
||||||
|
{
|
||||||
|
WorldUtil.dropItemStack( stack, player.worldObj, player.posX, player.posY, player.posZ );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set the new upgrade
|
||||||
|
ItemPocketComputer.setUpgrade( pocketStack, newUpgrade );
|
||||||
|
m_computer.setUpgrade( newUpgrade );
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
case 1:
|
||||||
|
{
|
||||||
|
// unequip
|
||||||
|
if( !(m_computer.getEntity() instanceof EntityPlayer) )
|
||||||
|
{
|
||||||
|
throw new LuaException( "Cannot find player" );
|
||||||
|
}
|
||||||
|
|
||||||
|
ItemStack pocketStack = m_computer.getStack();
|
||||||
|
EntityPlayer player = (EntityPlayer) m_computer.getEntity();
|
||||||
|
InventoryPlayer inventory = player.inventory;
|
||||||
|
|
||||||
|
IPocketUpgrade previousUpgrade = m_computer.getUpgrade();
|
||||||
|
|
||||||
|
if( previousUpgrade == null ) throw new LuaException( "Nothing to unequip" );
|
||||||
|
|
||||||
|
ItemPocketComputer.setUpgrade( pocketStack, null );
|
||||||
|
m_computer.setUpgrade( null );
|
||||||
|
|
||||||
|
ItemStack stack = previousUpgrade.getCraftingItem();
|
||||||
|
if( stack != null )
|
||||||
|
{
|
||||||
|
stack = InventoryUtil.storeItems( stack, inventory, 0, 36, inventory.currentItem );
|
||||||
|
if( stack != null )
|
||||||
|
{
|
||||||
|
WorldUtil.dropItemStack( stack, player.worldObj, player.posX, player.posY, player.posZ );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -117,6 +117,14 @@ public class PocketServerComputer extends ServerComputer implements IPocketAcces
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public ItemStack getStack() {
|
||||||
|
return m_stack;
|
||||||
|
}
|
||||||
|
|
||||||
|
public IPocketUpgrade getUpgrade() {
|
||||||
|
return m_upgrade;
|
||||||
|
}
|
||||||
|
|
||||||
public void update( Entity entity, ItemStack stack )
|
public void update( Entity entity, ItemStack stack )
|
||||||
{
|
{
|
||||||
if( m_entity != null ) setPosition( entity.getPosition() );
|
if( m_entity != null ) setPosition( entity.getPosition() );
|
||||||
|
|||||||
@@ -260,7 +260,7 @@ public class ItemPocketComputer extends Item implements IComputerItem, IMedia
|
|||||||
stack,
|
stack,
|
||||||
entity
|
entity
|
||||||
);
|
);
|
||||||
computer.addAPI( new PocketAPI() );
|
computer.addAPI( new PocketAPI( computer ) );
|
||||||
computer.setUpgrade( getUpgrade( stack ) );
|
computer.setUpgrade( getUpgrade( stack ) );
|
||||||
ComputerCraft.serverComputerRegistry.add( instanceID, computer );
|
ComputerCraft.serverComputerRegistry.add( instanceID, computer );
|
||||||
if( inventory != null )
|
if( inventory != null )
|
||||||
@@ -479,4 +479,21 @@ public class ItemPocketComputer extends Item implements IComputerItem, IMedia
|
|||||||
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static void setUpgrade( ItemStack stack, IPocketUpgrade upgrade )
|
||||||
|
{
|
||||||
|
NBTTagCompound compound = stack.getTagCompound();
|
||||||
|
if( compound == null ) stack.setTagCompound( compound = new NBTTagCompound() );
|
||||||
|
|
||||||
|
if( upgrade == null )
|
||||||
|
{
|
||||||
|
compound.removeTag( "upgrade" );
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
compound.setString( "upgrade", upgrade.getUpgradeID().toString() );
|
||||||
|
}
|
||||||
|
|
||||||
|
compound.removeTag( "upgrade_info" );
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user