CC-Tweaked/src/main/java/dan200/computercraft/shared/peripheral/commandblock/CommandBlockPeripheral.java

93 lines
2.8 KiB
Java
Raw Normal View History

/*
* This file is part of ComputerCraft - http://www.computercraft.info
2019-01-01 01:10:18 +00:00
* Copyright Daniel Ratcliffe, 2011-2019. Do not distribute without permission.
* Send enquiries to dratcliffe@gmail.com
*/
package dan200.computercraft.shared.peripheral.commandblock;
import dan200.computercraft.api.lua.ILuaContext;
import dan200.computercraft.api.lua.LuaException;
import dan200.computercraft.api.peripheral.IComputerAccess;
import dan200.computercraft.api.peripheral.IPeripheral;
import net.minecraft.tileentity.TileEntityCommandBlock;
2017-05-06 23:07:42 +00:00
import javax.annotation.Nonnull;
import static dan200.computercraft.core.apis.ArgumentHelper.getString;
public class CommandBlockPeripheral implements IPeripheral
{
2017-05-01 14:48:44 +00:00
private final TileEntityCommandBlock m_commandBlock;
2017-05-01 14:48:44 +00:00
public CommandBlockPeripheral( TileEntityCommandBlock commandBlock )
{
m_commandBlock = commandBlock;
}
2017-05-01 14:48:44 +00:00
// IPeripheral methods
2017-05-06 23:07:42 +00:00
@Nonnull
2017-05-01 14:48:44 +00:00
@Override
public String getType()
{
return "command";
}
2017-05-06 23:07:42 +00:00
@Nonnull
2017-05-01 14:48:44 +00:00
@Override
public String[] getMethodNames()
{
return new String[] {
"getCommand",
"setCommand",
"runCommand",
};
}
2017-05-01 14:48:44 +00:00
@Override
2017-05-06 23:07:42 +00:00
public Object[] callMethod( @Nonnull IComputerAccess computer, @Nonnull ILuaContext context, int method, @Nonnull final Object[] arguments ) throws LuaException, InterruptedException
2017-05-01 14:48:44 +00:00
{
switch( method )
2017-05-01 14:48:44 +00:00
{
case 0: // getCommand
return context.executeMainThreadTask( () -> new Object[] {
m_commandBlock.getCommandBlockLogic().getCommand(),
} );
2017-05-01 14:48:44 +00:00
case 1:
{
// setCommand
final String command = getString( arguments, 0 );
context.issueMainThreadTask( () ->
{
m_commandBlock.getCommandBlockLogic().setCommand( command );
m_commandBlock.getCommandBlockLogic().updateCommand();
return null;
} );
return null;
2017-05-01 14:48:44 +00:00
}
case 2: // runCommand
return context.executeMainThreadTask( () ->
{
m_commandBlock.getCommandBlockLogic().trigger( m_commandBlock.getWorld() );
int result = m_commandBlock.getCommandBlockLogic().getSuccessCount();
if( result > 0 )
{
return new Object[] { true };
}
else
{
return new Object[] { false, "Command failed" };
}
} );
2017-05-01 14:48:44 +00:00
}
return null;
}
@Override
public boolean equals( IPeripheral other )
{
return other != null && other.getClass() == getClass();
}
}