| name | hytale-spawning-npcs |
| description | Spawns NPCs in Hytale plugins using the NPCPlugin helper. Use when spawning NPCs, equipping NPC inventory, setting NPC armor, creating NPC commands, or working with INonPlayerCharacter. Triggers - NPC, spawn NPC, NPCPlugin, INonPlayerCharacter, NPCEntity, NPC inventory, NPC armor, Kweebec, spawnNPC, NPC command. |
Hytale Spawning NPCs Skill
Use this skill when spawning Non-Player Characters (NPCs) using the NPCPlugin helper. This is the convenient alternative to manually spawning entities with Holder<EntityStore>.
Prerequisite: Familiarity with the Entity Component System (ECS). See the hytale-ecs skill.
Quick Reference
| Concept | Description |
|---|
| NPCPlugin | Helper class that simplifies NPC spawning |
| INonPlayerCharacter | Interface providing NPC-specific methods |
| NPCEntity | Component for accessing NPC inventory and settings |
| Ref<EntityStore> | ECS reference to the spawned NPC entity |
| InventoryHelper | Hytale API utility for equipping armor |
| Inventory | Object for managing NPC hotbar, armor, and items |
Required Imports
import com.example.npc.NPCPlugin;
import hytale.server.plugin.npc.INonPlayerCharacter;
import hytale.server.plugin.npc.NPCEntity;
import com.hypixel.hytale.server.core.universe.PlayerRef;
import com.hypixel.hytale.server.core.universe.Universe;
import com.hypixel.hytale.server.core.universe.world.World;
import com.hypixel.hytale.server.core.universe.world.storage.EntityStore;
import com.hypixel.hytale.component.Ref;
import com.hypixel.hytale.component.Store;
import com.hypixel.hytale.math.vector.Vector3d;
import com.hypixel.hytale.math.vector.Vector3f;
import com.hypixel.hytale.server.core.inventory.Inventory;
import com.hypixel.hytale.server.core.inventory.InventoryHelper;
import com.hypixel.hytale.server.core.inventory.ItemStack;
import com.hypixel.hytale.server.core.command.system.CommandContext;
import org.apache.commons.lang3.tuple.Pair;
import java.util.Objects;
Step-by-Step Process
1. Spawn the NPC
Use NPCPlugin.get().spawnNPC(...) to create the entity, assign its model, and position it in the world.
Pair<Ref<EntityStore>, INonPlayerCharacter> result = NPCPlugin.get().spawnNPC(
store,
"Kweebec_Sapling",
null,
position,
rotation
);
| Parameter | Type | Description |
|---|
store | Store<EntityStore> | The entity store where the NPC will exist |
entityKey | String | The key/name of the entity model/type (e.g., "Kweebec_Sapling") |
config | Object | Optional configuration, pass null for defaults |
position | Vector3d | World position to spawn the NPC |
rotation | Vector3f | Facing direction of the NPC |
2. Handle the Result
The method returns a Pair. Always check for null to confirm the spawn succeeded.
if (result != null) {
Ref<EntityStore> npcRef = result.first();
INonPlayerCharacter npc = result.second();
setupNPCInventory(npcRef, store);
}
| Return | Type | Description |
|---|
result.first() | Ref<EntityStore> | ECS reference to add components or modify the entity |
result.second() | INonPlayerCharacter | NPC-specific methods interface |
3. Access the NPC Inventory
Retrieve the NPCEntity component to access inventory settings:
NPCEntity npcComponent = store.getComponent(
npcRef,
Objects.requireNonNull(NPCEntity.getComponentType())
);
npcComponent.setInventorySize(3, 9, 0);
4. Add Items and Armor
Use the Inventory object to equip items and armor:
Inventory inventory = npcComponent.getInventory();
inventory.getHotbar().addItemStackToSlot((short) 0, new ItemStack("Weapon_Mace_Thorium", 1));
InventoryHelper.useArmor(inventory.getArmor(), "Armor_Thorium_Head");
inventory.setActiveHotbarSlot((byte) 0);
Complete Example - NPC Spawn Command
import com.example.npc.NPCPlugin;
import hytale.server.plugin.npc.INonPlayerCharacter;
import hytale.server.plugin.npc.NPCEntity;
import com.hypixel.hytale.component.Ref;
import com.hypixel.hytale.component.Store;
import com.hypixel.hytale.math.vector.Vector3d;
import com.hypixel.hytale.math.vector.Vector3f;
import com.hypixel.hytale.server.core.command.system.CommandContext;
import com.hypixel.hytale.server.core.inventory.Inventory;
import com.hypixel.hytale.server.core.inventory.InventoryHelper;
import com.hypixel.hytale.server.core.inventory.ItemStack;
import com.hypixel.hytale.server.core.universe.PlayerRef;
import com.hypixel.hytale.server.core.universe.world.World;
import com.hypixel.hytale.server.core.universe.world.storage.EntityStore;
import org.apache.commons.lang3.tuple.Pair;
import javax.annotation.Nonnull;
import java.util.Objects;
public class SpawnNpcCommand extends AbstractPlayerCommand {
public SpawnNpcCommand() {
super("npc", "spawn npc");
}
@Override
protected void execute(
@Nonnull CommandContext commandContext,
@Nonnull Store<EntityStore> store,
@Nonnull Ref<EntityStore> ref,
@Nonnull PlayerRef playerRef,
@Nonnull World world) {
Vector3d position = playerRef.getTransform().getPosition();
Vector3f rotation = new Vector3f(0, 0, 0);
Pair<Ref<EntityStore>, INonPlayerCharacter> result = NPCPlugin.get().spawnNPC(
store, "Kweebec_Sapling", null, position, rotation);
if (result != null) {
Ref<EntityStore> npcRef = result.first();
INonPlayerCharacter npc = result.second();
setupNPCInventory(npcRef, store);
}
}
public void setupNPCInventory(Ref<EntityStore> npcRef, Store<EntityStore> store) {
NPCEntity npcComponent = store.getComponent(
npcRef, Objects.requireNonNull(NPCEntity.getComponentType()));
if (npcComponent == null)
return;
npcComponent.setInventorySize(3, 9, 0);
addItemsToNPCInventory(npcComponent.getInventory());
}
public void addItemsToNPCInventory(Inventory inventory) {
inventory.getHotbar().addItemStackToSlot((short) 0, new ItemStack("Weapon_Mace_Thorium", 1));
InventoryHelper.useArmor(inventory.getArmor(), "Armor_Thorium_Head");
inventory.setActiveHotbarSlot((byte) 0);
}
}
Registering the Command
Register the command in your main plugin class:
public class MyHytaleMod extends JavaPlugin {
@Override
protected void setup() {
this.getCommandRegistry().registerCommand(new SpawnNpcCommand());
}
}
Key Points
- NPCPlugin simplifies spawning: Abstracts Holder boilerplate — creates the entity, attaches the model, and positions it automatically.
- Always null-check the result:
spawnNPC() can return null if spawning fails.
- Initialize inventory before adding items: Call
npcComponent.setInventorySize(rows, cols, offset) before accessing the inventory.
- InventoryHelper for armor: Use
InventoryHelper.useArmor() — a Hytale API utility — to equip armor pieces.
- Entity key is the model/type name: Pass the NPC type key (e.g.,
"Kweebec_Sapling") matching assets in the server data.
- ECS reference for further modification: Use
result.first() (Ref<EntityStore>) to add/remove components on the NPC after spawning.
Troubleshooting
| Issue | Solution |
|---|
spawnNPC() returns null | Verify the entity key (e.g., "Kweebec_Sapling") matches a valid entity type |
| NPC has no items | Ensure setInventorySize() is called before adding items |
| NPC not visible | Confirm the spawn position is within a loaded chunk |
NPCEntity.getComponentType() returns null | Verify NPCPlugin dependency is loaded and entity module is available |
| NPC facing wrong direction | Adjust the Vector3f rotation values |
Related Skills
hytale-ecs — Entity Component System patterns
hytale-items — Item registry and ItemStack usage
hytale-commands — Command creation patterns
Reference