Skip to content

Guide for old KelpNpc library ( v0.2.0)

PXAV edited this page Feb 14, 2021 · 1 revision

This page will teach you how to create and spawn your own NPCs using Kelp.

Creating a new KelpNpc object

First of all, you have to create a new instance of an NPC.

Tip: NPCs are spawned on the client-side, so it is recommended to create an individual instance for every player seeing the NPC to ensure packets and entity ids remain unique.

Because the constructor for KelpNpcs is not accessible, you have to use the corresponding factory class KelpNpcFactory like this:

public class MyNpcTest {

  private KelpNpcFactory npcFactory;

  @Inject
  public MyNpcTest(KelpNpcFactory npcFactory) {
    this.npcFactory = npcFactory;
    
  }

  public void spawnMyNpc(KelpPlayer player) {
    KelpNpc npc = npcFactory.newKelpNpc();
  }

}

Unlike commands, for example, NPCs are not bound to a class, so you can create NPCs anywhere you want.

Assigning properties to NPCs

You can give your NPC properties by appending a . and a property method. For example:

npc.customName("§bThis is a custom name");

Because property methods return the current NPC object, you can always append a point without repeating the npc.<method> call.

Detailed documentation of property methods can be found in the JavaDocs of the KelpNpc class.

Mandatory properties

  • Giving your NPC a location is mandatory so that the NPC knows where to spawn.
npc.location(new Location(...));

Changing skins

There are multiple ways to set the skin of your NPC.

1. Using GameProfile UUID

One is to set the UUID of your NPC to the UUID of the player whose skin it should take.

KelpNpc npc = npcFactory.newKelpNpc();
npc.uuid("858fdbb6-871c-48fa-b8e8-69bf66c6a102");

You can get the UUID of a player on websites like NameMC or the Mojang API. Note that the Mojang API shortens the UUID by cutting the - (dashes) away, which are needed by Kelp. So when you want to use the API, remember to convert the UUID into the conventional format.

GET https://api.mojang.com/users/profiles/minecraft/<username>

Possible output:

{"name":"pxav","id":"858fdbb6871c48fab8e869bf66c6a102"}

The major drawback of this method is that the player whose UUID you pick can change their skin at any time, so will your NPC skin. So you do not have control over your skin. To avoid this and ensure consistency of your skin, look at the second method.

2. Using skin texture and signature

Every skin has a texture and a signature, which can both be queried via the Mojang API:

GET https://sessionserver.mojang.com/session/minecraft/profile/<shortenedUuid>?unsigned=false

Possible output:

{
  "id" : "858fdbb6871c48fab8e869bf66c6a102",
  "name" : "pxav",
  "properties" : [ {
    "name" : "textures",
    "value" : "ewogICJ0aW1lc3RhbXAiIDogMTU5MjQ4ODg1MDc0NSwKICAicHJvZmlsZUlkIiA6ICI4NThmZGJiNjg3MWM0OGZhYjhlODY5YmY2NmM2YTEwMiIsCiAgInByb2ZpbGVOYW1lIiA6ICJweGF2IiwKICAic2lnbmF0dXJlUmVxdWlyZWQiIDogdHJ1ZSwKICAidGV4dHVyZXMiIDogewogICAgIlNLSU4iIDogewogICAgICAidXJsIiA6ICJodHRwOi8vdGV4dHVyZXMubWluZWNyYWZ0Lm5ldC90ZXh0dXJlL2YyZmVhZThkMzFlYzc0NTQ4MTE5Y2E0NThmNWRhNTc2MzU0ZmE5NzUxYWE0YWU2M2Y1NDYwODJmODA2NTFiMmIiCiAgICB9CiAgfQp9",
    "signature" : "sd/uN32SL73hcW1RN0fRATCLnp8Y4WJlYPwodVI5L1ekmlNIhXVobMkwv423QKz3rPtHdeveyFumZizLqcR66f9CGGqOaF9HDgnTAU/dv0svSVaYfuZjXAP5bFuJ78vpSQMVbsHfS4sJKstEJzIvcWhHoSJzhHVEHCan1ytK8rLRp+SqN/HzUV3ZQp+dRxEZ+hz+CF0RJo38a1asr7MAtNYxriLRgaxVUhf9f5JH3jY1bY2DnFXKdroWWrvlYhXaswK52UUQ8dbaiQj2bS8lj89EW8aXlLl4Vqg5Nm34xVpnIMSAuinnWBn3UK1Dwfi3/aBn55dUteTS4HbQcsK8GmhHTM1UoqL81V8iXSdWm/bAOr0wpXq8xbuIzBaFcwPWOt2jZJCLEROLZCg7IgY8hzbxmFI2UoUzEys3gUigLnOHG3rUhDX/mPAyWwqhTJ+TFcBv9n8sSPTrdiwVZfjJeT1t8HkPMP6CWwY41SadmgtYnrFrtLVA3ZJGO7SafzO5sg1fdBw/pllb8J/zmf6hADiEqappiAi1DSPOrjvYsQM0C9PwA8DGb7Gr85Qfz/8CJe8/ZT6tSZVI+4D9YbflAiHjJWnP5Tn3CnLn/16Ks/VsyD6RUY+tN/A/EkgHNdPNY7jcswKHj+X0W4BmOg7ErPuJLaZ07SblXHXj3JOtxQA="
  } ]
}

The values of "value" (which is equal to texture) and "signature" can be passed to the npc as follows:

KelpNpc npc = npcFactory.newKelpNpc();
npc.skinTexture("yourSkinTexture");
npc.skinSignature("yourSkinSignature");

The advantage of this technique is that your skin remains constant as you can download the skin texture once and save it in a config file for example. This makes you independent of other players changing their skin.

Spawning your NPC

If you have all the mandatory properties set, you can spawn your NPC by simply calling the spawn method. It takes a player as parameter. This player will see the NPC, others not.

KelpNpc npc = npcFactory.newKelpNpc();
    
npc.customName("§bThis is a custom name");
npc.location(new Location(world, x, y, z));
    
npc.spawn(player);

Despawning your NPC

npc.deSpawn(player);

The despawn method takes a player as parameter for whom the NPC will be despawned. This method also removes the title lines of the NPC.

Updating properties

If you have changed any property and want to make it visible to the player, you need to send these changes to them. This is achieved by the refresh method:

npc.refresh(player);
Clone this wiki locally