updated patrickfreed's custom event system

Patrick's Custom Event System Changes:
----------------------------------------
  * FPlayerLeaveEvent now fires for all faction players in cmdDisband()
  * FPlayerLeaveEvent removed from cmdJoin()
  * FPlayerJoinEvent now only fires when the faction is different in cmdLeade
  * Added FactionRenameEvent, firing on cmdTag()
  * Added FactionRelationEvent, firing on FRelationCommand extensions
  * Fixed FPlayerJoinEvent to fire after tag is set in cmdCreate()
  * Added getFactionId() to FactionCreateEvent

TODO:
-------
  * cmdLeader() might need a FPlayerLeaveEvent for the same reason it needs
    a FPlayerJoinEvent?

On branch CustomFactionEvents

	modified:   src/com/massivecraft/factions/cmd/CmdCreate.java
	modified:   src/com/massivecraft/factions/cmd/CmdDisband.java
	modified:   src/com/massivecraft/factions/cmd/CmdJoin.java
	modified:   src/com/massivecraft/factions/cmd/CmdKick.java
	modified:   src/com/massivecraft/factions/cmd/CmdLeader.java
	modified:   src/com/massivecraft/factions/cmd/CmdTag.java
	modified:   src/com/massivecraft/factions/cmd/FRelationCommand.java
	modified:   src/com/massivecraft/factions/event/FPlayerLeaveEvent.java
	modified:   src/com/massivecraft/factions/event/FactionCreateEvent.java
	new file:   src/com/massivecraft/factions/event/FactionRelationEvent.java
	new file:   src/com/massivecraft/factions/event/FactionRenameEvent.java
	modified:   src/com/massivecraft/factions/event/LandClaimEvent.java
This commit is contained in:
donington 2012-03-09 17:09:33 -05:00
parent 021bf52c62
commit 6329fd0eaf
12 changed files with 287 additions and 123 deletions

View File

@ -58,9 +58,9 @@ public class CmdCreate extends FCommand
return; return;
} }
FactionCreateEvent createEvent = new FactionCreateEvent(tag, me); // trigger the faction creation event (cancellable)
FactionCreateEvent createEvent = new FactionCreateEvent(me, tag);
Bukkit.getServer().getPluginManager().callEvent(createEvent); Bukkit.getServer().getPluginManager().callEvent(createEvent);
if(createEvent.isCancelled()) return; 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
@ -75,10 +75,15 @@ public class CmdCreate extends FCommand
return; return;
} }
// finish setting up the Faction
faction.setTag(tag);
// trigger the faction join event for the creator
FPlayerJoinEvent joinEvent = new FPlayerJoinEvent(FPlayers.i.get(me),faction,FPlayerJoinEvent.PlayerJoinReason.CREATE); FPlayerJoinEvent joinEvent = new FPlayerJoinEvent(FPlayers.i.get(me),faction,FPlayerJoinEvent.PlayerJoinReason.CREATE);
Bukkit.getServer().getPluginManager().callEvent(joinEvent); Bukkit.getServer().getPluginManager().callEvent(joinEvent);
// join event cannot be cancelled or you'll have an empty faction
faction.setTag(tag); // finish setting up the FPlayer
fme.setRole(Rel.LEADER); fme.setRole(Rel.LEADER);
fme.setFaction(faction); fme.setFaction(faction);

View File

@ -53,13 +53,18 @@ public class CmdDisband extends FCommand
Bukkit.getServer().getPluginManager().callEvent(disbandEvent); Bukkit.getServer().getPluginManager().callEvent(disbandEvent);
if(disbandEvent.isCancelled()) return; if(disbandEvent.isCancelled()) return;
// Send FPlayerLeaveEvent for each player in the faction
for ( FPlayer fplayer : faction.getFPlayers() )
{
Bukkit.getServer().getPluginManager().callEvent(new FPlayerLeaveEvent(fplayer, faction, FPlayerLeaveEvent.PlayerLeaveReason.DISBAND));
}
// 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

@ -8,9 +8,7 @@ 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.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
{ {
@ -83,13 +81,7 @@ public class CmdJoin extends FCommand
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)) // trigger the join event (cancellable)
{
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); FPlayerJoinEvent joinEvent = new FPlayerJoinEvent(FPlayers.i.get(me),faction,FPlayerJoinEvent.PlayerJoinReason.COMMAND);
Bukkit.getServer().getPluginManager().callEvent(joinEvent); Bukkit.getServer().getPluginManager().callEvent(joinEvent);
if (joinEvent.isCancelled()) return; if (joinEvent.isCancelled()) return;

