This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Kilobyte's profile picture

[API][Prob all CC versions] Peripheral Easymess layer - Write your peripherals much faster and more efficient

Started by Kilobyte, 03 April 2013 - 10:22 AM
Kilobyte #1
Posted 03 April 2013 - 12:22 PM
First of all: i am not sure if this topic fits here. Its an additional abstraction layer for CC API and therefore directly related to Peripherals. on the other hand its no actual peripheral. Please move this topic if it doesn't fit here
——————————————————-
When rewriting kilobytesPeripherals i was making a LuaJ like value api and some reflection magic to make coding easier. And now i thought, hey, lets share it. Its not 100% stable yet, but about 98%. Here an example peripheral:


import dan200.computer.api.IComputerAccess;
import de.kilobyte22.lib.ccvalueapi.tileentity.PeripheralTileEntity;
import de.kilobyte22.lib.ccvalueapi.reflection.PeripheralFunction;
import de.kilobyte22.lib.ccvalueapi.LuaError;
import de.kilobyte22.lib.ccvalueapi.values.Varargs;
public class TileEntityMyPeripheral extends PeripheralTileEntity {
	@Override
	public String getType() {
		return "myPeripheral";
	}
	@PeripheralFunction("myFirstFunction") // The name this function will be called with ingame
	public Varargs function1(IComputerAccess computer, Varargs args) {
		// The function must have the param types IComputerAccess, Varargs
		// and has to return Varargs
		//What it will do: accept 2 strings and combine them (basicly string1..string2 in lua)
		return new Varargs(args.checkstring(1) + args.checkstring(2));
	}
	@PeripheralFunction("getHealth")
	public Varargs iwantzhealth(IComputerAccess computer, Varargs args) {
		// returns the players health or an error if player is not in current world
		EntityPlayer entityPlayer = worldObj.getPlayerEntityByName(args.checkstring(1));
		if (entityPlayer == null)
			throw new LuaError("Player offline or in different world"); // This will arrive in Lua as if you used error()
		return new Varargs(entityPlayer.getHealth());
	}
	@PeripheralFunction("broadcastAttached")
	public Varargs broadcastAttached(IComputerAccess computer, Varargs args) {
		// broadcast the event. INFO: it will always pass the side the peripheral is attached to as first param
		broadcastEvent("event_broadcast", args); // broadcastEvent() actually has 4 overloadings, check them out in the source
		return new Varargs();
	}
}

please note, that the includes aren't all right :P/> but otherwise it should work (yes, its an untested example, but its mostly for telling you how it works anyways).

Repo Link: https://bitbucket.or...c-value-api/src
Download Link: https://bitbucket.or...e-api/downloads -> 'Branches' tab -> 'master'

Note: There are no binary distributions. this API is intended to be included in your project. just drop the contents of the src folder on the repo into src/minecraft in MCP

Happy hacking and greets, Kilobyte
TehSomeLuigi #2
Posted 04 April 2013 - 01:44 AM
Looks cool, I MAY use this, though I might not since the CC API isn't too complex, though this does make the code more readable, but at the same time it does mean depending on something else. I'll look closer at it later, it seems like it has good value handling which is something I'm interested in.
Kilobyte #3
Posted 04 April 2013 - 05:51 AM
Looks cool, I MAY use this, though I might not since the CC API isn't too complex, though this does make the code more readable, but at the same time it does mean depending on something else. I'll look closer at it later, it seems like it has good value handling which is something I'm interested in.

Not really a dependency… its more intended that you ship it with your mod just as binary (put in same zip as your mod) ;)/>

EDIT: ouch.. had some stupid stuff in there :/ lemme fix
EDIT2: Fixed
Xfel #4
Posted 05 April 2013 - 05:55 AM
That looks nice! I'm actually using something quite alike in arp (though I don'T find the time to update the mod, that part works fine). I wouldn't bind it to the tile entity though, it's much better if you create the peripherals seperately (also eases the creation of turtle upgrades).
Kilobyte #5
Posted 05 April 2013 - 06:10 AM
<snip>
I wouldn't bind it to the tile entity though, it's much better if you create the peripherals seperately (also eases the creation of turtle upgrades).

Yeah, Actually a very good idea :P/> Got some nice ideas on that already :P/>

EDIT: Peripherals should work now in my local copy, gonna commit to dev branch now. Turtle upgrades Pending.
Please note: All usages of dan22.computer,api.IComputerAccess are replaced by de.kilobyte22,lib.ccvalueapi.IComputerAccess which also works as ITurtleAccess.some turtle only stuff actually works with computers, ie. getWorld() (returns the world, the Peripheral is located it, Not computer though).
It also adds new Methods like isTurtle() or isComputer()

also, you need to move the Functions to a class that extends Peripheral. Your TileEntity also needs to override createPeripheral() and it needs to return a Peripheral. The Peripheral class is a generic btw. i recommed making it like this:

public class MyPeripheral extends Peripheral<MyTileEntity> {}
where MyTileEntity is the TileEntity Class