Skip to content

Custom Entry

fzzyhmstrs edited this page Nov 18, 2024 · 1 revision

Links a code-defined CustomLootableEntry to a data-driven Lootable pool/table. These entries can be used when a pre-defined entry type doesn't exist, and creating an entirely new entry type isn't desirable or feasible.

When possible use one of the pre-defined types first; listed in Home

Creating a CustomLootableEntry

In order for the Lootable pool entry to work, there has to be a corresponding custom entry defined and registered in-code.

The process is simple

  1. Define a class that implements CustomLootableEntry with your desired functionality
  2. Define a CustomLootableEntryDisplay for use by the client in creating choice tiles. If you are using your pool only for randomly provided loot, this display can return empty lists and/or description.
  3. Register these classes using LootablesApi
  4. Link to one or more tables using this pool entry type.

Example CustomLootableEntry

A simple entry implementation that summons a cow at the player's location.

//kotlin
object CowCustomEntry: CustomLootableEntry {
    
    override fun apply(player: ServerPlayerEntity, origin: Vec3d) {
        val cow = EntityType.COW.create(player.world)
        cow.setPosition(origin.x, origin.y, origin.z)
        player.world.spawnEntity(cow)
    }
    
    /*
    override fun serverDescription(playerEntity: ServerPlayerEntity): Text? {
        // If you need server context or entry context to make your description, you can do that here
        // otherwise clientDescription in the display should be used
    }
    */
    
}
//kotlin
final class CowCustomEntry implements CustomLootableEntry {
    
	public CowCustomEntry{}
	
    @Override
    public void apply(ServerPlayerEntity player, Vec3d origin) {
        val cow = EntityType.COW.create(player.world);
		cow.setPosition(origin.x, origin.y, origin.z);
        player.world.spawnEntity(cow);
    }
    
    /*
    @Override
    @Nullable
    public Text serverDescription(ServerPlayerEntity playerEntity) {
        // If you need server context or entry context to make your description, you can do that here
        // otherwise clientDescription in the display should be used
    }
    */
    
}

Example CustomLootableEntryDisplay

Following from our CowCustomEntry above, the display that would go along with would look like

object CowCustomEntryDisplay: CustomLootableEntryDisplay {
    
    //you probably would have at least one actual TileIcon, but examples of those can be found elsewhere
    override fun provideIcons(): List<TileIcon> {
        return listOf()
    }
    
    //since we don't need any particular server state here, providing the description on the client is better
    override fun clientDescription(): Text {
        return Text.translatable("my_mod.custom.cow")
    }
}
final class CowCustomEntryDisplay implements CustomLootableEntryDisplay {
    
	public CowCustomEntryDisplay {}
	
    //you probably would have at least one actual TileIcon, but examples of those can be found elsewhere
    @Override
	public List<TileIcon> provideIcons()  {
        return List.of();
    }
    
    //since we don't need any particular server state here, providing the description on the client is better
    @Override
    @Nullable
    public Text clientDescription() {
        return Text.translatable("my_mod.custom.cow");
    }
}

Registering CowEntry

Now that we have our entry and display, lets register them. This should be done in Common code, both the server and client need to know at least some of this information.

LootablesApi.registerCustomEntry(Identifier.of("my_mod", "cow_entry"), CowCustomEntry, CowCustomEntryDisplay)
LootablesApi.registerCustomEntry(Identifier.of("my_mod", "cow_entry"), new CowCustomEntry(), new CowCustomEntryDisplay());

Adding to a Table

See the JSON definition, Lootable Table, Lootable Pool, and examples below for how to link your new custom entry to a table.

JSON Definition

Key Type Value Example Note
"type" String
"lootables:custom"
"id" String
"my_mod:cow_entry"         
Must match to a registered custom entry

Examples

Link your custom entry up to a lootable pool

{
  "type": "lootables:custom",
  "id": "my_mod:cow_entry"
}

Clone this wiki locally