View File

@ -63,6 +63,7 @@ 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;
// trigger the leave event (cancellable) [reason:kicked]
FPlayerLeaveEvent event = new FPlayerLeaveEvent(you, you.getFaction(), FPlayerLeaveEvent.PlayerLeaveReason.KICKED); FPlayerLeaveEvent event = new FPlayerLeaveEvent(you, you.getFaction(), FPlayerLeaveEvent.PlayerLeaveReason.KICKED);
Bukkit.getServer().getPluginManager().callEvent(event); Bukkit.getServer().getPluginManager().callEvent(event);
if (event.isCancelled()) return; if (event.isCancelled()) return;

View File

@ -67,9 +67,14 @@ public class CmdLeader extends FCommand
} }
} }
// only perform a FPlayerJoinEvent when newLeader isn't actually in the faction
// (only possibly triggered by console)
if (newLeader.getFaction() != targetFaction)
{
FPlayerJoinEvent event = new FPlayerJoinEvent(FPlayers.i.get(me),targetFaction,FPlayerJoinEvent.PlayerJoinReason.LEADER); FPlayerJoinEvent event = new FPlayerJoinEvent(FPlayers.i.get(me),targetFaction,FPlayerJoinEvent.PlayerJoinReason.LEADER);
Bukkit.getServer().getPluginManager().callEvent(event); Bukkit.getServer().getPluginManager().callEvent(event);
if (event.isCancelled()) return; 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

@ -2,9 +2,12 @@ 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.Faction; import com.massivecraft.factions.Faction;
import com.massivecraft.factions.Factions; import com.massivecraft.factions.Factions;
import com.massivecraft.factions.event.FactionRenameEvent;
import com.massivecraft.factions.integration.SpoutFeatures; import com.massivecraft.factions.integration.SpoutFeatures;
import com.massivecraft.factions.struct.Permission; import com.massivecraft.factions.struct.Permission;
import com.massivecraft.factions.util.MiscUtil; import com.massivecraft.factions.util.MiscUtil;
@ -48,6 +51,11 @@ public class CmdTag extends FCommand
return; return;
} }
// trigger the faction rename event (cancellable)
FactionRenameEvent renameEvent = new FactionRenameEvent(fme, tag);
Bukkit.getServer().getPluginManager().callEvent(renameEvent);
if(renameEvent.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.econCostTag, "to change the faction tag", "for changing the faction tag")) return; if ( ! payForCommand(Conf.econCostTag, "to change the faction tag", "for changing the faction tag")) return;

View File

