Useful Machine Methods
Useful Machine Methods
Ejecting an item out of your machine
1. In your machine class, call the method "ejectItem" like so:
1 2 | this.ejectItem(item); //item being an ItemStack Object //This method is preferred to ejecting/dropping items with your own code as this can be hooked into by other developers using the API |
2. If you'd like to listen for a machine calling this method, you can use the custom event "MachineEjectItemEvent" like so:
1 2 3 4 5 6 7 8 9 10 | @EventHandler public void onMachineEjectItem(MachineEjectItemEvent event) { ItemStack item = event.getItem(); //Because we obviously don't want our machine throwing out TNT blocks :3 if(item.getType() == Material.TNT) { event.setCancelled(true); } } //This is useful in case you would like to do something with the items a machine is dropping rather than just have it drop onto the ground |
Getting block drops
1. Getting the drops of a block can be found using this method here.
1 2 3 | Set<ItemStack> drops = this.getBlockDrops(facingBlock); //This is preffered to block.getDrops() as this will also get blocks from inventory and get the proper drop item for machines //For use in classes that extend MachineBlock |
Adding items to blocks
1. If you'd like to try and add an ItemStack to a block, use this method:
1 2 3 4 | this.addItemToBlock(block, item, ejectItem); //Will always return a Set of items that didn't fit //Will try and add the item to the blocks inventory, if not, and if ejectItem is true, it will call this.ejectItem() on the ItemStack //For use in classes that extend MachineBlock |
Creating and accessing a machine's inventory
1. To create an inventory for a machine, place a code similar to this in the machines constructor:
1 2 | this.setInventory(inventoryname, inventorysize); //This will create your machine's inventory with the name and size specified |
2. To access this inventory, use:
1 2 | Inventory inventory = this.getInventory(); //Will grab the machines inventory |
Comments