User Tools

Site Tools


items

Modding::Items

To enable content creators to create intricate gamemodes Chunk Stories offers a complete items system. Items are defined in .items files, using the usual Definitions file format syntax.

.items files format

The structure of those files is laid out as :

item dull_item {
    //Width of this item in the inventory
    width: 2
    max-stack-size: 50
}

item unsafe_grenade {
    customClass: me.example.mod.ItemRustyGrenade
    // You can declare custom properties and then get their value in your custom class constructor !
    maxSafety: 0.4
}

Item.java and ItemPile.java

The Item system revolves around three classes : The Item class, The ItemType class and the ItemPile class. The ItemType class is the 'definition' of the item, it's a static/final field your Items objects are initialized in. It does not contain the information of one item, instead it relies on the Item class to store it's data. The ItemPile serves only to stack and store the Item objects in the actual game. You are only allowed to mess with the Item class. Code is provided with the API provided with the game.

A typical example would be this grenade class:

/** An unsafe grenade found in military surplus */
class ItemRustyGrenade extends Item {

    //To be extra clear: this variable exists per-item instance and isn't global
    float changeToBlowUser;

    //Item is initialized with ItemType
    public ItemRustyGrenade(ItemType type)
    {
        super(type);
        changeToBlowUser = Math.random() * Float.parseFloat(type.resolveProperty("maxSafety", 1.0));
        itemRenderer = new RustyItemRenderer(this);
    }

    //ItemPile is the itemPile selected by the user when he tries to interact with it
    public boolean handleInteraction(Entity user, ItemPile itemPile, Input input, Controller controller)
    {
        if(!input.getName().equals("mouse.right"))
            return false;
        
        if(Math.random() < chanceToBlowUser)
        {
            // If he's unlucky he gets blowns to smithereens
            user.getWorld().createExplosion(user.getLocation(), 15f);
        }
        else
        {
            user.sendMessage("Lucky boy");
            // Throw the grenade as normal
            ...
        }
        
        //This item handled the interaction, it should not trigger anything else
        return true;
    }
}
items.txt · Last modified: 2018/09/10 12:06 by gobrosse