openperipheral.api.architecture.IArchitectureAccess Java Examples

The following examples show how to use openperipheral.api.architecture.IArchitectureAccess. 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: TileEntityGlassesBridge.java    From OpenPeripheral-Addons with MIT License 6 votes vote down vote up
private void queueEvent(String event, EntityPlayer user, IEventArgsSource source) {
	final GameProfile gameProfile = user.getGameProfile();
	final UUID userId = gameProfile.getId();
	final String idString = userId != null? userId.toString() : null;
	final String userName = gameProfile.getName();

	for (IArchitectureAccess computer : computers) {
		final Object[] extra = source.getArgs(computer);
		final Object[] args = new Object[3 + extra.length];
		System.arraycopy(extra, 0, args, 3, extra.length);
		args[0] = computer.peripheralName();
		args[1] = userName;
		args[2] = idString;

		computer.signal(event, args);
	}
}
 
Example #2
Source File: CraftingCallback.java    From OpenPeripheral-Integration with MIT License 5 votes vote down vote up
public CraftingCallback(IArchitectureAccess access, IConverter converter, ICraftingGrid craftingGrid, IMEMonitor<IAEItemStack> monitor, IActionHost actionHost, ICraftingCPU wantedCpu, IAEItemStack requestedStack) {
	this.access = access;
	this.converter = converter;
	this.monitor = monitor;
	this.source = new MachineSource(actionHost);
	this.actionHost = actionHost;
	this.craftingGrid = craftingGrid;
	this.wantedCpu = wantedCpu;
	this.requestedStack = converter.fromJava(requestedStack);
}
 
Example #3
Source File: EnvironmentGeneratorTest.java    From OpenPeripheral with MIT License 5 votes vote down vote up
@Test
public void testCommonConnectivity() throws Exception {
	final Node node = setupOpenComputersApiMock();

	ICodeGenerator generator = new PeripheralCodeGenerator();

	Map<String, IMethodExecutor> methods = Maps.newHashMap();
	Class<? extends ManagedEnvironment> cls = generateClass("TestClass\u2658", CommonAwareTargetClass.class, ImmutableSet.<Class<?>> of(), methods, generator);

	final CommonAwareTargetClass target = mock(CommonAwareTargetClass.class);
	ManagedEnvironment o = cls.getConstructor(CommonAwareTargetClass.class).newInstance(target);

	ContextEnvironment environment = mock(ContextEnvironment.class);
	final String nodeAddress = "node_13";
	when(node.address()).thenReturn(nodeAddress);
	when(node.host()).thenReturn(environment);
	when(environment.node()).thenReturn(node);

	final OpenComputersEnv env = mock(OpenComputersEnv.class);
	final IArchitectureAccess access = mock(IArchitectureAccess.class);
	when(env.createAccess(node, environment)).thenReturn(access);
	ModuleOpenComputers.ENV = env;

	o.onConnect(node);
	verify(target).addComputer(access);

	o.onDisconnect(node);
	verify(target).removeComputer(access);
}
 
Example #4
Source File: EnvironmentGeneratorTest.java    From OpenPeripheral with MIT License 5 votes vote down vote up
@Test
public void testConnectivity() throws Exception {
	final Node node = setupOpenComputersApiMock();

	ICodeGenerator generator = new PeripheralCodeGenerator();

	Map<String, IMethodExecutor> methods = Maps.newHashMap();
	Class<? extends ManagedEnvironment> cls = generateClass("TestClass\u2653", AwareTargetClass.class, ImmutableSet.<Class<?>> of(), methods, generator);

	final AwareTargetClass target = mock(AwareTargetClass.class);
	ManagedEnvironment o = cls.getConstructor(AwareTargetClass.class).newInstance(target);

	ContextEnvironment environment = mock(ContextEnvironment.class);
	final String nodeAddress = "node_11";
	when(node.address()).thenReturn(nodeAddress);
	when(node.host()).thenReturn(environment);
	when(environment.node()).thenReturn(node);

	final OpenComputersEnv env = mock(OpenComputersEnv.class);
	final IArchitectureAccess access = mock(IArchitectureAccess.class);
	when(env.createAccess(node, environment)).thenReturn(access);
	ModuleOpenComputers.ENV = env;

	o.onConnect(node);
	verify(target).addComputer(access);
	verifyOcSpecificConnectCall(target, node);

	o.onDisconnect(node);
	verify(target).removeComputer(access);
	verifyOcSpecificDisconnectCall(target, node);
}
 
