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

Very Basic Framework (Your First Peripheral)

Started by Casper7526, 13 March 2012 - 05:17 PM
Casper7526 #1
Posted 13 March 2012 - 06:17 PM
This framework was from the very first peripheral I made, it should work as I don't believe the API has changed.

Note #1: Doesn't use Forge Sprites/Textures
Note #2: Change everything to your own Names
Note #3: If this is too hard to understand, you SHOULDN'T be making a peripheral.

mod_Block.java
Spoiler

package net.minecraft.src;
import dan200.computer.api.IComputerAccess;
import dan200.computer.api.IPeripheral;
public class mod_Block extends BaseMod
{
public static final Block Namehere = new BlockName(160, 0).setBlockName("name").setHardness(3F).setResistance(4F).setLightValue(1F);

	public void load()
	{
		Namehere.blockIndexInTexture = ModLoader.addOverride("/terrain.png", "/images/block.png");
		ModLoader.RegisterBlock(Namehere);
		ModLoader.AddName(Namehere, "In-Game Name Here");
		ModLoader.AddRecipe(new ItemStack(Namehere, 1), new Object [] {"#", Character.valueOf('#'), Block.dirt});
		ModLoader.RegisterTileEntity(TileEntityName.class, "name");
	}

	public String getVersion()
	  {
			  return "1.1";
	   }
}
TileEntityName.java
Spoiler

package net.minecraft.src;

import dan200.computer.api.IComputerAccess;
import dan200.computer.api.IPeripheral;
import net.minecraft.src.*;


public class TileEntityName extends TileEntity
	implements IPeripheral
{



	public synchronized void detach(IComputerAccess icomputeraccess) // tried sync and without
	{
	}

	public void attach(IComputerAccess icomputeraccess, String s)
	{
	}

	public String getType() {
				return "name";
	}

	public String[] getMethodNames()
	{
	return (new String[] { "test" });
	}

	public Object[] callMethod(IComputerAccess icomputeraccess, int i, Object aobj[])
	throws Exception
	{
	if (i == 0)
	{
	return (new Object[] {"param1", "param2", 7123, true, false, "param6"});
	}
	return null;
	}

	public boolean canAttachToSide(int i)
	{
	return true;
	}

}

BlockName.java
Spoiler

package net.minecraft.src;
import java.util.*;
import dan200.computer.api.IComputerAccess;
import dan200.computer.api.IPeripheral;

public class BlockName extends BlockContainer
{

	 public TileEntity getBlockEntity() {			
	 return new TileEntityName();
	 }


		public BlockName(int i, int j)
		{
			super(i, j, Material.iron);
		}


	public int idDropped(int i, Random random, int j)
	{
		   return mod_Block.Namehere.blockID;
	}
	public int quantityDropped(Random random)
	{
				return 1;
	}

}
Liraal #2
Posted 13 March 2012 - 08:34 PM
I see I am a longer way from making a PDA that i thought I am :mellow:/>/>
Hackingroelz #3
Posted 14 March 2012 - 09:06 PM
I''ve made my first peripheral. I know it works since peripheral.isPresent() returns true, but I have no idea how to call a method that I made. Could somebody explain this to me?
Casper7526 #4
Posted 14 March 2012 - 11:04 PM
peripheral.getMethods(side)
peripheral.call(side, method)
ComputerCraftFan11 #5
Posted 01 April 2012 - 09:57 PM
How can I make a function or something to make the peripheral do something?
bbqroast #6
Posted 25 July 2012 - 11:12 PM
How can I make a function or something to make the peripheral do something?
See the TileEntityName.class example? In the getMethodNames() function it has an array of strings, the example has a single function "test". Say you wanted some functions for changing the block color, in that array you could create the functions setColorRed(), setColorGreen() like this:

public String[] getMethodNames()
	    {
	    return (new String[] { "setColorRed", "setColorGreen" });
	    }
Then in the call method function you can respond to them:

public Object[] callMethod(IComputerAccess icomputeraccess, int i, Object aobj[])
	    throws Exception
	    {
	    if (i == 0)
	    {
	    // Do logic for the setColorRed() function
	    } else if (i == 1) {
	    // Do logic for the setColorGreen() function
	    }
	    return null;
	    }
I believe "i" is the index of the function name in the array.
Note:You do all the peripheral programming in Java, not LUA.