User Tools

Site Tools


custom_classes

Mods Programming::Custom Classes

As mentioned in the mods generalities page, content definitions can reference a custom class, using the 'class' property :

entity flying_pig {
	class: com.example.entity.Pig
	canFly: true
}

Custom classes can be used to make in-depth changes and additions to the way some game objects work. To be valid, they have to extend the base class ( or any subclass of the baseclass ) for whatever they are defining.

Customizable classes

Class Constructor Instanced Notes
io.xol.chunkstories.api.voxel.Voxel Voxel(VoxelDefinition def) Once per voxel type Can use 8 bit of metadata
io.xol.chunkstories.api.item.Item Item(ItemDefinition def) Once per ingame instance Not to be confused with ItemPile
io.xol.chunkstories.api.entity.Entity Entity(EntityDefinition def, World world) Once per ingame instance Takes a World parameter
io.xol.chunkstories.api.particles.ParticleTypeHandler ParticleTypeHandler(ParticleTypeDefinition def) Once per particle type Can provide its own storage structure
io.xol.chunkstories.api.world.generator.WorldGenerator WorldGenerator(WorldGeneratorDefinition def, World world) Once per world -
io.xol.chunkstories.api.net.Packet Packet() Once per packet received/sent Must have a void constructor

Most custom classes ( and the base class they extend for that matter ) have a constructor that takes 'SomethingType' argument. These are all subclasses of the NamedWithProperties class, and thus expose the resolveProperty() method. This allows to have one custom class used by multiple definitions that alter it's behavior by the use of custom parameters: see example below

Other customizable classes

These are classes used by the main customizable classes above: they use these to delegate complexity and/or as data storage mediums

Class Purpose Instanced Notes
io.xol.chunkstories.api.entity.traits.Trait ECS-like system Once per entity using it Not a true ECS, read more
io.xol.chunkstories.api.voxel.components.VoxelComponent Extra info beyond 8 bit metadata Once per voxel that defines some Not an ECS at all, more like freely attached data with an observer pattern
io.xol.chunkstories.api.particles.ParticleTypeHandler.ParticleData Particle data storage Once per particle Inner class of ParticleTypeHandler

Example

import io.xol.chunkstories.api.item.Item;
// [...]

//We extent the 'Item' class
public class ItemFood extends Item {

  private final float calories;

  public ItemFood(ItemType type) {
    super(type);
    //We resolve the property from the ItemType that wraps the definition
    calories = Float.parseFloat(type.resolveProperty("calories", "10.0"));
  }
  
  @Override
  public boolean onControllerInput(Entity owner, ItemPile itemPile, Input input, Controller controller)
  {
    if(owner.getWorld() instanceof WorldMaster)
    {
      if(input.getName().equals("mouse.right") && owner instanceof EntityPlayer)
      {
        if(((EntityPlayer) owner).getFoodLevel() >= 100)
          return true;
        
        ((EntityPlayer)owner).setFoodLevel(((EntityPlayer) owner).getFoodLevel() + calories);
        itemPile.setAmount(itemPile.getAmount() - 1);
        return true;
      }
    }
    
    return false;
  }

}

Notes on custom classes

The class files for a custom class must be inside a jar files within a loaded mod. You can reference classes from other mods, but you cannot reference classes outside of the mods classpath, so if you wanted to modify the core engine itself and link directly to a class inside that, the mod loader simply won't find it. This is for security reasons.

custom_classes.txt · Last modified: 2018/09/10 11:20 by gobrosse