Almost finished money refactoring

This commit is contained in:
Olof Larsson 2011-10-12 17:25:01 +02:00
parent d37a4d6ff7
commit b202acb685
29 changed files with 510 additions and 270 deletions

View File

@ -2,7 +2,7 @@ name: Factions
version: 1.6.0_dev
main: com.massivecraft.factions.P
authors: [Olof Larsson, Brett Flannigan]
softdepend: [PermissionsEx, Permissions, Essentials, EssentialsChat, HeroChat, iChat, LocalAreaChat, nChat, ChatManager, AuthMe, iConomy, Register, Spout, WorldEdit, WorldGuard]
softdepend: [PermissionsEx, Permissions, Essentials, EssentialsChat, HeroChat, iChat, LocalAreaChat, nChat, ChatManager, AuthMe, Register, Spout, WorldEdit, WorldGuard]
permissions:
factions.kit.admin:
description: All faction permissions.

View File

@ -208,7 +208,7 @@ public class Board
} else {
FLocation flocationHere = topLeft.getRelative(dx, dz);
Faction factionHere = getFactionAt(flocationHere);
Relation relation = faction.getRelation(factionHere);
Relation relation = faction.getRelationTo(factionHere);
if (factionHere.isNone()) {
row += ChatColor.GRAY+"-";
} else if (factionHere.isSafeZone()) {
@ -225,7 +225,7 @@ public class Board
if (!fList.containsKey(factionHere.getTag()))
fList.put(factionHere.getTag(), Conf.mapKeyChrs[chrIdx++]);
char tag = fList.get(factionHere.getTag());
row += factionHere.getRelation(faction).getColor() + "" + tag;
row += factionHere.getRelationTo(faction).getColor() + "" + tag;
} else {
row += ChatColor.GRAY+"-";
}

View File

@ -194,9 +194,8 @@ public class Conf
public static String capePeaceful = "https://github.com/MassiveCraft/Factions/raw/master/capes/peaceful.png";
// Economy settings
public static boolean econRegisterEnabled = false;
public static boolean econIConomyEnabled = false;
public static boolean econEssentialsEcoEnabled = false;
public static boolean econEnabled = false;
public static String econUniverseAccount = "";
public static double econCostClaimWilderness = 30.0;
public static double econCostClaimFromFactionBonus = 30.0;
public static double econClaimAdditionalMultiplier = 0.5;

View File

@ -4,13 +4,17 @@ import org.bukkit.ChatColor;
import org.bukkit.Location;
import org.bukkit.entity.Player;
import com.massivecraft.factions.iface.EconomyParticipator;
import com.massivecraft.factions.iface.RelationParticipator;
import com.massivecraft.factions.integration.Econ;
import com.massivecraft.factions.integration.SpoutFeatures;
import com.massivecraft.factions.integration.Worldguard;
import com.massivecraft.factions.struct.ChatMode;
import com.massivecraft.factions.struct.Relation;
import com.massivecraft.factions.struct.Role;
import com.massivecraft.factions.util.RelationUtil;
import com.massivecraft.factions.zcore.persist.PlayerEntity;
import com.nijikokun.register.payment.Method.MethodAccount;
/**
@ -24,7 +28,7 @@ import com.massivecraft.factions.zcore.persist.PlayerEntity;
* This means you can use the == operator. No .equals method necessary.
*/
public class FPlayer extends PlayerEntity
public class FPlayer extends PlayerEntity implements EconomyParticipator
{
//private transient String playerName;
private transient FLocation lastStoodAt = new FLocation(); // Where did this player stand the last time we checked?
@ -116,6 +120,13 @@ public class FPlayer extends PlayerEntity
// FIELD: chatMode
private ChatMode chatMode;
// FIELD: account
public MethodAccount getAccount()
{
if ( ! Econ.shouldBeUsed()) return null;
return Econ.getMethod().getAccount(this.getId());
}
// -------------------------------------------- //
// Construct
// -------------------------------------------- //
@ -325,7 +336,7 @@ public class FPlayer extends PlayerEntity
public String getNameAndRelevant(Faction faction)
{
// Which relation?
Relation rel = this.getRelation(faction);
Relation rel = this.getRelationTo(faction);
// For member we show title
if (rel == Relation.MEMBER) {
@ -359,7 +370,7 @@ public class FPlayer extends PlayerEntity
return "";
}
return this.getRelation(faction).getColor()+getChatTag();
return this.getRelationTo(faction).getColor()+getChatTag();
}
public String getChatTag(FPlayer fplayer)
@ -368,39 +379,48 @@ public class FPlayer extends PlayerEntity
return "";
}
return this.getRelation(fplayer).getColor()+getChatTag();
return this.getRelationTo(fplayer).getColor()+getChatTag();
}
// -------------------------------
// Relation and relation colors
// -------------------------------
public Relation getRelation(Faction faction)
@Override
public String describeTo(RelationParticipator that, boolean ucfirst)
{
return faction.getRelation(this);
return RelationUtil.describeThatToMe(that, this, ucfirst);
}
public Relation getRelation(FPlayer fplayer)
@Override
public String describeTo(RelationParticipator that)
{
return this.getFaction().getRelation(fplayer);
return RelationUtil.describeThatToMe(that, this);
}
@Override
public Relation getRelationTo(RelationParticipator rp)
{
return RelationUtil.getRelationTo(this, rp);
}
@Override
public Relation getRelationTo(RelationParticipator rp, boolean ignorePeaceful)
{
return RelationUtil.getRelationTo(this, rp, ignorePeaceful);
}
public Relation getRelationToLocation()
{
return Board.getFactionAt(new FLocation(this)).getRelation(this);
return Board.getFactionAt(new FLocation(this)).getRelationTo(this);
}
public ChatColor getRelationColor(Faction faction)
@Override
public ChatColor getRelationColor(RelationParticipator rp)
{
return faction.getRelationColor(this);
return RelationUtil.getRelationColor(this, rp);
}
public ChatColor getRelationColor(FPlayer fplayer)
{
return this.getRelation(fplayer).getColor();
}
//----------------------------------------------//
// Health
//----------------------------------------------//
@ -519,17 +539,17 @@ public class FPlayer extends PlayerEntity
public boolean isInAllyTerritory()
{
return Board.getFactionAt(new FLocation(this)).getRelation(this).isAlly();
return Board.getFactionAt(new FLocation(this)).getRelationTo(this).isAlly();
}
public boolean isInNeutralTerritory()
{
return Board.getFactionAt(new FLocation(this)).getRelation(this).isNeutral();
return Board.getFactionAt(new FLocation(this)).getRelationTo(this).isNeutral();
}
public boolean isInEnemyTerritory()
{
return Board.getFactionAt(new FLocation(this)).getRelation(this).isEnemy();
return Board.getFactionAt(new FLocation(this)).getRelationTo(this).isEnemy();
}
public void sendFactionHereMessage()
@ -569,13 +589,15 @@ public class FPlayer extends PlayerEntity
}
// if economy is enabled and they're not on the bypass list, make 'em pay
if (makePay && Econ.enabled() && ! this.isAdminBypassing())
if (makePay && Econ.shouldBeUsed() && ! this.isAdminBypassing())
{
double cost = Conf.econCostLeave;
if ( ! Econ.modifyMoney(this, -cost, "to leave your faction.", "for leaving your faction.")) return;
/*
// pay up
if (cost > 0.0) {
String costString = Econ.moneyString(cost);
if (!Econ.deductMoney(this.getName(), cost)) {
if ( ! Econ.deductMoney(this.getName(), cost)) {
msg("<b>It costs <h>%s<b> to leave your faction, which you can't currently afford.", costString);
return;
}
@ -587,7 +609,7 @@ public class FPlayer extends PlayerEntity
String costString = Econ.moneyString(-cost);
Econ.addMoney(this.getName(), -cost);
msg("<i>You have been paid <h>%s<i> for leaving your faction.", costString);
}
}*/
}
if (myFaction.isNormal())
@ -671,7 +693,7 @@ public class FPlayer extends PlayerEntity
return false;
}
if (otherFaction.getRelation(this) == Relation.ALLY)
if (otherFaction.getRelationTo(this) == Relation.ALLY)
{
if (notifyFailure)
msg("<b>You can't claim the land of your allies.");
@ -723,15 +745,16 @@ public class FPlayer extends PlayerEntity
}
// if economy is enabled and they're not on the bypass list, make 'em pay
if (Econ.enabled() && ! this.isAdminBypassing())
if (Econ.shouldBeUsed() && ! this.isAdminBypassing())
{
double cost = Econ.calculateClaimCost(ownedLand, otherFaction.isNormal());
String costString = Econ.moneyString(cost);
//String costString = Econ.moneyString(cost);
if(Conf.bankFactionPaysLandCosts && this.hasFaction())
{
Faction faction = this.getFaction();
if ( ! Econ.modifyMoney(faction, -cost, "to claim this land", "for claiming this land")) return false;
/*
if( ! faction.removeMoney(cost))
{
msg("<b>It costs <h>%s<b> to claim this land, which your faction can't currently afford.", costString);
@ -741,16 +764,17 @@ public class FPlayer extends PlayerEntity
{
// TODO: Only I can see this right?
msg("%s<i> has paid <h>%s<i> to claim some land.", faction.getTag(this), costString);
}
}*/
}
else
{
if ( ! Econ.deductMoney(this.getId(), cost))
if ( ! Econ.modifyMoney(this, -cost, "to claim this land", "for claiming this land")) return false;
/*if ( ! Econ.deductMoney(this.getId(), cost))
{
msg("<b>Claiming this land will cost <h>%s<b>, which you can't currently afford.", costString);
return false;
}
sendMessage("You have paid "+costString+" to claim this land.");
sendMessage("You have paid "+costString+" to claim this land.");*/
}
}

