Added Event System

This commit is contained in:
patrickfreed 2012-03-01 20:16:45 -05:00
parent 54b97651b6
commit 021bf52c62
14 changed files with 458 additions and 6 deletions

View File

@ -9,11 +9,13 @@ import java.util.Map;
import java.util.Map.Entry; import java.util.Map.Entry;
import java.util.TreeMap; import java.util.TreeMap;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.block.Block; import org.bukkit.block.Block;
import com.google.gson.reflect.TypeToken; import com.google.gson.reflect.TypeToken;
import com.massivecraft.factions.event.LandUnclaimEvent;
import com.massivecraft.factions.integration.LWCFeatures; import com.massivecraft.factions.integration.LWCFeatures;
import com.massivecraft.factions.iface.RelationParticipator; import com.massivecraft.factions.iface.RelationParticipator;
import com.massivecraft.factions.util.AsciiCompass; import com.massivecraft.factions.util.AsciiCompass;
@ -82,10 +84,15 @@ public class Board
Entry<FLocation, String> entry = iter.next(); Entry<FLocation, String> entry = iter.next();
if (entry.getValue().equals(factionId)) if (entry.getValue().equals(factionId))
{ {
if(Conf.onUnclaimResetLwcLocks && LWCFeatures.getEnabled()) LandUnclaimEvent unclaimEvent = new LandUnclaimEvent(entry.getKey(), entry.getValue());
LWCFeatures.clearAllChests(entry.getKey()); Bukkit.getServer().getPluginManager().callEvent(unclaimEvent);
if(!unclaimEvent.isCancelled())
{
if(Conf.onUnclaimResetLwcLocks && LWCFeatures.getEnabled())
LWCFeatures.clearAllChests(entry.getKey());
iter.remove(); iter.remove();
}
} }
} }
} }

View File

@ -3,10 +3,13 @@ package com.massivecraft.factions;
import java.util.HashSet; import java.util.HashSet;
import java.util.Set; import java.util.Set;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import com.massivecraft.factions.event.FPlayerLeaveEvent;
import com.massivecraft.factions.event.LandClaimEvent;
import com.massivecraft.factions.iface.EconomyParticipator; import com.massivecraft.factions.iface.EconomyParticipator;
import com.massivecraft.factions.iface.RelationParticipator; import com.massivecraft.factions.iface.RelationParticipator;
import com.massivecraft.factions.integration.Econ; import com.massivecraft.factions.integration.Econ;
@ -508,6 +511,10 @@ public class FPlayer extends PlayerEntity implements EconomyParticipator
if ( ! Econ.modifyMoney(this, -cost, "to leave your faction.", "for leaving your faction.")) return; if ( ! Econ.modifyMoney(this, -cost, "to leave your faction.", "for leaving your faction.")) return;
} }
FPlayerLeaveEvent leaveEvent = new FPlayerLeaveEvent(this,myFaction,FPlayerLeaveEvent.PlayerLeaveReason.LEAVE);
Bukkit.getServer().getPluginManager().callEvent(leaveEvent);
if (leaveEvent.isCancelled()) return;
// Am I the last one in the faction? // Am I the last one in the faction?
if (myFaction.getFPlayers().size() == 1) if (myFaction.getFPlayers().size() == 1)
{ {
@ -657,6 +664,10 @@ public class FPlayer extends PlayerEntity implements EconomyParticipator
} }
} }
LandClaimEvent claimEvent = new LandClaimEvent(flocation, forFaction.getId(), this.getId());
Bukkit.getServer().getPluginManager().callEvent(claimEvent);
if(claimEvent.isCancelled()) return false;
if (LWCFeatures.getEnabled() && forFaction.isNormal() && Conf.onCaptureResetLwcLocks) if (LWCFeatures.getEnabled() && forFaction.isNormal() && Conf.onCaptureResetLwcLocks)
LWCFeatures.clearOtherChests(flocation, this.getFaction()); LWCFeatures.clearOtherChests(flocation, this.getFaction());

View File

