Jump to content




Please Help! Custom peripherals are not recognized...

java help peripheral

  • You cannot reply to this topic
2 replies to this topic

#1 cupcode

  • Members
  • 4 posts

Posted 27 April 2014 - 01: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;[/font]
[font=arial, helvetica, sans-serif]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;[/font]
[font=arial, helvetica, sans-serif]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;
}[/font]
[font=arial, helvetica, sans-serif]@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[i] = 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) {

}[/font]
[font=arial, helvetica, sans-serif]@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);
}[/font]
[font=arial, helvetica, sans-serif]@Override
public boolean isValid() {
  return true;
}[/font]
[font=arial, helvetica, sans-serif]@Override
public void setPowerStatus(boolean parHasPower) {
  powered = parHasPower;
}[/font]
[font=arial, helvetica, sans-serif]@Override
public boolean isPowered() {
  return powered;
}[/font]
[font=arial, helvetica, sans-serif]@Override
public IGridInterface getGrid() {
  return gridInterface;
}[/font]
[font=arial, helvetica, sans-serif]@Override
public void setGrid(IGridInterface parGridInterface) {
  gridInterface = parGridInterface;
}[/font]
[font=arial, helvetica, sans-serif]@Override
public World getWorld() {
  return worldObj;
}[/font]
[font=arial, helvetica, sans-serif]@Override
public float getPowerDrainPerTick() {
  return 1f;
}
}[/font]
[font=arial, helvetica, sans-serif]


#2 Engineer

  • Members
  • 1,378 posts
  • LocationThe Netherlands

Posted 27 April 2014 - 07: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

#3 cupcode

  • Members
  • 4 posts

Posted 29 April 2014 - 12:49 PM

Thanks that worked for me! Pretty easy to be honest but I made addons only for CC 1.5





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users