1
0
mirror of https://github.com/SquidDev-CC/CC-Tweaked synced 2025-07-15 00:12:53 +00:00
SquidDev 9f8774960f Generate documentation stubs from Javadocs
illuaminate does not handle Java files, for obvious reasons. In order to
get around that, we have a series of stub files within /doc/stub which
mirrored the Java ones. While this works, it has a few problems:

 - The link to source code does not work - it just links to the stub
   file.
 - There's no guarantee that documentation remains consistent with the
   Java code. This change found several methods which were incorrectly
   documented beforehand.

We now replace this with a custom Java doclet[1], which extracts doc
comments from @LuaFunction annotated methods and generates stub-files
from them. These also contain a @source annotation, which allows us to
correctly link them back to the original Java code.

There's some issues with this which have yet to be fixed. However, I
don't think any of them are major blockers right now:

 - The custom doclet relies on Java 9 - I think it's /technically/
   possible to do this on Java 8, but the API is significantly uglier.
   This means that we need to run javadoc on a separate JVM.

   This is possible, and it works locally and on CI, but is definitely
   not a nice approach.

 - illuaminate now requires the doc stubs to be generated in order for
   the linter to pass, which does make running the linter locally much
   harder (especially given the above bullet point).

   We could notionally include the generated stubs (or at least a cut
   down version of them) in the repo, but I'm not 100% sure about that.

[1]: https://docs.oracle.com/javase/9/docs/api/jdk/javadoc/doclet/package-summary.html
2020-07-03 13:31:26 +01:00

157 lines
5.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;
/**
* Control the current pocket computer, adding or removing upgrades.
*
* This API is only available on pocket computers. As such, you may use its presence to determine what kind of computer
* you are using:
*
* <pre>
* if pocket then
* print("On a pocket computer")
* else
* print("On something else")
* end
* </pre>
*
* @cc.module pocket
*/
public class PocketAPI implements ILuaAPI
{
private final PocketServerComputer computer;
public PocketAPI( PocketServerComputer computer )
{
this.computer = computer;
}
@Override
public String[] getNames()
{
return new String[] { "pocket" };
}
/**
* Search the player's inventory for another upgrade, replacing the existing one with that item if found.
*
* This inventory search starts from the player's currently selected slot, allowing you to prioritise upgrades.
*
* @return The result of equipping.
* @cc.treturn boolean If an item was equipped.
* @cc.treturn string|nil The reason an item was not equipped.
*/
@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 };
}
/**
* Remove the pocket computer's current upgrade.
*
* @return The result of unequipping.
* @cc.treturn boolean If the upgrade was unequipped.
* @cc.treturn string|nil The reason an upgrade was not unequipped.
*/
@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;
}
}