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' or 'customClass' property :

entity flying_pig
	class: com.example.entity.Pig
	canFly: true
end

Custom classes are … well classes. 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(VoxelType type) Once per voxel definition Can use 8 bit of metadata
io.xol.chunkstories.api.item.Item Item(ItemType type) Once per ingame instance Not to be confused with ItemPile
io.xol.chunkstories.api.entity.EntityBase EntityBase(EntityType type, Location loc) Once per ingame instance Entity.java is just the Interface
io.xol.chunkstories.api.particles.ParticleTypeHandler ParticleTypeHandler(ParticleType type) Once per particle type definition Provides it's own storage structure
io.xol.chunkstories.api.world.generator.WorldGenerator WorldGenerator(WorldGeneratorType type, 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.components.EntitityComponent 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/01/24 15:40 by gobrosse