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

Creating Peripherals and Turtle Upgrades (Updated for 1.5)

Started by dan200, 13 March 2012 - 12:47 PM
dan200 #1
Posted 13 March 2012 - 01:47 PM
The ComputerCraft Peripheral API is a way for minecraft mod authors to make blocks that can be interacted with by ComputerCraft computers and turtles, in the same manner as Disk Drives, Modems and Monitors, or to make Turtle upgrades that can be crafted into new types of Turtles.

The peripheral API can now be found in the "api" folder of the main ComputerCraft download

For peripherals, the download includes the source files for the two interfaces you need to interact with to make peripherals. All you need to do is create a new block with a TileEntity, and have the entity implement IPeripheral.

For turtle upgrades, create a class implementing ITurtleUpgrade, and call TurtleAPI.registerUpgrade() during your mod load() method. Turtle upgrades can either be new tools for your turtle (like the Pickaxe, Sword, etc), or new peripherals that permanently attach to the turtle (like the turtles' Wireless Modem or Crafting Table), and turtles with the upgrades can be created by crafting. In order to correctly store the types of turtle upgrade that each turtle has, each turtle upgrade designed must have a unique "UpgradeID", which is an integer in the range 0-255. When you release your upgrade, please use the following wiki page to choose an unused ID: http://www.computerc...tle_Upgrade_IDs

Both parts of the API include complete, thorough javadoc documentation. To make a mod, add these files to your MCP install, and implement the requisite interfaces. You do not need to and should not distribute the class files for these files with your mod. If all goes well, when you re-obfuscate your mod and install alongside ComputerCraft, your computers should be able to interact with your peripherals using the "peripheral" API, and turtles should be able to be crafted with your upgrades.

Feel free to post any questions about the API you have in this thread, but please read the documenation first, and be aware that this API is designed for users who already know how to produce mods for Minecraft, so we won't help you with basic mod setup.

Happy hacking!
rockymc #2
Posted 13 March 2012 - 04:02 PM
Just a question: how do we make the methods for the Peripheral in Lua? Because what I understood here was:

If all goes well, then when you re-obfuscate your mod and install alongside ComputerCraft, and place your block alongside a computer, and the computer will be able to access it via the "peripheral" API in Lua.

What I did understood was that we make the periph, and it's methods are already implemented in Lua.
Espen #3
Posted 13 March 2012 - 04:44 PM
Just a question: how do we make the methods for the Peripheral in Lua? Because what I understood here was:
If all goes well, then when you re-obfuscate your mod and install alongside ComputerCraft, and place your block alongside a computer, and the computer will be able to access it via the "peripheral" API in Lua.
What I did understood was that we make the periph, and it's methods are already implemented in Lua.
You program the methods within Java, of course. What these methods will do is up to you.
And then you just have to make sure to implement the two Interfaces correctly that Dan provided, so that the peripheral API can interact with your methods.
It's all in the documentation really.
rockymc #4
Posted 13 March 2012 - 05:58 PM
Just a question: how do we make the methods for the Peripheral in Lua? Because what I understood here was:
If all goes well, then when you re-obfuscate your mod and install alongside ComputerCraft, and place your block alongside a computer, and the computer will be able to access it via the "peripheral" API in Lua.
What I did understood was that we make the periph, and it's methods are already implemented in Lua.
You program the methods within Java, of course. What these methods will do is up to you.
And then you just have to make sure to implement the two Interfaces correctly that Dan provided, so that the peripheral API can interact with your methods.
It's all in the documentation really.

You really need to implement IComputerAccess?
dan200 #5
Posted 13 March 2012 - 08:31 PM
Just a question: how do we make the methods for the Peripheral in Lua? Because what I understood here was:
If all goes well, then when you re-obfuscate your mod and install alongside ComputerCraft, and place your block alongside a computer, and the computer will be able to access it via the "peripheral" API in Lua.
What I did understood was that we make the periph, and it's methods are already implemented in Lua.
You program the methods within Java, of course. What these methods will do is up to you.
And then you just have to make sure to implement the two Interfaces correctly that Dan provided, so that the peripheral API can interact with your methods.
It's all in the documentation really.

You really need to implement IComputerAccess?

You don't implement IComputerAccess. Read the javadocs.
oreillynz #6
Posted 16 March 2012 - 09:37 AM
Just in case the file structure changes in future, could an API function be added to get the file handler(s) for the contents of a disk?
(an example use case could be linking a machine module to a receiver, by having the module read the ID / location / whatever of it's parent from the disk. Doing so via an API future-proofs any changes to where files are stored outside the game)
Bunny83 #7
Posted 29 April 2012 - 12:46 AM
Just in case the file structure changes in future, could an API function be added to get the file handler(s) for the contents of a disk?
(an example use case could be linking a machine module to a receiver, by having the module read the ID / location / whatever of it's parent from the disk. Doing so via an API future-proofs any changes to where files are stored outside the game)

Something like that should be handled by your "device driver" (aka API) inside the computer / turtle. Your peripheral device can send any data with queueEvent into the computer. The computer can answer with a call of one of your api functions. For example if your peripheral wants it preferences to be loaded from disk (or from elsewhere) you could send an event from Java to the computer. Something like queueEvent("extModule_wantMyConfiguration")

The computer(your device driver) should react to this event by sending the required information. Inside the computer you can just use the file api to load the data from the virtual file system.

It works like in real computers. It would be terrible if your pc mouse could have direct access to your filesystem and delete some files (probably "cat.txt" :)/>/> ).

If your peripheral should work on it's own you can save your own data independently from the attached computer in any place you like. It doesn't have to be inside the "computer" folder. If the device wants to store something in the attached computer it should always go through the computer.
louitzie #8
Posted 21 May 2012 - 03:34 PM
is it possible to get a turtles inventory?
Xfel #9
Posted 21 May 2012 - 03:58 PM
Turtles implement the IInventory interface like any chest or furnace. At least, it is accessible to buildcraft and redpower item processors.
zwap1233 #10
Posted 21 July 2012 - 12:59 PM
whit "TileEntity" do you mean the basemod class or the block class?
xuma202 #11
Posted 23 July 2012 - 11:04 PM
For a SMP mod do I have to call queueEvent on all Clients or is this synchronized by Computer Craft already, guess so.
Cloudy #12
Posted 24 July 2012 - 11:00 AM
What you have to remember about ComputerCraft in SMP is that all the logic is handled server side. You only have to call QueueEvent on the server.
xuma202 #13
Posted 26 July 2012 - 12:52 PM
I'd like to suggest a change in the IPeripheral API. With Peripheral Cable (http://www.computerc...eral-cable-v02/) one computer can now be connected to a peripheral more than just one time. When now the computer detaches from the peripheral you have no chance to determine which connection was broken.

So please change the following functions:

attach(IComputerAccess computer, java.lang.String computerSide, int worldSide)

worldSide will be 0,1,2,3,4 or 5 depending on the side relative to the peripheral where the computer/cable got attched

detach(IComputerAccess computer, String computerSide)

computerSide will be the same as computerSide from attach to specify which connection was broken.

These changes would really be very helpful!

xuma
Cloudy #14
Posted 26 July 2012 - 03:37 PM
I see your point, however in vanilla usage it works fine - if anything, it should be Xfel who needs to handle this. Changing the interface would mean current peripherals would be incompatible with the future version. You could probably do it by counting how many times you attached, and only fully detach when the specified IComputerAccess has detached all instances.
xuma202 #15
Posted 26 July 2012 - 04:00 PM
Yes I'll ask Xfel for that. But getting the side where the Computer is placed has nothing to do with the peripheralcables. I would also apreciate having access to the Coords and the orientation of a Computer. And updates are needed anyways at least when porting the peripherals to CC 1.4 or later MC 1.3.
Cloudy #16
Posted 26 July 2012 - 05:45 PM
You can just store it when you connect to the peripheral. I'm not going to add something that will change the interface on an update which won't break compatibility with existing peripherals. Why would you need the side anyway on detach?

And getting your location is easy - you use the coords in your tile entity - should be easy to find out where the computer is from that location.
xuma202 #17
Posted 26 July 2012 - 07:22 PM
Ok That's right I know that the Computer can only change it's position if it's not at all attached to a peripheral.

And yes getting coordinates is also easy as long as the connection is not only done with Peripheral cables. But for this case Xfel is responsible.

So updating the API would maybe simplify the work of Peripheral Developers but is not needed. Thanks for your time I'll try going with what I have this should be possible.
xuma202 #18
Posted 30 July 2012 - 10:55 AM
Dan are you sure you've putten up the right link. When I download it it not contains anything related to TurtleUpgrades and the archive is called ComputerCraftAPI1.32.zip
dan200 #19
Posted 30 July 2012 - 01:13 PM
Dan are you sure you've putten up the right link. When I download it it not contains anything related to TurtleUpgrades and the archive is called ComputerCraftAPI1.32.zip

My bad. Fixed now.
djblocksaway #20
Posted 01 August 2012 - 05:36 AM
dan200 would you do the honor of joining my computercraft server o.o

that would be truly great if you can.

if you cant then that's ok :ph34r:/>/>
cmurtheepic #21
Posted 01 August 2012 - 07:44 PM
the computer should come with casper's (admin) tutorial program
Pharap #22
Posted 07 August 2012 - 12:22 AM
Are there any back copies of this for older versions? (I can't get 1.4 working properly)
brucelong #23
Posted 13 September 2012 - 03:58 PM
How would someone go about coding a peripheral? Is it like coding a mod, or is it more difficult? I'm a newb at this, and I wanted to make a laptop peripheral.
EDIT: Didn't read the ENtire first post… <facepalm>
EDIT#2: How could you implement the screen into a GUI? Like, the os.
EDIT#3: Nevermind, figured it out.
absorr #24
Posted 18 September 2012 - 09:44 PM
Can anyone give me an example on how to use getMethodNames() from IPeripheral because I don't want to mess that up.
immibis #25
Posted 19 September 2012 - 02:20 AM
return new String[] {"firstMethodName","method2Name","nameOfThirdMethod","nameOfFourthMethod"};
absorr #26
Posted 25 September 2012 - 06:09 PM
return new String[] {"firstMethodName","method2Name","nameOfThirdMethod","nameOfFourthMethod"};
so, for one of those method entries I would do something like "myTurtle.doThis" or what?
Tiin57 #27
Posted 15 October 2012 - 03:52 PM
Could this be updated to 1.43/1.45pre1?
Xfel #28
Posted 16 October 2012 - 11:54 AM
As far as I know and have experienced, the api didn't change for these updates
Tiin57 #29
Posted 16 October 2012 - 03:11 PM
Ah, thanks!
sirdabalot #30
Posted 10 November 2012 - 10:19 AM
Newb question, what does it mean by "add these files to your MCP install"? Meaning which directory should I put the dan200 folder in?
faubiguy #31
Posted 10 November 2012 - 10:58 AM
Put the dan200 folder (containing /computer and /turtle directories) in [MCP Directory]/src/common/
sirdabalot #32
Posted 10 November 2012 - 11:02 AM
Put the dan200 folder (containing /computer and /turtle directories) in [MCP Directory]/src/common/

Fankyhoo. :unsure:/>/>
immibis #33
Posted 21 November 2012 - 03:26 PM
Can this please be updated? The API changed in 1.45 I think.
FunshineX #34
Posted 21 November 2012 - 10:37 PM
With 1.45:

java.lang.NoSuchMethodError: net.minecraftforge.common.Configuration.getOrCreateProperty(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)Lnet/minecraftforge/common/Property;
at dan200.ComputerCraft.setupConfig(ComputerCraft.java:90)
Cloudy #35
Posted 21 November 2012 - 11:09 PM
Can this please be updated? The API changed in 1.45 I think.

We do plan to release an update - but the API didn't change in 1.4.5 (we didn't even release a new update for 1.4.5).

Edit: derp, our version numbering is getting confusing! The API has only had one small change - and that's removal of the non size sensitive methods in IComputerAccess for mounting folders. Decompile the API for now, or put the binary class files in the lib folder (the only thing you'll miss is the documentation!) - I'll document the stuff added (e.g. not breaking!) and release it soon.

With 1.45:

java.lang.NoSuchMethodError: net.minecraftforge.common.Configuration.getOrCreateProperty(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)Lnet/minecraftforge/common/Property;
at dan200.ComputerCraft.setupConfig(ComputerCraft.java:90)

That is part of CC itself - and it can't find a forge class. How exactly are you trying to run it?
immibis #36
Posted 22 November 2012 - 12:28 AM
Well, there was a breaking change in some version past 1.41. Maybe not breaking to mods that use the API, but breaking to mods that bundled it.
Edit: Cloudy told me in IRC it was 1.42.
FunshineX #37
Posted 01 December 2012 - 02:25 PM
That is part of CC itself - and it can't find a forge class. How exactly are you trying to run it?

I don't remember now. But using latest forge and mcp, and this api, I had to change Vec3D to just Vec3 in ITurtleAccess to make it work
bbqroast #38
Posted 02 December 2012 - 04:07 PM
Installation tutorial:
1. Setup and install MCP with Forge
2. Download the API at the link provided.
3. Place the "dan200" folder in (MCP folder, with the "jars", "docs" and other folders)/src/common/
4. Open eclipse. Browse to the /Minecraft/common/dan200.turtle.api/ITurtleAccess.java and change "Vec3D" to "Vec3" on line 20 (it should have a red line under it).

Thanks to Funshine for pointing this out.
Wouto1997 #39
Posted 23 January 2013 - 10:40 AM
At the top of this forum I read that you need a TileEntity which implements an IPeripheral interface, to make an interface… Is this required, or can you also do with a block? since I don't really need TileEntity features.
Cloudy #40
Posted 23 January 2013 - 01:39 PM
A tile entity is required. Don't fret though, they aren't scary.
Zinal #41
Posted 02 February 2013 - 10:07 PM
Is there any way to download the 1.46 API version? I'm trying to make a turtle upgrade for an FTB Server but FTB (Beta Pack A) is using version 1.46 of ComputerCraft, not 1.48.

Oh, as a side-note (Might have to do with the version change): ITurtleUpgrade.createPeripheral is giving me an AbstractError, which i found very wierd
ee_man #42
Posted 30 March 2013 - 08:34 AM
I don't have permission to edit this list (http://www.computercraft.info/wiki/Turtle_Upgrade_IDs)
My mod (http://www.minecraftforum.net/topic/1715756-) use 245 and 246 ids
Cloudy #43
Posted 30 March 2013 - 11:02 AM
Then request permission on the forum thread… check the wiki discussion forum.
Shnupbups #44
Posted 30 March 2013 - 11:16 AM
I don't have permission to edit this list (http://www.computerc...tle_Upgrade_IDs)
My mod (http://www.minecraft.&#46;&#46;/topic/1715756-) use 245 and 246 ids
I have added them for you.
ee_man #45
Posted 08 April 2013 - 05:10 AM
I don't have permission to edit this list (http://www.computerc...tle_Upgrade_IDs)
My mod (http://www.minecraft.&#46;&#46;/topic/1715756-) use 245 and 246 ids
I have added them for you.
Thanks
Khalory #46
Posted 10 April 2013 - 06:33 AM
This seems pretty stupid since I've already managed to get the API ready to use but… Where is the documentation? I can't find it inside the download so am I just blind or is it somewhere else?
Cloudy #47
Posted 10 April 2013 - 08:30 AM
This seems pretty stupid since I've already managed to get the API ready to use but… Where is the documentation? I can't find it inside the download so am I just blind or is it somewhere else?

It is javadoc comments in the API source.
Khalory #48
Posted 10 April 2013 - 09:02 AM
It is javadoc comments in the API source.

The API I have doesn't have them so maybe I wasn't supposed to do what I did? I opened up the main ComputerCraft download and decompiled and deobfuscated the API that was inside but there are no javadocs in what I have. Is there a separate, already deobfuscated source elsewhere that has the javadocs?
Espen #49
Posted 10 April 2013 - 09:26 AM
The API I have doesn't have them so maybe I wasn't supposed to do what I did? I opened up the main ComputerCraft download and decompiled and deobfuscated the API that was inside but there are no javadocs in what I have. Is there a separate, already deobfuscated source elsewhere that has the javadocs?
The API source files (including comments) are within the ComputerCraft zip file under "api\src".
Copy the contents from that folder into your Mod's project.

However when you're packing up your mod for release, you needn't and shouldn't include the API!
Edited on 10 April 2013 - 07:29 AM
Khalory #50
Posted 10 April 2013 - 09:43 AM
The API source files (including comments) are within the ComputerCraft zip file under "api\src".
Copy the contents from that folder into your Mod's project.

However when you're packing up your mod for release, you needn't and shouldn't include the API!

Thanks to your help I found out that I was just using a version of the download that was before the api\src folder was added to the .zip file. I can definitely find the newest version of the API, thanks.
gudenau #51
Posted 27 June 2013 - 08:10 PM
I can not add my ids to the wiki page, why is this?
Xfel #52
Posted 02 July 2013 - 02:02 AM
Then request permission on the forum thread… check the wiki discussion forum.
JustPingo #53
Posted 02 September 2013 - 05:38 AM
Hi !
When I put the API, there's no computer when I launch the game with eclipse, and if I put CC in jars' mods folder, it crash.
What should I do please ?

Thanks in advance.
GammaPaladin #54
Posted 25 September 2013 - 08:46 PM
Sooooooo… I'm working on a peripheral for my own convenience, mostly, and I'm having a slight problem. I'm returning an Object[] with 3 Integers in the array, but for some reason, when I call the function, I only get the first Integer's value.

This is my callMethod block. It only has one method for now, but I plan to add a few later, hence the switch.
@Override
	public Object[] callMethod(IComputerAccess computer, int method,
			Object[] arguments) throws Exception
	{
		if (computer == data.controller)
		{
			switch (method)
			{
				case 0:
					return getPos();
					
			}
		}
		return null;
	}

That's working, because getPos() is called. Here is the getPos function:
private Integer[] getPos()
	{
		
		Integer x = new Integer(xCoord);
		Integer y = new Integer(yCoord);
		Integer z = new Integer(zCoord);
		
		OverNet.log(Level.INFO, "Coordinates: (" + x.toString() + ", " + y.toString() + ", " + z.toString() + ")");
		
		Integer[] pos = new Integer[3];
		pos[0] = x;
		pos[1] = y;
		pos[2] = z;
		
		return pos;
	}

I know the array initialization is ugly, I was just trying to do it in the most basic way possible to ferret out any possible problems. The log statement works, I get the correct xyz coordinates displayed in the log:
2013-09-25 17:31:48 [INFO] [OverNet] Coordinates: (1106, 4, 1534)

But if I try:
local gps = peripheral.wrap("right")

print(textutils.serialize(gps.locate()))

I just get 1106.

I'm sure I've missed something obvious, so I figured I'd see if anyone can spot whatever it is I've missed.
tikiking1 #55
Posted 29 September 2013 - 07:33 PM
Is there any way to make a custom computer? I'm thinking of making a PDA-style item, and none of the classes in the Javadoc seem like they'd help. Would I have to decompile the "actual" mod and use whichever class makes the GUI?
sockeye #56
Posted 06 November 2013 - 04:36 AM
I created a peripheral. Please add Turtle ID 70 as Crop Manager turtle and give me permission to create a post about my mod here. My mod is here.
M4sh3dP0t4t03 #57
Posted 20 December 2013 - 05:53 PM
Snip snip
The array that you return isn't returned as a table in Lua, but instead as a list. So either you could do:

--Lua side fix
local gps = peripheral.wrap("right")
local x, y, z = gps.locate()
Or Java side:

@Override
        public Object[] callMethod(IComputerAccess computer, int method,
                        Object[] arguments) throws Exception
        {
                if (computer == data.controller)
                {
                        switch (method)
                        {
                                case 0:
                                        return new Object[] {getPos()};

                        }
                }
                return null;
        }
_CR_ #58
Posted 15 April 2014 - 09:40 PM
Hi, I just registered 'cause I've recently developed a mod that adds a turtle chunkloader and I wanted to post it here. I've seen I can't start a new topic, can you please give me the rights to do so?
I also wanted to ask before releasing the mod: I have to follow some logic in choosing the turtle upgrade ID other then looking on the wiki if it's already used? thanks :)/>
Cranium #59
Posted 15 April 2014 - 09:52 PM
You actually should be able to create a topic as a New Member.
Take a look at the forum guidelines in the announcements, and it explains the restrictions on new members.
Lyqyd #60
Posted 15 April 2014 - 10:13 PM
The Peripherals section may not have the topic creation permissions for new members because it's a relatively rare situation, so I probably didn't think of it when changing around the new members restrictions last time. I'll adjust them later this evening.

As for the upgrade ID, be sure to reserve your number in the wiki when you publish the mod. If you don't already have edit permissions, you could post in the sticky post in the wiki discussion section.
_CR_ #61
Posted 16 April 2014 - 09:47 PM
I still can't create a new topic :(/>
Lyqyd #62
Posted 16 April 2014 - 10:57 PM
I adjusted the permissions for this section when you made your last post. I'll review them again, but you should be able to create a topic here just fine.
_CR_ #63
Posted 16 April 2014 - 11:20 PM
now I can, thanks!
chrdov #64
Posted 20 June 2014 - 02:16 AM
dan200, I have a question that I feel is a little dumb, but I will ask anyway:

I have a suggestion for the computercraft forums, and I don't know where to put it.

Do you hold the answer I seek?

/offtopic: I don't mind if you call me sandvich, or my old username, chrdov.
AssossaGPB #65
Posted 20 June 2014 - 02:43 AM
I'm trying to make a peripheral for computercraft 1.63, but computercraft wont recognize it as a peripheral. I have tried everything the people on irc told me, even looked at redpowers peripheral classes, but I cant get it to work, please help!
Lyqyd #66
Posted 20 June 2014 - 05:54 AM
dan200, I have a question that I feel is a little dumb, but I will ask anyway:

I have a suggestion for the computercraft forums, and I don't know where to put it.

Do you hold the answer I seek?

/offtopic: I don't mind if you call me sandvich, or my old username, chrdov.

This isn't even the right place to ask where the right place is, but you're probably looking for the Forum Discussion section.

I'm trying to make a peripheral for computercraft 1.63, but computercraft wont recognize it as a peripheral. I have tried everything the people on irc told me, even looked at redpowers peripheral classes, but I cant get it to work, please help!

We don't know what the people on IRC told you. Have you got a TE that implements IPeripheral and a block that implements IPeripheralProvider and returns the TE for that block? You should probably start a new thread in the Peripheral Help and Discussion section to get further help.