@ -2,12 +2,16 @@ package com.massivecraft.factions.cmd;
import java.util.ArrayList; import java.util.ArrayList;
import org.bukkit.Bukkit;
import com.massivecraft.factions.Conf; import com.massivecraft.factions.Conf;
import com.massivecraft.factions.FPlayer; import com.massivecraft.factions.FPlayer;
import com.massivecraft.factions.FPlayers; import com.massivecraft.factions.FPlayers;
import com.massivecraft.factions.Faction; import com.massivecraft.factions.Faction;
import com.massivecraft.factions.Factions; import com.massivecraft.factions.Factions;
import com.massivecraft.factions.P; import com.massivecraft.factions.P;
import com.massivecraft.factions.event.FPlayerJoinEvent;
import com.massivecraft.factions.event.FactionCreateEvent;
import com.massivecraft.factions.struct.Permission; import com.massivecraft.factions.struct.Permission;
import com.massivecraft.factions.struct.Rel; import com.massivecraft.factions.struct.Rel;
@ -54,6 +58,11 @@ public class CmdCreate extends FCommand
return; return;
} }
FactionCreateEvent createEvent = new FactionCreateEvent(tag, me);
Bukkit.getServer().getPluginManager().callEvent(createEvent);
if(createEvent.isCancelled()) return;
// if economy is enabled, they're not on the bypass list, and this command has a cost set, make 'em pay // if economy is enabled, they're not on the bypass list, and this command has a cost set, make 'em pay
if ( ! payForCommand(Conf.econCostCreate, "to create a new faction", "for creating a new faction")) return; if ( ! payForCommand(Conf.econCostCreate, "to create a new faction", "for creating a new faction")) return;
@ -66,6 +75,9 @@ public class CmdCreate extends FCommand
return; return;
} }
FPlayerJoinEvent joinEvent = new FPlayerJoinEvent(FPlayers.i.get(me),faction,FPlayerJoinEvent.PlayerJoinReason.CREATE);
Bukkit.getServer().getPluginManager().callEvent(joinEvent);
faction.setTag(tag); faction.setTag(tag);
fme.setRole(Rel.LEADER); fme.setRole(Rel.LEADER);
fme.setFaction(faction); fme.setFaction(faction);

View File