View File

@ -8,13 +8,17 @@ import org.bukkit.ChatColor;
import org.bukkit.Location;
import org.bukkit.entity.Player;
import com.massivecraft.factions.iface.EconomyParticipator;
import com.massivecraft.factions.iface.RelationParticipator;
import com.massivecraft.factions.integration.Econ;
import com.massivecraft.factions.struct.Relation;
import com.massivecraft.factions.struct.Role;
import com.massivecraft.factions.util.*;
import com.massivecraft.factions.zcore.persist.Entity;
import com.nijikokun.register.payment.Method.MethodAccount;
public class Faction extends Entity
public class Faction extends Entity implements EconomyParticipator
{
// FIELD: relationWish
private Map<String, Relation> relationWish;
@ -106,10 +110,15 @@ public class Faction extends Entity
// FIELD: lastPlayerLoggedOffTime
private transient long lastPlayerLoggedOffTime;
// FIELD: money
// FIELD: account (fake field)
// Bank functions
private double money;
public double getMoney() { return this.money; }
public double money; // Deprecated TODO: Hantera.
public MethodAccount getAccount()
{
return Econ.getMethod().getAccount("faction-"+this.getId());
}
/*public double getMoney() { return this.money; }
public boolean addMoney(double amount)
{
if ( amount > 0.0 )
@ -127,7 +136,7 @@ public class Faction extends Entity
this.money -= amount;
return true;
}
}*/
// -------------------------------------------- //
// Construct
@ -191,6 +200,36 @@ public class Faction extends Entity
// Relation and relation colors TODO
// -------------------------------
@Override
public String describeTo(RelationParticipator that, boolean ucfirst)
{
return RelationUtil.describeThatToMe(that, this, ucfirst);
}
@Override
public String describeTo(RelationParticipator that)
{
return RelationUtil.describeThatToMe(that, this);
}
@Override
public Relation getRelationTo(RelationParticipator rp)
{
return RelationUtil.getRelationTo(rp, this);
}
@Override
public Relation getRelationTo(RelationParticipator rp, boolean ignorePeaceful)
{
return RelationUtil.getRelationTo(rp, this, ignorePeaceful);
}
@Override
public ChatColor getRelationColor(RelationParticipator rp)
{
return RelationUtil.getRelationColor(this, rp);
}
public Relation getRelationWish(Faction otherFaction)
{
if (this.relationWish.containsKey(otherFaction.getId()))
@ -212,12 +251,12 @@ public class Faction extends Entity
}
}
public Relation getRelation(Faction otherFaction)
/*public Relation getRelationTo(Faction otherFaction)
{
return getRelation(otherFaction, false);
return getRelationTo(otherFaction, false);
}
public Relation getRelation(Faction otherFaction, boolean ignorePeaceful)
public Relation getRelationTo(Faction otherFaction, boolean ignorePeaceful)
{
if (!otherFaction.isNormal() || !this.isNormal())
{
@ -242,13 +281,13 @@ public class Faction extends Entity
return this.getRelationWish(otherFaction);
}
public Relation getRelation(FPlayer fplayer)
public Relation getRelationTo(FPlayer fplayer)
{
if (fplayer == null)
return Relation.NEUTRAL;
else
return getRelation(fplayer.getFaction());
}
return getRelationTo(fplayer.getFaction());
}*/
//----------------------------------------------//
// Power
@ -444,12 +483,12 @@ public class Faction extends Entity
public ChatColor getRelationColor(Faction otherFaction)
{
return this.getRelation(otherFaction).getColor();
return this.getRelationTo(otherFaction).getColor();
}
public ChatColor getRelationColor(FPlayer fplayer)
{
return this.getRelation(fplayer).getColor();
return this.getRelationTo(fplayer).getColor();
}
//----------------------------------------------//

