2014-10-08 06:38:26 +02:00
|
|
|
package com.massivecraft.factions.engine;
|
2013-04-19 10:07:53 +02:00
|
|
|
|
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;
|
|
|
|
|
2013-04-19 10:07:53 +02:00
|
|
|
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;
|
2013-04-24 11:16:37 +02:00
|
|
|
import com.massivecraft.factions.entity.Faction;
|
2014-09-17 13:17:33 +02:00
|
|
|
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;
|
2014-10-02 11:45:06 +02:00
|
|
|
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;
|
2013-04-19 10:07:53 +02:00
|
|
|
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;
|
2013-04-19 10:07:53 +02:00
|
|
|
|
2016-02-25 22:28:09 +01:00
|
|
|
public class EngineEcon extends Engine
|
2013-04-19 10:07:53 +02:00
|
|
|
{
|
|
|
|
// -------------------------------------------- //
|
|
|
|
// INSTANCE & CONSTRUCT
|
|
|
|
// -------------------------------------------- //
|
|
|
|
|
2014-10-08 06:38:26 +02:00
|
|
|
private static EngineEcon i = new EngineEcon();
|
|
|
|
public static EngineEcon get() { return i; }
|
2013-04-19 10:07:53 +02:00
|
|
|
|
2013-04-24 13:26:59 +02:00
|
|
|
// -------------------------------------------- //
|
|
|
|
// TAKE ON LEAVE
|
|
|
|
// -------------------------------------------- //
|
|
|
|
|
|
|
|
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
|
2014-06-04 16:47:01 +02:00
|
|
|
public void takeOnLeave(EventFactionsMembershipChange event)
|
2013-04-24 13:26:59 +02:00
|
|
|
{
|
|
|
|
// 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;
|
2013-04-24 13:26:59 +02:00
|
|
|
|
|
|
|
// ... then transfer all money to the player.
|
2014-09-18 00:18:07 +02:00
|
|
|
double money = Money.get(oldFaction);
|
|
|
|
if (money == 0) return;
|
|
|
|
Econ.transferMoney(mplayer, oldFaction, mplayer, money);
|
2013-04-24 13:26:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// -------------------------------------------- //
|
|
|
|
// TAKE ON DISBAND
|
|
|
|
// -------------------------------------------- //
|
|
|
|
|
|
|
|
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
|
2014-06-04 16:47:01 +02:00
|
|
|
public void takeOnDisband(EventFactionsDisband event)
|
2013-04-24 13:26:59 +02:00
|
|
|
{
|
2016-01-24 20:07:09 +01:00
|
|
|
// If there is a mplayer ...
|
|
|
|
MPlayer mplayer = event.getMPlayer();
|
|
|
|
if (mplayer == null) return;
|
2013-04-24 13:26:59 +02:00
|
|
|
|
|
|
|
// ... and economy is enabled ...
|
2014-09-17 13:17:33 +02:00
|
|
|
if (!Econ.isEnabled()) return;
|
2013-04-24 13:26:59 +02:00
|
|
|
|
|
|
|
// ... then transfer all the faction money to the sender.
|
|
|
|
Faction faction = event.getFaction();
|
|
|
|
|
|
|
|
double amount = Money.get(faction);
|
2016-02-17 17:55:22 +01:00
|
|
|
|
|
|
|
// Check that there is an amount
|
|
|
|
if (amount == 0) return;
|
|
|
|
|
2013-12-03 12:59:46 +01:00
|
|
|
String amountString = Money.format(amount);
|
2013-04-24 13:26:59 +02:00
|
|
|
|
2016-01-24 20:07:09 +01:00
|
|
|
Econ.transferMoney(faction, mplayer, mplayer, amount, true);
|
2013-04-24 13:26:59 +02:00
|
|
|
|
2016-01-24 20:07:09 +01:00
|
|
|
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()+".");
|
2013-04-24 13:26:59 +02:00
|
|
|
}
|
|
|
|
|
2013-04-19 10:07:53 +02:00
|
|
|
// -------------------------------------------- //
|
2013-04-24 11:16:37 +02:00
|
|
|
// PAY FOR ACTION
|
2013-04-19 10:07:53 +02:00
|
|
|
// -------------------------------------------- //
|
|
|
|
|
2014-06-04 16:47:01 +02:00
|
|
|
public static void payForAction(EventFactionsAbstractSender event, Double cost, String desc)
|
2013-04-19 10:07:53 +02:00
|
|
|
{
|
2016-01-24 20:07:09 +01:00
|
|
|
// If there is an mplayer ...
|
|
|
|
MPlayer mplayer = event.getMPlayer();
|
|
|
|
if (mplayer == null) return;
|
2013-04-19 10:07:53 +02:00
|
|
|
|
2013-04-24 11:16:37 +02:00
|
|
|
// ... and there is a cost ...
|
|
|
|
if (cost == null) return;
|
|
|
|
if (cost == 0) return;
|
|
|
|
|
|
|
|
// ... that the sender can't afford ...
|
2016-01-24 20:07:09 +01:00
|
|
|
if (Econ.payForAction(cost, mplayer, desc)) return;
|
2013-04-19 10:07:53 +02:00
|
|
|
|
2013-04-19 12:27:39 +02:00
|
|
|
// ... then cancel.
|
2013-04-19 10:07:53 +02:00
|
|
|
event.setCancelled(true);
|
|
|
|
}
|
|
|
|
|
2013-04-19 12:27:39 +02:00
|
|
|
@EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true)
|
2014-10-13 11:42:40 +02:00
|
|
|
public void payForAction(EventFactionsChunksChange event)
|
2013-04-19 12:27:39 +02:00
|
|
|
{
|
2014-10-13 11:42:40 +02:00
|
|
|
double cost = 0;
|
|
|
|
List<String> typeNames = new ArrayList<String>();
|
2013-04-24 11:16:37 +02:00
|
|
|
|
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);
|
|
|
|
}
|
2013-04-24 11:16:37 +02:00
|
|
|
|
2014-10-13 11:42:40 +02:00
|
|
|
String desc = Txt.implodeCommaAnd(typeNames) + " this land";
|
2013-04-24 11:16:37 +02:00
|
|
|
payForAction(event, cost, desc);
|
2013-04-19 12:27:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true)
|
2014-06-04 16:47:01 +02:00
|
|
|
public void payForAction(EventFactionsMembershipChange event)
|
2013-04-19 12:27:39 +02:00
|
|
|
{
|
2013-04-24 11:16:37 +02:00
|
|
|
Double cost = null;
|
|
|
|
String desc = null;
|
|
|
|
|
|
|
|
if (event.getReason() == MembershipChangeReason.JOIN)
|
|
|
|
{
|
2014-09-17 13:17:33 +02:00
|
|
|
cost = MConf.get().econCostJoin;
|
2013-04-24 11:16:37 +02:00
|
|
|
desc = "join a faction";
|
|
|
|
}
|
|
|
|
else if (event.getReason() == MembershipChangeReason.LEAVE)
|
|
|
|
{
|
2014-09-17 13:17:33 +02:00
|
|
|
cost = MConf.get().econCostLeave;
|
2013-04-24 11:16:37 +02:00
|
|
|
desc = "leave a faction";
|
|
|
|
}
|
|
|
|
else if (event.getReason() == MembershipChangeReason.KICK)
|
|
|
|
{
|
2014-09-17 13:17:33 +02:00
|
|
|
cost = MConf.get().econCostKick;
|
2013-04-24 11:16:37 +02:00
|
|
|
desc = "kick someone from a faction";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
payForAction(event, cost, desc);
|
2013-04-19 12:27:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true)
|
2014-06-04 16:47:01 +02:00
|
|
|
public void payForCommand(EventFactionsRelationChange event)
|
2013-04-19 12:27:39 +02:00
|
|
|
{
|
2014-09-17 13:17:33 +02:00
|
|
|
Double cost = MConf.get().econRelCost.get(event.getNewRelation());
|
2016-02-25 09:48:02 +01:00
|
|
|
String desc = CmdFactions.get().cmdFactionsRelation.cmdFactionsRelationSet.getDesc();
|
2013-04-24 11:16:37 +02:00
|
|
|
payForAction(event, cost, desc);
|
2013-04-19 12:27:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true)
|
2014-06-04 16:47:01 +02:00
|
|
|
public void payForCommand(EventFactionsHomeChange event)
|
2013-04-19 12:27:39 +02:00
|
|
|
{
|
2014-09-17 13:17:33 +02:00
|
|
|
Double cost = MConf.get().econCostSethome;
|
2016-02-25 22:28:09 +01:00
|
|
|
String desc = CmdFactions.get().cmdFactionsSethome.getDesc();
|
2013-04-24 11:16:37 +02:00
|
|
|
|
|
|
|
payForAction(event, cost, desc);
|
2013-04-19 12:27:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true)
|
2014-06-04 16:47:01 +02:00
|
|
|
public void payForCommand(EventFactionsCreate event)
|
2013-04-19 12:27:39 +02:00
|
|
|
{
|
2014-09-17 13:17:33 +02:00
|
|
|
Double cost = MConf.get().econCostCreate;
|
2016-02-25 22:28:09 +01:00
|
|
|
String desc = CmdFactions.get().cmdFactionsCreate.getDesc();
|
2013-04-24 11:16:37 +02:00
|
|
|
|
|
|
|
payForAction(event, cost, desc);
|
2013-04-19 12:27:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true)
|
2014-06-04 16:47:01 +02:00
|
|
|
public void payForCommand(EventFactionsDescriptionChange event)
|
2013-04-19 12:27:39 +02:00
|
|
|
{
|
2014-09-17 13:17:33 +02:00
|
|
|
Double cost = MConf.get().econCostDescription;
|
2016-02-25 22:28:09 +01:00
|
|
|
String desc = CmdFactions.get().cmdFactionsDescription.getDesc();
|
2013-04-24 08:39:26 +02:00
|
|
|
|
2013-04-24 11:16:37 +02:00
|
|
|
payForAction(event, cost, desc);
|
2013-04-19 12:27:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true)
|
2014-06-04 16:47:01 +02:00
|
|
|
public void payForCommand(EventFactionsNameChange event)
|
2013-04-19 12:27:39 +02:00
|
|
|
{
|
2014-09-17 13:17:33 +02:00
|
|
|
Double cost = MConf.get().econCostName;
|
2016-02-25 22:28:09 +01:00
|
|
|
String desc = CmdFactions.get().cmdFactionsName.getDesc();
|
2013-04-24 11:16:37 +02:00
|
|
|
|
|
|
|
payForAction(event, cost, desc);
|
2013-04-19 12:27:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true)
|
2014-06-04 16:47:01 +02:00
|
|
|
public void payForCommand(EventFactionsTitleChange event)
|
2013-04-19 12:27:39 +02:00
|
|
|
{
|
2014-09-17 13:17:33 +02:00
|
|
|
Double cost = MConf.get().econCostTitle;
|
2016-02-25 22:28:09 +01:00
|
|
|
String desc = CmdFactions.get().cmdFactionsTitle.getDesc();
|
2013-04-24 11:16:37 +02:00
|
|
|
|
|
|
|
payForAction(event, cost, desc);
|
2013-04-19 12:27:39 +02:00
|
|
|
}
|
2013-04-19 12:44:28 +02:00
|
|
|
|
|
|
|
@EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true)
|
2014-10-02 11:45:06 +02:00
|
|
|
public void payForCommand(EventFactionsFlagChange event)
|
2013-04-24 11:16:37 +02:00
|
|
|
{
|
2014-10-02 11:45:06 +02:00
|
|
|
Double cost = MConf.get().econCostFlag;
|
2016-02-25 22:28:09 +01:00
|
|
|
String desc = CmdFactions.get().cmdFactionsFlag.getDesc();
|
2013-04-24 11:16:37 +02:00
|
|
|
|
|
|
|
payForAction(event, cost, desc);
|
2013-04-19 12:44:28 +02:00
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2014-09-17 13:17:33 +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
|
|
|
|
2013-04-24 11:16:37 +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)
|
2013-04-24 11:16:37 +02:00
|
|
|
{
|
2014-09-17 13:17:33 +02:00
|
|
|
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
|
|
|
|
2013-04-24 11:16:37 +02:00
|
|
|
payForAction(event, cost, desc);
|
2013-04-19 14:08:45 +02:00
|
|
|
}
|
2013-04-24 11:16:37 +02:00
|
|
|
|
|
|
|
|
2013-04-19 10:07:53 +02:00
|
|
|
}
|