@ -1,6 +1,10 @@
package com.massivecraft.factions.cmd; package com.massivecraft.factions.cmd;
import org.bukkit.Bukkit;
import com.massivecraft.factions.Conf; import com.massivecraft.factions.Conf;
import com.massivecraft.factions.event.FPlayerLeaveEvent;
import com.massivecraft.factions.event.FactionDisbandEvent;
import com.massivecraft.factions.integration.Econ; import com.massivecraft.factions.integration.Econ;
import com.massivecraft.factions.FPlayers; import com.massivecraft.factions.FPlayers;
import com.massivecraft.factions.Faction; import com.massivecraft.factions.Faction;
@ -45,12 +49,17 @@ public class CmdDisband extends FCommand
return; return;
} }
FactionDisbandEvent disbandEvent = new FactionDisbandEvent(me, faction.getId());
Bukkit.getServer().getPluginManager().callEvent(disbandEvent);
if(disbandEvent.isCancelled()) return;
// Inform all players // Inform all players
for (FPlayer fplayer : FPlayers.i.getOnline()) for (FPlayer fplayer : FPlayers.i.getOnline())
{ {
String who = senderIsConsole ? "A server admin" : fme.describeTo(fplayer); String who = senderIsConsole ? "A server admin" : fme.describeTo(fplayer);
if (fplayer.getFaction() == faction) if (fplayer.getFaction() == faction)
{ {
Bukkit.getServer().getPluginManager().callEvent(new FPlayerLeaveEvent(fplayer,faction, FPlayerLeaveEvent.PlayerLeaveReason.DISBAND));
fplayer.msg("<h>%s<i> disbanded your faction.", who); fplayer.msg("<h>%s<i> disbanded your faction.", who);
} }
else else

View File

@ -1,10 +1,16 @@
package com.massivecraft.factions.cmd; package com.massivecraft.factions.cmd;
import org.bukkit.Bukkit;
import com.massivecraft.factions.Conf; import com.massivecraft.factions.Conf;
import com.massivecraft.factions.FPlayers;
import com.massivecraft.factions.Faction; import com.massivecraft.factions.Faction;
import com.massivecraft.factions.FPlayer; import com.massivecraft.factions.FPlayer;
import com.massivecraft.factions.P; import com.massivecraft.factions.P;
import com.massivecraft.factions.event.FPlayerJoinEvent;
import com.massivecraft.factions.event.FPlayerLeaveEvent;
import com.massivecraft.factions.struct.Permission; import com.massivecraft.factions.struct.Permission;
import com.massivecraft.factions.zcore.persist.EM;
public class CmdJoin extends FCommand public class CmdJoin extends FCommand
{ {
@ -76,6 +82,18 @@ public class CmdJoin extends FCommand
if (samePlayer && ! payForCommand(Conf.econCostJoin, "to join a faction", "for joining a faction")) return; if (samePlayer && ! payForCommand(Conf.econCostJoin, "to join a faction", "for joining a faction")) return;
fme.msg("<i>%s successfully joined %s.", fplayer.describeTo(fme, true), faction.getTag(fme)); fme.msg("<i>%s successfully joined %s.", fplayer.describeTo(fme, true), faction.getTag(fme));
if(!EM.detached(myFaction))
{
FPlayerLeaveEvent leaveEvent = new FPlayerLeaveEvent(FPlayers.i.get(me),faction,FPlayerLeaveEvent.PlayerLeaveReason.JOINOTHER);
Bukkit.getServer().getPluginManager().callEvent(leaveEvent);
if (leaveEvent.isCancelled()) return;
}
FPlayerJoinEvent joinEvent = new FPlayerJoinEvent(FPlayers.i.get(me),faction,FPlayerJoinEvent.PlayerJoinReason.COMMAND);
Bukkit.getServer().getPluginManager().callEvent(joinEvent);
if (joinEvent.isCancelled()) return;
if (!samePlayer) if (!samePlayer)
fplayer.msg("<i>%s moved you into the faction %s.", fme.describeTo(fplayer, true), faction.getTag(fplayer)); fplayer.msg("<i>%s moved you into the faction %s.", fme.describeTo(fplayer, true), faction.getTag(fplayer));
faction.msg("<i>%s joined your faction.", fplayer.describeTo(faction, true)); faction.msg("<i>%s joined your faction.", fplayer.describeTo(faction, true));

View File

@ -1,9 +1,12 @@
package com.massivecraft.factions.cmd; package com.massivecraft.factions.cmd;
import org.bukkit.Bukkit;
import com.massivecraft.factions.Conf; import com.massivecraft.factions.Conf;
import com.massivecraft.factions.FPlayer; import com.massivecraft.factions.FPlayer;
import com.massivecraft.factions.Faction; import com.massivecraft.factions.Faction;
import com.massivecraft.factions.P; import com.massivecraft.factions.P;
import com.massivecraft.factions.event.FPlayerLeaveEvent;
import com.massivecraft.factions.struct.FPerm; import com.massivecraft.factions.struct.FPerm;
import com.massivecraft.factions.struct.Permission; import com.massivecraft.factions.struct.Permission;
import com.massivecraft.factions.struct.Rel; import com.massivecraft.factions.struct.Rel;
@ -60,6 +63,10 @@ public class CmdKick extends FCommand
// if economy is enabled, they're not on the bypass list, and this command has a cost set, make 'em pay // if economy is enabled, they're not on the bypass list, and this command has a cost set, make 'em pay
if ( ! payForCommand(Conf.econCostKick, "to kick someone from the faction", "for kicking someone from the faction")) return; if ( ! payForCommand(Conf.econCostKick, "to kick someone from the faction", "for kicking someone from the faction")) return;
FPlayerLeaveEvent event = new FPlayerLeaveEvent(you, you.getFaction(), FPlayerLeaveEvent.PlayerLeaveReason.KICKED);
Bukkit.getServer().getPluginManager().callEvent(event);
if (event.isCancelled()) return;
yourFaction.msg("%s<i> kicked %s<i> from the faction! :O", fme.describeTo(yourFaction, true), you.describeTo(yourFaction, true)); yourFaction.msg("%s<i> kicked %s<i> from the faction! :O", fme.describeTo(yourFaction, true), you.describeTo(yourFaction, true));
you.msg("%s<i> kicked you from %s<i>! :O", fme.describeTo(you, true), yourFaction.describeTo(you)); you.msg("%s<i> kicked you from %s<i>! :O", fme.describeTo(you, true), yourFaction.describeTo(you));
if (yourFaction != myFaction) if (yourFaction != myFaction)

View File

@ -1,8 +1,11 @@
package com.massivecraft.factions.cmd; package com.massivecraft.factions.cmd;
import org.bukkit.Bukkit;
import com.massivecraft.factions.FPlayer; import com.massivecraft.factions.FPlayer;
import com.massivecraft.factions.FPlayers; import com.massivecraft.factions.FPlayers;
import com.massivecraft.factions.Faction; import com.massivecraft.factions.Faction;
import com.massivecraft.factions.event.FPlayerJoinEvent;
import com.massivecraft.factions.struct.Permission; import com.massivecraft.factions.struct.Permission;
import com.massivecraft.factions.struct.Rel; import com.massivecraft.factions.struct.Rel;
import com.massivecraft.factions.util.RelationUtil; import com.massivecraft.factions.util.RelationUtil;
@ -64,6 +67,10 @@ public class CmdLeader extends FCommand
} }
} }
FPlayerJoinEvent event = new FPlayerJoinEvent(FPlayers.i.get(me),targetFaction,FPlayerJoinEvent.PlayerJoinReason.LEADER);
Bukkit.getServer().getPluginManager().callEvent(event);
if (event.isCancelled()) return;
// if target player is currently leader, demote and replace him // if target player is currently leader, demote and replace him
if (targetFactionCurrentLeader == newLeader) if (targetFactionCurrentLeader == newLeader)
{ {

View File

@ -1,7 +1,10 @@
package com.massivecraft.factions.cmd; package com.massivecraft.factions.cmd;
import org.bukkit.Bukkit;
import com.massivecraft.factions.Board; import com.massivecraft.factions.Board;
import com.massivecraft.factions.Conf; import com.massivecraft.factions.Conf;
import com.massivecraft.factions.event.LandUnclaimEvent;
import com.massivecraft.factions.integration.Econ; import com.massivecraft.factions.integration.Econ;
import com.massivecraft.factions.integration.SpoutFeatures; import com.massivecraft.factions.integration.SpoutFeatures;
import com.massivecraft.factions.FLocation; import com.massivecraft.factions.FLocation;
@ -52,6 +55,10 @@ public class CmdUnclaim extends FCommand
} }
} }
LandUnclaimEvent unclaimEvent = new LandUnclaimEvent(flocation, otherFaction.getId());
Bukkit.getServer().getPluginManager().callEvent(unclaimEvent);
if(unclaimEvent.isCancelled()) return;
Board.removeAt(flocation); Board.removeAt(flocation);
SpoutFeatures.updateTerritoryDisplayLoc(flocation); SpoutFeatures.updateTerritoryDisplayLoc(flocation);
myFaction.msg("%s<i> unclaimed some land.", fme.describeTo(myFaction, true)); myFaction.msg("%s<i> unclaimed some land.", fme.describeTo(myFaction, true));