View File

@ -22,6 +22,7 @@ import com.massivecraft.factions.listeners.FactionsBlockListener;
import com.massivecraft.factions.listeners.FactionsChatEarlyListener;
import com.massivecraft.factions.listeners.FactionsEntityListener;
import com.massivecraft.factions.listeners.FactionsPlayerListener;
import com.massivecraft.factions.listeners.FactionsServerListener;
import com.massivecraft.factions.struct.ChatMode;
import com.massivecraft.factions.util.MapFLocToStringSetTypeAdapter;
import com.massivecraft.factions.util.MyLocationTypeAdapter;
@ -43,6 +44,7 @@ public class P extends MPlugin
public final FactionsChatEarlyListener chatEarlyListener;
public final FactionsEntityListener entityListener;
public final FactionsBlockListener blockListener;
public final FactionsServerListener serverListener;
// Persistance related
private boolean locked = false;
@ -59,8 +61,11 @@ public class P extends MPlugin
this.chatEarlyListener = new FactionsChatEarlyListener(this);
this.entityListener = new FactionsEntityListener(this);
this.blockListener = new FactionsBlockListener(this);
this.serverListener = new FactionsServerListener(this);
}
public static PermissionHandler Permissions;
private static EssentialsChat essChat;
@ -83,15 +88,15 @@ public class P extends MPlugin
//setupPermissions();
integrateEssentialsChat();
setupSpout(this);
Econ.setup(this);
Econ.monitorPlugins();
Econ.doSetup();
Econ.oldMoneyDoTransfer();
if(Conf.worldGuardChecking)
{
Worldguard.init(this);
}
// Register events
// Player Events
this.registerEvent(Event.Type.PLAYER_CHAT, this.playerListener, Event.Priority.Highest);
this.registerEvent(Event.Type.PLAYER_CHAT, this.chatEarlyListener, Event.Priority.Lowest);
this.registerEvent(Event.Type.PLAYER_COMMAND_PREPROCESS, this.playerListener, Event.Priority.Normal);
@ -103,6 +108,8 @@ public class P extends MPlugin
this.registerEvent(Event.Type.PLAYER_BUCKET_EMPTY, this.playerListener, Event.Priority.Normal);
this.registerEvent(Event.Type.PLAYER_BUCKET_FILL, this.playerListener, Event.Priority.Normal);
this.registerEvent(Event.Type.PLAYER_KICK, this.playerListener, Event.Priority.Normal);
// Entity Events
this.registerEvent(Event.Type.ENDERMAN_PICKUP, this.entityListener, Event.Priority.Normal);
this.registerEvent(Event.Type.ENDERMAN_PLACE, this.entityListener, Event.Priority.Normal);
this.registerEvent(Event.Type.ENTITY_DEATH, this.entityListener, Event.Priority.Normal);
@ -112,12 +119,18 @@ public class P extends MPlugin
this.registerEvent(Event.Type.ENTITY_TARGET, this.entityListener, Event.Priority.Normal);
this.registerEvent(Event.Type.PAINTING_BREAK, this.entityListener, Event.Priority.Normal);
this.registerEvent(Event.Type.PAINTING_PLACE, this.entityListener, Event.Priority.Normal);
// Block Events
this.registerEvent(Event.Type.BLOCK_BREAK, this.blockListener, Event.Priority.Normal);
this.registerEvent(Event.Type.BLOCK_DAMAGE, this.blockListener, Event.Priority.Normal);
this.registerEvent(Event.Type.BLOCK_PLACE, this.blockListener, Event.Priority.Normal);
this.registerEvent(Event.Type.BLOCK_PISTON_EXTEND, this.blockListener, Event.Priority.Normal);
this.registerEvent(Event.Type.BLOCK_PISTON_RETRACT, this.blockListener, Event.Priority.Normal);
// Server Events
this.registerEvent(Event.Type.PLUGIN_ENABLE, this.serverListener, Event.Priority.Monitor);
this.registerEvent(Event.Type.PLUGIN_DISABLE, this.serverListener, Event.Priority.Monitor);
postEnable();
}

View File

@ -48,7 +48,7 @@ public class CmdBalance extends FCommand
return;
}
msg("<a>%s balance: %s", faction.getTag(fme), Econ.moneyString(faction.getMoney()));
msg("<a>%s balance: %s", faction.getTag(fme), Econ.moneyString(faction.getAccount().balance()));
}
}

View File

@ -3,10 +3,6 @@ package com.massivecraft.factions.cmd;
import com.massivecraft.factions.Conf;
import com.massivecraft.factions.integration.Econ;
import com.massivecraft.factions.struct.Permission;
import com.massivecraft.factions.FPlayers;
import com.massivecraft.factions.Faction;
import com.massivecraft.factions.P;
import com.massivecraft.factions.FPlayer;
public class CmdDeposit extends FCommand
@ -33,11 +29,8 @@ public class CmdDeposit extends FCommand
public void perform()
{
if ( ! Conf.bankEnabled) return;
Faction faction = myFaction;
double amount = this.argAsDouble(0, 0);
Econ.transferMoney(fme, fme, myFaction, this.argAsDouble(0, 0));
/*
if( amount > 0.0 )
{
String amountString = Econ.moneyString(amount);
@ -61,7 +54,7 @@ public class CmdDeposit extends FCommand
}
}
}
}
}*/
}
}

