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

Please Help! Custom peripherals are not recognized...

Started by cupcode, 27 April 2014 - 12:34 AM
cupcode #1
Posted 27 April 2014 - 02:34 AM
So here is my problem,
I am trying to make a custom peripheral for CC 1.63 but the computers wont
recognize my peripheral. When I try to wrap it and get its type with type() it returns nil.
Please help :3

Here is my code:


package de.mrpixelp.itempedia.tileentity;

import java.awt.image.TileObserver;
import java.util.HashMap;
import java.util.Iterator;
import dan200.computercraft.api.lua.ILuaContext;
import dan200.computercraft.api.peripheral.IComputerAccess;
import dan200.computercraft.api.peripheral.IPeripheral;
import appeng.api.IAEItemStack;
import appeng.api.IItemList;
import appeng.api.Util;
import appeng.api.WorldCoord;
import appeng.api.me.tiles.IGridMachine;
import appeng.api.me.util.IGridInterface;
import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.world.World;

public class TileEntityCCMEInterface extends TileEntity implements IPeripheral, IGridMachine {
public static final String PERIPHERAL_TYPE = "ccmeinterface";
public static final String[] PERIPHERAL_METHODS = new String[]{"receiveItem", "listItems", "hasItem"};

public IGridInterface gridInterface;
public boolean powered;

@Override
public String getType() {
return PERIPHERAL_TYPE;
}

@Override
public String[] getMethodNames() {
return PERIPHERAL_METHODS;
}

@Override
public Object[] callMethod(IComputerAccess parComputer, ILuaContext parLuaContext, int parMethod, Object[] parArguments) throws Exception {
String methodName = PERIPHERAL_METHODS[parMethod];
if(methodName == "receiveItem") {
if(parArguments.length != 3) throw new Exception("Correct use: receiveItem <number:id> <number:meta> <number:amount> <string:side>");
int argID;
int argMeta;
int argAmount;
String argSide;
try {
argID = ((Double)parArguments[0]).intValue();
argMeta = ((Double)parArguments[1]).intValue();
argAmount = ((Double)parArguments[2]).intValue();
argSide = (String)parArguments[3];
}
catch(Exception e) {
throw new Exception("Correct use: receiveItem <number:id> <number:meta> <number:amount> <string:side>");
}
if(!(argSide == "top" || argSide == "bottom" || argSide == "north" || argSide == "east" || argSide == "south" || argSide == "west")) {
throw new Exception("Side has to be: top, bottom, north, east, south, west!");
}
IAEItemStack itemsReturned = gridInterface.getCellArray().extractItems(Util.createItemStack(new ItemStack(argID, argAmount, argMeta)));
return new Object[]{itemsReturned.getStackSize()};
}
else if(methodName == "listItems") {
if(parArguments.length != 0) throw new Exception("Correct use: listItems");
IItemList items = gridInterface.getCellArray().getAvailableItems();
HashMap<String, Object>[] allItems = new HashMap[items.size()];
Iterator<IAEItemStack> iterator = items.iterator();
for(int i = 0; iterator.hasNext(); i++) {
IAEItemStack itemStack = iterator.next();
HashMap<String, Object> map = new HashMap<String, Object>();
map.put("id", itemStack.getItemID());
map.put("meta", itemStack.getItemDamage());
map.put("amount", itemStack.getStackSize());
allItems = map;
}
return new Object[]{allItems};
}
else if(methodName == "hasItem") {
if(parArguments.length != 3) throw new Exception("Correct use: hasItem <number:id> <number:meta> <number:amount>");
int argID;
int argMeta;
int argAmount;
try {
argID = ((Double)parArguments[0]).intValue();
argMeta = ((Double)parArguments[1]).intValue();
argAmount = ((Double)parArguments[2]).intValue();
}
catch(Exception e) {
throw new Exception("Correct use: hasItem <number:id> <number:meta> <number:amount>");
}
IAEItemStack itemsReturned = gridInterface.getCellArray().extractItems(Util.createItemStack(new ItemStack(argID, argAmount, argMeta)));
return new Object[]{itemsReturned.getStackSize()};
}
return null;
}

@Override
public void attach(IComputerAccess parComputer) {

}

@Override
public void detach(IComputerAccess parComputer) {

}

@Override
public boolean equals(IPeripheral parOther) {
return parOther.getClass().equals(this.getClass());
}

@Override
public WorldCoord getLocation() {
return new WorldCoord(xCoord, yCoord, zCoord);
}

@Override
public boolean isValid() {
return true;
}

@Override
public void setPowerStatus(boolean parHasPower) {
powered = parHasPower;
}

@Override
public boolean isPowered() {
return powered;
}

@Override
public IGridInterface getGrid() {
return gridInterface;
}

@Override
public void setGrid(IGridInterface parGridInterface) {
gridInterface = parGridInterface;
}

@Override
public World getWorld() {
return worldObj;
}

@Override
public float getPowerDrainPerTick() {
return 1f;
}
}

Engineer #2
Posted 27 April 2014 - 08:15 PM
As of the new CC overhaul (1.6+), we developers need to implement our peripherals via IPeripheralProvider.

So, the most basic IPeripheralWrapper you could have is the following one:

// package and imports here

public class PeripheralHandler implements IPeripheralProvider {
  @Override
   public IPeripheral getPeripheral(World world, int x, int y, int z, int side) {
       TileEntity tile = world.getBlockTileEntity(x, y, z);
       if(tile instanceof IPeripheral)
            return (IPeripheral) tile;
       return null;
   }
}

This is the most basic IPeripheralProvider, which by the way I think should be implemented by default, but dan200 said that this is intentional design.
For myself, I created a wrapper around IPeripheral's, and this interface is just a beautifull opportunity to implement it properly. Anyway, nothing is wrong with your code :P/>
cupcode #3
Posted 29 April 2014 - 01:49 PM
Thanks that worked for me! Pretty easy to be honest but I made addons only for CC 1.5