Set a Citizens NPC to have no gravity

Discussion in 'Spigot Plugin Development' started by ChickenSaysBak, Jun 29, 2018.

  1. Hi, I've been trying to figure out how to set a Citizens NPC to have no gravity using the API (as you would do /npc gravity) Not sure if it's addTrait, but if it is, how do you even use that method? Thanks!
     
  2. Here is the code that citizens itself uses to toggle an NPC's gravity: https://github.com/CitizensDev/Citi...t/citizensnpcs/commands/NPCCommands.java#L581
    As you can see it references the gravity trait, which is not part of the citizens API (see https://github.com/CitizensDev/Citi...main/java/net/citizensnpcs/trait/Gravity.java). So to do this you could do one of the following things:
    * Depend on Citizens instead of CitizensAPI.
    * Trigger the gravity command, somehow..
    * Implement the trait yourself, somehow..
    * Ask the devs of Citizens to add/expose the gravity trait in their API.
    * Use reflection: You can add the trait if it does not exist yet via
    Code (Text):
    npc.getTrait(CitizensAPI.getTraitFactory().getTraitClass("gravity"));
    And then you would have to use reflection to set the 'gravity' flag inside the trait.. for example by invoking the gravitate(boolean) method via reflection. Something like this:
    Code (Text):
    traitClass.getDeclaredMethod("gravitate", boolean.class).invoke(traitObject, true)
    The mentioned code is completely untested.
     
  3. Thank you! I'll check it out