View File

@ -1,6 +1,5 @@
package com.massivecraft.factions.cmd;
import com.massivecraft.factions.Conf;
import com.massivecraft.factions.integration.Econ;
import com.massivecraft.factions.FPlayers;
import com.massivecraft.factions.Faction;
@ -71,10 +70,13 @@ public class CmdDisband extends FCommand
}
}
if (Conf.bankEnabled)
if (Econ.shouldBeUsed())
{
double amount = faction.getMoney();
Econ.addMoney(fme.getId(), amount); //Give all the faction's money to the disbander
//Give all the faction's money to the disbander
double amount = faction.getAccount().balance();
fme.getAccount().add(amount);
faction.getAccount().remove();
if (amount > 0.0)
{
String amountString = Econ.moneyString(amount);

View File

@ -2,7 +2,6 @@ package com.massivecraft.factions.cmd;
import java.util.ArrayList;
import com.massivecraft.factions.Conf;
import com.massivecraft.factions.P;
import com.massivecraft.factions.integration.Econ;
import com.massivecraft.factions.struct.Permission;
@ -84,7 +83,7 @@ public class CmdHelp extends FCommand
pageLines.add( p.cmdBase.cmdSethome.getUseageTemplate() );
helpPages.add(pageLines);
if (Econ.enabled() && Conf.bankEnabled)
if (Econ.shouldBeUsed())
{
pageLines = new ArrayList<String>();
pageLines.add( "" );

View File

@ -101,7 +101,7 @@ public class CmdHome extends FCommand
continue;
FPlayer fp = FPlayers.i.get(p);
if (fme.getRelation(fp) != Relation.ENEMY)
if (fme.getRelationTo(fp) != Relation.ENEMY)
continue;
Location l = p.getLocation();

View File

@ -49,16 +49,16 @@ public class CmdPay extends FCommand
{
String amountString = Econ.moneyString(amount);
if( amount > us.getMoney() )
if( amount > us.getAccount().balance() )
{
amount = us.getMoney();
amount = us.getAccount().balance();
}
us.removeMoney(amount);
them.addMoney(amount);
us.getAccount().subtract(amount);
them.getAccount().add(amount);
msg("<i>You have paid "+amountString+" from "+us.getTag()+"'s bank to "+them.getTag()+"'s bank.");
msg("<i>"+us.getTag()+" now has "+Econ.moneyString(us.getMoney()));
msg("<i>"+us.getTag()+" now has "+Econ.moneyString(us.getAccount().balance()));
P.p.log(fme.getName() + " paid "+amountString+" from "+us.getTag()+"'s bank to "+them.getTag()+"'s bank.");
for (FPlayer fplayer : FPlayers.i.getOnline())

View File

@ -69,7 +69,7 @@ public class CmdShow extends FCommand
}
// show the land value
if (Econ.enabled())
if (Econ.shouldBeUsed())
{
double value = Econ.calculateTotalLandValue(faction.getLandRounded());
double refund = value * Conf.econClaimRefundMultiplier;
@ -82,7 +82,7 @@ public class CmdShow extends FCommand
//Show bank contents
if(Conf.bankEnabled) {
msg("<a>Bank contains: <i>"+Econ.moneyString(faction.getMoney()));
msg("<a>Bank contains: <i>"+Econ.moneyString(faction.getAccount().balance()));
}
}
@ -98,11 +98,11 @@ public class CmdShow extends FCommand
continue;
}
listpart = otherFaction.getTag(fme)+p.txt.parse("<i>")+", ";
if (otherFaction.getRelation(faction).isAlly())
if (otherFaction.getRelationTo(faction).isAlly())
{
allyList += listpart;
}
else if (otherFaction.getRelation(faction).isEnemy())
else if (otherFaction.getRelationTo(faction).isEnemy())
{
enemyList += listpart;
}

View File

@ -29,7 +29,7 @@ public class CmdUnclaimall extends FCommand
public void perform()
{
String moneyBack = "<i>";
if (Econ.enabled())
if (Econ.shouldBeUsed())
{
double refund = Econ.calculateTotalLandRefund(myFaction.getLandRounded());
// a real refund

View File

@ -2,12 +2,7 @@ package com.massivecraft.factions.cmd;
import com.massivecraft.factions.Conf;
import com.massivecraft.factions.integration.Econ;
import com.massivecraft.factions.FPlayers;
import com.massivecraft.factions.Faction;
import com.massivecraft.factions.P;
import com.massivecraft.factions.FPlayer;
import com.massivecraft.factions.struct.Permission;
import com.massivecraft.factions.struct.Role;
public class CmdWithdraw extends FCommand
@ -34,7 +29,9 @@ public class CmdWithdraw extends FCommand
{
if ( ! Conf.bankEnabled) return;
if ( ! Conf.bankMembersCanWithdraw && ! assertMinRole(Role.MODERATOR))
Econ.transferMoney(fme, myFaction, fme, this.argAsDouble(0, 0));
/*if ( ! Conf.bankMembersCanWithdraw && ! assertMinRole(Role.MODERATOR))
{
msg("<b>Only faction moderators or admins are able to withdraw from the bank.");
return;
@ -69,7 +66,7 @@ public class CmdWithdraw extends FCommand
fplayer.msg("%s<i> has withdrawn %s", fme.getNameAndRelevant(fplayer), amountString);
}
}
}
}*/
}
}

View File

@ -275,14 +275,25 @@ public abstract class FCommand extends MCommand<P>
// if economy is enabled and they're not on the bypass list, make 'em pay; returns true unless person can't afford the cost
public boolean payForCommand(double cost)
{
if ( ! Econ.enabled() || this.fme == null || cost == 0.0 || fme.isAdminBypassing())
if ( ! Econ.shouldBeUsed() || this.fme == null || cost == 0.0 || fme.isAdminBypassing())
{
return true;
}
String desc = this.getHelpShort().toLowerCase();
Faction faction = fme.getFaction();
if(Conf.bankFactionPaysLandCosts && fme.hasFaction())
{
if ( ! Econ.modifyMoney(myFaction, -cost, "to "+desc, "for "+desc)) return false;
}
else
{
if ( ! Econ.modifyMoney(fme, -cost, "to "+desc, "for "+desc)) return false;
}
return true;
/*
// pay up
if (cost > 0.0)
@ -290,7 +301,7 @@ public abstract class FCommand extends MCommand<P>
String costString = Econ.moneyString(cost);
if(Conf.bankFactionPaysCosts && fme.hasFaction() )
{
if(!faction.removeMoney(cost))
if( ! faction.getAccount().subtract(cost))
{
sendMessage("It costs "+costString+" to "+desc+", which your faction can't currently afford.");
return false;
@ -303,7 +314,7 @@ public abstract class FCommand extends MCommand<P>
}
else
{
if (!Econ.deductMoney(fme.getName(), cost))
if ( ! Econ.deductMoney(fme.getName(), cost))
{
sendMessage("It costs "+costString+" to "+desc+", which you can't currently afford.");
return false;
@ -318,7 +329,7 @@ public abstract class FCommand extends MCommand<P>
if(Conf.bankFactionPaysCosts && fme.hasFaction() )
{
faction.addMoney(-cost);
faction.getAccount().add(-cost);
sendMessage(faction.getTag()+" has been paid "+costString+" to "+desc+".");
}
else
@ -329,6 +340,6 @@ public abstract class FCommand extends MCommand<P>
sendMessage("You have been paid "+costString+" to "+desc+".");
}
return true;
return true;*/
}
}

View File

@ -49,7 +49,7 @@ public abstract class FRelationCommand extends FCommand
if ( ! payForCommand(targetRelation.getRelationCost())) return;
myFaction.setRelationWish(them, targetRelation);
Relation currentRelation = myFaction.getRelation(them, true);
Relation currentRelation = myFaction.getRelationTo(them, true);
ChatColor currentRelationColor = currentRelation.getColor();
if (targetRelation.value == currentRelation.value)
{

View File

@ -0,0 +1,10 @@
package com.massivecraft.factions.iface;
import com.nijikokun.register.payment.Method.MethodAccount;
public interface EconomyParticipator extends RelationParticipator
{
public MethodAccount getAccount();
public void msg(String str, Object... args);
}

View File

@ -0,0 +1,16 @@
package com.massivecraft.factions.iface;
import org.bukkit.ChatColor;
import com.massivecraft.factions.struct.Relation;
public interface RelationParticipator
{
public String describeTo(RelationParticipator that);
public String describeTo(RelationParticipator that, boolean ucfirst);
public Relation getRelationTo(RelationParticipator that);
public Relation getRelationTo(RelationParticipator that, boolean ignorePeaceful);
public ChatColor getRelationColor(RelationParticipator to);
}

View File

@ -1,172 +1,214 @@
package com.massivecraft.factions.integration;
import org.bukkit.event.Event;
import org.bukkit.Bukkit;
import org.bukkit.plugin.Plugin;
import com.massivecraft.factions.listeners.FactionsServerListener;
import com.earth2me.essentials.api.Economy;
import com.nijikokun.register.payment.Methods;
import com.nijikokun.register.Register;
import com.nijikokun.register.payment.Method;
import com.nijikokun.register.payment.Method.MethodAccount;
import com.iConomy.*;
import com.iConomy.system.*;
import com.nijikokun.register.payment.Methods;
import com.massivecraft.factions.Conf;
import com.massivecraft.factions.FPlayer;
import com.massivecraft.factions.Faction;
import com.massivecraft.factions.Factions;
import com.massivecraft.factions.P;
import com.massivecraft.factions.iface.EconomyParticipator;
import com.massivecraft.factions.struct.Role;
import com.massivecraft.factions.util.RelationUtil;
public class Econ
{
private static boolean registerUse = false;
private static boolean iConomyUse = false;
private static boolean essEcoUse = false;
// TODO: WHY put this here instead of at the same place as the other listeners?
public static void monitorPlugins()
private static Register register = null;
public static Method getMethod()
{
P.p.getServer().getPluginManager().registerEvent(Event.Type.PLUGIN_ENABLE, new FactionsServerListener(P.p), Event.Priority.Monitor, P.p);
P.p.getServer().getPluginManager().registerEvent(Event.Type.PLUGIN_DISABLE, new FactionsServerListener(P.p), Event.Priority.Monitor, P.p);
if ( ! isSetup()) return null;
return Methods.getMethod();
}
public static void setup(P factions)
public static boolean shouldBeUsed()
{
if (enabled())
return Conf.econEnabled && getMethod() != null;
}
public static boolean isSetup()
{
return register != null && register.isEnabled();
}
public static void doSetup()
{
if (isSetup()) return;
Plugin plug = Bukkit.getServer().getPluginManager().getPlugin("Register");
if (plug != null && plug.getClass().getName().equals("com.nijikokun.register.Register") && plug.isEnabled())
{
return;
}
if (!registerHooked())
{
Plugin plug = factions.getServer().getPluginManager().getPlugin("Register");
if (plug != null && plug.getClass().getName().equals("com.nijikokun.register.Register") && plug.isEnabled())
P.p.log("Integration with Register (economy): successful");
if ( ! Conf.econEnabled)
{
registerSet(true);
P.p.log("NOTE: Economy is disabled. Enable in conf \"econRegisterEnabled\": true");
}
}
if (!iConomyHooked())
{
Plugin plug = factions.getServer().getPluginManager().getPlugin("iConomy");
if (plug != null && plug.getClass().getName().equals("com.iConomy.iConomy") && plug.isEnabled())
{
iConomySet(true);
}
}
if (!essentialsEcoHooked())
{
Plugin plug = factions.getServer().getPluginManager().getPlugin("Essentials");
if (plug != null && plug.isEnabled())
{
essentialsEcoSet(true);
}
}
}
public static void registerSet(boolean enable)
{
registerUse = enable;
if (enable) {
P.p.log("Register hook available, "+(Conf.econRegisterEnabled ? "and interface is enabled" : "but disabled (\"econRegisterEnabled\": false)")+".");
}
else {
P.p.log("Un-hooked from Register.");
}
P.p.cmdBase.cmdHelp.updateHelp();
}
public static void iConomySet(boolean enable)
{
iConomyUse = enable;
if (enable && !registerUse) {
P.p.log("iConomy hook available, "+(Conf.econIConomyEnabled ? "and interface is enabled" : "but disabled (\"econIConomyEnabled\": false)")+".");
}
else {
P.p.log("Un-hooked from iConomy.");
}
P.p.cmdBase.cmdHelp.updateHelp();
}
public static void essentialsEcoSet(boolean enable)
{
essEcoUse = enable;
if (enable && !registerUse)
{
P.p.log("EssentialsEco hook available, "+(Conf.econEssentialsEcoEnabled ? "and interface is enabled" : "but disabled (\"econEssentialsEcoEnabled\": false)")+".");
}
else
{
P.p.log("Un-hooked from EssentialsEco.");
P.p.log("Integration with Register (economy): failed");
}
P.p.cmdBase.cmdHelp.updateHelp();
}
public static boolean registerHooked()
public static MethodAccount getUniverseAccount()
{
return registerUse;
if (Conf.econUniverseAccount == null) return null;
if (Conf.econUniverseAccount.length() == 0) return null;
return getMethod().getAccount(Conf.econUniverseAccount);
}
public static boolean iConomyHooked()
public static void modifyUniverseMoney(double delta)
{
return iConomyUse;
MethodAccount acc = getUniverseAccount();
if (acc == null) return;
acc.add(delta);
}
public static boolean essentialsEcoHooked()
public static boolean canInvokerTransferFrom(EconomyParticipator invoker, EconomyParticipator from)
{
return essEcoUse;
Faction fInvoker = RelationUtil.getFaction(invoker);
Faction fFrom = RelationUtil.getFaction(from);
// This is a system invoker. Accept it.
if (fInvoker == null) return true;
// Bypassing players can do any kind of transaction
if (invoker instanceof FPlayer && ((FPlayer)invoker).isAdminBypassing()) return true;
// You can deposit to anywhere you feel like. It's your loss if you can't withdraw it again.
if (invoker == from) return true;
// 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.
if (invoker == fInvoker && fInvoker == fFrom) return true;
// If you are part of the same faction as from and members can withdraw or you are at least moderator... then it is ok.
if (fInvoker == fFrom && (Conf.bankMembersCanWithdraw || ((FPlayer)invoker).getRole().value < Role.MODERATOR.value)) return true;
// Otherwise you may not! ;,,;
invoker.msg("<h>%s<b> don't have the right to transfer money from <h>%s<b>.", invoker.describeTo(invoker, true), from.describeTo(invoker));
return false;
}
public static boolean registerAvailable()
public static boolean transferMoney(EconomyParticipator invoker, EconomyParticipator from, EconomyParticipator to, double amount)
{
return Conf.econRegisterEnabled && registerUse && Methods.hasMethod();
}
// If economy is enabled in conf.json, and we're successfully hooked into an economy plugin
public static boolean enabled()
{
return (Conf.econRegisterEnabled && registerUse && Methods.hasMethod())
|| (Conf.econIConomyEnabled && iConomyUse)
|| (Conf.econEssentialsEcoEnabled && essEcoUse);
}
// mainly for internal use, for a little less code repetition
public static Holdings getIconomyHoldings(String playerName)
{
if ( ! enabled())
// The amount must be positive.
// If the amount is negative we must flip and multiply amount with -1.
if (amount < 0)
{
return null;
amount *= -1;
EconomyParticipator temp = from;
from = to;
to = temp;
}
Account account = iConomy.getAccount(playerName);
if (account == null)
// Check the rights
if ( ! canInvokerTransferFrom(invoker, from)) return false;
//Faction fFrom = RelationUtil.getFaction(from);
//Faction fTo = RelationUtil.getFaction(to);
//Faction fInvoker = RelationUtil.getFaction(invoker);
// Is there enough money for the transaction to happen?
if ( ! from.getAccount().hasEnough(amount))
{
return null;
// There was not enough money to pay
if (invoker != null)
{
invoker.msg("<h>%s<b> can't afford to transfer <h>%s<b> to %s.", from.describeTo(invoker, true), moneyString(amount), to.describeTo(invoker));
}
return false;
}
Holdings holdings = account.getHoldings();
return holdings;
// Transfer money
from.getAccount().subtract(amount);
to.getAccount().add(amount);
// Inform
if (invoker == null)
{
from.msg("<h>%s<i> was transfered from <h>%s<i> to <h>%s<i>.", moneyString(amount), from.describeTo(from), to.describeTo(from));
to.msg ("<h>%s<i> was transfered from <h>%s<i> to <h>%s<i>.", moneyString(amount), from.describeTo(to), to.describeTo(to));
}
else if (invoker == from || invoker == to)
{
from.msg("<h>%s<i> transfered <h>%s<i> to <h>%s<i>.", from.describeTo(from), moneyString(amount), to.describeTo(from));
to.msg ("<h>%s<i> transfered <h>%s<i> to <h>%s<i>.", from.describeTo(to), moneyString(amount), to.describeTo(to));
}
else
{
from.msg("<h>%s<b> was transfered from <h>%s<b> to <h>%s<b> by <h>%s<g>.", moneyString(amount), from.describeTo(from), to.describeTo(from), invoker.describeTo(from));
to.msg ("<h>%s<g> was transfered from <h>%s<g> to <h>%s<g> by <h>%s<g>.", moneyString(amount), from.describeTo(to), to.describeTo(to), invoker.describeTo(to));
}
return true;
}
public static MethodAccount getRegisterAccount(String playerName)
public static boolean modifyMoney(EconomyParticipator ep, double delta, String toDoThis, String forDoingThis)
{
if (!enabled())
MethodAccount acc = ep.getAccount();
String You = ep.describeTo(ep, true);
if (delta >= 0)
{
return null;
// The player should gain money
// There is no risk of failure
acc.add(delta);
modifyUniverseMoney(-delta);
ep.msg("<h>%<g> gained %s<i> %s.", You, moneyString(delta), forDoingThis);
return true;
}
if (!Methods.getMethod().hasAccount(playerName))
else
{
return null;
// The player should loose money
// The player might not have enough.
if (acc.hasEnough(-delta))
{
// There is enough money to pay
acc.add(delta);
modifyUniverseMoney(-delta);
ep.msg("<h>%<b> lost %s<i> %s.", You, moneyString(-delta), forDoingThis);
return true;
}
else
{
// There was not enough money to pay
ep.msg("<h>%<g> can't afford %s<i> %s.", You, moneyString(-delta), toDoThis);
return false;
}
}
MethodAccount account = Methods.getMethod().getAccount(playerName);
return account;
}
// format money string based on server's set currency type, like "24 gold" or "$24.50"
public static String moneyString(double amount)
{
return registerAvailable() ? Methods.getMethod().format(amount)
: (iConomyUse ? iConomy.format(amount) : Economy.format(amount));
return getMethod().format(amount);
}
public static void oldMoneyDoTransfer()
{
if ( ! shouldBeUsed()) return;
for (Faction faction : Factions.i.get())
{
faction.getAccount().add(faction.money);
faction.money = 0;
}
}
// whether a player can afford specified amount
public static boolean canAfford(String playerName, double amount) {
/*public static boolean canAfford(String playerName, double amount) {
// if Economy support is not enabled, they can certainly afford to pay nothing
if (!enabled())
{
@ -204,10 +246,10 @@ public class Econ
return false;
}
}
}
}*/
// deduct money from their account; returns true if successful
public static boolean deductMoney(String playerName, double amount)
/*public static boolean deductMoney(String playerName, double amount)
{
if (!enabled())
{
@ -251,10 +293,10 @@ public class Econ
return false;
}
}
}
}*/
// add money to their account; returns true if successful
public static boolean addMoney(String playerName, double amount)
/*public static boolean addMoney(String playerName, double amount)
{
if (!enabled())
{
@ -294,15 +336,15 @@ public class Econ
return false;
}
}
}
}*/
// calculate the cost for claiming land
public static double calculateClaimCost(int ownedLand, boolean takingFromAnotherFaction)
{
if (!enabled())
if ( ! shouldBeUsed())
{
return 0.0;
return 0d;
}
// basic claim cost, plus land inflation cost, minus the potential bonus given for claiming from another faction

View File

@ -101,7 +101,7 @@ public class SpoutFeatures
factionA = playerA.getFaction();
for (FPlayer playerB : players)
{
updateSingle(playerB.getPlayer(), playerA.getPlayer(), factionA.getRelation(playerB), factionA, playerA.getTitle(), playerA.getRole());
updateSingle(playerB.getPlayer(), playerA.getPlayer(), factionA.getRelationTo(playerB), factionA, playerA.getTitle(), playerA.getRole());
}
}
}
@ -121,7 +121,7 @@ public class SpoutFeatures
for (FPlayer playerB : players)
{
Player player2 = playerB.getPlayer();
Relation rel = factionA.getRelation(playerB);
Relation rel = factionA.getRelationTo(playerB);
updateSingle(player2, player, rel, factionA, playerA.getTitle(), playerA.getRole());
updateSingle(player, player2, rel, playerB.getFaction(), playerB.getTitle(), playerB.getRole());
}
@ -149,7 +149,7 @@ public class SpoutFeatures
{
continue;
}
updateSingle(playerB.getPlayer(), playerA.getPlayer(), factionA.getRelation(factionB), factionA, playerA.getTitle(), playerA.getRole());
updateSingle(playerB.getPlayer(), playerA.getPlayer(), factionA.getRelationTo(factionB), factionA, playerA.getTitle(), playerA.getRole());
}
}
}
@ -168,7 +168,7 @@ public class SpoutFeatures
{
Player player1 = playerA.getPlayer();
Player player2 = playerB.getPlayer();
Relation rel = factionA.getRelation(factionB);
Relation rel = factionA.getRelationTo(factionB);
updateSingle(player2, player1, rel, factionA, playerA.getTitle(), playerA.getRole());
updateSingle(player1, player2, rel, factionB, playerB.getTitle(), playerB.getRole());
}

View File

@ -156,7 +156,7 @@ public class FactionsBlockListener extends BlockListener
return false;
}
Relation rel = pistonFaction.getRelation(otherFaction);
Relation rel = pistonFaction.getRelationTo(otherFaction);
boolean online = otherFaction.hasPlayersOnline();
if
@ -222,7 +222,7 @@ public class FactionsBlockListener extends BlockListener
}
Faction myFaction = me.getFaction();
Relation rel = myFaction.getRelation(otherFaction);
Relation rel = myFaction.getRelationTo(otherFaction);
boolean ownershipFail = Conf.ownedAreasEnabled && (Conf.ownedAreaDenyBuild || Conf.ownedAreaPainBuild) && !otherFaction.playerHasOwnershipRights(me, loc);
// Cancel and/or cause pain (depending on configuration) if we are not in our own territory

