MoarBows - Wiki

Jul 3, 2018
MoarBows - Wiki
  • [​IMG]
    Welcome to the MoarBows documentation!
    You can find the resource here.

    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 call 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 boolean shoot(EntityShootBowEvent e, Arrow a, Player p, ItemStack i) {
            // TODO
            return true;
        }

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

        @Override
        public void land(Arrow a) {
            // TODO
        }
    }
    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) 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
    • land(Arrow a) will be called when the arrow lands on the ground, without hitting any entity
      • a returns blah blah blah
    The shoot method returns a boolean. If you set this boolean to true, the arrow will be sent as normal. If you set it to false, the arrow will not receive any additional effect like particles, and will not call the land or the hit method. If you wish to cancel the shoot event, just cancel it and set this boolean to false. Otherwise, just keep it to true.



    That being said, let's actually code the bow effects:
    Code (Text):
    @Override
        public boolean 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 true;
        }

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

    • 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)
      • 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
      • There is an util called MoarBowsUtils you can use to get a formatted crafting recipe using MaterialData which can save both a material and a durability.
    • 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!

    Additional stuff
    You can use the MoarBowsUtils#getBowFromItem(ItemStack) method to return the corresponding MoarBow class depending on an ItemStack.
    You can for instance use this to check if a player is holding a specific bow. E.g:
    Code (Text):
    MoarBow mbow = MoarBowsUtils.getBowFromItem(player.getInventory().getItemInMainHand());
    if (mbow.getID().equals("FIRE_BOW"))
        player.sendMessage(ChatColor.YELLOW + "Watch out, your hands are burning!");
  • Loading...
  • Loading...