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

Making a peripheral (Questions)

Started by Bubba, 12 November 2012 - 10:46 AM
Bubba #1
Posted 12 November 2012 - 11:46 AM
Hello again,

Now that I've finally managed to get ComputerCraft running from Eclipse, I am looking to create a peripheral just to try things out. For some reason peripheral.isPresent(side) just returns nil. Could someone look over this code and tell me what I'm doing wrong? I'm sure it's something stupid but I can't seem to figure this out.

BlockPeripheralBlock.java
Spoiler

package peripheral.common;

import net.minecraft.src.Block;
import net.minecraft.src.CreativeTabs;
import net.minecraft.src.Material;
import net.minecraft.src.TileEntity;

import java.io.PrintStream;
import java.util.ArrayList;
import java.util.List;

import dan200.computer.api.IComputerAccess;
import net.minecraft.src.World;

public class BlockPeripheralBlock extends Block
{
	public BlockPeripheralBlock(int id, int texture)
	{
		super(id, texture, Material.cloth);
		this.setCreativeTab(CreativeTabs.tabBlock);
	}

}

TileEntityPeripheral.java
Spoiler

package peripheral.common;

import java.util.ArrayList;
import java.util.List;

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

public class TileEntityPeripheral extends TileEntity implements IPeripheral
{
	IComputerAccess computers = null;

	public String getType()
	{
		return "Peripheral";
	}
	
	
	public String[] getMethodNames()
	{
		return new String[] { "getBlockId", "setBlockId" };
	}

	public Object[] callMethod(IComputerAccess computer, int method, Object[] arguments)
	{
		if (method == 0)
		{
			System.out.println("Called 1");
		} else if (method == 1) {
			System.out.println("Called 2");
		}

		return null;
	}
	
  
	public boolean canAttachToSide(int side)
	{
		return true;

	}
	
	public void attach(IComputerAccess computer, String computerSide)
	{
	}
	
	public void detach(IComputerAccess computer)
	{
	}
}


Peripheral.java
Spoiler

package peripheral.common;

import net.minecraft.src.Block;
import net.minecraft.src.TileEntity;
import cpw.mods.fml.common.Mod;
import cpw.mods.fml.common.Mod.Init;
import cpw.mods.fml.common.event.FMLInitializationEvent;
import cpw.mods.fml.common.network.NetworkMod;
import cpw.mods.fml.common.registry.GameRegistry;
import cpw.mods.fml.common.registry.LanguageRegistry;

@Mod(modid = "Peripheral", name = "Peripheral", version = "1.0")
@NetworkMod(clientSideRequired = true, serverSideRequired = false)
public class peripheral
{
	public static Block peripheralBlock;
	
	@Mod.Instance("Peripheral")
	public static Object instance;
	@Init
	public void load(FMLInitializationEvent event)
	{
		GameRegistry.registerTileEntity(TileEntityPeripheral.class, "TileEntityPeripheral");
		peripheralBlock = new BlockPeripheralBlock(250, 0).setBlockName("peripheralBlock");
		GameRegistry.registerBlock(peripheralBlock);
		LanguageRegistry.addName(peripheralBlock, "Peripheral Block");
	}
}


Eclipse is not giving me any errors.
Note: This is not intended for release (obviously). It is merely a test of the CC api and creating peripherals.
Orwell #2
Posted 12 November 2012 - 01:16 PM
Your tile entity is never loaded because minecraft can't derive it from the block. Add this method to the class BlockPeripheralBlock:

public TileEntity createNewTileEntity(World par1World)
{
  return new TileEntityPeripheral();
}
Bubba #3
Posted 12 November 2012 - 01:33 PM
Your tile entity is never loaded because minecraft can't derive it from the block. Add this method to the class BlockPeripheralBlock:

public TileEntity createNewTileEntity(World par1World)
{
  return new TileEntityPeripheral();
}

Added to the code, but there was no effect. Methinks I just need to learn Java better before I start trying to write peripherals.
Orwell #4
Posted 12 November 2012 - 02:12 PM
Oh yea, you should try adding this as well to the BlockPeripheralBlock class:

public boolean hasTileEntity(int metadata)
{
	return true;
}

I never needed to do this though. Also, I always add the '@Override' annotation to those functions, maybe it matters?
Bubba #5
Posted 12 November 2012 - 02:57 PM
Oh yea, you should try adding this as well to the BlockPeripheralBlock class:

public boolean hasTileEntity(int metadata)
{
	return true;
}

I never needed to do this though. Also, I always add the '@Override' annotation to those functions, maybe it matters?

Hmm still no success. I think I'm going to try starting over to see if I've done something wrong along the way.

Using an @Override on those functions doesn't work because the BlockPeripheralBlock class does not extend TileEntity. Do you mean I should add @Override to any functions that are in any extended classes?
Orwell #6
Posted 12 November 2012 - 03:19 PM
Using an @Override on those functions doesn't work because the BlockPeripheralBlock class does not extend TileEntity. Do you mean I should add @Override to any functions that are in any extended classes?

BlockPeripheral extends Block. So that's why. In theory I think you should add @Override to any function that 'overrides' a function from the class it extends (and any parent class higher in the hierarchy). So in your case it's practically every function (it gives an error when it isn't an existing function in the parent classes). Not all code I've seen does this this though, so I don't know about that. :P/>/>
Bubba #7
Posted 12 November 2012 - 03:34 PM
Using an @Override on those functions doesn't work because the BlockPeripheralBlock class does not extend TileEntity. Do you mean I should add @Override to any functions that are in any extended classes?

BlockPeripheral extends Block. So that's why. In theory I think you should add @Override to any function that 'overrides' a function from the class it extends (and any parent class higher in the hierarchy). So in your case it's practically every function (it gives an error when it isn't an existing function in the parent classes). Not all code I've seen does this this though, so I don't know about that. :P/>/>

Yeah I'm not quite sure I understand what the whole deal is with @Override. Some people use it and some people don't so I'm not really sure what to think. Thanks for your help though :)/>/>
faubiguy #8
Posted 12 November 2012 - 04:03 PM
Try making it extend BlockContainer, instead of Block.
sirdabalot #9
Posted 16 November 2012 - 04:03 AM
Try making it extend BlockContainer, instead of Block.

Absolutly spot on, fixed my probem. And I'm trying to make a peripheral identical to yours.