View File

@ -80,7 +80,7 @@ public class FactionsChatEarlyListener extends PlayerListener
myFaction.sendMessage(message);
for (FPlayer fplayer : FPlayers.i.getOnline())
{
if(myFaction.getRelation(fplayer) == Relation.ALLY)
if(myFaction.getRelationTo(fplayer) == Relation.ALLY)
{
//Send to all our allies
fplayer.sendMessage(message);

View File

@ -317,7 +317,7 @@ public class FactionsEntityListener extends EntityListener
return false;
}
Relation relation = defendFaction.getRelation(attackFaction);
Relation relation = defendFaction.getRelationTo(attackFaction);
// You can not hurt neutral factions
if (Conf.disablePVPBetweenNeutralFactions && relation.isNeutral())
@ -477,7 +477,7 @@ public class FactionsEntityListener extends EntityListener
}
Faction myFaction = me.getFaction();
Relation rel = myFaction.getRelation(otherFaction);
Relation rel = myFaction.getRelationTo(otherFaction);
boolean ownershipFail = Conf.ownedAreasEnabled && Conf.ownedAreaDenyBuild && !otherFaction.playerHasOwnershipRights(me, loc);
// Cancel if we are not in our own territory and building should be denied

View File

@ -385,7 +385,7 @@ public class FactionsPlayerListener extends PlayerListener
}
Faction myFaction = me.getFaction();
Relation rel = myFaction.getRelation(otherFaction);
Relation rel = myFaction.getRelationTo(otherFaction);
boolean ownershipFail = Conf.ownedAreasEnabled && Conf.ownedAreaDenyUseage && !otherFaction.playerHasOwnershipRights(me, loc);
// Cancel if we are not in our own territory
@ -446,7 +446,7 @@ public class FactionsPlayerListener extends PlayerListener
Faction myFaction = me.getFaction();
Relation rel = myFaction.getRelation(otherFaction);
Relation rel = myFaction.getRelationTo(otherFaction);
boolean ownershipFail = Conf.ownedAreasEnabled && Conf.ownedAreaProtectMaterials && !otherFaction.playerHasOwnershipRights(me, loc);
// You may use any block unless it is another faction's territory...

