Factions/src/com/massivecraft/factions/engine/EngineEcon.java

254 lines
8.4 KiB
Java
Raw Normal View History

package com.massivecraft.factions.engine;
2014-10-13 11:42:40 +02:00
import java.util.ArrayList;
import java.util.List;
import java.util.Map.Entry;
import java.util.Set;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import com.massivecraft.factions.Factions;
2016-02-25 22:28:09 +01:00
import com.massivecraft.factions.cmd.CmdFactions;
import com.massivecraft.factions.entity.Faction;
import com.massivecraft.factions.entity.MConf;
import com.massivecraft.factions.entity.MPlayer;
2014-06-04 16:47:01 +02:00
import com.massivecraft.factions.event.EventFactionsAbstractSender;
2014-10-13 11:42:40 +02:00
import com.massivecraft.factions.event.EventFactionsChunksChange;
2014-06-04 16:47:01 +02:00
import com.massivecraft.factions.event.EventFactionsChunkChangeType;
import com.massivecraft.factions.event.EventFactionsCreate;
import com.massivecraft.factions.event.EventFactionsDescriptionChange;
import com.massivecraft.factions.event.EventFactionsDisband;
import com.massivecraft.factions.event.EventFactionsHomeChange;
import com.massivecraft.factions.event.EventFactionsHomeTeleport;
import com.massivecraft.factions.event.EventFactionsInvitedChange;
import com.massivecraft.factions.event.EventFactionsMembershipChange;
import com.massivecraft.factions.event.EventFactionsMembershipChange.MembershipChangeReason;
import com.massivecraft.factions.event.EventFactionsNameChange;
import com.massivecraft.factions.event.EventFactionsFlagChange;
2014-06-04 16:47:01 +02:00
import com.massivecraft.factions.event.EventFactionsRelationChange;
import com.massivecraft.factions.event.EventFactionsTitleChange;
import com.massivecraft.factions.integration.Econ;
2016-02-25 22:28:09 +01:00
import com.massivecraft.massivecore.Engine;
2014-06-04 14:02:23 +02:00
import com.massivecraft.massivecore.money.Money;
2014-10-13 11:42:40 +02:00
import com.massivecraft.massivecore.ps.PS;
import com.massivecraft.massivecore.util.Txt;
2016-02-25 22:28:09 +01:00
public class EngineEcon extends Engine
{
// -------------------------------------------- //
// INSTANCE & CONSTRUCT
// -------------------------------------------- //
private static EngineEcon i = new EngineEcon();
public static EngineEcon get() { return i; }
// -------------------------------------------- //
// TAKE ON LEAVE
// -------------------------------------------- //
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
2014-06-04 16:47:01 +02:00
public void takeOnLeave(EventFactionsMembershipChange event)
{
// If a player is leaving the faction ...
if (event.getReason() != MembershipChangeReason.LEAVE) return;
// ... and that player was the last one in the faction ...
2014-09-17 13:29:58 +02:00
MPlayer mplayer = event.getMPlayer();
Faction oldFaction = mplayer.getFaction();
if (oldFaction.getMPlayers().size() > 1) return;
// ... then transfer all money to the player.
double money = Money.get(oldFaction);
if (money == 0) return;
Econ.transferMoney(mplayer, oldFaction, mplayer, money);
}
// -------------------------------------------- //
// TAKE ON DISBAND
// -------------------------------------------- //
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
2014-06-04 16:47:01 +02:00
public void takeOnDisband(EventFactionsDisband event)
{
// If there is a mplayer ...
MPlayer mplayer = event.getMPlayer();
if (mplayer == null) return;
// ... and economy is enabled ...
if (!Econ.isEnabled()) return;
// ... then transfer all the faction money to the sender.
Faction faction = event.getFaction();
double amount = Money.get(faction);
// Check that there is an amount
if (amount == 0) return;
String amountString = Money.format(amount);
Econ.transferMoney(faction, mplayer, mplayer, amount, true);
mplayer.msg("<i>You have been given the disbanded faction's bank, totaling %s.", amountString);
Factions.get().log(mplayer.getName() + " has been given bank holdings of "+amountString+" from disbanding "+faction.getName()+".");
}
// -------------------------------------------- //
// PAY FOR ACTION
// -------------------------------------------- //
2014-06-04 16:47:01 +02:00
public static void payForAction(EventFactionsAbstractSender event, Double cost, String desc)
{
// If there is an mplayer ...
MPlayer mplayer = event.getMPlayer();
if (mplayer == null) return;
// ... and there is a cost ...
if (cost == null) return;
if (cost == 0) return;
// ... that the sender can't afford ...
if (Econ.payForAction(cost, mplayer, desc)) return;
// ... then cancel.
event.setCancelled(true);
}
@EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true)
2014-10-13 11:42:40 +02:00
public void payForAction(EventFactionsChunksChange event)
{
2014-10-13 11:42:40 +02:00
double cost = 0;
List<String> typeNames = new ArrayList<String>();
2014-10-13 11:42:40 +02:00
for (Entry<EventFactionsChunkChangeType, Set<PS>> typeChunks : event.getTypeChunks().entrySet())
{
final EventFactionsChunkChangeType type = typeChunks.getKey();
final Set<PS> chunks = typeChunks.getValue();
Double typeCost = MConf.get().econChunkCost.get(type);
if (typeCost == null) continue;
if (typeCost == 0) continue;
typeCost *= chunks.size();
cost += typeCost;
typeNames.add(type.now);
}
2014-10-13 11:42:40 +02:00
String desc = Txt.implodeCommaAnd(typeNames) + " this land";
payForAction(event, cost, desc);
}
@EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true)
2014-06-04 16:47:01 +02:00
public void payForAction(EventFactionsMembershipChange event)
{
Double cost = null;
String desc = null;
if (event.getReason() == MembershipChangeReason.JOIN)
{
cost = MConf.get().econCostJoin;
desc = "join a faction";
}
else if (event.getReason() == MembershipChangeReason.LEAVE)
{
cost = MConf.get().econCostLeave;
desc = "leave a faction";
}
else if (event.getReason() == MembershipChangeReason.KICK)
{
cost = MConf.get().econCostKick;
desc = "kick someone from a faction";
}
else
{
return;
}
payForAction(event, cost, desc);
}
@EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true)
2014-06-04 16:47:01 +02:00
public void payForCommand(EventFactionsRelationChange event)
{
Double cost = MConf.get().econRelCost.get(event.getNewRelation());
2016-02-25 09:48:02 +01:00
String desc = CmdFactions.get().cmdFactionsRelation.cmdFactionsRelationSet.getDesc();
payForAction(event, cost, desc);
}
@EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true)
2014-06-04 16:47:01 +02:00
public void payForCommand(EventFactionsHomeChange event)
{
Double cost = MConf.get().econCostSethome;
2016-02-25 22:28:09 +01:00
String desc = CmdFactions.get().cmdFactionsSethome.getDesc();
payForAction(event, cost, desc);
}
@EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true)
2014-06-04 16:47:01 +02:00
public void payForCommand(EventFactionsCreate event)
{
Double cost = MConf.get().econCostCreate;
2016-02-25 22:28:09 +01:00
String desc = CmdFactions.get().cmdFactionsCreate.getDesc();
payForAction(event, cost, desc);
}
@EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true)
2014-06-04 16:47:01 +02:00
public void payForCommand(EventFactionsDescriptionChange event)
{
Double cost = MConf.get().econCostDescription;
2016-02-25 22:28:09 +01:00
String desc = CmdFactions.get().cmdFactionsDescription.getDesc();
payForAction(event, cost, desc);
}
@EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true)
2014-06-04 16:47:01 +02:00
public void payForCommand(EventFactionsNameChange event)
{
Double cost = MConf.get().econCostName;
2016-02-25 22:28:09 +01:00
String desc = CmdFactions.get().cmdFactionsName.getDesc();
payForAction(event, cost, desc);
}
@EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true)
2014-06-04 16:47:01 +02:00
public void payForCommand(EventFactionsTitleChange event)
{
Double cost = MConf.get().econCostTitle;
2016-02-25 22:28:09 +01:00
String desc = CmdFactions.get().cmdFactionsTitle.getDesc();
payForAction(event, cost, desc);
}
@EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true)
public void payForCommand(EventFactionsFlagChange event)
{
Double cost = MConf.get().econCostFlag;
2016-02-25 22:28:09 +01:00
String desc = CmdFactions.get().cmdFactionsFlag.getDesc();
payForAction(event, cost, desc);
}
2013-04-19 14:08:45 +02:00
@EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true)
2014-06-04 16:47:01 +02:00
public void payForCommand(EventFactionsInvitedChange event)
2013-04-19 14:08:45 +02:00
{
Double cost = event.isNewInvited() ? MConf.get().econCostInvite : MConf.get().econCostDeinvite;
2016-02-25 22:28:09 +01:00
String desc = CmdFactions.get().cmdFactionsInvite.getDesc();
2013-04-19 14:08:45 +02:00
payForAction(event, cost, desc);
}
@EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true)
2014-06-04 16:47:01 +02:00
public void payForCommand(EventFactionsHomeTeleport event)
{
Double cost = MConf.get().econCostHome;
2016-02-25 22:28:09 +01:00
String desc = CmdFactions.get().cmdFactionsHome.getDesc();
2013-04-19 14:08:45 +02:00
payForAction(event, cost, desc);
2013-04-19 14:08:45 +02:00
}
}