1.3 Creating a new ore

So my goal with this is to implement something that acts like diamond ore. When mined normally it will drop some other item, I am going to used our example item. When mined with fortune it will have a chance of dropping more of that item, and when mined with silk touch it will drop the original ore.

To begin we will need to add a new block. so in our ModBlocks class we will add a new block called gem_ore. I am going to use all the same materials, hardness, and resistance as our example_ore.

public static final RegistryObject<Block> GEM_ORE = BLOCKS.register("gem_ore", () -> new Block(Block.Properties.create(Material.ROCK).hardnessAndResistance(3f, 3f)));

New I also need to register a BlockItem for it, and again I am going to use basically the same code as for the example_ore this will be done in our ModEventSubscriber class

@SubscribeEvent
public static void onRegisterItems(RegistryEvent.Register<Item> event) {
    event.getRegistry().registerAll(
        setup(new BlockItem(ModBlocks.EXAMPLE_ORE.get(), new Item.Properties().group(ModItemGroups.MOD_ITEM_GROUP)), ModBlocks.EXAMPLE_ORE.get().getRegistryName()),
        setup(new BlockItem(ModBlocks.GEM_ORE.get(), new Item.Properties().group(ModItemGroups.MOD_ITEM_GROUP)), ModBlocks.GEM_ORE.get().getRegistryName())
    );
}

Remember to add a comma at the end of the line adding the example ore’s block item!

Now we have all of the java code needed to create the block. Now we can make our resource and data pack parts. so first let’s create our block model. Create resources/assets/example_mod/models/block/gem_ore.json with

{
  "parent": "block/cube_all",
  "textures": {
    "all": "example_mod:block/gem_ore"
    }
}

Then create resources/assets/example_mod/models/item/gem_ore.json and fill it with

{
  "parent": "example_mod:block/gem_ore"
}

Next we need to create our blockstates in resources/assets/example_mod/blockstates/gem_ore.json with

{
  "variants": {
    "": {
      "model": "example_mod:block/gem_ore"
      }
  }
}

Now we can go ahead and add our texture you can download the texture I am using from my github here and drop it in your resources/assets/example_mod/textures/block/gem_ore.json

Now we should have a mostly working block, with a proper block item.

It won’t drop anything on break, so let’s focus on that next.

Lets start with a very simple loot table that will just drop our example_item item that was made in cadiboo’s tutorial. It will look a lot like our example_ore‘s loot table. So go ahead and create a file at resources/data/example_mod/loot_tables/blocks/gem_ore.json and we will start with

{
  "type": "minecraft:block",
  "pools": [
    {
      "rolls": 1,
      "entries": [
        {
          "type": "minecraft:item",
          "name": "example_mod:example_item"
        }
      ]
    }
  ]
}

So now our ore drops the item normally, but there is a problem with silk touch. That’s because we have not told minecraft that we care about silk touch! That is also part of the same file.

To make this, or just about any other special behaviors, work we will have to change most of our json file. First copy the object inside the entries array, the, on that same object we want to change the type to minecraft:alternatives, then delete the name line and add a children array containing your original copied object. The file should look a bit like this

{
  "type": "minecraft:block",
  "pools": [
    {
      "rolls": 1,
      "entries": [
        {
          "type": "minecraft:alternatives",
          "children": [
            {
              "type": "minecraft:item",
              "name": "example_mod:example_item"
            }
          ]
        }
      ]
    }
  ]
}

At this point the behavior is unchanged, but we have the structure setup to allow us to drop different items. Let go ahead and add a second object, just the same, at the beginning of the children array. On that first object change the name to example_mod:gem_ore.

"children": [
  {
    "type": "minecraft:item",
    "name": "example_mod:gem_ore"
  },
  {
    "type": "minecraft:item",
    "name": "example_mod:example_item"
  }
]

Now the block always drops the ore, this shows us that minecraft:alternatives picks from the list of children starting at the top going down. We don’t actually want it to always drop ore, so we will add a condition. So between the type and name attributes add an array called conditions

{
  "type": "minecraft:item",
  "conditions": [
  ],
  "name": "example_mod:gem_ore"
}

So at this point there are 2 conditions that appear like they would work just as well, tool_enchantment and match_tool, in 1.15 only match_tool appears to work, and is also the one used by minecraft blocks’ loot tables, so we will be using that. You can read more about match_tool on the minecraft gamepedia loot tables wiki page

Following the structure form the wiki page, can fill out the condition object, putting it inside our conditions array, the enchantment we want to use is minecraft:silk_touch. We actually still need to include a minimum level, even thought silk touch does not really have different levels. So we will require a minimum of level 1, the only real level to exist.

"conditions": [
  {
    "condition": "minecraft:match_tool",
    "predicate": {
      "enchantments": [
        {
          "enchantment": "minecraft:silk_touch",
          "levels": {
            "min": 1
          }
        }
      ]
    }
  }
]

So now silk touch works nicely, but as you can see even with a ridiculously high level of fortune we only get 1 of the normal drop. for that we are going to need to change our other child object to add functions in much the same way we added conditions to the other. For that just add an array functions between type and name

There is no technical reason for it to be between type and name, but the convention used in the minecraft loot tables always has type as the first attribute of the object, and name as the last. I will be copying the minecraft style while working on my mods.

So inside of our function array we want to use the apply_bonus function (minecraft wiki) for that we will just add an object into our functions array, and for our arguments we will want our enchantment to be minecraft:fortune and our formula is minecraft:ore_drops there are other formulas, but this will make our ore act identically to ores like diamonds.

{
  "type": "minecraft:item",
  "functions": [
    {
      "function": "minecraft:apply_bonus",
      "enchantment": "minecraft:fortune",
      "formula": "minecraft:ore_drops"
    }
  ],
  "name": "example_mod:example_item"
}

Now we have full support for fortune, there is one last function we need to add for full feature parity with minecraft ores. Right now we completely ignore explosions, so we want to add one more function to our functions array, and that’s explosion decay (minecraft wiki). After adding that our functions array should look a bit like this.

{
  "function": "minecraft:explosion_decay"
}

And with that final addition our gem ore works exactly like minecraft’s diamond ore.

You can see my code from this post here github.com/cody-code-wy/1.15.2-Example-Mod/tree/d324f2

Leave Comment

Your email address will not be published. Required fields are marked *