1
0
mirror of https://github.com/SquidDev-CC/CC-Tweaked synced 2024-06-25 22:53:22 +00:00
CC-Tweaked/src/main/java/dan200/computercraft/shared/pocket/apis/PocketAPI.java
SquidDev f9e13ca67a Update CC: Tweaked to 1.13
Look, I originally had this split into several commits, but lots of
other cleanups got mixed in. I then backported some of the cleanups to
1.12, did other tidy ups there, and eventually the web of merges was
unreadable.

Yes, this is a horrible mess, but it's still nicer than it was. Anyway,
changes:

 - Flatten everything. For instance, there are now three instances of
   BlockComputer, two BlockTurtle, ItemPocketComputer. There's also no
   more BlockPeripheral (thank heavens) - there's separate block classes
   for each peripheral type.

 - Remove pretty much all legacy code. As we're breaking world
   compatibility anyway, we can remove all the code to load worlds from
   1.4 days.
 - The command system is largely rewriten to take advantage of 1.13's
   new system. It's very fancy!

 - WidgetTerminal now uses Minecraft's "GUI listener" system.

 - BREAKING CHANGE: All the codes in keys.lua are different, due to the
   move to LWJGL 3. Hopefully this won't have too much of an impact.

   I don't want to map to the old key codes on the Java side, as there
   always ends up being small but slight inconsistencies. IMO it's
   better to make a clean break - people should be using keys rather
   than hard coding the constants anyway.

 - commands.list now allows fetching sub-commands. The ROM has already
   been updated to allow fancy usage such as commands.time.set("noon").

 - Turtles, modems and cables can be waterlogged.
2019-04-02 20:59:48 +01:00

152 lines
5.7 KiB
Java

/*
* This file is part of ComputerCraft - http://www.computercraft.info
* Copyright Daniel Ratcliffe, 2011-2019. 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.ILuaContext;
import dan200.computercraft.api.lua.LuaException;
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.EntityPlayer;
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.util.NonNullList;
import net.minecraftforge.items.wrapper.PlayerMainInvWrapper;
import javax.annotation.Nonnull;
public class PocketAPI implements ILuaAPI
{
private final PocketServerComputer m_computer;
public PocketAPI( PocketServerComputer computer )
{
m_computer = computer;
}
@Override
public String[] getNames()
{
return new String[] {
"pocket"
};
}
@Nonnull
@Override
public String[] getMethodNames()
{
return new String[] {
"equipBack",
"unequipBack"
};
}
@Override
public Object[] callMethod( @Nonnull ILuaContext context, int method, @Nonnull Object[] arguments ) throws LuaException, InterruptedException
{
switch( method )
{
case 0:
// equipBack
return context.executeMainThreadTask( () ->
{
Entity entity = m_computer.getEntity();
if( !(entity instanceof EntityPlayer) ) return new Object[] { false, "Cannot find player" };
EntityPlayer player = (EntityPlayer) entity;
InventoryPlayer inventory = player.inventory;
IPocketUpgrade previousUpgrade = m_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.posX, player.posY, player.posZ );
}
}
}
// Set the new upgrade
m_computer.setUpgrade( newUpgrade );
return new Object[] { true };
} );
case 1:
// unequipBack
return context.executeMainThreadTask( () ->
{
Entity entity = m_computer.getEntity();
if( !(entity instanceof EntityPlayer) ) return new Object[] { false, "Cannot find player" };
EntityPlayer player = (EntityPlayer) entity;
InventoryPlayer inventory = player.inventory;
IPocketUpgrade previousUpgrade = m_computer.getUpgrade();
if( previousUpgrade == null ) return new Object[] { false, "Nothing to unequip" };
m_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.posX, player.posY, player.posZ );
}
}
return new Object[] { true };
} );
default:
return null;
}
}
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;
}
}