This commit is contained in:
Florian Nücke 2013-12-29 16:29:40 +01:00
commit 85961a7cba
7 changed files with 268 additions and 0 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 806 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 806 B

View File

@ -0,0 +1,25 @@
package li.cil.oc.api.example.block;
import net.minecraft.block.Block;
import net.minecraft.block.material.Material;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.world.World;
public class ExampleBlock extends Block {
public ExampleBlock(int id,Material material){
super(id,material);
setTextureName("opencomputers:placeholder");
}
@Override
public boolean hasTileEntity(int metadata) {
return true;
}
@Override
public TileEntity createTileEntity(World world, int metadata) {
return new ExampleTileEntity();
}
}

View File

@ -0,0 +1,82 @@
package li.cil.oc.api.example.block;
import li.cil.oc.api.Network;
import li.cil.oc.api.Persistable;
import li.cil.oc.api.network.*;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity;
public class ExampleTileEntity extends TileEntity implements Environment/*or ManagedEnvironment*/, Persistable { //if you don't need any of these you can ignore them
/**
* ********************************************Environment***************************
*/
private Node node;
public ExampleTileEntity() {
node = Network.newNode(this, Visibility.Network)
.withConnector()
.withComponent("ExampleComponent")
.create();
}
protected boolean addedToNetwork = false;
@Override
public void updateEntity() {
super.updateEntity();
if (!addedToNetwork) {
addedToNetwork = true;
Network.joinOrCreateNetwork(this);
}
}
// ----------------------------------------------------------------------- //
@Override
public Node node() {
return node;
}
@Override
public void onConnect(Node node) {
}
@Override
public void onDisconnect(Node node) {
}
@Override
public void onMessage(Message message) {
}
/**
* ********************************************Persistable***************************
*/
@Override
public void load(NBTTagCompound nbt) {
}
@Override
public void save(NBTTagCompound nbt) {
}
/**
* ********************************************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 ", " Block "};
}
}

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"};
}
}