View File

@ -6,7 +6,6 @@ import org.bukkit.event.server.PluginDisableEvent;
import org.bukkit.event.server.PluginEnableEvent;
import com.massivecraft.factions.P;
import com.massivecraft.factions.integration.Econ;
import com.massivecraft.factions.integration.SpoutFeatures;
@ -22,19 +21,7 @@ public class FactionsServerListener extends ServerListener
public void onPluginDisable(PluginDisableEvent event)
{
String name = event.getPlugin().getDescription().getName();
if (Econ.registerHooked() && name.equals("Register"))
{
Econ.registerSet(false);
}
else if (Econ.iConomyHooked() && name.equals("iConomy"))
{
Econ.iConomySet(false);
}
else if (Econ.essentialsEcoHooked() && name.equals("Essentials"))
{
Econ.essentialsEcoSet(false);
}
else if (name.equals("Spout"))
if (name.equals("Spout"))
{
SpoutFeatures.setAvailable(false, "");
}
@ -45,19 +32,7 @@ public class FactionsServerListener extends ServerListener
{
Plugin plug = event.getPlugin();
String name = plug.getDescription().getName();
if ( ! Econ.registerHooked() && name.equals("Register") && plug.getClass().getName().equals("com.nijikokun.register.Register"))
{
Econ.registerSet(true);
}
else if ( ! Econ.iConomyHooked() && name.equals("iConomy") && plug.getClass().getName().equals("com.iConomy.iConomy"))
{
Econ.iConomySet(true);
}
else if ( ! Econ.essentialsEcoHooked() && name.equals("Essentials"))
{
Econ.essentialsEcoSet(true);
}
else if (name.equals("Spout"))
if (name.equals("Spout"))
{
SpoutFeatures.setAvailable(true, plug.getDescription().getFullName());
}

View File

@ -15,7 +15,8 @@ public enum Relation
public final int value;
public final String nicename;
private Relation(final int value, final String nicename) {
private Relation(final int value, final String nicename)
{
this.value = value;
this.nicename = nicename;
}
@ -29,22 +30,22 @@ public enum Relation
// TODO: Insane way to use enums!!!?
public boolean isMember()
{
return this.value == MEMBER.value;
return this == MEMBER;
}
public boolean isAlly()
{
return this.value == ALLY.value;
return this == ALLY;
}
public boolean isNeutral()
{
return this.value == NEUTRAL.value;
return this == NEUTRAL;
}
public boolean isEnemy()
{
return this.value == ENEMY.value;
return this == ENEMY;
}
public boolean isAtLeast(Relation relation)
@ -59,15 +60,15 @@ public enum Relation
public ChatColor getColor()
{
if (this.value == MEMBER.value)
if (this == MEMBER)
{
return Conf.colorMember;
}
else if (this.value == ALLY.value)
else if (this == ALLY)
{
return Conf.colorAlly;
}
else if (this.value == NEUTRAL.value)
else if (this == NEUTRAL)
{
return Conf.colorNeutral;
}

View File

@ -8,8 +8,7 @@ import org.bukkit.entity.CreatureType;
import org.bukkit.entity.Entity;
public class MiscUtil
{
{
public static CreatureType creatureTypeFromEntity(Entity entity)
{
if ( ! (entity instanceof Creature))

View File

@ -0,0 +1,120 @@
package com.massivecraft.factions.util;
import org.bukkit.ChatColor;
import com.massivecraft.factions.FPlayer;
import com.massivecraft.factions.Faction;
import com.massivecraft.factions.iface.RelationParticipator;
import com.massivecraft.factions.struct.Relation;
import com.massivecraft.factions.zcore.util.TextUtil;
public class RelationUtil
{
public static String describeThatToMe(RelationParticipator that, RelationParticipator me, boolean ucfirst)
{
String ret = "";
Faction fthat = getFaction(that);
if (fthat == null) return "ERROR"; // ERROR
Faction fme = getFaction(me);
if (fme == null) return "ERROR"; // ERROR
if (that instanceof Faction)
{
if (me instanceof FPlayer && fme == fthat)
{
ret = "your faction";
}
else
{
ret = "the faction "+fthat.getTag();
}
}
else if(that instanceof FPlayer)
{
FPlayer fplayerthat = (FPlayer)that;
if (that == me)
{
ret = "you";
}
else if (fthat == fme)
{
ret = fplayerthat.getNameAndTitle();
}
else
{
ret = fplayerthat.getNameAndTag();
}
}
if (ucfirst)
{
ret = TextUtil.upperCaseFirst(ret);
}
return ""+getRelationColor(me, that)+ret;
}
public static String describeThatToMe(RelationParticipator that, RelationParticipator me)
{
return describeThatToMe(that, me, false);
}
public static Relation getRelationTo(RelationParticipator me, RelationParticipator that)
{
return getRelationTo(that, me, false);
}
public static Relation getRelationTo(RelationParticipator me, RelationParticipator that, boolean ignorePeaceful)
{
Faction fthat = getFaction(that);
if (fthat == null) return Relation.NEUTRAL; // ERROR
Faction fme = getFaction(me);
if (fme == null) return Relation.NEUTRAL; // ERROR
if ( ! fthat.isNormal() || ! fme.isNormal())
{
return Relation.NEUTRAL;
}
if (fthat.equals(fme))
{
return Relation.MEMBER;
}
if ( ! ignorePeaceful && (fme.isPeaceful() || fthat.isPeaceful()))
{
return Relation.NEUTRAL;
}
if(fme.getRelationWish(fthat).value >= fthat.getRelationWish(fme).value)
{
return fthat.getRelationWish(fme);
}
return fme.getRelationWish(fthat);
}
public static Faction getFaction(RelationParticipator rp)
{
if (rp instanceof Faction)
{
return (Faction)rp;
}
if (rp instanceof FPlayer)
{
return ((FPlayer)rp).getFaction();
}
// ERROR
return null;
}
public static ChatColor getRelationColor(RelationParticipator me, RelationParticipator that)
{
return getRelationTo(that, me).getColor();
}
}