dan200.computercraft.api.peripheral.IComputerAccess Java Examples

The following examples show how to use dan200.computercraft.api.peripheral.IComputerAccess. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ComputerCraftEnv.java    From OpenPeripheral with MIT License 5 votes vote down vote up
public IMethodCall addPeripheralArgs(IMethodCall call, IComputerAccess access, ILuaContext context) {
	final CCArchitectureAccess wrapper = new CCArchitectureAccess(access, converter);
	return addCommonArgs(call, context)
			.setEnv(Constants.ARG_ARCHITECTURE, wrapper)
			.setEnv(Constants.ARG_ACCESS, wrapper)
			.setEnv(Constants.ARG_COMPUTER, access);
}
 
Example #2
Source File: TileEntityTurbineComputerPort.java    From BigReactors with MIT License 5 votes vote down vote up
@Override
@Optional.Method(modid = "ComputerCraft")
public Object[] callMethod(IComputerAccess computer, ILuaContext context, int method, Object[] arguments) {
       try {
           return callMethod(method, arguments);
       } catch(Exception e) {
       	BRLog.info("Exception encountered when invoking computercraft method: %s", e.getMessage());
           e.printStackTrace();
       }
       return null;
   }
 
Example #3
Source File: TileEntityReactorComputerPort.java    From BigReactors with MIT License 5 votes vote down vote up
@Override
@Optional.Method(modid = "ComputerCraft")
public Object[] callMethod(IComputerAccess computer, ILuaContext context, int method, Object[] arguments) throws LuaException {
       try {
           return callMethod(method, arguments);
       } catch(Exception e) {
       	// Rethrow errors as LuaExceptions for CC
       	throw new LuaException(e.getMessage());
       }
   }
 
Example #4
Source File: ModuleComputerCraft.java    From OpenPeripheral with MIT License 5 votes vote down vote up
public static void init() {
	final MethodSelector peripheralSelector = new MethodSelector(Constants.ARCH_COMPUTER_CRAFT)
			.allowReturnSignal()
			.addDefaultEnv()
			.addProvidedEnv(Constants.ARG_ACCESS, IArchitectureAccess.class)
			.addProvidedEnv(Constants.ARG_COMPUTER, IComputerAccess.class)
			.addProvidedEnv(Constants.ARG_CONTEXT, ILuaContext.class);

	PERIPHERAL_METHODS_FACTORY = new ComposedMethodsFactory<IndexedMethodMap>(AdapterRegistry.PERIPHERAL_ADAPTERS, peripheralSelector) {
		@Override
		protected IndexedMethodMap wrapMethods(Class<?> targetCls, Map<String, IMethodExecutor> methods) {
			return new IndexedMethodMap(methods);
		}
	};

	// can't push events, so not allowing return signals
	final MethodSelector objectSelector = new MethodSelector(Constants.ARCH_COMPUTER_CRAFT)
			.addDefaultEnv()
			.addProvidedEnv(Constants.ARG_CONTEXT, ILuaContext.class);

	OBJECT_METHODS_FACTORY = new ComposedMethodsFactory<IndexedMethodMap>(AdapterRegistry.OBJECT_ADAPTERS, objectSelector) {
		@Override
		protected IndexedMethodMap wrapMethods(Class<?> targetCls, Map<String, IMethodExecutor> methods) {
			return new IndexedMethodMap(methods);
		}
	};

	CommandDump.addArchSerializer("ComputerCraft", "peripheral", DocBuilder.TILE_ENTITY_DECORATOR, PERIPHERAL_METHODS_FACTORY);
	CommandDump.addArchSerializer("ComputerCraft", "object", DocBuilder.SCRIPT_OBJECT_DECORATOR, OBJECT_METHODS_FACTORY);

	final IConverter converter = new TypeConversionRegistryCC();
	// CC converter is default one (legacy behaviour)
	TypeConvertersProvider.INSTANCE.registerConverter(Constants.ARCH_COMPUTER_CRAFT, converter);

	TypeClassifier.INSTANCE.registerType(ILuaObject.class, SingleArgType.OBJECT);

	ENV = new ComputerCraftEnv(converter);
}
 
Example #5
Source File: TileEntityUniversalSensor.java    From PneumaticCraft with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Called on a event sensor
 * @param newRedstoneStrength
 */
@Optional.Method(modid = ModIds.COMPUTERCRAFT)
private void notifyComputers(Object... arguments){
    for(IComputerAccess computer : attachedComputers) {
        computer.queueEvent(getType(), arguments);
    }
}
 
Example #6
Source File: AdapterPeripheral.java    From OpenPeripheral with MIT License 5 votes vote down vote up
@Override
public void attach(IComputerAccess computer) {
	computer.mount(MOUNT_NAME, AdapterPeripheral.MOUNT);
	computer.mount("rom/help/" + computer.getAttachmentName(), docMount);
	if (target instanceof IAttachable) {
		IArchitectureAccess access = accessCache.getOrCreate(computer);
		((IAttachable)target).addComputer(access);
	}
	if (target instanceof IComputerCraftAttachable) ((IComputerCraftAttachable)target).addComputer(computer);
}
 
