From 8c656eea6e9f36a8749acf0ec2ab5ac3953ec179 Mon Sep 17 00:00:00 2001 From: MTM123 Date: Thu, 29 Aug 2019 15:04:51 +0300 Subject: [PATCH] Implement event for role changes --- .../event/FPlayerRoleChangeEvent.java | 50 +++++++++++++++++++ .../factions/zcore/persist/MemoryFPlayer.java | 13 ++++- 2 files changed, 62 insertions(+), 1 deletion(-) create mode 100644 src/main/java/com/massivecraft/factions/event/FPlayerRoleChangeEvent.java diff --git a/src/main/java/com/massivecraft/factions/event/FPlayerRoleChangeEvent.java b/src/main/java/com/massivecraft/factions/event/FPlayerRoleChangeEvent.java new file mode 100644 index 00000000..fdbec66a --- /dev/null +++ b/src/main/java/com/massivecraft/factions/event/FPlayerRoleChangeEvent.java @@ -0,0 +1,50 @@ +package com.massivecraft.factions.event; + +import com.massivecraft.factions.FPlayer; +import com.massivecraft.factions.Faction; +import com.massivecraft.factions.struct.Role; +import org.bukkit.event.Cancellable; + +/** + * Represents {@link Role} change of a factions player + * @see FPlayer#getRole() + */ +public class FPlayerRoleChangeEvent extends FactionPlayerEvent implements Cancellable { + + private boolean cancelled; + private final Role from; + private Role to; + + public FPlayerRoleChangeEvent(Faction faction, FPlayer fPlayer, Role from, Role to) { + super(faction, fPlayer); + this.from = from; + this.to = to; + } + + public FPlayerRoleChangeEvent(Faction faction, FPlayer fPlayer, Role to) { + this(faction, fPlayer, fPlayer.getRole(), to); + } + + @Override + public boolean isCancelled() { + return false; + } + + @Override + public void setCancelled(boolean cancelled) { + this.cancelled = cancelled; + } + + public Role getFrom() { + return from; + } + + public Role getTo() { + return to; + } + + public void setTo(Role to) { + this.to = to; + } + +} diff --git a/src/main/java/com/massivecraft/factions/zcore/persist/MemoryFPlayer.java b/src/main/java/com/massivecraft/factions/zcore/persist/MemoryFPlayer.java index e5bcc3de..afa7366d 100644 --- a/src/main/java/com/massivecraft/factions/zcore/persist/MemoryFPlayer.java +++ b/src/main/java/com/massivecraft/factions/zcore/persist/MemoryFPlayer.java @@ -241,7 +241,18 @@ public abstract class MemoryFPlayer implements FPlayer { return this.role; } - public void setRole(Role role) { this.role = role; } + public void setRole(Role role) { + if (this.role == role) { + return; + } + + FPlayerRoleChangeEvent event = new FPlayerRoleChangeEvent(getFaction(), this, role); + Bukkit.getPluginManager().callEvent(event); + + if (!event.isCancelled()) { + this.role = event.getTo(); + } + } public double getPowerBoost() { return this.powerBoost;