View File

@ -0,0 +1,60 @@
package com.massivecraft.factions.event;
import org.bukkit.event.Cancellable;
import org.bukkit.event.Event;
import org.bukkit.event.HandlerList;
import com.massivecraft.factions.FPlayer;
import com.massivecraft.factions.Faction;
public class FPlayerJoinEvent extends Event implements Cancellable
{
private static final HandlerList handlers = new HandlerList();
FPlayer fplayer;
Faction faction;
PlayerJoinReason reason;
boolean cancelled = false;
public enum PlayerJoinReason
{
CREATE, LEADER, COMMAND
}
public FPlayerJoinEvent(FPlayer fp, Faction f, PlayerJoinReason r)
{
fplayer = fp;
faction = f;
reason = r;
}
public FPlayer getFPlayer()
{
return fplayer;
}
public Faction getFaction()
{
return faction;
}
public PlayerJoinReason getReason()
{
return reason;
}
public HandlerList getHandlers()
{
return handlers;
}
public static HandlerList getHandlerList()
{
return handlers;
}
@Override
public boolean isCancelled()
{
return cancelled;
}
@Override
public void setCancelled(boolean c)
{
cancelled = c;
}
}

View File

@ -0,0 +1,71 @@
package com.massivecraft.factions.event;
import org.bukkit.event.Cancellable;
import org.bukkit.event.Event;
import org.bukkit.event.HandlerList;
import com.massivecraft.factions.FPlayer;
import com.massivecraft.factions.Faction;
public class FPlayerLeaveEvent extends Event implements Cancellable
{
private static final HandlerList handlers = new HandlerList();
private PlayerLeaveReason reason;
FPlayer FPlayer;
Faction Faction;
boolean cancelled = false;
public enum PlayerLeaveReason
{
KICKED, DISBAND, RESET, JOINOTHER, LEAVE
}
public FPlayerLeaveEvent(FPlayer p, Faction f, PlayerLeaveReason r)
{
FPlayer = p;
Faction = f;
reason = r;
}
public HandlerList getHandlers()
{
return handlers;
}
public static HandlerList getHandlerList()
{
return handlers;
}
public PlayerLeaveReason getReason()
{
return reason;
}
public FPlayer getFPlayer()
{
return FPlayer;
}
public Faction getFaction()
{
return Faction;
}
@Override
public boolean isCancelled()
{
return cancelled;
}
@Override
public void setCancelled(boolean c)
{
if (reason == PlayerLeaveReason.DISBAND || reason == PlayerLeaveReason.RESET)
{
cancelled = false;
return;
}
cancelled = c;
}
}

