←  Peripherals and Turtle Upgrades

ComputerCraft | Programmable Computers for Minecraft

»

Creating Peripherals and Turtle Upgrades (...

dan200's Photo dan200 13 Mar 2012

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!
Quote

rockymc's Photo rockymc 13 Mar 2012

Just a question: how do we make the methods for the Peripheral in Lua? Because what I understood here was:

View Postdan200, on 13 March 2012 - 01:47 PM, said:


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.
Quote

Espen's Photo Espen 13 Mar 2012

View Postrockymc, on 13 March 2012 - 04:02 PM, said:

Just a question: how do we make the methods for the Peripheral in Lua? Because what I understood here was:

View Postdan200, on 13 March 2012 - 01:47 PM, said:

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.
Quote

rockymc's Photo rockymc 13 Mar 2012

View PostEspen, on 13 March 2012 - 04:44 PM, said:

View Postrockymc, on 13 March 2012 - 04:02 PM, said:

Just a question: how do we make the methods for the Peripheral in Lua? Because what I understood here was:

View Postdan200, on 13 March 2012 - 01:47 PM, said:

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?
Quote

dan200's Photo dan200 13 Mar 2012

View Postrockymc, on 13 March 2012 - 05:58 PM, said:

View PostEspen, on 13 March 2012 - 04:44 PM, said:

View Postrockymc, on 13 March 2012 - 04:02 PM, said:

Just a question: how do we make the methods for the Peripheral in Lua? Because what I understood here was:

View Postdan200, on 13 March 2012 - 01:47 PM, said:

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.
Quote

oreillynz's Photo oreillynz 16 Mar 2012

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)
Quote

Bunny83's Photo Bunny83 28 Apr 2012

View Postoreillynz, on 16 March 2012 - 09:37 AM, said:

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.
Quote

louitzie's Photo louitzie 21 May 2012

is it possible to get a turtles inventory?
Quote

Xfel's Photo Xfel 21 May 2012

Turtles implement the IInventory interface like any chest or furnace. At least, it is accessible to buildcraft and redpower item processors.
Quote

zwap1233's Photo zwap1233 21 Jul 2012

whit "TileEntity" do you mean the basemod class or the block class?
Quote

xuma202's Photo xuma202 23 Jul 2012

For a SMP mod do I have to call queueEvent on all Clients or is this synchronized by Computer Craft already, guess so.
Quote

Cloudy's Photo Cloudy 24 Jul 2012

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.
Quote

xuma202's Photo xuma202 26 Jul 2012

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
Quote

Cloudy's Photo Cloudy 26 Jul 2012

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.
Quote

xuma202's Photo xuma202 26 Jul 2012

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.
Quote

Cloudy's Photo Cloudy 26 Jul 2012

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.
Quote

xuma202's Photo xuma202 26 Jul 2012

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.
Quote

xuma202's Photo xuma202 30 Jul 2012

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
Quote

dan200's Photo dan200 30 Jul 2012

View Postxuma202, on 30 July 2012 - 09:55 AM, said:

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.
Quote

djblocksaway's Photo djblocksaway 01 Aug 2012

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:/>
Quote