Example Component

This commit is contained in:
Johannes Lohrer 2013-12-29 12:09:19 +01:00
parent 2aba89c84e
commit 107abb042f
4 changed files with 161 additions and 0 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 806 B

View File

@ -0,0 +1,16 @@
package li.cil.oc.api.example.component;
import net.minecraft.item.Item;
public class ExampleItem extends Item{
//for convenience reasons
public static ExampleItem thisItem;
public ExampleItem(int id){
super(id);
thisItem = this;
setTextureName("opencomputers:placeholder");
}
//add all the important methods for your item like images and logic (not lua stuff)
}

View File

@ -0,0 +1,59 @@
package li.cil.oc.api.example.component;
import cpw.mods.fml.common.event.FMLInitializationEvent;
import li.cil.oc.api.Driver;
import li.cil.oc.api.driver.Item;
import li.cil.oc.api.driver.Slot;
import li.cil.oc.api.network.ManagedEnvironment;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity;
public class ExampleItemDriver implements Item {
@Override
public boolean worksWith(ItemStack item) {
return item.getItem().equals(ExampleItem.thisItem);
}
@Override
public ManagedEnvironment createEnvironment(ItemStack stack, TileEntity container) {
//return the environment for the item
return new ExampleItemEnvironment();
}
@Override
public Slot slot(ItemStack stack) {
return Slot.Upgrade;//This is the part where you define in which slot the item fits.
}
/**
* This is a example implementation of dataTag. If you prefere a different way to retrieve the compund
* feel free to use it
* @param stack the item to get the child tag from.
* @return
*/
@Override
public NBTTagCompound dataTag(ItemStack stack) {
if (!stack.hasTagCompound()) {
stack.setTagCompound(new NBTTagCompound("tag"));
}
NBTTagCompound nbt = stack.getTagCompound();
if (!nbt.hasKey("yournamespace" + "data")) {
nbt.setCompoundTag("yournamespace" + "data", new NBTTagCompound());
}
return nbt.getCompoundTag("yournamespace" + "data");
}
//This is supposed to be your proxy where you init your stuff
public class Proxy{
public void init(FMLInitializationEvent e) {
//in the init methode add this to register the driver
Driver.add(new ExampleItemDriver());
}
}
}

View File

@ -0,0 +1,86 @@
package li.cil.oc.api.example.component;
import li.cil.oc.api.Network;
import li.cil.oc.api.network.*;
import net.minecraft.nbt.NBTTagCompound;
public class ExampleItemEnvironment implements ManagedEnvironment {
/**
* ********************************************Default methods***************************
*/
private Node node;
public ExampleItemEnvironment() {
//for a more detailed description read the documentation of each parameter
node = Network
//Visibility defines who can reach the component
.newNode(this, Visibility.Network)
//if the component shall be reached in the component network,
// Visibility defines from where the component can be accessed with lua (
.withComponent("ExampleItem", Visibility.Neighbors)
//if you want to have power
.withConnector()
//finally creates the node required!
.create();
}
@Override
public Node node() {
return node;
}
@Override
public void onConnect(Node node) {
//do stuff
}
@Override
public void onDisconnect(Node node) {
//do stuff
}
@Override
public void onMessage(Message message) {
//do stuff
}
@Override
public boolean canUpdate() {
//used to indicate if the component shall be updated every tick only called once!
return false;
}
@Override
public void update() {
//do stuff on update (only called if canUpdate returns true)
}
@Override
public void load(NBTTagCompound nbt) {
//example if you want different implementation do so!
if (node != null)
node.load(nbt.getCompoundTag("node"));
}
@Override
public void save(NBTTagCompound nbt) {
//example if you want different implementation do so!
if (node != null) {
NBTTagCompound compound = new NBTTagCompound();
node.save(compound);
nbt.setCompoundTag("node", compound);
}
}
/**
* ********************************************Custom methods***************************
*/
//This is a example method that can be called from lua. The lua name and java name don't need
//to be the same
@LuaCallback("test")
public Object[] testName(Context computer, Arguments arguments) {
return new Object[]{"Hello", " Lua"};
}
}