Solved [Citizens2] Trait Help

Discussion in 'Plugin Development' started by Emalton, May 10, 2015.

Thread Status:
Not open for further replies.
  1. Offline

    Emalton

    Hello everyone!
    Can anyone help me spawn a Citizen NPC with the Sentry trait? I want to be able to spawn a NPC with my plugin and have it attack a player. The code that I have so far is below:
    Code:
            NPCRegistry Registry = CitizensAPI.getNPCRegistry();
            NPC DD = Registry.createNPC(EntityType.PLAYER, “Death Dealer”);
            DD.spawn(Bukkit.getServer().getWorld(“DeathDealers”).getSpawnLocation());
            DD.addTrait(sentry); // I’d like to add the trait sentry here.
    Thanks in advance!
    Emalton
     
    Last edited: May 10, 2015
  2. Offline

    xTigerRebornx

    @Emalton You should be able to get it using the TraitFactory class (I believe there is a method that gets the Trait by name), though not sure if Sentry has been updated for Citizens2.
    Its internal name is sentry (case sensitive)
     
  3. Offline

    Emalton

    @xTigerRebornx Thanks for the reply! Once I use TraitFactory, how do I set the NPC Trait?

    EDIT:
    Trying this:
    Code:
    TraitFactory TFactory = null;
    DD.addTrait(TFactory.getTrait("sentry"));
    EDIT2:
    Doesn't work :(
     
    Last edited: May 10, 2015
  4. Offline

    xTigerRebornx

    @Emalton I assume using the addTrait() method in NPC, haven't worked much w/ cititzens
     
  5. Offline

    Emalton

    @xTigerRebornx Yeah it isn't working. :(

    If anyone else has any possible solutions, please let me know!

    Does someone else have something I can try?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 12, 2016
  6. Offline

    Totom3

    @Emalton It's very simple, just do:
    Code:
    npc.addTrait(TraitClass.class);
    If you already have the trait instance (not in your case though), you can also attach it to the NPC with:
    Code:
    npc.addTrait(traitInstance);
    Now specifically for the sentry class, you already know it comes from an add-on to Citizens, not from the API nor the implementation. You could import the said add-on and use the Sentry class directly, but the following should also work just fine, and will not require any additional compile-time dependencies:
    Code:
    // assuming 'sentry' really is the internal name
    Class<? extends Trait> sentryClass = CitizensAPI.getTraitFactory().getTraitClass("sentry");
     
    Emalton likes this.
  7. Offline

    Emalton

    @Totom3 Oh my gosh it works! Thanks a bunch!
     
    Totom3 likes this.
Thread Status:
Not open for further replies.

Share This Page