Skip to content

Custom Loot Tables and Loot Items

Ryandw11 edited this page Mar 19, 2023 · 1 revision

CustomStructures allows developers to programmatically create LootTables and LootItems.

Loot Tables

Loot Tables are defined by extending the abstract LootTable class.

public class CustomLootTable extends LootTable {

    public CustomLootTable() {
        super();

        LootItem lootItem = new PotatoItem();

        addLootItem(1, lootItem);
    }


    @Override
    public String getName() {
        return "exampleLootTable";
    }

    @Override
    public int getRolls() {
        return 10;
    }

    @Override
    public void setRolls(int i) {}
}

Loot Tables are registered using your CustomStructureAddon class.

myAddon.registerLootTable(new CustomLootTable());

Config Loot Items

To add a custom loot item that can be used in a loot table configuration file, you must extend the ConfigLootItem class.

public class SuperLootItem extends ConfigLootItem {
    private ItemStack itemStack;

    public SuperLootItem(LootTable lootTable, String itemID, int weight, String amount) {
        super(lootTable, itemID, weight, amount);
    }

    @Override
    public void constructItem(ConfigurationSection configurationSection) {
        Material material = Material.getMaterial(configurationSection.getString("Material", "DIRT"));
        if (material == null) {
            material = Material.DIRT;
        }

        itemStack = new ItemStack(material, 1);
        ItemMeta itemMeta = itemStack.getItemMeta();
        itemMeta.setDisplayName(ChatColor.GOLD + "SUPER " + material);
        itemMeta.setLore(Arrays.asList("A rare SUPER ITEM!", "This is a very magical item."));
        itemMeta.addEnchant(Enchantment.DAMAGE_ALL, 100, true);
        itemStack.setItemMeta(itemMeta);
    }

    @Override
    public ItemStack getItemStack() {
        ItemStack cloneItem = itemStack.clone();
        itemStack.setAmount(getAmount());

        return cloneItem;
    }
}

Then you register it using your CustomStructureAddon instance:

myAddon.registerLootItem("SuperItem", SuperLootItem.class);

Then you can use it in a loot table configuration file as so:

  item4:
    Type: SUPERITEM
    Material: 'GRASS_BLOCK'
    Weight: 20
    Amount: '[1;5]'