Example #7
Source File: TileEntityBase.java    From PneumaticCraft with GNU General Public License v3.0 5 votes vote down vote up
@Override
@Optional.Method(modid = ModIds.COMPUTERCRAFT)
public Object[] callMethod(IComputerAccess computer, ILuaContext context, int method, Object[] arguments) throws LuaException, InterruptedException{
    try {
        return luaMethods.get(method).call(arguments);
    } catch(Exception e) {
        throw new LuaException(e.getMessage());
    }
}
 
Example #8
Source File: TileEntityDroneInterface.java    From PneumaticCraft with GNU General Public License v3.0 5 votes vote down vote up
private void sendEvent(String name, Object... parms){
    if(Loader.isModLoaded(ModIds.COMPUTERCRAFT)) {
        for(IComputerAccess computer : attachedComputers) {
            computer.queueEvent(name, parms);
        }
    }
}
 
Example #9
Source File: AdapterPeripheral.java    From OpenPeripheral with MIT License 5 votes vote down vote up
@Override
public void detach(IComputerAccess computer) {
	if (target instanceof IAttachable) {
		IArchitectureAccess access = accessCache.remove(computer);
		if (access != null) ((IAttachable)target).removeComputer(access);
	}

	if (target instanceof IComputerCraftAttachable) ((IComputerCraftAttachable)target).removeComputer(computer);
}
 
Example #10
Source File: TileEntityDroneInterface.java    From PneumaticCraft with GNU General Public License v3.0 5 votes vote down vote up
@Override
@Optional.Method(modid = ModIds.COMPUTERCRAFT)
public Object[] callMethod(IComputerAccess computer, ILuaContext context, int method, Object[] arguments) throws LuaException{
    try {
        return luaMethods.get(method).call(arguments);
    } catch(Exception e) {
        throw new LuaException(e.getMessage());
    }
}
 
Example #11
Source File: AdapterPeripheral.java    From OpenPeripheral with MIT License 4 votes vote down vote up
@Override
public Object[] callMethod(final IComputerAccess computer, final ILuaContext context, final int index, final Object[] arguments) throws LuaException, InterruptedException {
	// this should throw if peripheral isn't attached
	computer.getAttachmentName();

	final IMethodExecutor method = methods.getMethod(index);
	Preconditions.checkNotNull(method, "Invalid method index: %d", index);

	final IMethodCall preparedCall = prepareCall(method, computer, context);

	final Optional<String> returnSignal = method.getReturnSignal();
	if (returnSignal.isPresent()) {
		final int callbackId = SignallingGlobals.instance.nextCallbackId();
		final String returnSignalId = returnSignal.get();
		if (method.isAsynchronous()) {
			SignallingGlobals.instance.scheduleTask(new Runnable() {
				@Override
				public void run() {
					computer.queueEvent(returnSignalId, executeToSignal(callbackId, index, preparedCall, arguments));
				}
			});
		} else {
			context.issueMainThreadTask(new ILuaTask() {
				@Override
				public Object[] execute() {
					computer.queueEvent(returnSignalId, executeToSignal(callbackId, index, preparedCall, arguments));
					// this will be used as 'task_complete' result, so we will ignore it
					return NULL;
				}
			});
		}
		return new Object[] { callbackId };
	} else {
		if (method.isAsynchronous()) return executeCall(preparedCall, index, arguments);
		else {
			Object[] results = SynchronousExecutor.executeInMainThread(context, new SynchronousExecutor.Task() {
				@Override
				public Object[] execute() throws LuaException, InterruptedException {
					return executeCall(preparedCall, index, arguments);
				}
			});
			return results;
		}
	}
}
 
Example #12
Source File: AdapterPeripheral.java    From OpenPeripheral with MIT License 4 votes vote down vote up
private IMethodCall prepareCall(IMethodExecutor executor, IComputerAccess computer, ILuaContext context) {
	final IMethodCall call = executor.startCall(target);
	return ModuleComputerCraft.ENV.addPeripheralArgs(call, computer, context);
}
 
Example #13
Source File: AdapterPeripheral.java    From OpenPeripheral with MIT License 4 votes vote down vote up
@Override
protected IArchitectureAccess create(IComputerAccess computer) {
	return ModuleComputerCraft.ENV.createAccess(computer);
}
 
Example #14
Source File: ComputerCraftEnv.java    From OpenPeripheral with MIT License 4 votes vote down vote up
public CCArchitectureAccess(IComputerAccess access, IConverter converter) {
	super(converter);
	this.access = access;
}
 
Example #15
Source File: SafePeripheralFactory.java    From OpenPeripheral with MIT License 4 votes vote down vote up
@Override
public void attach(IComputerAccess computer) {}
 
Example #16
Source File: SafePeripheralFactory.java    From OpenPeripheral with MIT License 4 votes vote down vote up
@Override
public Object[] callMethod(IComputerAccess computer, ILuaContext context, int method, Object[] arguments) {
	return ArrayUtils.toArray("This peripheral is broken. You can show your log in #OpenMods");
}
 