Example #5
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 #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: 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 #8
Source File: PeripheralEnvironmentBase.java    From OpenPeripheral with MIT License 5 votes vote down vote up
protected void onDisconnect(IAttachable target, Node node) {
	final Environment host = node.host();
	if (host instanceof Context) {
		IArchitectureAccess access = accessCache.remove((Context)host);
		if (access != null) target.removeComputer(access);
	}
}
 
Example #9
Source File: PeripheralEnvironmentBase.java    From OpenPeripheral with MIT License 5 votes vote down vote up
protected void onConnect(IAttachable target, Node node) {
	final Environment host = node.host();
	if (host instanceof Context) {
		IArchitectureAccess access = accessCache.getOrCreate((Context)host);
		target.addComputer(access);
	}
}
 
Example #10
Source File: ModuleOpenComputers.java    From OpenPeripheral with MIT License 5 votes vote down vote up
public static void init() {
	final MethodSelector peripheralSelector = new MethodSelector(Constants.ARCH_OPEN_COMPUTERS)
			.allowReturnSignal()
			.addDefaultEnv()
			.addProvidedEnv(Constants.ARG_ACCESS, IArchitectureAccess.class)
			.addProvidedEnv(Constants.ARG_CONTEXT, Context.class)
			.addProvidedEnv(Constants.ARG_NODE, Node.class);

	PERIPHERAL_METHODS_FACTORY = new EnvironmentMethodsFactory<ManagedEnvironment>(
			AdapterRegistry.PERIPHERAL_ADAPTERS,
			peripheralSelector,
			PERIPHERAL_CLASS_PREFIX,
			new PeripheralCodeGenerator());

	InjectedClassesManager.instance.registerProvider(PERIPHERAL_CLASS_PREFIX, new EnvironmentClassBytesProvider<ManagedEnvironment>(PERIPHERAL_METHODS_FACTORY));

	final MethodSelector objectSelector = new MethodSelector(Constants.ARCH_OPEN_COMPUTERS)
			// .allowReturnSignal() // for symmetry with CC
			.addDefaultEnv()
			.addProvidedEnv(Constants.ARG_CONTEXT, Context.class);

	OBJECT_METHODS_FACTORY = new EnvironmentMethodsFactory<Value>(
			AdapterRegistry.OBJECT_ADAPTERS,
			objectSelector,
			OBJECT_CLASS_PREFIX,
			new ObjectCodeGenerator());

	InjectedClassesManager.instance.registerProvider(OBJECT_CLASS_PREFIX, new EnvironmentClassBytesProvider<Value>(OBJECT_METHODS_FACTORY));

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

	final IConverter converter = new TypeConversionRegistryOC();
	TypeConvertersProvider.INSTANCE.registerConverter(Constants.ARCH_OPEN_COMPUTERS, converter);

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

	ENV = new OpenComputersEnv(converter);
}
 
Example #11
Source File: TileEntityPIM.java    From OpenPeripheral-Addons with MIT License 5 votes vote down vote up
private void fireEvent(String eventName, Object... args) {
	synchronized (computers) {
		for (IArchitectureAccess computer : computers) {
			Object[] extendedArgs = ArrayUtils.add(args, computer.peripheralName());
			computer.signal(eventName, extendedArgs);
		}
	}
}
 
Example #12
Source File: TileEntityGlassesBridge.java    From OpenPeripheral-Addons with MIT License 5 votes vote down vote up
public void handleUserEvent(final GlassesClientEvent evt) {
	queueEvent(evt.getEventName(), evt.sender, new IEventArgsSource() {
		@Override
		public Object[] getArgs(IArchitectureAccess access) {
			return evt.getEventArgs(access);
		}
	});
}
 
Example #13
Source File: TileEntityGlassesBridge.java    From OpenPeripheral-Addons with MIT License 5 votes vote down vote up
private void queueEvent(String event, EntityPlayer user, final Object... args) {
	queueEvent(event, user, new IEventArgsSource() {
		@Override
		public Object[] getArgs(IArchitectureAccess access) {
			return args;
		}
	});
}
 