View File

@ -0,0 +1,57 @@
package com.massivecraft.factions.event;
import org.bukkit.entity.Player;
import org.bukkit.event.Cancellable;
import org.bukkit.event.Event;
import org.bukkit.event.HandlerList;
import com.massivecraft.factions.FPlayer;
import com.massivecraft.factions.FPlayers;
public class FactionCreateEvent extends Event implements Cancellable
{
private static final HandlerList handlers = new HandlerList();
private String factionTag;
private Player sender;
private boolean cancelled;
public FactionCreateEvent(String tag, Player sender)
{
this.factionTag = tag;
this.sender = sender;
this.cancelled = false;
}
public FPlayer getFPlayer()
{
return FPlayers.i.get(sender);
}
public String getFactionTag()
{
return factionTag;
}
public HandlerList getHandlers()
{
return handlers;
}
public static HandlerList getHandlerList()
{
return handlers;
}
@Override
public boolean isCancelled()
{
return cancelled;
}
@Override
public void setCancelled(boolean c)
{
this.cancelled = c;
}
}

View File

@ -0,0 +1,64 @@
package com.massivecraft.factions.event;
import org.bukkit.entity.Player;
import org.bukkit.event.Cancellable;
import org.bukkit.event.Event;
import org.bukkit.event.HandlerList;
import com.massivecraft.factions.FPlayer;
import com.massivecraft.factions.FPlayers;
import com.massivecraft.factions.Faction;
import com.massivecraft.factions.Factions;
public class FactionDisbandEvent extends Event implements Cancellable
{
private static final HandlerList handlers = new HandlerList();
private boolean cancelled;
private String id;
private Player sender;
public FactionDisbandEvent(Player sender, String factionId)
{
cancelled = false;
this.sender = sender;
this.id = factionId;
}
public HandlerList getHandlers()
{
return handlers;
}
public static HandlerList getHandlerList()
{
return handlers;
}
public Faction getFaction()
{
return Factions.i.get(id);
}
public FPlayer getFPlayer()
{
return FPlayers.i.get(sender);
}
public Player getPlayer()
{
return sender;
}
@Override
public boolean isCancelled()
{
return cancelled;
}
@Override
public void setCancelled(boolean c)
{
cancelled = c;
}
}

View File

@ -0,0 +1,66 @@
package com.massivecraft.factions.event;
import org.bukkit.event.Cancellable;
import org.bukkit.event.Event;
import org.bukkit.event.HandlerList;
import com.massivecraft.factions.FLocation;
import com.massivecraft.factions.FPlayer;
import com.massivecraft.factions.FPlayers;
import com.massivecraft.factions.Faction;
import com.massivecraft.factions.Factions;
public class LandClaimEvent extends Event implements Cancellable
{
private static final HandlerList handlers = new HandlerList();
private boolean cancelled;
private FLocation location;
private String factionId, playerId;
public LandClaimEvent(FLocation loc, String id, String pid)
{
cancelled = false;
location = loc;
factionId = id;
playerId = pid;
}
public HandlerList getHandlers()
{
return handlers;
}
public static HandlerList getHandlerList()
{
return handlers;
}
public FPlayer getFPlayer()
{
return FPlayers.i.get(playerId);
}
public FLocation getLocation()
{
return this.location;
}
public Faction getFaction()
{
return Factions.i.get(factionId);
}
@Override
public boolean isCancelled()
{
return cancelled;
}
@Override
public void setCancelled(boolean c)
{
this.cancelled = c;
}
}

View File

@ -0,0 +1,56 @@
package com.massivecraft.factions.event;
import org.bukkit.event.Cancellable;
import org.bukkit.event.Event;
import org.bukkit.event.HandlerList;
import com.massivecraft.factions.FLocation;
import com.massivecraft.factions.Faction;
import com.massivecraft.factions.Factions;
public class LandUnclaimEvent extends Event implements Cancellable
{
private static final HandlerList handlers = new HandlerList();
private boolean cancelled;
private String factionId;
private FLocation location;
public LandUnclaimEvent(FLocation loc, String id)
{
cancelled = false;
location = loc;
factionId = id;
}
public HandlerList getHandlers()
{
return handlers;
}
public static HandlerList getHandlerList()
{
return handlers;
}
public Faction getFaction()
{
return Factions.i.get(factionId);
}
public FLocation getLocation()
{
return this.location;
}
@Override
public boolean isCancelled()
{
return cancelled;
}
@Override
public void setCancelled(boolean c) {
cancelled = c;
}
}