Package dan200.computercraft.api.detail


package dan200.computercraft.api.detail
The detail system provides a standard way for mods to return descriptions of common game objects, such as blocks or items, as well as registering additional detail to be included in those descriptions.

For instance, the built-in turtle.getItemDetail() method uses in order to provide information about the selected item:


 local item = turtle.getItemDetail(nil, true)
 --[[
 item = {
   name = "minecraft:wheat",
   displayName = "Wheat",
   count = 1,
   maxCount = 64,
   tags = {},
 }
 ]]
 

Built-in detail providers

While you can define your own detail providers (perhaps for types from your own mod), CC comes with several built-in detail registries for vanilla and mod-loader objects:
  • VanillaDetailRegistries, for vanilla objects
  • dan200.computercraft.api.detail.ForgeDetailRegistries for Forge-specific objects
  • dan200.computercraft.api.detail.FabricDetailRegistries for Fabric-specific objects

Example: Returning details from methods

Here we define a getHeldItem() method for pocket computers which finds the currently held item of the player and returns it to the user using VanillaDetailRegistries.ITEM_STACK and DetailRegistry.getDetails(java.lang.Object).
@LuaFunction(mainThread = true)
public final @Nullable Map<String, ?> getHeldItem() {
    if (!(pocket.getEntity() instanceof LivingEntity entity)) return null;

    var heldItem = entity.getItemInHand(InteractionHand.MAIN_HAND);
    return heldItem.isEmpty() ? null : VanillaDetailRegistries.ITEM_STACK.getDetails(heldItem);
}

Example: Registering custom detail registries

Here we define a new detail provider for items that includes the nutrition and saturation values in the returned object.
VanillaDetailRegistries.ITEM_STACK.addProvider((out, stack) -> {
    var food = stack.getItem().getFoodProperties();
    if (food == null) return;

    out.put("saturation", food.getSaturationModifier());
    out.put("nutrition", food.getNutrition());
});