Creating a Machine

How to create a new Machine

1. Make a new class and have it extend "MachineBlock"

2. The constructor should look something like this:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
public MachineName(int id) {
    super(id);
    
    //Each machine needs a block that represents it (The block that gets placed when the machine is created)
    this.setBlockRepresentation(Material.DISPENSER);

    //Each item needs an item the represents it (The item in your inventory that when placed, yields your machine)
    this.setItemRepresentation(new MachineItem(Material.DISPENSER, /* This is your items Material */
        "Block Breaker", /*This is your item name */
        "Will break those scrubby blocks for you")); /* This is your item's lore or description */
    
    //This is your machines crafting recipe
    ShapedRecipe recipe = new ShapedRecipe(this.getItemRepresentation().getItem());
    recipe.shape("CIC", "CPC", "CRC");
    recipe.setIngredient('C', Material.COBBLESTONE);
    recipe.setIngredient('I', Material.IRON_PICKAXE);
    recipe.setIngredient('P', Material.PISTON_BASE);
    recipe.setIngredient('R', Material.REDSTONE);

    //This is also where you will set the default values for your persistent data
    
    //Set your machines crafting recipe
    this.setRecipe(recipe);
}

3. There are various methods in the MachineBlock class that can be overriden like so:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
@Override
public void onTick() {
    //Called every 5 ticks
}

@Override
public void onPlace() {
    //Called when the machine is placed down
}

@Override
public void onRemove() {
    //Called when the machine is removed
}

@Override
public void onPowered() {
    //Called when the machine is powered by redstone (Not 100% accurate)
}

@Override
public void onInteract(PlayerInteractEvent event) {
    //Called when a player interacts with the machine
}

4. Generally speaking the API only saves each machines id and location, but if you want you can add persistent data that the API will also save. You use persistent data like so:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
//If you wanted to add an inventory to your machine, you could add an inventory object as a persistent data
//Persistent data can be added anywhere, but should more often than not be placed in the constructor
public MachineName(int id) {
    super(id);
    
    //Creates an inventory of 9 spaces with the name "Inventory Name" and binds it to the key "inventory"
    this.setPersistentData("inventory", Bukkit.getServer().createInventory(null, 9, "Inventory Name"));
    
    //To get the inventory back, you can do this
    Inventory inventory = (Inventory) this.getPersistentData("inventory");
    //You must cast to inventory as getPersistentData returns an Object
}

5. Once you have the machine done the way you'd like, go to your plugins onEnable method and this line of code:

1
2
MachineHub.registerMachine(new MachineName(0));
//0 being the id of the machine. Remember, these must be completely unique, even plugin to plugin

Comments

Posts Quoted:
Reply
Clear All Quotes