(by alkarinv) Added a new Factions event for power loss named PowerLossEvent. PowerLossEvent happens after each player death.

Backported from 1.7 branch.
This commit is contained in:
Brettflan 2012-12-19 06:18:56 -06:00
parent 3a653e39db
commit 214b87edd8
2 changed files with 117 additions and 11 deletions

View File

@ -0,0 +1,85 @@
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;
import org.bukkit.entity.Player;
public class PowerLossEvent extends Event implements Cancellable
{
private static final HandlerList handlers = new HandlerList();
private boolean cancelled;
private Faction faction;
private FPlayer fplayer;
private String message;
public PowerLossEvent(Faction f, FPlayer p)
{
cancelled = false;
faction = f;
fplayer = p;
}
@Override
public HandlerList getHandlers()
{
return handlers;
}
public static HandlerList getHandlerList()
{
return handlers;
}
public Faction getFaction()
{
return faction;
}
public String getFactionId()
{
return faction.getId();
}
public String getFactionTag()
{
return faction.getTag();
}
public FPlayer getFPlayer()
{
return fplayer;
}
public Player getPlayer()
{
return fplayer.getPlayer();
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
@Override
public boolean isCancelled()
{
return cancelled;
}
@Override
public void setCancelled(boolean c)
{
this.cancelled = c;
}
}

View File

@ -8,6 +8,7 @@ import java.util.List;
import java.text.MessageFormat;
import java.util.Set;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.block.Block;
import org.bukkit.entity.Creeper;
@ -46,6 +47,7 @@ import com.massivecraft.factions.FPlayer;
import com.massivecraft.factions.FPlayers;
import com.massivecraft.factions.Faction;
import com.massivecraft.factions.P;
import com.massivecraft.factions.event.PowerLossEvent;
import com.massivecraft.factions.struct.Relation;
import com.massivecraft.factions.util.MiscUtil;
@ -70,36 +72,55 @@ public class FactionsEntityListener implements Listener
Player player = (Player) entity;
FPlayer fplayer = FPlayers.i.get(player);
Faction faction = Board.getFactionAt(new FLocation(player.getLocation()));
PowerLossEvent powerLossEvent = new PowerLossEvent(faction,fplayer);
// Check for no power loss conditions
if (faction.isWarZone())
{
// war zones always override worldsNoPowerLoss either way, thus this layout
if (! Conf.warZonePowerLoss)
{
fplayer.msg("<i>You didn't lose any power since you were in a war zone.");
return;
powerLossEvent.setMessage("<i>You didn't lose any power since you were in a war zone.");
powerLossEvent.setCancelled(true);
}
if (Conf.worldsNoPowerLoss.contains(player.getWorld().getName()))
{
fplayer.msg("<b>The world you are in has power loss normally disabled, but you still lost power since you were in a war zone.");
powerLossEvent.setMessage("<b>The world you are in has power loss normally disabled, but you still lost power since you were in a war zone.\n<i>Your power is now <h>%d / %d");
}
}
else if (faction.isNone() && !Conf.wildernessPowerLoss && !Conf.worldsNoWildernessProtection.contains(player.getWorld().getName()))
{
fplayer.msg("<i>You didn't lose any power since you were in the wilderness.");
return;
powerLossEvent.setMessage("<i>You didn't lose any power since you were in the wilderness.");
powerLossEvent.setCancelled(true);
}
else if (Conf.worldsNoPowerLoss.contains(player.getWorld().getName()))
{
fplayer.msg("<i>You didn't lose any power due to the world you died in.");
return;
powerLossEvent.setMessage("<i>You didn't lose any power due to the world you died in.");
powerLossEvent.setCancelled(true);
}
else if (Conf.peacefulMembersDisablePowerLoss && fplayer.hasFaction() && fplayer.getFaction().isPeaceful())
{
fplayer.msg("<i>You didn't lose any power since you are in a peaceful faction.");
return;
powerLossEvent.setMessage("<i>You didn't lose any power since you are in a peaceful faction.");
powerLossEvent.setCancelled(true);
}
else {
powerLossEvent.setMessage("<i>Your power is now <h>%d / %d");
}
// call Event
Bukkit.getPluginManager().callEvent(powerLossEvent);
// Call player onDeath if the event is not cancelled
if(!powerLossEvent.isCancelled())
{
fplayer.onDeath();
}
// Send the message from the powerLossEvent
final String msg = powerLossEvent.getMessage();
if (msg != null && !msg.isEmpty())
{
fplayer.msg(msg,fplayer.getPowerRounded(),fplayer.getPowerMaxRounded());
}
fplayer.onDeath();
fplayer.msg("<i>Your power is now <h>"+fplayer.getPowerRounded()+" / "+fplayer.getPowerMaxRounded());
}
/**