Solved [FactionsUUID] PowerChangeEvent ?

Discussion in 'Spigot Plugin Development' started by Alex0589, Jul 27, 2018.

  1. So I'm developing a plugin for FactionsUUID, but I noticed that there's only the PowerLossEvent and not the PowerChangeEvent like in massive core. I need an event that gets called when a faction gets power
  2. If you look at factionsuuid code, factions do not gain power. In fact, player doesnt gain power each minute it is like a "mirror"
    Players power is only updated when the power is needed to make an operation such as get Player power, login, logout, get faction power and so on...
    This basically means that since there is no faction gain power event you you instead use player command preprocess event and listen to every action that makes a power change to a player.
    You will have to look into factions code in order to achive this.
  3. Is this the only way? Because it looks a little bit strange as every faction plugin has an event for this (es. MassiveCore)
  4. It is. If you look into the code you will see that when you call fplayer.getPower(); it calls to updatePower();
    When you call faction.getPower(); it does for every faction member fplayer.getPower();

    So you will have to listen to every aspect of when update power is called looking at the code.

    FactionsUUID code sucks a little bit.
    It has parts with good code and others that sucks hard.
    In fact power part is a tricky one cause it is actually a good idea to only update power when it is needed taking the time past since Last update but also is not really the best.
    I kinda understand why is done this way, to avoid lag and all of that...
    There is tons of code that should be improved.
  5. Don't really know if it's a good idea but I can put in the onEnable a method that saves the faction id and it's power into an HashMap and than start an Async task which checks every second if the faction power has changed, if yes the HashMap is udated and a custom event is called (PowerChangeEvent):

    Code (Text):
       private void savePower(){
            Factions.getInstance().getAllFactions().forEach(faction -> {
                factions.put(faction.getId(), faction.getPowerRounded());
    Code (Text):
     private void updatePower(){
            new BukkitRunnable(){

                public void run() {
                    factions.keySet().forEach( temp -> {
                        Faction faction = Factions.getInstance().getFactionById(temp);

                        int current = faction.getPowerRounded();
                        int saved = factions.get(temp);

                        if(current != saved){
                            factions.put(temp, current);
                            Bukkit.getPluginManager().callEvent(new PowerChangeEvent(faction, current, saved));

    Code (Text):
    package listeners.custom;

    import com.massivecraft.factions.Faction;
    import org.bukkit.event.Cancellable;
    import org.bukkit.event.Event;
    import org.bukkit.event.HandlerList;

    public class PowerChangeEvent extends Event implements Cancellable {

        private static final HandlerList HANDLERS = new HandlerList();
        private boolean cancelled;

        private Faction faction;
        private int updatedPower;
        private int oldPower;

        public PowerChangeEvent(Faction faction, int updatedPower,int oldPower) {
            this.faction = faction;
            this.updatedPower = updatedPower;
            this.oldPower = oldPower;
            this.cancelled = false;

        public HandlerList getHandlers() {
            return HANDLERS;

        public static HandlerList getHandlerList() {
            return HANDLERS;

        public boolean isCancelled() {
            return cancelled;

        public void setCancelled(boolean cancelled) {
            this.cancelled = cancelled;

        public Faction getFaction() {
            return faction;

        public int getUpdatedPower() {
            return updatedPower;

        public int getOldPower() {
            return oldPower;
    Maybe the timer isn't the best thing but this is the base.
  6. Not a good idea since I told you that power is updated per System time passed.
    Player power is updated even if less than a minute has passed.
    This will end up causing problems since every time you check the power condition change will be true. In fact, since getpowerRoundeeƱd calls to faction.getPower which calls fplayer.getPower the sum of the floating numbers of power among all the faction members will give you maybe an extra 1 power and making the condition become true. Also doing this... The delay is kinda too fast it will cause some lag
  7. I changed the delay to make it better and it works!
  8. Ofc it works cause what you are actually doing is update de power at that time xD.
    If that's what you needed its Okey but keep in mind that.
    Glad you made it work