MoarBows - Wiki - Historical

Applied By Indyuce: Jul 2, 2018 at 10:54 PM

MoarBows - Wiki
[​IMG]
Welcome to the MoarBows documentation!
Registering a new bow using MoarBows API
To register a new bow in MoarBows, you'll have to use the MoarBow constructor and register an instance of this class using the correct method while the plugin is loading (not enabling).

For this tutorial, we'll be creating a new bow: the cursed bow, which does fire arrows, but damages the shooter each time he uses the bow, and deals decreased damage to hit entities.

The first step is actually coding what your bow actually does. To do that, you'll need to create a class that extends the SpecialBow interface. You should just called this class something like CursedBow, something in relation of your bow name, but it doesn't really matter. Here is the template for your class :
Code (Text):
public class CursedBow implements SpecialBow {
    @Override
    public TaskState shoot(EntityShootBowEvent e, Arrow a, Player p, ItemStack i) {
        // TODO
        return TaskState.CONTINUE;
    }

    @Override
    public TaskState hit(EntityDamageByEntityEvent e, Arrow a, Entity p, Player t, Object... obj) {
        // TODO
        return null;
    }

    @Override
    public TaskState land(Arrow a) {
        // TODO
        return null;
    }
}
Let's identify what the three methods actually do.

  • shoot(EntityShootBowEvent e, Arrow a, Player p, ItemStack i) will be called whenever the player uses the bow and shoots an arrow.
    • e returns the corresponding instance of EntityShootBowEvent
    • a returns the arrow
    • p returns the shooter (player)
    • i returns the bow ItemStack
  • hit(EntityDamageByEntityEvent e, Arrow a, Entity p, Player t, Object... obj) will be called whenever the arrow actually hits an entity.
    • e returns the corresponding event
    • a returns the arrow
    • p returns the hit entity
    • t returns the shooter
    • Don't mind about obj
  • land(Arrow a) will be called when the arrow lands on the ground, without hitting any entity
    • a returns blah blah blah
The methods all return a TaskState. It can either be CONTINUE or BREAK. For land and hit, the returned value doesn't change anything, just let it to return null;
For shoot, if the returned value is BREAK, the player will not shoot the arrow. If the returned value is CONTINUE, then the player will just shoot the arrow as usual.

That said, let's actually code the bow effects:
Code (Text):
@Override
    public TaskState shoot(EntityShootBowEvent e, Arrow a, Player p, ItemStack i) {
        p.damage(4);
        p.sendMessage(ChatColor.RED + "You just hurt yourself using the Cursed Bow! This bow also deals decreased damage.");
        return TaskState.CONTINUE;
    }

    @Override
    public TaskState hit(EntityDamageByEntityEvent e, Arrow a, Entity p, Player t, Object... obj) {
        e.setDamage(e.getDamage() * .5);
        return TaskState.CONTINUE;
    }

  • shoot(EntityShootBowEvent e, Arrow a, Player p, ItemStack i)
    • Whenever the player uses this bow, the player loses 4 health.
    • He is also sent a message explaining what the bow just did..
  • hit(EntityDamageByEntityEvent e, Arrow a, Entity p, Player t, Object... obj)
    • We set the event damage to 50% of the initial damage!
  • land(Arrow a)
    • We don't need that method, the bow isn't supposed to do anything when it just hits the ground : in this case, we just keep this method empty.

Now that we have the class that says what the bow does, we need to actually register the bow!
We'll use the MoarBow constructor to group all the data of the bow and use its register method to register it in MoarBows.
Put this in the onLoad() of your main plugin class:
Code (Text):
public void onLoad() {
        MoarBow cursedBow = new MoarBow(new CursedBow(),
                "CURSED_BOW",
                "Cursed Bow",
                new String[] { "This bow was blessed by evil magic.", "You shouldn't manipulate it..." },
                0,
                30,
                "redstone:128,0,0",
                MoarBowsUtils.formatCraftingRecipe(new MaterialData(Material.FLINT), new MaterialData(Material.FLINT), new MaterialData(Material.FLINT), new MaterialData(Material.FLINT), new MaterialData(Material.BOW), new MaterialData(Material.FLINT), new MaterialData(Material.FLINT), new MaterialData(Material.FLINT), new MaterialData(Material.FLINT)),
                new BowModifier[0]);
        cursedBow.register(true);
    }
Let's now analyze all the arguments of the MoarBow constructor:

  • The first argument is a new instance of the class we created earlier! This class dictates what the bow does in-game.
  • The second argument is the bow ID : if the name is for instance "Cursed Bow", the ID should be CURSED_BOW. Two bows must not have the same ID!
  • The third argument is the bow item name (the name that will be displayed in-game).
  • The fourth arg is the item lore (description) - it's a String array
  • The fifth arg is the item durability - just put 0, but you can use it to create custom textured bows (see below) - it's an integer
  • The sixth arg is the bow cooldown - i'ts a double
  • The seventh arg is the bow particle effect. It is formatted in a very specific way (take a look at how it is formatted for the default bows, in bows.yml)
  • The eigth arg is the bow crafting recipe. it's a string array - Again, it is formatted in a very specific way
  • The last arg is the bow modifiers. These are the values that depends on the bow itself. For instance, it can be the damage value, the duration... For the moment, just set it to null and hard-code your values.
Once you created a new instance of MoarBow, use the MoarBow#register(boolean) method to register your bow. If the boolean arg is set to true, MoarBows will display a message in the console when the bow is being registered.

Phew! There you go, now head to plugin.yml and make sure your plugin has MoarBows as dependency, so it loads after MoarBows! Export your project, and you're good to go.
The bow you created just behave as a default bow. This means you can edit its values in the bows.yml once it's registered.
Keep in mind you'll have to register the bow each time the server starts, because MoarBows doesn't store anywhere data about non-default bows! However this doesn't mean the bows.yml data of your bow will be gone when your server goes off, as long as the bow still has the same bow ID!