Example #17
Source File: ComputerCraftEnv.java    From OpenPeripheral with MIT License 4 votes vote down vote up
public IArchitectureAccess createAccess(final IComputerAccess access) {
	return new CCArchitectureAccess(access, converter);
}
 
Example #18
Source File: SafePeripheralFactory.java    From OpenPeripheral with MIT License 4 votes vote down vote up
@Override
public void detach(IComputerAccess computer) {}
 
Example #19
Source File: TileEntityTurbineComputerPort.java    From BigReactors with MIT License 4 votes vote down vote up
@Override
@Optional.Method(modid = "ComputerCraft")
public void detach(IComputerAccess computer) {
}
 
Example #20
Source File: TileEntityReactorComputerPort.java    From BigReactors with MIT License 4 votes vote down vote up
@Override
@Optional.Method(modid = "ComputerCraft")
public void detach(IComputerAccess computer) {
}
 
Example #21
Source File: TileEntityTurbineComputerPort.java    From BigReactors with MIT License 4 votes vote down vote up
@Override
@Optional.Method(modid = "ComputerCraft")
public void attach(IComputerAccess computer) {
}
 
Example #22
Source File: PeripheralMotor.java    From Framez with GNU General Public License v3.0 4 votes vote down vote up
@Override
public Object[] callMethod(IComputerAccess computer, ILuaContext context, int method, Object[] arguments) throws LuaException,
InterruptedException {

    if (method == 0) {
        if (arguments.length == 0)
            throw new LuaException("At least 1 argument is required (face)");
        return new Object[] { te.setFace(toFD(arguments[0])) };
    } else if (method == 1) {
        return new Object[] { te.getFace().ordinal() };
    } else if (method == 2) {
        if (!(te.getMovement() instanceof MovementSlide))
            throw new LuaException("This is not a slider motor!");
        if (arguments.length == 0)
            throw new LuaException("At least 1 argument is required (direction)");
        ((IMovementSlide) te.getMovement()).setDirection(toFD(arguments[0]));
        return new Object[] {};
    } else if (method == 3) {
        if (!(te.getMovement() instanceof MovementSlide))
            throw new LuaException("This is not a slider motor!");
        return new Object[] { ((IMovementSlide) te.getMovement()).getDirection().ordinal() };
    } else if (method == 4) {
        throw new LuaException("Not implemented yet, sorry D:");
        // if (arguments.length < 2)
        // throw new LuaException("At least 2 arguments are required (face, direction)");
        //
        // ForgeDirection face = toFD(arguments[0]);
        // ForgeDirection direction = toFD(arguments[1]);
        //
        // if (face == null || direction == null)
        // throw new LuaException("Invalid directions!");
        // if (face == direction || face == direction.getOpposite())
        // throw new LuaException("Motors cannot push or pull blocks!");
        //
        // te.setFace(face, true);
        // te.setDirection(direction, true);
        //
        // return new Object[] { true };
    } else if (method == 5) {
        return new Object[] { te.move() };
    }

    return null;
}
 
Example #23
Source File: TileEntityReactorComputerPort.java    From BigReactors with MIT License 4 votes vote down vote up
@Override
@Optional.Method(modid = "ComputerCraft")
public void attach(IComputerAccess computer) {
}
 
Example #24
Source File: WrappedPeripheral.java    From OpenPeripheral-Addons with MIT License 4 votes vote down vote up
@Override
public void detach(IComputerAccess computer) {
	if (peripheral != null) peripheral.detach(computer);
}
 
Example #25
Source File: WrappedPeripheral.java    From OpenPeripheral-Addons with MIT License 4 votes vote down vote up
@Override
public void attach(IComputerAccess computer) {
	if (peripheral != null) peripheral.attach(computer);
}
 
Example #26
Source File: WrappedPeripheral.java    From OpenPeripheral-Addons with MIT License 4 votes vote down vote up
@Override
public Object[] callMethod(IComputerAccess computer, ILuaContext context, int method, Object[] arguments) throws LuaException, InterruptedException {
	return (peripheral != null)? peripheral.callMethod(computer, context, method, arguments) : null;
}
 
Example #27
Source File: TileEntityUniversalSensor.java    From PneumaticCraft with GNU General Public License v3.0 4 votes vote down vote up
@Override
@Optional.Method(modid = ModIds.COMPUTERCRAFT)
public void detach(IComputerAccess computer){
    attachedComputers.remove(computer);
}
 
Example #28
Source File: TileEntityUniversalSensor.java    From PneumaticCraft with GNU General Public License v3.0 4 votes vote down vote up
@Override
@Optional.Method(modid = ModIds.COMPUTERCRAFT)
public void attach(IComputerAccess computer){
    attachedComputers.add(computer);
}
 
Example #29
Source File: TileEntityBase.java    From PneumaticCraft with GNU General Public License v3.0 4 votes vote down vote up
@Override
@Optional.Method(modid = ModIds.COMPUTERCRAFT)
public void detach(IComputerAccess computer){}
 
Example #30
Source File: TileEntityBase.java    From PneumaticCraft with GNU General Public License v3.0 4 votes vote down vote up
@Override
@Optional.Method(modid = ModIds.COMPUTERCRAFT)
public void attach(IComputerAccess computer){}