2011-10-05 12:13:54 +02:00
package com.massivecraft.factions.integration ;
Added basic support for iConomy, where most Factions commands can be made to cost (or give) money. For claiming land, there are some extra features. Each additional land claimed by default costs more than the last, with the multiplier being configurable. For example, the first claim might cost $30, the 2nd $45, the third $60, and so forth. When land is claimed from a weakened faction, there is a configurable bonus amount of money deducted from the cost of claiming the land, as an incentive; this number can be changed to a negative value to instead make it cost more to claim such land. When land is unclaimed, a configurable percentage of the cost of claiming the land can be refunded (defaults to 70% of the cost). The total value of a faction's claimed land is now shown in the info given by /f who [faction tag], along with the depreciated (refund) value.
2011-08-02 01:05:01 +02:00
2011-10-13 14:41:07 +02:00
import java.util.HashSet ;
import java.util.Set ;
2013-04-22 09:37:53 +02:00
import com.massivecraft.factions.EconomyParticipator ;
2013-04-09 13:24:55 +02:00
import com.massivecraft.factions.FPerm ;
2014-09-17 13:17:33 +02:00
import com.massivecraft.factions.entity.MConf ;
import com.massivecraft.factions.entity.MPlayer ;
2013-04-22 09:37:53 +02:00
import com.massivecraft.factions.entity.Faction ;
2011-10-12 17:25:01 +02:00
import com.massivecraft.factions.util.RelationUtil ;
2014-06-04 14:02:23 +02:00
import com.massivecraft.massivecore.money.Money ;
2012-01-17 02:38:14 +01:00
2011-10-10 01:21:05 +02:00
public class Econ
{
2013-04-19 09:50:33 +02:00
// -------------------------------------------- //
// STATE
// -------------------------------------------- //
2014-09-17 13:17:33 +02:00
public static boolean isEnabled ( )
2012-02-22 18:43:53 +01:00
{
2014-09-17 13:17:33 +02:00
return MConf . get ( ) . econEnabled & & Money . enabled ( ) ;
2012-02-22 18:43:53 +01:00
}
2013-04-19 09:50:33 +02:00
// -------------------------------------------- //
// UTIL
// -------------------------------------------- //
2014-09-17 13:17:33 +02:00
public static boolean payForAction ( double cost , MPlayer usender , String actionDescription )
2012-02-22 18:43:53 +01:00
{
2014-09-17 13:17:33 +02:00
if ( ! isEnabled ( ) ) return true ;
2013-04-19 09:50:33 +02:00
if ( cost = = 0D ) return true ;
2013-04-25 14:15:25 +02:00
if ( usender . isUsingAdminMode ( ) ) return true ;
2014-09-17 13:17:33 +02:00
2013-04-25 14:15:25 +02:00
Faction usenderFaction = usender . getFaction ( ) ;
2013-04-24 08:39:26 +02:00
2014-09-17 13:17:33 +02:00
if ( MConf . get ( ) . bankEnabled & & MConf . get ( ) . bankFactionPaysCosts & & usenderFaction . isNormal ( ) )
2013-04-19 09:50:33 +02:00
{
2013-04-25 14:15:25 +02:00
return modifyMoney ( usenderFaction , - cost , actionDescription ) ;
2013-04-19 09:50:33 +02:00
}
else
{
2013-04-25 14:15:25 +02:00
return modifyMoney ( usender , - cost , actionDescription ) ;
2013-04-19 09:50:33 +02:00
}
}
// -------------------------------------------- //
// ASSORTED
// -------------------------------------------- //
2012-01-30 06:11:39 +01:00
2013-04-22 09:37:53 +02:00
public static void modifyUniverseMoney ( Object universe , double delta )
2011-10-08 22:03:44 +02:00
{
2014-09-17 13:17:33 +02:00
if ( ! isEnabled ( ) ) return ;
2011-10-13 06:44:59 +02:00
2014-09-17 13:17:33 +02:00
if ( MConf . get ( ) . econUniverseAccount = = null ) return ;
if ( MConf . get ( ) . econUniverseAccount . length ( ) = = 0 ) return ;
2013-04-22 09:37:53 +02:00
2014-09-17 13:17:33 +02:00
if ( ! Money . exists ( MConf . get ( ) . econUniverseAccount ) ) return ;
2012-01-17 02:38:14 +01:00
2014-09-17 13:17:33 +02:00
Money . spawn ( MConf . get ( ) . econUniverseAccount , null , delta ) ;
Added basic support for iConomy, where most Factions commands can be made to cost (or give) money. For claiming land, there are some extra features. Each additional land claimed by default costs more than the last, with the multiplier being configurable. For example, the first claim might cost $30, the 2nd $45, the third $60, and so forth. When land is claimed from a weakened faction, there is a configurable bonus amount of money deducted from the cost of claiming the land, as an incentive; this number can be changed to a negative value to instead make it cost more to claim such land. When land is unclaimed, a configurable percentage of the cost of claiming the land can be refunded (defaults to 70% of the cost). The total value of a faction's claimed land is now shown in the info given by /f who [faction tag], along with the depreciated (refund) value.
2011-08-02 01:05:01 +02:00
}
2012-01-17 02:38:14 +01:00
2014-09-17 13:17:33 +02:00
public static void sendBalanceInfo ( MPlayer to , EconomyParticipator about )
2011-10-13 14:41:07 +02:00
{
2013-12-03 12:59:46 +01:00
to . msg ( " <a>%s's<i> balance is <h>%s<i>. " , about . describeTo ( to , true ) , Money . format ( Money . get ( about ) ) ) ;
2011-10-13 14:41:07 +02:00
}
2012-01-17 02:38:14 +01:00
2011-10-13 14:41:07 +02:00
public static boolean canIControllYou ( EconomyParticipator i , EconomyParticipator you )
2011-10-08 22:03:44 +02:00
{
2011-10-23 02:43:25 +02:00
Faction fI = RelationUtil . getFaction ( i ) ;
Faction fYou = RelationUtil . getFaction ( you ) ;
2011-10-12 17:25:01 +02:00
// This is a system invoker. Accept it.
2011-10-23 02:43:25 +02:00
if ( fI = = null ) return true ;
2011-10-12 17:25:01 +02:00
// Bypassing players can do any kind of transaction
2014-09-17 13:17:33 +02:00
if ( i instanceof MPlayer & & ( ( MPlayer ) i ) . isUsingAdminMode ( ) ) return true ;
2011-10-12 17:25:01 +02:00
// You can deposit to anywhere you feel like. It's your loss if you can't withdraw it again.
2011-10-13 14:41:07 +02:00
if ( i = = you ) return true ;
2011-10-12 17:25:01 +02:00
// A faction can always transfer away the money of it's members and its own money...
// This will however probably never happen as a faction does not have free will.
// Ohh by the way... Yes it could. For daily rent to the faction.
2011-10-23 02:43:25 +02:00
if ( i = = fI & & fI = = fYou ) return true ;
2011-10-12 17:25:01 +02:00
2011-10-24 11:56:41 +02:00
// Factions can be controlled by those that have permissions
2013-05-17 08:37:32 +02:00
if ( you instanceof Faction )
{
if ( i instanceof Faction & & FPerm . WITHDRAW . has ( ( Faction ) i , fYou ) ) return true ;
2014-09-17 13:17:33 +02:00
if ( i instanceof MPlayer & & FPerm . WITHDRAW . has ( ( MPlayer ) i , fYou , false ) ) return true ;
2013-05-17 08:37:32 +02:00
}
2011-10-12 17:25:01 +02:00
// Otherwise you may not! ;,,;
return false ;
2011-09-22 11:22:24 +02:00
}
2011-10-12 17:25:01 +02:00
public static boolean transferMoney ( EconomyParticipator invoker , EconomyParticipator from , EconomyParticipator to , double amount )
2012-01-17 02:38:14 +01:00
{
2013-12-03 20:19:55 +01:00
return transferMoney ( from , to , invoker , amount , true ) ;
2012-01-17 02:38:14 +01:00
}
2013-12-03 20:19:55 +01:00
public static boolean transferMoney ( EconomyParticipator from , EconomyParticipator to , EconomyParticipator by , double amount , boolean notify )
2011-10-08 22:03:44 +02:00
{
2014-09-17 13:17:33 +02:00
if ( ! isEnabled ( ) ) return false ;
2011-10-13 06:44:59 +02:00
2011-10-12 17:25:01 +02:00
// The amount must be positive.
// If the amount is negative we must flip and multiply amount with -1.
if ( amount < 0 )
2011-10-08 22:03:44 +02:00
{
2011-10-12 17:25:01 +02:00
amount * = - 1 ;
EconomyParticipator temp = from ;
from = to ;
to = temp ;
Added basic support for iConomy, where most Factions commands can be made to cost (or give) money. For claiming land, there are some extra features. Each additional land claimed by default costs more than the last, with the multiplier being configurable. For example, the first claim might cost $30, the 2nd $45, the third $60, and so forth. When land is claimed from a weakened faction, there is a configurable bonus amount of money deducted from the cost of claiming the land, as an incentive; this number can be changed to a negative value to instead make it cost more to claim such land. When land is unclaimed, a configurable percentage of the cost of claiming the land can be refunded (defaults to 70% of the cost). The total value of a faction's claimed land is now shown in the info given by /f who [faction tag], along with the depreciated (refund) value.
2011-08-02 01:05:01 +02:00
}
2011-10-12 17:25:01 +02:00
// Check the rights
2013-12-07 11:47:44 +01:00
if ( ! canIControllYou ( by , from ) )
{
by . msg ( " <h>%s<i> lacks permission to control <h>%s's<i> money. " , by . describeTo ( by , true ) , from . describeTo ( by ) ) ;
return false ;
}
2011-10-12 17:25:01 +02:00
// Is there enough money for the transaction to happen?
2013-04-22 09:37:53 +02:00
if ( Money . get ( from ) < amount )
2011-10-12 17:25:01 +02:00
{
// There was not enough money to pay
2013-12-03 20:19:55 +01:00
if ( by ! = null & & notify )
2013-04-19 09:50:33 +02:00
{
2013-12-03 20:19:55 +01:00
by . msg ( " <h>%s<b> can't afford to transfer <h>%s<b> to %s<b>. " , from . describeTo ( by , true ) , Money . format ( amount ) , to . describeTo ( by ) ) ;
2013-04-19 09:50:33 +02:00
}
2011-10-12 17:25:01 +02:00
return false ;
}
// Transfer money
2013-12-03 20:19:55 +01:00
if ( Money . move ( from , to , by , amount ) )
2013-04-19 09:50:33 +02:00
{
2013-12-03 18:02:15 +01:00
if ( notify )
2013-04-19 09:50:33 +02:00
{
2013-12-03 20:19:55 +01:00
sendTransferInfo ( by , from , to , amount ) ;
2012-12-28 23:46:32 +01:00
}
2013-12-03 18:02:15 +01:00
return true ;
2012-12-28 23:46:32 +01:00
}
2013-12-03 18:02:15 +01:00
else
2013-04-22 09:37:53 +02:00
{
2013-12-03 18:02:15 +01:00
// if we get here something with the transaction failed
2013-12-03 20:19:55 +01:00
if ( by ! = null & & notify )
2013-12-03 18:02:15 +01:00
{
2013-12-03 20:19:55 +01:00
by . msg ( " Unable to transfer %s<b> to <h>%s<b> from <h>%s<b>. " , Money . format ( amount ) , to . describeTo ( by ) , from . describeTo ( by , true ) ) ;
2013-12-03 18:02:15 +01:00
}
return false ;
2013-04-22 09:37:53 +02:00
}
2011-10-13 14:41:07 +02:00
}
2014-09-17 13:17:33 +02:00
public static Set < MPlayer > getUPlayers ( EconomyParticipator ep )
2011-10-13 14:41:07 +02:00
{
2014-09-17 13:17:33 +02:00
Set < MPlayer > uplayers = new HashSet < MPlayer > ( ) ;
2011-10-13 14:41:07 +02:00
if ( ep = = null )
{
// Add nothing
}
2014-09-17 13:17:33 +02:00
else if ( ep instanceof MPlayer )
2011-10-13 14:41:07 +02:00
{
2014-09-17 13:17:33 +02:00
uplayers . add ( ( MPlayer ) ep ) ;
2011-10-13 14:41:07 +02:00
}
else if ( ep instanceof Faction )
{
2013-04-22 19:57:11 +02:00
uplayers . addAll ( ( ( Faction ) ep ) . getUPlayers ( ) ) ;
2011-10-13 14:41:07 +02:00
}
2013-04-22 19:57:11 +02:00
return uplayers ;
2011-10-13 14:41:07 +02:00
}
public static void sendTransferInfo ( EconomyParticipator invoker , EconomyParticipator from , EconomyParticipator to , double amount )
{
2014-09-17 13:17:33 +02:00
Set < MPlayer > recipients = new HashSet < MPlayer > ( ) ;
2013-04-22 19:57:11 +02:00
recipients . addAll ( getUPlayers ( invoker ) ) ;
recipients . addAll ( getUPlayers ( from ) ) ;
recipients . addAll ( getUPlayers ( to ) ) ;
2011-10-13 14:41:07 +02:00
2011-10-12 17:25:01 +02:00
if ( invoker = = null )
{
2014-09-17 13:17:33 +02:00
for ( MPlayer recipient : recipients )
2011-10-13 14:41:07 +02:00
{
2013-12-03 12:59:46 +01:00
recipient . msg ( " <h>%s<i> was transfered from <h>%s<i> to <h>%s<i>. " , Money . format ( amount ) , from . describeTo ( recipient ) , to . describeTo ( recipient ) ) ;
2011-10-13 14:41:07 +02:00
}
}
else if ( invoker = = from )
{
2014-09-17 13:17:33 +02:00
for ( MPlayer recipient : recipients )
2011-10-13 14:41:07 +02:00
{
2013-12-03 12:59:46 +01:00
recipient . msg ( " <h>%s<i> <h>gave %s<i> to <h>%s<i>. " , from . describeTo ( recipient , true ) , Money . format ( amount ) , to . describeTo ( recipient ) ) ;
2011-10-13 14:41:07 +02:00
}
2011-10-12 17:25:01 +02:00
}
2011-10-13 14:41:07 +02:00
else if ( invoker = = to )
2011-10-12 17:25:01 +02:00
{
2014-09-17 13:17:33 +02:00
for ( MPlayer recipient : recipients )
2011-10-13 14:41:07 +02:00
{
2013-12-03 12:59:46 +01:00
recipient . msg ( " <h>%s<i> <h>took %s<i> from <h>%s<i>. " , to . describeTo ( recipient , true ) , Money . format ( amount ) , from . describeTo ( recipient ) ) ;
2011-10-13 14:41:07 +02:00
}
2011-10-12 17:25:01 +02:00
}
else
2011-10-08 22:03:44 +02:00
{
2014-09-17 13:17:33 +02:00
for ( MPlayer recipient : recipients )
2011-10-13 14:41:07 +02:00
{
2013-12-03 12:59:46 +01:00
recipient . msg ( " <h>%s<i> transfered <h>%s<i> from <h>%s<i> to <h>%s<i>. " , invoker . describeTo ( recipient , true ) , Money . format ( amount ) , from . describeTo ( recipient ) , to . describeTo ( recipient ) ) ;
2011-10-13 14:41:07 +02:00
}
Added basic support for iConomy, where most Factions commands can be made to cost (or give) money. For claiming land, there are some extra features. Each additional land claimed by default costs more than the last, with the multiplier being configurable. For example, the first claim might cost $30, the 2nd $45, the third $60, and so forth. When land is claimed from a weakened faction, there is a configurable bonus amount of money deducted from the cost of claiming the land, as an incentive; this number can be changed to a negative value to instead make it cost more to claim such land. When land is unclaimed, a configurable percentage of the cost of claiming the land can be refunded (defaults to 70% of the cost). The total value of a faction's claimed land is now shown in the info given by /f who [faction tag], along with the depreciated (refund) value.
2011-08-02 01:05:01 +02:00
}
}
2012-03-13 15:48:34 +01:00
public static boolean hasAtLeast ( EconomyParticipator ep , double delta , String toDoThis )
{
2014-09-17 13:17:33 +02:00
if ( ! isEnabled ( ) ) return true ;
2012-03-13 15:48:34 +01:00
2013-04-22 09:37:53 +02:00
if ( Money . get ( ep ) < delta )
2012-03-13 15:48:34 +01:00
{
if ( toDoThis ! = null & & ! toDoThis . isEmpty ( ) )
2013-04-22 09:37:53 +02:00
{
2013-12-03 12:59:46 +01:00
ep . msg ( " <h>%s<i> can't afford <h>%s<i> %s. " , ep . describeTo ( ep , true ) , Money . format ( delta ) , toDoThis ) ;
2013-04-22 09:37:53 +02:00
}
2012-03-13 15:48:34 +01:00
return false ;
}
return true ;
}
2013-04-19 09:50:33 +02:00
public static boolean modifyMoney ( EconomyParticipator ep , double delta , String actionDescription )
2011-10-08 22:03:44 +02:00
{
2014-09-17 13:17:33 +02:00
if ( ! isEnabled ( ) ) return false ;
2013-04-19 09:50:33 +02:00
if ( delta = = 0 ) return true ;
2011-10-12 17:25:01 +02:00
String You = ep . describeTo ( ep , true ) ;
2013-04-19 09:50:33 +02:00
boolean hasActionDesctription = ( actionDescription ! = null & & ! actionDescription . isEmpty ( ) ) ;
2012-01-31 18:07:47 +01:00
2013-12-03 20:19:55 +01:00
if ( Money . spawn ( ep , null , delta ) )
2011-10-08 22:03:44 +02:00
{
2013-04-22 09:37:53 +02:00
modifyUniverseMoney ( ep , - delta ) ;
if ( hasActionDesctription )
2013-04-19 09:50:33 +02:00
{
2013-04-22 09:37:53 +02:00
if ( delta > 0 )
2013-04-19 09:50:33 +02:00
{
2013-12-03 12:59:46 +01:00
ep . msg ( " <h>%s<i> gained <h>%s<i> since did %s. " , You , Money . format ( delta ) , actionDescription ) ;
2013-04-19 09:50:33 +02:00
}
2013-04-22 09:37:53 +02:00
else
2013-04-19 09:50:33 +02:00
{
2013-12-03 12:59:46 +01:00
ep . msg ( " <h>%s<i> lost <h>%s<i> since did %s. " , You , Money . format ( - delta ) , actionDescription ) ;
2013-04-19 09:50:33 +02:00
}
2012-12-28 23:46:32 +01:00
}
2013-04-22 09:37:53 +02:00
return true ;
2011-09-22 11:22:24 +02:00
}
2011-10-12 17:25:01 +02:00
else
2011-10-08 22:03:44 +02:00
{
2013-04-22 09:37:53 +02:00
if ( hasActionDesctription )
2011-10-12 17:25:01 +02:00
{
2013-04-22 09:37:53 +02:00
if ( delta > 0 )
2013-04-19 09:50:33 +02:00
{
2013-12-03 12:59:46 +01:00
ep . msg ( " <h>%s<i> would have gained <h>%s<i> since did %s, but the deposit failed. " , You , Money . format ( delta ) , actionDescription ) ;
2013-04-19 09:50:33 +02:00
}
2013-04-22 09:37:53 +02:00
else
2013-04-19 09:50:33 +02:00
{
2013-12-03 12:59:46 +01:00
ep . msg ( " <h>%s<i> can't afford <h>%s<i> to %s. " , You , Money . format ( - delta ) , actionDescription ) ;
2013-04-19 09:50:33 +02:00
}
2011-10-12 17:25:01 +02:00
}
2013-04-22 09:37:53 +02:00
return false ;
2011-09-22 11:22:24 +02:00
}
}
2013-04-24 08:39:26 +02:00
Added basic support for iConomy, where most Factions commands can be made to cost (or give) money. For claiming land, there are some extra features. Each additional land claimed by default costs more than the last, with the multiplier being configurable. For example, the first claim might cost $30, the 2nd $45, the third $60, and so forth. When land is claimed from a weakened faction, there is a configurable bonus amount of money deducted from the cost of claiming the land, as an incentive; this number can be changed to a negative value to instead make it cost more to claim such land. When land is unclaimed, a configurable percentage of the cost of claiming the land can be refunded (defaults to 70% of the cost). The total value of a faction's claimed land is now shown in the info given by /f who [faction tag], along with the depreciated (refund) value.
2011-08-02 01:05:01 +02:00
}