@ -1,7 +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.Faction; import com.massivecraft.factions.Faction;
import com.massivecraft.factions.event.FactionRelationEvent;
import com.massivecraft.factions.integration.SpoutFeatures; import com.massivecraft.factions.integration.SpoutFeatures;
import com.massivecraft.factions.struct.FFlag; import com.massivecraft.factions.struct.FFlag;
import com.massivecraft.factions.struct.Permission; import com.massivecraft.factions.struct.Permission;
@ -47,13 +50,22 @@ public abstract class FRelationCommand 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(targetRelation.getRelationCost(), "to change a relation wish", "for changing a relation wish")) return; if ( ! payForCommand(targetRelation.getRelationCost(), "to change a relation wish", "for changing a relation wish")) return;
// try to set the new relation
Rel oldRelation = myFaction.getRelationTo(them, true);
myFaction.setRelationWish(them, targetRelation); myFaction.setRelationWish(them, targetRelation);
Rel currentRelation = myFaction.getRelationTo(them, true); Rel currentRelation = myFaction.getRelationTo(them, true);
// if the relation change was successful
if (targetRelation == currentRelation) if (targetRelation == currentRelation)
{ {
// trigger the faction relation event
FactionRelationEvent relationEvent = new FactionRelationEvent(myFaction, them, oldRelation, currentRelation);
Bukkit.getServer().getPluginManager().callEvent(relationEvent);
them.msg("%s<i> is now %s.", myFaction.describeTo(them, true), targetRelation.getDescFactionOne()); them.msg("%s<i> is now %s.", myFaction.describeTo(them, true), targetRelation.getDescFactionOne());
myFaction.msg("%s<i> is now %s.", them.describeTo(myFaction, true), targetRelation.getDescFactionOne()); myFaction.msg("%s<i> is now %s.", them.describeTo(myFaction, true), targetRelation.getDescFactionOne());
} }
// inform the other faction of your request
else else
{ {
them.msg("%s<i> wishes to be %s.", myFaction.describeTo(them, true), targetRelation.getColor()+targetRelation.getDescFactionOne()); them.msg("%s<i> wishes to be %s.", myFaction.describeTo(them, true), targetRelation.getColor()+targetRelation.getDescFactionOne());
@ -62,6 +74,7 @@ public abstract class FRelationCommand extends FCommand
} }
// TODO: The ally case should work!! // TODO: The ally case should work!!
// * this might have to be bumped up to make that happen, & allow ALLY,NEUTRAL only
if ( targetRelation != Rel.TRUCE && them.getFlag(FFlag.PEACEFUL)) if ( targetRelation != Rel.TRUCE && them.getFlag(FFlag.PEACEFUL))
{ {
them.msg("<i>This will have no effect while your faction is peaceful."); them.msg("<i>This will have no effect while your faction is peaceful.");

View File

@ -7,6 +7,7 @@ import org.bukkit.event.HandlerList;
import com.massivecraft.factions.FPlayer; import com.massivecraft.factions.FPlayer;
import com.massivecraft.factions.FPlayers; import com.massivecraft.factions.FPlayers;
import com.massivecraft.factions.Factions;
public class FactionCreateEvent extends Event implements Cancellable public class FactionCreateEvent extends Event implements Cancellable
{ {
@ -16,7 +17,7 @@ public class FactionCreateEvent extends Event implements Cancellable
private Player sender; private Player sender;
private boolean cancelled; private boolean cancelled;
public FactionCreateEvent(String tag, Player sender) public FactionCreateEvent(Player sender, String tag)
{ {
this.factionTag = tag; this.factionTag = tag;
this.sender = sender; this.sender = sender;
@ -28,6 +29,11 @@ public class FactionCreateEvent extends Event implements Cancellable
return FPlayers.i.get(sender); return FPlayers.i.get(sender);
} }
public String getFactionId()
{
return Factions.i.getNextId();
}
public String getFactionTag() public String getFactionTag()
{ {
return factionTag; return factionTag;

View File

@ -0,0 +1,56 @@
package com.massivecraft.factions.event;
import org.bukkit.event.Event;
import org.bukkit.event.HandlerList;
import com.massivecraft.factions.struct.Rel;
import com.massivecraft.factions.Faction;
public class FactionRelationEvent extends Event {
private static final HandlerList handlers = new HandlerList();
private Faction fsender;
private Faction ftarget;
private Rel foldrel;
private Rel frel;
public FactionRelationEvent(Faction sender, Faction target, Rel oldrel, Rel rel)
{
fsender = sender;
ftarget = target;
foldrel = oldrel;
frel = rel;
}
public HandlerList getHandlers()
{
return handlers;
}
public static HandlerList getHandlerList()
{
return handlers;
}
public Rel getOldRelation()
{
return foldrel;
}
public Rel getRelation()
{
return frel;
}
public Faction getFaction()
{
return fsender;
}
public Faction getTargetFaction()
{
return ftarget;
}
}

View File

@ -0,0 +1,73 @@
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.Faction;
public class FactionRenameEvent extends Event implements Cancellable {
private static final HandlerList handlers = new HandlerList();
private boolean cancelled;
private FPlayer fplayer;
private Faction faction;
private String tag;
public FactionRenameEvent(FPlayer sender, String newTag)
{
fplayer = sender;
faction = sender.getFaction();
tag = newTag;
this.cancelled = false;
}
public Faction getFaction()
{
return(faction);
}
public FPlayer getFPlayer()
{
return(fplayer);
}
public Player getPlayer()
{
return(fplayer.getPlayer());
}
public String getOldFactionTag()
{
return(faction.getTag());
}
public String getFactionTag()
{
return(tag);
}
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

@ -18,12 +18,12 @@ public class LandClaimEvent extends Event implements Cancellable
private FLocation location; private FLocation location;
private String factionId, playerId; private String factionId, playerId;
public LandClaimEvent(FLocation loc, String id, String pid) public LandClaimEvent(FLocation loc, String fid, String pid)
{ {
cancelled = false; cancelled = false;
location = loc; location = loc;
factionId = id; this.factionId = fid;
playerId = pid; this.playerId = pid;
} }
public HandlerList getHandlers() public HandlerList getHandlers()