Example #14
Source File: AdapterInterface.java    From OpenPeripheral-Integration with MIT License 5 votes vote down vote up
@ScriptCallable(description = "Requests the specified item to get crafted.")
public void requestCrafting(IActionHost host,
		@Env(Constants.ARG_ACCESS) IArchitectureAccess access,
		@Env(Constants.ARG_CONVERTER) IConverter converter,
		@Arg(name = "fingerprint", description = "Details of the item you want to craft. Can be found with .getStackInSlot on inventory and .getAvailableItems on AE network") ItemFingerprint needle,
		@Optionals @Arg(name = "qty", description = "The quantity of items you want to craft") Long quantity,
		@Arg(name = "cpu", description = "The name of the CPU you want to use") String wantedCpuName) {
	ICraftingGrid craftingGrid = getCraftingGrid(host);
	if (quantity == null) quantity = 1L;

	ICraftingCPU wantedCpu = findCpu(craftingGrid, wantedCpuName);

	IStorageGrid storageGrid = getStorageGrid(host);
	IMEMonitor<IAEItemStack> monitor = storageGrid.getItemInventory();

	IAEItemStack stack = findCraftableStack(storageGrid.getItemInventory().getStorageList(), needle);
	Preconditions.checkArgument(stack != null, "Can't find craftable item fingerprint %s", needle);

	final IAEItemStack toCraft = stack.copy();
	toCraft.setStackSize(quantity);

	// Create a new CraftingCallback. This callback is called when
	// the network finished calculating the required items. It can do two things for us:
	// a) It sends an event when items are missing to complete the request
	// b) Otherwise it starts the crafting job, which itself is a CraftingRequester OSsending more events to the computer.
	final CraftingCallback craftingCallback = new CraftingCallback(access, converter, craftingGrid, monitor, host, wantedCpu, toCraft);

	// We will need access to the worldObj of the ME Interface -> cast to TileEntity
	final TileEntity tileEntity = (TileEntity)host;

	// Tell the craftingGrid to begin calculating and to report everything to the CraftingCallback
	craftingGrid.beginCraftingJob(tileEntity.getWorldObj(), getGrid(host), new MachineSource(host), toCraft, craftingCallback);

}
 
Example #15
Source File: TileEntityPIM.java    From OpenPeripheral-Addons with MIT License 4 votes vote down vote up
@Override
public void addComputer(IArchitectureAccess computer) {
	synchronized (computers) {
		computers.add(computer);
	}
}
 
Example #16
Source File: TileEntityPIM.java    From OpenPeripheral-Addons with MIT License 4 votes vote down vote up
@Override
public synchronized void removeComputer(IArchitectureAccess computer) {
	synchronized (computers) {
		computers.remove(computer);
	}
}
 
Example #17
Source File: TileEntityGlassesBridge.java    From OpenPeripheral-Addons with MIT License 4 votes vote down vote up
@Override
public void removeComputer(IArchitectureAccess computer) {
	computers.remove(computer);
}
 
Example #18
Source File: TileEntityGlassesBridge.java    From OpenPeripheral-Addons with MIT License 4 votes vote down vote up
@Override
public void addComputer(IArchitectureAccess computer) {
	if (!computers.contains(computer)) {
		computers.add(computer);
	}
}
 
Example #19
Source File: OpenComputersEnv.java    From OpenPeripheral with MIT License 4 votes vote down vote up
public IArchitectureAccess createAccess(Node ownNode, Context context) {
	return new OCArchitectureAccess(ownNode, context, converter);
}
 
Example #20
Source File: PeripheralEnvironmentBase.java    From OpenPeripheral with MIT License 4 votes vote down vote up
@Override
protected IArchitectureAccess create(Context context) {
	return ModuleOpenComputers.ENV.createAccess(node(), context);
}
 
Example #21
Source File: TileEntitySelector.java    From OpenPeripheral-Addons with MIT License 4 votes vote down vote up
@Override
public void removeComputer(IArchitectureAccess computer) {
	computers.remove(computer);
}
 
Example #22
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 #23
Source File: TileEntitySelector.java    From OpenPeripheral-Addons with MIT License 4 votes vote down vote up
@Override
public void addComputer(IArchitectureAccess computer) {
	computers.add(computer);
}
 
Example #24
Source File: TileEntitySelector.java    From OpenPeripheral-Addons with MIT License 4 votes vote down vote up
private void signalSlotClick(int slot) {
	for (IArchitectureAccess computer : computers) {
		final Object[] args = new Object[] { computer.createIndex(slot), computer.peripheralName() };
		computer.signal("slot_click", args);
	}
}
 
Example #25
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 #26
Source File: CraftingRequester.java    From OpenPeripheral-Integration with MIT License 4 votes vote down vote up
public CraftingRequester(IActionHost original, IArchitectureAccess access, Object requestedStack) {
	this.original = original;
	this.access = access;
	this.requestedStack = requestedStack;
}
 
Example #27
Source File: TileEntityGlassesBridge.java    From OpenPeripheral-Addons with MIT License votes vote down vote up
public Object[] getArgs(IArchitectureAccess access);