Questions regarding Factions API

Discussion in 'Plugin Development' started by Caedus, Jul 1, 2016.

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

    Caedus

    Hi,

    I'm referring to Factions by MassiveCraft here.

    https://www.massivecraft.com/factions-develop
    https://github.com/MassiveCraft/Factions/tree/master/src/com/massivecraft/factions

    My questions are:

    Does anyone know how to get the Player from a Factions Event? I know how to get MPlayer, obviously, but that doesn't return the players name nor their UUID, so i'm curious as to whether I can get the Player from the event. You cannot cast MPlayer to Player.

    Secondly, when the EventFactionsCreate Event is called, it will still be called even if the player doesn't have funds to create a faction (the faction isn't created, but the Event doesn't recognize this). I tried to circumvent this by getting the supposed "Faction" that the player should have and checking if it's null, which would ideally imply that the event was cancelled and no faction was created. Alas, my code to check this doesn't seem to yield results. I've tried a couple of things, most recently this:
    Code:
    @EventHandler
        public void onFactionCreate(EventFactionsCreate e){
    
           
            handleNewFaction(e.getMPlayer().getFaction());
       
           
        }
    
        public void handleNewFaction(Faction f){
           
            Faction ff = f;
           
            if(ff==null)
                Bukkit.broadcastMessage("cancelled");
            else 
                Bukkit.broadcastMessage("not cancelled "+ff.getName()+" / "+f.getName());
        }
    
    With this code, no matter if I successfully create the faction with sufficient money or if it fails because I can't afford it, the same thing gets printed to chat:

    Code:
    not cancelled Wilderness / Wilderness
    I've read through the docs and googled quite a bit, but i'm drawing a blank here.

    Any help is appreciated.
     
  2. Offline

    Esophose

    @Caedus
    1). You can use e.getSender() and that returns a CommandSender which can be casted to a Player if it is an instance of one.
    2). Try checking if the event is cancelled with e.isCancelled(). Maybe that will work?

    Disclaimer: I have no experience with the FactionsAPI and found these using the GitHub page.
     
    Caedus likes this.
  3. Offline

    Caedus

    @Esophose You're a legend yo.

    Your solution to #1 was correct, it works. Cheers :)

    For #2 though, unfortunately isCancelled() doesn't seem to work :(
     
  4. Offline

    Esophose

    @Caedus
    I was second guessing if that would work after I posted it haha.

    Try instead listening to the EventFactionsMembershipChange and using if(e.getReason() == MembershipChangeReason.CREATE) for when the faction is created. That will only be true if the player joined the faction and they are the first member.

    Good luck!
     
    Caedus likes this.
  5. Offline

    Caedus

    @Esophose

    You're actually a genius. I haven't tried it yet, but I will tomorrow. Cheers for the idea! :)

    @Esophose So the aforementioned suggestion worked! Thanks
     
    Last edited: Jul 2, 2016
Thread Status:
Not open for further replies.

Share This Page