In progress: Using MassiveCraftCore and Allman indentation style and minor refactoring.

This commit is contained in:
Olof Larsson
2011-10-08 22:03:44 +02:00
parent 61998f459d
commit 0ce9cce9d3
75 changed files with 4605 additions and 2033 deletions

View File

@ -12,7 +12,7 @@ import com.nijikokun.register.payment.Method.MethodAccount;
import com.iConomy.*;
import com.iConomy.system.*;
import com.massivecraft.factions.Conf;
import com.massivecraft.factions.Factions;
import com.massivecraft.factions.P;
public class Econ {
@ -21,11 +21,11 @@ public class Econ {
private static boolean essEcoUse = false;
public static void monitorPlugins() {
Factions.instance.getServer().getPluginManager().registerEvent(Event.Type.PLUGIN_ENABLE, new FactionsServerListener(), Event.Priority.Monitor, Factions.instance);
Factions.instance.getServer().getPluginManager().registerEvent(Event.Type.PLUGIN_DISABLE, new FactionsServerListener(), Event.Priority.Monitor, Factions.instance);
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);
}
public static void setup(Factions factions) {
public static void setup(P factions) {
if (enabled()) {
return;
}
@ -50,80 +50,96 @@ public class Econ {
}
}
public static void registerSet(boolean enable) {
public static void registerSet(boolean enable)
{
registerUse = enable;
if (enable) {
Factions.log("Register hook available, "+(Conf.econRegisterEnabled ? "and interface is enabled" : "but disabled (\"econRegisterEnabled\": false)")+".");
P.p.log("Register hook available, "+(Conf.econRegisterEnabled ? "and interface is enabled" : "but disabled (\"econRegisterEnabled\": false)")+".");
}
else {
Factions.log("Un-hooked from Register.");
P.p.log("Un-hooked from Register.");
}
FCommandHelp.updateHelp();
}
public static void iConomySet(boolean enable) {
public static void iConomySet(boolean enable)
{
iConomyUse = enable;
if (enable && !registerUse) {
Factions.log("iConomy hook available, "+(Conf.econIConomyEnabled ? "and interface is enabled" : "but disabled (\"econIConomyEnabled\": false)")+".");
P.p.log("iConomy hook available, "+(Conf.econIConomyEnabled ? "and interface is enabled" : "but disabled (\"econIConomyEnabled\": false)")+".");
}
else {
Factions.log("Un-hooked from iConomy.");
P.p.log("Un-hooked from iConomy.");
}
FCommandHelp.updateHelp();
}
public static void essentialsEcoSet(boolean enable) {
public static void essentialsEcoSet(boolean enable)
{
essEcoUse = enable;
if (enable && !registerUse) {
Factions.log("EssentialsEco hook available, "+(Conf.econEssentialsEcoEnabled ? "and interface is enabled" : "but disabled (\"econEssentialsEcoEnabled\": false)")+".");
if (enable && !registerUse)
{
P.p.log("EssentialsEco hook available, "+(Conf.econEssentialsEcoEnabled ? "and interface is enabled" : "but disabled (\"econEssentialsEcoEnabled\": false)")+".");
}
else {
Factions.log("Un-hooked from EssentialsEco.");
else
{
P.p.log("Un-hooked from EssentialsEco.");
}
FCommandHelp.updateHelp();
}
public static boolean registerHooked() {
public static boolean registerHooked()
{
return registerUse;
}
public static boolean iConomyHooked() {
public static boolean iConomyHooked()
{
return iConomyUse;
}
public static boolean essentialsEcoHooked() {
public static boolean essentialsEcoHooked()
{
return essEcoUse;
}
public static boolean registerAvailable() {
public static boolean registerAvailable()
{
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())
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()) {
public static Holdings getIconomyHoldings(String playerName)
{
if ( ! enabled())
{
return null;
}
Account account = iConomy.getAccount(playerName);
if (account == null) {
if (account == null)
{
return null;
}
Holdings holdings = account.getHoldings();
return holdings;
}
public static MethodAccount getRegisterAccount(String playerName) {
if (!enabled()) {
public static MethodAccount getRegisterAccount(String playerName)
{
if (!enabled())
{
return null;
}
if (!Methods.getMethod().hasAccount(playerName)) {
if (!Methods.getMethod().hasAccount(playerName))
{
return null;
}
@ -133,7 +149,8 @@ public class Econ {
// format money string based on server's set currency type, like "24 gold" or "$24.50"
public static String moneyString(double amount) {
public static String moneyString(double amount)
{
return registerAvailable() ? Methods.getMethod().format(amount)
: (iConomyUse ? iConomy.format(amount) : Economy.format(amount));
}
@ -141,102 +158,129 @@ public class Econ {
// whether a player can afford specified amount
public static boolean canAfford(String playerName, double amount) {
// if Economy support is not enabled, they can certainly afford to pay nothing
if (!enabled()) {
if (!enabled())
{
return true;
}
if (registerAvailable()) {
if (registerAvailable())
{
MethodAccount holdings = getRegisterAccount(playerName);
if (holdings == null) {
if (holdings == null)
{
return false;
}
return holdings.hasEnough(amount);
}
else if (iConomyUse) {
else if (iConomyUse)
{
Holdings holdings = getIconomyHoldings(playerName);
if (holdings == null) {
if (holdings == null)
{
return false;
}
return holdings.hasEnough(amount);
}
else {
try {
else
{
try
{
return Economy.hasEnough(playerName, amount);
}
catch (Exception ex) {
catch (Exception ex)
{
return false;
}
}
}
// deduct money from their account; returns true if successful
public static boolean deductMoney(String playerName, double amount) {
if (!enabled()) {
public static boolean deductMoney(String playerName, double amount)
{
if (!enabled())
{
return true;
}
if (registerAvailable()) {
if (registerAvailable())
{
MethodAccount holdings = getRegisterAccount(playerName);
if (holdings == null || !holdings.hasEnough(amount)) {
if (holdings == null || !holdings.hasEnough(amount))
{
return false;
}
return holdings.subtract(amount);
}
else if (iConomyUse) {
else if (iConomyUse)
{
Holdings holdings = getIconomyHoldings(playerName);
if (holdings == null || !holdings.hasEnough(amount)) {
if (holdings == null || !holdings.hasEnough(amount))
{
return false;
}
holdings.subtract(amount);
return true;
}
else {
try {
if (!Economy.hasEnough(playerName, amount)) {
else
{
try
{
if (!Economy.hasEnough(playerName, amount))
{
return false;
}
Economy.subtract(playerName, amount);
return true;
}
catch (Exception ex) {
catch (Exception ex)
{
return false;
}
}
}
// add money to their account; returns true if successful
public static boolean addMoney(String playerName, double amount) {
if (!enabled()) {
public static boolean addMoney(String playerName, double amount)
{
if (!enabled())
{
return true;
}
if (registerAvailable()) {
if (registerAvailable())
{
MethodAccount holdings = getRegisterAccount(playerName);
if (holdings == null) {
if (holdings == null)
{
return false;
}
return holdings.add(amount);
}
else if (iConomyUse) {
else if (iConomyUse)
{
Holdings holdings = getIconomyHoldings(playerName);
if (holdings == null) {
if (holdings == null)
{
return false;
}
holdings.add(amount);
return true;
}
else {
try {
else
{
try
{
Economy.add(playerName, amount);
return true;
}
catch (Exception ex) {
catch (Exception ex)
{
return false;
}
}
@ -244,8 +288,10 @@ public class Econ {
// calculate the cost for claiming land
public static double calculateClaimCost(int ownedLand, boolean takingFromAnotherFaction) {
if (!enabled()) {
public static double calculateClaimCost(int ownedLand, boolean takingFromAnotherFaction)
{
if (!enabled())
{
return 0.0;
}
@ -256,12 +302,14 @@ public class Econ {
}
// calculate refund amount for unclaiming land
public static double calculateClaimRefund(int ownedLand) {
public static double calculateClaimRefund(int ownedLand)
{
return calculateClaimCost(ownedLand - 1, false) * Conf.econClaimRefundMultiplier;
}
// calculate value of all owned land
public static double calculateTotalLandValue(int ownedLand) {
public static double calculateTotalLandValue(int ownedLand)
{
double amount = 0;
for (int x = 0; x < ownedLand; x++) {
amount += calculateClaimCost(x, false);
@ -270,7 +318,8 @@ public class Econ {
}
// calculate refund amount for all owned land
public static double calculateTotalLandRefund(int ownedLand) {
public static double calculateTotalLandRefund(int ownedLand)
{
return calculateTotalLandValue(ownedLand) * Conf.econClaimRefundMultiplier;
}
}

View File

@ -2,39 +2,45 @@ package com.massivecraft.factions.integration;
import org.bukkit.entity.Player;
import org.bukkit.event.player.PlayerChatEvent;
import org.bukkit.plugin.Plugin;
import com.massivecraft.factions.Factions;
import com.massivecraft.factions.P;
import com.earth2me.essentials.chat.EssentialsChat;
import com.earth2me.essentials.chat.IEssentialsChatListener;
public class EssentialsFeatures {
public class EssentialsFeatures
{
private static EssentialsChat essChat;
public static void integrateChat(EssentialsChat instance) {
public static void integrateChat(EssentialsChat instance)
{
essChat = instance;
try {
essChat.addEssentialsChatListener("Factions", new IEssentialsChatListener() {
try
{
essChat.addEssentialsChatListener("Factions", new IEssentialsChatListener()
{
public boolean shouldHandleThisChat(PlayerChatEvent event)
{
return Factions.instance.shouldLetFactionsHandleThisChat(event);
return P.p.shouldLetFactionsHandleThisChat(event);
}
public String modifyMessage(PlayerChatEvent event, Player target, String message)
{
return message.replace("{FACTION}", Factions.instance.getPlayerFactionTagRelation(event.getPlayer(), target)).replace("{FACTION_TITLE}", Factions.instance.getPlayerTitle(event.getPlayer()));
return message.replace("{FACTION}", P.p.getPlayerFactionTagRelation(event.getPlayer(), target)).replace("{FACTION_TITLE}", P.p.getPlayerTitle(event.getPlayer()));
}
});
Factions.log("Found and will integrate chat with "+essChat.getDescription().getFullName());
P.p.log("Found and will integrate chat with "+essChat.getDescription().getFullName());
}
catch (NoSuchMethodError ex) {
catch (NoSuchMethodError ex)
{
essChat = null;
}
}
public static void unhookChat() {
if (essChat != null) {
public static void unhookChat()
{
if (essChat != null)
{
essChat.removeEssentialsChatListener("Factions");
}
}

View File

@ -4,8 +4,9 @@ import com.massivecraft.factions.Board;
import com.massivecraft.factions.Conf;
import com.massivecraft.factions.FLocation;
import com.massivecraft.factions.FPlayer;
import com.massivecraft.factions.FPlayers;
import com.massivecraft.factions.Faction;
import com.massivecraft.factions.Factions;
import com.massivecraft.factions.P;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
@ -24,25 +25,30 @@ import org.getspout.spoutapi.SpoutManager;
import org.getspout.spoutapi.gui.WidgetAnchor;
public class SpoutFeatures {
public class SpoutFeatures
{
private transient static AppearanceManager spoutApp;
private transient static boolean spoutMe = false;
private transient static Map<String, GenericLabel> territoryLabels = new HashMap<String, GenericLabel>();
// set integration availability
public static void setAvailable(boolean enable, String pluginName) {
public static void setAvailable(boolean enable, String pluginName)
{
spoutMe = enable;
if (spoutMe) {
if (spoutMe)
{
spoutApp = SpoutManager.getAppearanceManager();
Factions.log("Found and will use features of "+pluginName);
P.p.log("Found and will use features of "+pluginName);
}
else {
else
{
spoutApp = null;
}
}
// If any Spout feature is enabled in conf.json, and we're successfully hooked into it
public static boolean enabled() {
public static boolean enabled()
{
return spoutMe && (
Conf.spoutFactionTagsOverNames
|| Conf.spoutFactionTitlesOverNames
@ -54,24 +60,30 @@ public class SpoutFeatures {
// update displayed current territory for specified player; returns false if unsuccessful
public static boolean updateTerritoryDisplay(FPlayer player) {
if (!spoutMe || Conf.spoutTerritoryDisplayPosition == 0) {
public static boolean updateTerritoryDisplay(FPlayer player)
{
if (!spoutMe || Conf.spoutTerritoryDisplayPosition == 0)
{
return false;
}
SpoutPlayer sPlayer = SpoutManager.getPlayer(player.getPlayer());
if (!sPlayer.isSpoutCraftEnabled()) {
if (!sPlayer.isSpoutCraftEnabled())
{
return false;
}
GenericLabel label;
if (territoryLabels.containsKey(player.getName())) {
if (territoryLabels.containsKey(player.getName()))
{
label = territoryLabels.get(player.getName());
}
else {
else
{
label = new GenericLabel();
sPlayer.getMainScreen().attachWidget(Factions.instance, label);
switch (Conf.spoutTerritoryDisplayPosition) {
sPlayer.getMainScreen().attachWidget(P.p, label);
switch (Conf.spoutTerritoryDisplayPosition)
{
case 1: label.setAlign(WidgetAnchor.TOP_LEFT).setAnchor(WidgetAnchor.TOP_LEFT); break;
case 2: label.setAlign(WidgetAnchor.TOP_CENTER).setAnchor(WidgetAnchor.TOP_CENTER); break;
default: label.setAlign(WidgetAnchor.TOP_RIGHT).setAnchor(WidgetAnchor.TOP_RIGHT);
@ -81,7 +93,8 @@ public class SpoutFeatures {
Faction factionHere = Board.getFactionAt(new FLocation(player));
String msg = factionHere.getTag();
if (factionHere.getDescription().length() > 0) {
if (factionHere.getDescription().length() > 0)
{
msg += " - "+factionHere.getDescription();
}
label.setTextColor(getSpoutColor(player.getRelationColor(factionHere), 0));
@ -91,8 +104,10 @@ public class SpoutFeatures {
return true;
}
public static void playerDisconnect(FPlayer player) {
if (!enabled()) {
public static void playerDisconnect(FPlayer player)
{
if (!enabled())
{
return;
}
territoryLabels.remove(player.getName());
@ -100,33 +115,40 @@ public class SpoutFeatures {
// update all appearances between every player
public static void updateAppearances() {
if (!enabled()) {
public static void updateAppearances()
{
if (!enabled())
{
return;
}
Set<FPlayer> players = FPlayer.getAllOnline();
Set<FPlayer> players = FPlayers.i.getOnline();
Faction factionA;
for (FPlayer playerA : players) {
for (FPlayer playerA : players)
{
factionA = playerA.getFaction();
for (FPlayer playerB : players) {
for (FPlayer playerB : players)
{
updateSingle(playerB.getPlayer(), playerA.getPlayer(), factionA.getRelation(playerB), factionA, playerA.getTitle(), playerA.getRole());
}
}
}
// update all appearances related to a specific player
public static void updateAppearances(Player player) {
if (!enabled() || player == null) {
public static void updateAppearances(Player player)
{
if (!enabled() || player == null)
{
return;
}
Set<FPlayer> players = FPlayer.getAllOnline();
FPlayer playerA = FPlayer.get(player);
Set<FPlayer> players = FPlayers.i.getOnline();
FPlayer playerA = FPlayers.i.get(player);
Faction factionA = playerA.getFaction();
for (FPlayer playerB : players) {
for (FPlayer playerB : players)
{
Player player2 = playerB.getPlayer();
Relation rel = factionA.getRelation(playerB);
updateSingle(player2, player, rel, factionA, playerA.getTitle(), playerA.getRole());
@ -135,20 +157,25 @@ public class SpoutFeatures {
}
// update all appearances related to a single faction
public static void updateAppearances(Faction faction) {
if (!enabled() || faction == null) {
public static void updateAppearances(Faction faction)
{
if (!enabled() || faction == null)
{
return;
}
Set<FPlayer> players = FPlayer.getAllOnline();
Set<FPlayer> players = FPlayers.i.getOnline();
Faction factionA, factionB;
for (FPlayer playerA : players) {
for (FPlayer playerA : players)
{
factionA = playerA.getFaction();
for (FPlayer playerB : players) {
for (FPlayer playerB : players)
{
factionB = playerB.getFaction();
if (factionA != faction && factionB != faction) {
if (factionA != faction && factionB != faction)
{
continue;
}
updateSingle(playerB.getPlayer(), playerA.getPlayer(), factionA.getRelation(factionB), factionA, playerA.getTitle(), playerA.getRole());
@ -157,13 +184,17 @@ public class SpoutFeatures {
}
// update all appearances between two factions
public static void updateAppearances(Faction factionA, Faction factionB) {
if (!enabled() || factionA == null || factionB == null) {
public static void updateAppearances(Faction factionA, Faction factionB)
{
if (!enabled() || factionA == null || factionB == null)
{
return;
}
for (FPlayer playerA : factionA.getFPlayersWhereOnline(true)) {
for (FPlayer playerB : factionB.getFPlayersWhereOnline(true)) {
for (FPlayer playerA : factionA.getFPlayersWhereOnline(true))
{
for (FPlayer playerB : factionB.getFPlayersWhereOnline(true))
{
Player player1 = playerA.getPlayer();
Player player2 = playerB.getPlayer();
Relation rel = factionA.getRelation(factionB);
@ -175,71 +206,102 @@ public class SpoutFeatures {
// update a single appearance; internal use only by above public methods
private static void updateSingle(Player viewer, Player viewed, Relation relation, Faction viewedFaction, String viewedTitle, Role viewedRole) {
if (viewer == null || viewed == null) {
private static void updateSingle(Player viewer, Player viewed, Relation relation, Faction viewedFaction, String viewedTitle, Role viewedRole)
{
if (viewer == null || viewed == null)
{
return;
}
SpoutPlayer sPlayer = SpoutManager.getPlayer(viewer);
if (Conf.spoutFactionTagsOverNames || Conf.spoutFactionTitlesOverNames) {
if (viewedFaction.isNormal()) {
if (Conf.spoutFactionTagsOverNames || Conf.spoutFactionTitlesOverNames)
{
if (viewedFaction.isNormal())
{
String addTag = "";
if (Conf.spoutFactionTagsOverNames) {
if (Conf.spoutFactionTagsOverNames)
{
addTag += viewedFaction.getTag(relation.getColor().toString() + "[") + "]";
}
String rolePrefix = viewedRole.getPrefix();
if (Conf.spoutFactionTitlesOverNames && (!viewedTitle.isEmpty() || !rolePrefix.isEmpty())) {
if (Conf.spoutFactionTitlesOverNames && (!viewedTitle.isEmpty() || !rolePrefix.isEmpty()))
{
addTag += (addTag.isEmpty() ? "" : " ") + viewedRole.getPrefix() + viewedTitle;
}
spoutApp.setPlayerTitle(sPlayer, viewed, addTag + "\n" + viewed.getDisplayName());
}
else {
else
{
spoutApp.setPlayerTitle(sPlayer, viewed, viewed.getDisplayName());
}
}
if (
(Conf.spoutFactionAdminCapes && viewedRole.equals(Role.ADMIN))
|| (Conf.spoutFactionModeratorCapes && viewedRole.equals(Role.MODERATOR))
) {
if
(
(
Conf.spoutFactionAdminCapes
&&
viewedRole.equals(Role.ADMIN)
)
||
(
Conf.spoutFactionModeratorCapes
&&
viewedRole.equals(Role.MODERATOR)
)
)
{
String cape = "";
if (!viewedFaction.isNormal()) {
if (!viewedFaction.isNormal())
{
// yeah, no cape if no faction
}
else if (viewedFaction.isPeaceful()) {
else if (viewedFaction.isPeaceful())
{
cape = Conf.capePeaceful;
}
else if (relation.isNeutral()) {
else if (relation.isNeutral())
{
cape = Conf.capeNeutral;
}
else if (relation.isMember()) {
else if (relation.isMember())
{
cape = Conf.capeMember;
}
else if (relation.isEnemy()) {
else if (relation.isEnemy())
{
cape = Conf.capeEnemy;
}
else if (relation.isAlly()) {
else if (relation.isAlly())
{
cape = Conf.capeAlly;
}
if (cape.isEmpty()) {
if (cape.isEmpty())
{
spoutApp.resetPlayerCloak(sPlayer, viewed);
} else {
}
else
{
spoutApp.setPlayerCloak(sPlayer, viewed, cape);
}
}
else if (Conf.spoutFactionAdminCapes || Conf.spoutFactionModeratorCapes) {
else if (Conf.spoutFactionAdminCapes || Conf.spoutFactionModeratorCapes)
{
spoutApp.resetPlayerCloak(sPlayer, viewed);
}
}
// method to convert a Bukkit ChatColor to a Spout Color
private static Color getSpoutColor(ChatColor inColor, int alpha) {
if (inColor == null) {
private static Color getSpoutColor(ChatColor inColor, int alpha)
{
if (inColor == null)
{
return new Color(191, 191, 191, alpha);
}
switch (inColor.getCode()) {
switch (inColor.getCode())
{
case 0x1: return new Color(0, 0, 191, alpha);
case 0x2: return new Color(0, 191, 0, alpha);
case 0x3: return new Color(0, 191, 191, alpha);

View File

@ -1,6 +1,6 @@
package com.massivecraft.factions.integration;
import com.massivecraft.factions.Factions;
import com.massivecraft.factions.P;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
@ -28,24 +28,30 @@ import org.bukkit.entity.Player;
* Author: Spathizilla
*/
public class Worldguard {
public class Worldguard
{
private static WorldGuardPlugin wg;
private static boolean enabled = false;
public static void init(Plugin plugin) {
public static void init(Plugin plugin)
{
Plugin wgplug = plugin.getServer().getPluginManager().getPlugin("WorldGuard");
if (wgplug == null || !(wgplug instanceof WorldGuardPlugin)) {
if (wgplug == null || !(wgplug instanceof WorldGuardPlugin))
{
enabled = false;
wg = null;
Factions.log("Could not hook to WorldGuard. WorldGuard checks are disabled.");
} else {
P.p.log("Could not hook to WorldGuard. WorldGuard checks are disabled.");
}
else
{
wg = (WorldGuardPlugin) wgplug;
enabled = true;
Factions.log("Successfully hooked to WorldGuard.");
P.p.log("Successfully hooked to WorldGuard.");
}
}
public static boolean isEnabled() {
public static boolean isEnabled()
{
return enabled;
}
@ -53,8 +59,10 @@ public class Worldguard {
// Returns:
// True: PVP is allowed
// False: PVP is disallowed
public static boolean isPVP(Player player) {
if(!enabled) {
public static boolean isPVP(Player player)
{
if( ! enabled)
{
// No WG hooks so we'll always bypass this check.
return true;
}
@ -72,8 +80,10 @@ public class Worldguard {
// Returns:
// True: Regions found within chunk
// False: No regions found within chunk
public static boolean checkForRegionsInChunk(Location loc) {
if(!enabled) {
public static boolean checkForRegionsInChunk(Location loc)
{
if( ! enabled)
{
// No WG hooks so we'll always bypass this check.
return false;
}
@ -97,14 +107,20 @@ public class Worldguard {
List<ProtectedRegion> overlaps;
boolean foundregions = false;
try {
try
{
overlaps = region.getIntersectingRegions(allregionslist);
if(overlaps == null || overlaps.isEmpty()) {
if(overlaps == null || overlaps.isEmpty())
{
foundregions = false;
} else {
}
else
{
foundregions = true;
}
} catch (UnsupportedIntersectionException e) {
}
catch (UnsupportedIntersectionException e)
{
e.printStackTrace();
}