mirror of
https://github.com/SquidDev-CC/CC-Tweaked
synced 2025-07-01 01:23:12 +00:00

When creating a peripheral or custom Lua object, one must implement two methods: - getMethodNames(): String[] - Returns the name of the methods - callMethod(int, ...): Object[] - Invokes the method using an index in the above array. This has a couple of problems: - It's somewhat unwieldy to use - you need to keep track of array indices, which leads to ugly code. - Functions which yield (for instance, those which run on the main thread) are blocking. This means we need to spawn new threads for each CC-side yield. We replace this system with a few changes: - @LuaFunction annotation: One may annotate a public instance method with this annotation. This then exposes a peripheral/lua object method. Furthermore, this method can accept and return a variety of types, which often makes functions cleaner (e.g. can return an int rather than an Object[], and specify and int argument rather than Object[]). - MethodResult: Instead of returning an Object[] and having blocking yields, functions return a MethodResult. This either contains an immediate return, or an instruction to yield with some continuation to resume with. MethodResult is then interpreted by the Lua runtime (i.e. Cobalt), rather than our weird bodgey hacks before. This means we no longer spawn new threads when yielding within CC. - Methods accept IArguments instead of a raw Object array. This has a few benefits: - Consistent argument handling - people no longer need to use ArgumentHelper (as it doesn't exist!), or even be aware of its existence - you're rather forced into using it. - More efficient code in some cases. We provide a Cobalt-specific implementation of IArguments, which avoids the boxing/unboxing when handling numbers and binary strings.
125 lines
4.5 KiB
Java
125 lines
4.5 KiB
Java
/*
|
|
* This file is part of ComputerCraft - http://www.computercraft.info
|
|
* Copyright Daniel Ratcliffe, 2011-2020. Do not distribute without permission.
|
|
* Send enquiries to dratcliffe@gmail.com
|
|
*/
|
|
package dan200.computercraft.shared.pocket.apis;
|
|
|
|
import dan200.computercraft.api.lua.ILuaAPI;
|
|
import dan200.computercraft.api.lua.LuaFunction;
|
|
import dan200.computercraft.api.pocket.IPocketUpgrade;
|
|
import dan200.computercraft.shared.PocketUpgrades;
|
|
import dan200.computercraft.shared.pocket.core.PocketServerComputer;
|
|
import dan200.computercraft.shared.util.InventoryUtil;
|
|
import dan200.computercraft.shared.util.WorldUtil;
|
|
import net.minecraft.entity.Entity;
|
|
import net.minecraft.entity.player.PlayerEntity;
|
|
import net.minecraft.entity.player.PlayerInventory;
|
|
import net.minecraft.item.ItemStack;
|
|
import net.minecraft.util.NonNullList;
|
|
import net.minecraftforge.items.wrapper.PlayerMainInvWrapper;
|
|
|
|
public class PocketAPI implements ILuaAPI
|
|
{
|
|
private final PocketServerComputer computer;
|
|
|
|
public PocketAPI( PocketServerComputer computer )
|
|
{
|
|
this.computer = computer;
|
|
}
|
|
|
|
@Override
|
|
public String[] getNames()
|
|
{
|
|
return new String[] { "pocket" };
|
|
}
|
|
|
|
@LuaFunction( mainThread = true )
|
|
public final Object[] equipBack()
|
|
{
|
|
Entity entity = computer.getEntity();
|
|
if( !(entity instanceof PlayerEntity) ) return new Object[] { false, "Cannot find player" };
|
|
PlayerEntity player = (PlayerEntity) entity;
|
|
PlayerInventory inventory = player.inventory;
|
|
IPocketUpgrade previousUpgrade = computer.getUpgrade();
|
|
|
|
// 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 ) return new Object[] { false, "Cannot find a valid upgrade" };
|
|
|
|
// Remove the current upgrade
|
|
if( previousUpgrade != null )
|
|
{
|
|
ItemStack stack = previousUpgrade.getCraftingItem();
|
|
if( !stack.isEmpty() )
|
|
{
|
|
stack = InventoryUtil.storeItems( stack, new PlayerMainInvWrapper( inventory ), inventory.currentItem );
|
|
if( !stack.isEmpty() )
|
|
{
|
|
WorldUtil.dropItemStack( stack, player.getEntityWorld(), player.getPositionVec() );
|
|
}
|
|
}
|
|
}
|
|
|
|
// Set the new upgrade
|
|
computer.setUpgrade( newUpgrade );
|
|
|
|
return new Object[] { true };
|
|
}
|
|
|
|
@LuaFunction( mainThread = true )
|
|
public final Object[] unequipBack()
|
|
{
|
|
Entity entity = computer.getEntity();
|
|
if( !(entity instanceof PlayerEntity) ) return new Object[] { false, "Cannot find player" };
|
|
PlayerEntity player = (PlayerEntity) entity;
|
|
PlayerInventory inventory = player.inventory;
|
|
IPocketUpgrade previousUpgrade = computer.getUpgrade();
|
|
|
|
if( previousUpgrade == null ) return new Object[] { false, "Nothing to unequip" };
|
|
|
|
computer.setUpgrade( null );
|
|
|
|
ItemStack stack = previousUpgrade.getCraftingItem();
|
|
if( !stack.isEmpty() )
|
|
{
|
|
stack = InventoryUtil.storeItems( stack, new PlayerMainInvWrapper( inventory ), inventory.currentItem );
|
|
if( stack.isEmpty() )
|
|
{
|
|
WorldUtil.dropItemStack( stack, player.getEntityWorld(), player.getPositionVec() );
|
|
}
|
|
}
|
|
|
|
return new Object[] { true };
|
|
}
|
|
|
|
private static IPocketUpgrade findUpgrade( NonNullList<ItemStack> inv, int start, IPocketUpgrade previous )
|
|
{
|
|
for( int i = 0; i < inv.size(); i++ )
|
|
{
|
|
ItemStack invStack = inv.get( (i + start) % inv.size() );
|
|
if( !invStack.isEmpty() )
|
|
{
|
|
IPocketUpgrade newUpgrade = PocketUpgrades.get( invStack );
|
|
|
|
if( newUpgrade != null && newUpgrade != previous )
|
|
{
|
|
// Consume an item from this stack and exit the loop
|
|
invStack = invStack.copy();
|
|
invStack.shrink( 1 );
|
|
inv.set( (i + start) % inv.size(), invStack.isEmpty() ? ItemStack.EMPTY : invStack );
|
|
|
|
return newUpgrade;
|
|
}
|
|
}
|
|
}
|
|
|
|
return null;
|
|
}
|
|
}
|