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

241 lines
8.1 KiB
Java
Raw Normal View History

package com.massivecraft.factions.engine;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.plugin.Plugin;
import com.massivecraft.factions.Factions;
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;
import com.massivecraft.factions.event.EventFactionsChunkChange;
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;
import com.massivecraft.massivecore.EngineAbstract;
2014-06-04 14:02:23 +02:00
import com.massivecraft.massivecore.money.Money;
public class EngineEcon extends EngineAbstract
{
// -------------------------------------------- //
// INSTANCE & CONSTRUCT
// -------------------------------------------- //
private static EngineEcon i = new EngineEcon();
public static EngineEcon get() { return i; }
public EngineEcon() {}
// -------------------------------------------- //
// OVERRIDE
// -------------------------------------------- //
@Override
public Plugin getPlugin()
{
return Factions.get();
}
// -------------------------------------------- //
// 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 usender ...
MPlayer usender = event.getMSender();
if (usender == 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);
String amountString = Money.format(amount);
2013-12-03 20:19:55 +01:00
Econ.transferMoney(faction, usender, usender, amount, true);
usender.msg("<i>You have been given the disbanded faction's bank, totaling %s.", amountString);
Factions.get().log(usender.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 a sender ...
MPlayer usender = event.getMSender();
if (usender == 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, usender, desc)) return;
// ... then cancel.
event.setCancelled(true);
}
@EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true)
2014-06-04 16:47:01 +02:00
public void payForAction(EventFactionsChunkChange event)
{
2014-06-04 16:47:01 +02:00
EventFactionsChunkChangeType type = event.getType();
Double cost = MConf.get().econChunkCost.get(type);
String desc = type.toString().toLowerCase() + " 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());
String desc = Factions.get().getOuterCmdFactions().cmdFactionsRelationNeutral.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;
String desc = Factions.get().getOuterCmdFactions().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;
String desc = Factions.get().getOuterCmdFactions().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;
String desc = Factions.get().getOuterCmdFactions().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;
String desc = Factions.get().getOuterCmdFactions().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;
String desc = Factions.get().getOuterCmdFactions().cmdFactionsTitle.getDesc();
payForAction(event, cost, desc);
}
@EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true)
public void payForCommand(EventFactionsFlagChange event)
{
Double cost = MConf.get().econCostFlag;
String desc = Factions.get().getOuterCmdFactions().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;
String desc = Factions.get().getOuterCmdFactions().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;
String desc = Factions.get().getOuterCmdFactions().cmdFactionsHome.getDesc();
2013-04-19 14:08:45 +02:00
payForAction(event, cost, desc);
2013-04-19 14:08:45 +02:00
}
}