en
Switch Language
  1. PowerNukkitX
  2. Get Started
  3. Config

    1. server.properties
    2. nukkit.yml
    3. pnx-cli-config.ini
    4. Anti-xray
    5. Resource Encryption
    6. Compression Acceleration
    7. PNX with WaterDogPE
    8. Flat World
  4. FAQ

    1. To Newcomers
    2. How to use PNX-CLI
    3. Common Questions
    4. Terra Questions
    5. Spawn-point Questions
    6. Convert Worlds
  5. For Devs

    1. Java

      1. Goto JavaDoc
      2. Mod API

        1. Custom Block
        2. Custom Item
        3. Custom Entity
        4. Custom Enchantment
      3. Entity AI

        1. Behavior
        2. Memory
        3. Sensor
        4. Motion Controller
        5. Navigator
        6. Behavior Group
        7. Work Cycle
    2. JavaScript

      1. setup dev env
      2. Base LLSELib dev
      3. Hello World
      4. Basic knowledge
      5. Event Listener
      6. FAQ
    3. Resources

      1. Event List
      2. IDs(block & item)

2. API for custom items

author: Verox
API related to custom items is unified in the package cn.nukkit.item.customitem
To implement a custom item, you need to implement the CustomItem interface, or you can implement it by extending some basic custom item class that has already implemented the interface internally (Like ItemCustomTool or ItemCustom).

Basic custom item

You can implement a normal custom item by extending the ItemCustom class. You must override the getDefinition method and create a parameterless constructor, call the parent class constructor and pass in: String id id is the identifier of the custom item, which must be filled in, such as pnx:test String name name is the name displayed by the custom item, which can be left null. When it is null, the custom item will read the multilingual text from the language file in the texts folder of the resource pack, such as item.pnx:test=Test Item String textureName textureName is the texture path of the custom item, which must be filled in. You need to fill in according to the corresponding texture definition of textures\item_texture.json in the resource pack

Example:

public class Test extends ItemCustom {
    public Test() {
        super("pnx:test", "Test 1", "test");
    }
    public CustomItemDefinition getDefinition() {
        return CustomItemDefinition.simpleBuilder(this, ItemCreativeCategory.EQUIPMENT).build();
    }
}

Basic custom tool

You can implement a custom tool by extending the ItemCustomTool class. The implementation method is similar to the above. Note that CustomItemDefinition needs to use toolBuilder instead of simpleBuilder because it has a special interface.

Example:

public class MySword extends ItemCustomTool {
    public MySword() {
        super("powernukkitx:test_sward", "test sward", "test_sward");
    }
    public CustomItemDefinition getDefinition() {
        return CustomItemDefinition
                .toolBuilder(this, ItemCreativeCategory.EQUIPMENT)
                .allowOffHand(true) // Set to true to make the item be able to be held in the off hand
                .handEquipped(true) // Set to true to make the item be able to be held in the hand
                .foil(true) // Set to true to make the item shiny
                .build();
    }
    public int getMaxDurability() {
        return 1000;
    }
    public int getTier() {
        return ItemSwordDiamond.TIER_DIAMOND;
    }
    public int getAttackDamage() {
        return 30;
    }
    public int getEnchantAbility() {
        return 20;
    }
    public boolean isSword() {
        return true;
    }
}

Basic custom armor

You can implement a custom armor by extending the ItemCustomArmor class. The implementation method is similar to the above. Note that CustomItemDefinition needs to use armorBuilder instead of simpleBuilder because it has a special interface. At the same time, the custom armor needs to specify Attachables in the texture pack. Example:

public class MyArmor extends ItemCustomArmor {
    public MyArmor() {
        super("powernukkitx:pnx_armor", "pnx armor", "pnx_armor");
    }
    public CustomItemDefinition getDefinition() {
        return CustomItemDefinition
                .armorBuilder(this, ItemCreativeCategory.EQUIPMENT)
                .allowOffHand(true)
                .handEquipped(true)
                .build();
    }
    public boolean isChestplate() {
        return true;
    }
    public int getTier() {
        return ItemArmor.TIER_DIAMOND;
    }
    public int getMaxDurability() {
        return 666;
    }
    public int getEnchantAbility() {
        return 10;
    }
    public int getArmorPoints() {//盔甲防御力
        return 100;
    }
}

Basic custom edible item

You can implement a custom edible item by extending the ItemCustomEdible class. The implementation method is similar to the above. Note that CustomItemDefinition needs to use edibleBuilder instead of simpleBuilder because it has a special interface. At the same time, ItemCustomEdible must implement the method public abstract Map.Entry<Plugin, Food> getFood(); to define the food attributes for the server to register food Food Always make sure that the provided plugin is the plugin that registers the food. Otherwise, exception may occur during runtime.

Example:

public class MyApple extends ItemCustomEdible {
    public MyApple() {
        super("powernukkitx:pnx_apple", "pnx apple", "apple");
    }
    public Map.Entry<Plugin, Food> getFood() {
        return Map.entry(MainPlugin.INSTANCE, new FoodMilk());
    }
    public CustomItemDefinition getDefinition() {
        return CustomItemDefinition.edibleBuilder(this, ItemCreativeCategory.ITEMS).build();
    }
}

Custom item with custom behavior

You can implement a custom item with custom behavior by extending the Item class. The implementation method is similar to the above. Note that CustomItemDefinition needs to use customBuilder instead of simpleBuilder because it has a special interface. At the same time, the custom item needs to implement the CustomItem interface.

Example:

public class MyCustomItem extends Item implements CustomItem {
    /**
     * Argument 1: Item ID is 255, that is, the string item ID
     * Argument 2: Item meta is 0 by default
     * Argument 3: Item count is 1 by default
     * Argument 4: Item name
     */
    public MyCustomItem() {
        super(ItemID.STRING_IDENTIFIED_ITEM, 0, 1, "test");
    }
    public String getTextureName() {
        return "test";
    }
    public String getNamespaceId() {
        return "pnx:test";
    }
    public CustomItemDefinition getDefinition() {
        return CustomItemDefinition.customBuilder(this,ItemCreativeCategory.EQUIPMENT).build();
    }
}

CustomItemDefinition related API see JavaDoc


© PowerNukkitX Dev team