Custom Item Models in 1.9 and Up

Mar 10, 2017
Custom Item Models in 1.9 and Up
  • THE GOAL
    One of the most exciting things in 1.9 is that items can now show a different model depending on their damage (durability). Presumably this is so you can show items wearing down, but we can also exploit this to add 1000's of new items to Minecraft!

    For example, everything shown here is a wooden hoe, with different durability values:
    [​IMG]

    THE CODE
    This part is relatively easy. Pick an item with durability (e.g. a tool), and create an ItemStack for that item with a set durability, let's use a durability of 1 for this example:
    Code (Java):
    ItemStack customItem = new ItemStack(Material.WOOD_HOE, 1, (short)1);
    This will give you a wooden hoe that is slightly damaged, like so:
    [​IMG]

    But of course we don't want the durability bar to show up, we also don't want the durability to change if the item is used like a tool. Fortunately, there is a tag for that!

    https://hub.spigotmc.org/javadocs/s...ry/meta/ItemMeta.html#setUnbreakable(boolean)

    Code (Java):
    ItemStack customItem = new ItemStack(Material.WOOD_HOE, 1, (short)1);
    ItemMeta meta = customItem.getItemMeta();
    meta.setUnbreakable(true); // On versions 1.11 and above
    customItem.setItemMeta(meta);
    Which gives us this:
    [​IMG]

    Ok nice, no damage meter! But if making a custom item, we probably don't want all that vanilla lore. Fortunately there's more API! You can add flags to the item to hide lore, like so:

    Code (Java):
            ItemStack customItem = new ItemStack(Material.WOOD_HOE, 1, (short)1);
            ItemMeta meta = customItem.getItemMeta();
            meta.spigot().setUnbreakable(true);
            meta.addItemFlags(ItemFlag.HIDE_ATTRIBUTES, ItemFlag.HIDE_UNBREAKABLE);
            customItem.setItemMeta(meta);
    [​IMG]

    Awesome, we now have an item with a set durability that won't change, and nothing weird in the lore.

    That's about it for the code side of things! You probably want to set a display name and lore in there as well, but I'll leave that up to you.

    This item is ready to become the custom item of your dreams.... it just needs a new skin.

    THE RESOURCE PACK
    Here is where things get a little weird. If you don't have any experience creating a resource pack, it's pretty easy. Create a new empty folder, and put in it a pack.mcmeta file that looks something like this:

    Code (Text):
    {
      "pack": {
        "pack_format": 3,
        "description": "Official elMakers Magic Resource Pack"
      }
    }
    NOTE: for Minecraft 1.10 and below use pack_format: 2 instead.

    And then add subfolders going all the way up to "assets/minecraft/models/item".

    In the item folder, create a new file called wooden_hoe.json. This will be the "container" for all of your custom items. It tells the client which models to use for certain durability values of the wooden hoe.

    This file will look something like this:
    Code (Text):
    {
      "parent": "item/handheld",
      "textures": {
        "layer0": "items/wood_hoe"
      },
      "overrides": [
        {"predicate": {"damaged": 0, "damage": 0.01666666666667}, "model": "item/my_cool_custom_item"}
      ]
    }
    Ok, so let's break this down.

    We are overriding wood hoe, but extending the base version. So the "parent" and "textures" are telling the client to use the wood hoe defaults. This means that a wood hoe with zero damage will look normal.

    Now for the new stuff- the "overrides" block lets you specify different models to use depending on certain properties of the item. These properties are called "predicates", and for our purposes we will focus on the "damage" predicate.

    This will vary the model based on damage, as a percentage from 0 to 1. So the value "0.016666" in there comes from the ratio "1/60" - because I want to apply this model at a damage/durability value of 1, and a wood hoe has a max durability of 60.

    See the mc wiki for all the durabilities of items: http://minecraft.gamepedia.com/Hoe

    You can add as many damage predicates as you want, up to the max durability of the item- just do the "x/60" math for each one.

    You can also use this tool to auto-generate a template given a specific type of tool, saving you from the maths: http://accidentalgames.com/media/durabilityModels.php

    Preserving the original items
    If you want to still be able to use the vanilla damageable items, this is possible!
    Add another entry to your model file, so it ends up looking like this:

    Code (Text):
    {
      "parent": "item/handheld",
      "textures": {
        "layer0": "items/wood_hoe"
      },
      "overrides": [
        {"predicate": {"damaged": 0, "damage": 0.01666666666667}, "model": "item/my_cool_custom_item"},
        {"predicate": {"damaged": 1, "damage": 0}, "model": "item/wooden_hoe"}
      ]
    }
    This will cause damaged versions to continue to use the base model. Note that if you use the JSON generator I linked above, you'll need to edit the final entry- it needs to refer to the model name, so wooden_hoe instead of wood_hoe for instance.

    Finally, put your custom item model in the same item folder in the resource pack, in this case it would be in "my_cool_custom_item.json". (Creating custom item models is not in the scope of this tutorial, I think there are some good ones out there already and also tools like Cubik you can use)

    Zip up the root folder of your RP, add it to your <minecraft>/resourcepacks folder and you should be able to load it and see your custom item!

    [​IMG]

    If in doubt, feel free to take a look at my resource pack structure on github:

    https://github.com/elBukkit/MagicPlugin/tree/master/Magic/src/resource-pack/default

    FINAL NOTES
    Once you have this all working, there are some things to consider.
    • The higher-level tools will give you more item models to work with. A diamond hoe has a max durability of 1,562 (!)
    • When it's time to make it live, you'll need somewhere to host your resource pack! I think Dropbox will work, just get a direct link and add it to your server.properties file.
    • If you plan on updating your RP over time, keep in mind that you have to add the SHA1 hash to your server.properties as well, or rename the RP each time- otherwise clients will not redownload it.
    • Another cool 1.9 feature- items can now be worn as hats and will show up in-game! You need to add a "head" display block to the model json file for this to work, though- see here: https://github.com/elBukkit/MagicPl...ecraft/models/item/custom/magic_hat.json#L587
    I hope this helps you out! Please let me know if you find any issues anywhere in this tutorial - thanks for reading! :)
  • Loading...
  • Loading...