From 0c46809952eade23492b9cad9c02ab11e6dfadca Mon Sep 17 00:00:00 2001 From: Olof Larsson Date: Mon, 24 Oct 2011 11:07:06 +0200 Subject: [PATCH] Some refactoring and better messages --- src/com/massivecraft/factions/Board.java | 40 ++++------------ src/com/massivecraft/factions/Conf.java | 8 ++-- src/com/massivecraft/factions/FPlayer.java | 20 ++++---- src/com/massivecraft/factions/Faction.java | 20 ++++---- .../massivecraft/factions/cmd/CmdKick.java | 2 +- .../massivecraft/factions/cmd/FCommand.java | 4 +- .../factions/cmd/FRelationCommand.java | 18 ++++--- .../factions/iface/RelationParticipator.java | 10 ++-- .../integration/capi/PluginCapiListener.java | 2 +- .../listeners/FactionsEntityListener.java | 24 ++-------- .../massivecraft/factions/struct/FPerm.java | 2 +- src/com/massivecraft/factions/struct/Rel.java | 48 ++++++++++++------- .../factions/util/RelationUtil.java | 44 +++++++++-------- 13 files changed, 111 insertions(+), 131 deletions(-) diff --git a/src/com/massivecraft/factions/Board.java b/src/com/massivecraft/factions/Board.java index 3e22812b..f50c410d 100644 --- a/src/com/massivecraft/factions/Board.java +++ b/src/com/massivecraft/factions/Board.java @@ -171,10 +171,9 @@ public class Board int width = halfWidth * 2 + 1; int height = halfHeight * 2 + 1; - /*if (Conf.showMapFactionKey) - { - height--; - }*/ + //Make room for the list of tags + height--; + Map fList = new HashMap(); int chrIdx = 0; @@ -194,33 +193,17 @@ public class Board FLocation flocationHere = topLeft.getRelative(dx, dz); Faction factionHere = getFactionAt(flocationHere); - //Rel relation = faction.getRelationTo(factionHere); if (factionHere.isNone()) { row += ChatColor.GRAY+"-"; } - else /*if - ( - factionHere == faction - || - factionHere == factionLoc - || - relation.isAtLeast(Rel.ALLY) - || - (Conf.showNeutralFactionsOnMap && relation.equals(Rel.NEUTRAL)) - || - (Conf.showEnemyFactionsOnMap && relation.equals(Rel.ENEMY)) - )*/ + else { - if (!fList.containsKey(factionHere.getTag())) + if (!fList.containsKey(factionHere)) fList.put(factionHere, Conf.mapKeyChrs[chrIdx++]); - char tag = fList.get(factionHere); - row += factionHere.getColorTo(observer) + "" + tag; + char fchar = fList.get(factionHere); + row += factionHere.getColorTo(observer) + "" + fchar; } - /*else - { - row += ChatColor.GRAY+"-"; - }*/ } ret.add(row); } @@ -232,19 +215,14 @@ public class Board ret.set(1, asciiCompass.get(0)+ret.get(1).substring(3*3)); ret.set(2, asciiCompass.get(1)+ret.get(2).substring(3*3)); ret.set(3, asciiCompass.get(2)+ret.get(3).substring(3*3)); - - // Add the faction key - // TODO: relation color for them - //if (Conf.showMapFactionKey) - //{ String fRow = ""; for(Faction keyfaction : fList.keySet()) { - fRow += String.format("%s%s: %s ", keyfaction.getColorTo(observer), fList.get(keyfaction), keyfaction.getTag()); + fRow += ""+keyfaction.getColorTo(observer) + fList.get(keyfaction) + ": " + keyfaction.getTag() + " "; } + fRow = fRow.trim(); ret.add(fRow); - //} return ret; } diff --git a/src/com/massivecraft/factions/Conf.java b/src/com/massivecraft/factions/Conf.java index 01ab22b0..62709b95 100644 --- a/src/com/massivecraft/factions/Conf.java +++ b/src/com/massivecraft/factions/Conf.java @@ -100,7 +100,8 @@ public class Conf public static double homesTeleportAllowedEnemyDistance = 32.0; public static boolean homesTeleportIgnoreEnemiesIfInOwnTerritory = true; - public static boolean disablePVPBetweenNeutralFactions = false; + + public static Rel friendlyFireFromRel = Rel.TRUCE; public static boolean disablePVPForFactionlessPlayers = false; public static boolean enablePVPAgainstFactionlessInAttackersLand = false; @@ -165,7 +166,6 @@ public class Conf public static double econClaimAdditionalMultiplier = 0.5; public static double econClaimRefundMultiplier = 0.7; public static double econCostCreate = 100.0; - public static double econCostOwner = 15.0; public static double econCostSethome = 30.0; public static double econCostJoin = 0.0; public static double econCostLeave = 0.0; @@ -181,9 +181,9 @@ public class Conf public static double econCostShow = 0.0; public static double econCostOpen = 0.0; public static double econCostAlly = 0.0; - public static double econCostEnemy = 0.0; + public static double econCostTruce = 0.0; public static double econCostNeutral = 0.0; - public static double econCostNoBoom = 0.0; + public static double econCostEnemy = 0.0; //Faction banks, to pay for land claiming and other costs instead of individuals paying for them public static boolean bankEnabled = true; diff --git a/src/com/massivecraft/factions/FPlayer.java b/src/com/massivecraft/factions/FPlayer.java index b05e10e2..530113c3 100644 --- a/src/com/massivecraft/factions/FPlayer.java +++ b/src/com/massivecraft/factions/FPlayer.java @@ -268,27 +268,27 @@ public class FPlayer extends PlayerEntity implements EconomyParticipator // ------------------------------- @Override - public String describeTo(RelationParticipator that, boolean ucfirst) + public String describeTo(RelationParticipator observer, boolean ucfirst) { - return RelationUtil.describeThatToMe(this, that, ucfirst); + return RelationUtil.describeThatToMe(this, observer, ucfirst); } @Override - public String describeTo(RelationParticipator that) + public String describeTo(RelationParticipator observer) { - return RelationUtil.describeThatToMe(this, that); + return RelationUtil.describeThatToMe(this, observer); } @Override - public Rel getRelationTo(RelationParticipator rp) + public Rel getRelationTo(RelationParticipator observer) { - return RelationUtil.getRelationTo(this, rp); + return RelationUtil.getRelationOfThatToMe(this, observer); } @Override - public Rel getRelationTo(RelationParticipator rp, boolean ignorePeaceful) + public Rel getRelationTo(RelationParticipator observer, boolean ignorePeaceful) { - return RelationUtil.getRelationTo(this, rp, ignorePeaceful); + return RelationUtil.getRelationOfThatToMe(this, observer, ignorePeaceful); } public Rel getRelationToLocation() @@ -297,9 +297,9 @@ public class FPlayer extends PlayerEntity implements EconomyParticipator } @Override - public ChatColor getColorTo(RelationParticipator rp) + public ChatColor getColorTo(RelationParticipator observer) { - return RelationUtil.getColorOfThatToMe(this, rp); + return RelationUtil.getColorOfThatToMe(this, observer); } //----------------------------------------------// diff --git a/src/com/massivecraft/factions/Faction.java b/src/com/massivecraft/factions/Faction.java index a072e01d..6247aa54 100644 --- a/src/com/massivecraft/factions/Faction.java +++ b/src/com/massivecraft/factions/Faction.java @@ -186,33 +186,33 @@ public class Faction extends Entity implements EconomyParticipator // ------------------------------- @Override - public String describeTo(RelationParticipator that, boolean ucfirst) + public String describeTo(RelationParticipator observer, boolean ucfirst) { - return RelationUtil.describeThatToMe(this, that, ucfirst); + return RelationUtil.describeThatToMe(this, observer, ucfirst); } @Override - public String describeTo(RelationParticipator that) + public String describeTo(RelationParticipator observer) { - return RelationUtil.describeThatToMe(this, that); + return RelationUtil.describeThatToMe(this, observer); } @Override - public Rel getRelationTo(RelationParticipator rp) + public Rel getRelationTo(RelationParticipator observer) { - return RelationUtil.getRelationTo(this, rp); + return RelationUtil.getRelationOfThatToMe(this, observer); } @Override - public Rel getRelationTo(RelationParticipator rp, boolean ignorePeaceful) + public Rel getRelationTo(RelationParticipator observer, boolean ignorePeaceful) { - return RelationUtil.getRelationTo(this, rp, ignorePeaceful); + return RelationUtil.getRelationOfThatToMe(this, observer, ignorePeaceful); } @Override - public ChatColor getColorTo(RelationParticipator rp) + public ChatColor getColorTo(RelationParticipator observer) { - return RelationUtil.getColorOfThatToMe(this, rp); + return RelationUtil.getColorOfThatToMe(this, observer); } public Rel getRelationWish(Faction otherFaction) diff --git a/src/com/massivecraft/factions/cmd/CmdKick.java b/src/com/massivecraft/factions/cmd/CmdKick.java index 52b4fb18..52e2426f 100644 --- a/src/com/massivecraft/factions/cmd/CmdKick.java +++ b/src/com/massivecraft/factions/cmd/CmdKick.java @@ -52,7 +52,7 @@ public class CmdKick extends FCommand return; } - if (you.getRole().value >= fme.getRole().value) + if (you.getRole().isAtLeast(fme.getRole())) { // TODO add more informative messages. msg("Your rank is too low to kick this player."); diff --git a/src/com/massivecraft/factions/cmd/FCommand.java b/src/com/massivecraft/factions/cmd/FCommand.java index b4007f38..a034eb79 100644 --- a/src/com/massivecraft/factions/cmd/FCommand.java +++ b/src/com/massivecraft/factions/cmd/FCommand.java @@ -138,7 +138,7 @@ public abstract class FCommand extends MCommand

{ if (me == null) return true; - if (fme.getRole().value < role.value) + if (fme.getRole().isLessThan(role)) { msg("You must be "+role+" to "+this.getHelpShort()+"."); return false; @@ -356,7 +356,7 @@ public abstract class FCommand extends MCommand

return false; } - if (i.getRole().value > you.getRole().value || i.getRole().equals(Rel.LEADER) ) + if (i.getRole().isMoreThan(you.getRole()) || i.getRole().equals(Rel.LEADER) ) { return true; } diff --git a/src/com/massivecraft/factions/cmd/FRelationCommand.java b/src/com/massivecraft/factions/cmd/FRelationCommand.java index 0d946c08..7e691333 100644 --- a/src/com/massivecraft/factions/cmd/FRelationCommand.java +++ b/src/com/massivecraft/factions/cmd/FRelationCommand.java @@ -1,7 +1,5 @@ package com.massivecraft.factions.cmd; -import org.bukkit.ChatColor; - import com.massivecraft.factions.Conf; import com.massivecraft.factions.Faction; import com.massivecraft.factions.integration.SpoutFeatures; @@ -51,26 +49,26 @@ public abstract class FRelationCommand extends FCommand myFaction.setRelationWish(them, targetRelation); Rel currentRelation = myFaction.getRelationTo(them, true); - ChatColor currentRelationColor = currentRelation.getColor(); - if (targetRelation.value == currentRelation.value) + if (targetRelation == currentRelation) { - them.msg("Your faction is now "+currentRelationColor+targetRelation.toString()+" to "+currentRelationColor+myFaction.getTag()); - myFaction.msg("Your faction is now "+currentRelationColor+targetRelation.toString()+" to "+currentRelationColor+them.getTag()); + them.msg("%s is now %s.", myFaction.describeTo(them, true), targetRelation.getDescFactionOne()); + myFaction.msg("%s is now %s.", them.describeTo(myFaction, true), targetRelation.getDescFactionOne()); } else { - them.msg(currentRelationColor+myFaction.getTag()+" wishes to be your "+targetRelation.getColor()+targetRelation.toString()); + them.msg("%s wishes to be %s.", myFaction.describeTo(them, true), targetRelation.getColor()+targetRelation.getDescFactionOne()); them.msg("Type /"+Conf.baseCommandAliases.get(0)+" "+targetRelation+" "+myFaction.getTag()+" to accept."); - myFaction.msg(currentRelationColor+them.getTag()+" were informed that you wish to be "+targetRelation.getColor()+targetRelation); + myFaction.msg("%s were informed that you wish to be %s.", them.describeTo(myFaction, true), targetRelation.getColor()+targetRelation.getDescFactionOne()); } - if ( targetRelation != Rel.NEUTRAL && them.getFlag(FFlag.PEACEFUL)) + // TODO: The ally case should work!! + if ( targetRelation != Rel.TRUCE && them.getFlag(FFlag.PEACEFUL)) { them.msg("This will have no effect while your faction is peaceful."); myFaction.msg("This will have no effect while their faction is peaceful."); } - if ( targetRelation != Rel.NEUTRAL && myFaction.getFlag(FFlag.PEACEFUL)) + if ( targetRelation != Rel.TRUCE && myFaction.getFlag(FFlag.PEACEFUL)) { them.msg("This will have no effect while their faction is peaceful."); myFaction.msg("This will have no effect while your faction is peaceful."); diff --git a/src/com/massivecraft/factions/iface/RelationParticipator.java b/src/com/massivecraft/factions/iface/RelationParticipator.java index e7bf8ffb..714bc90c 100644 --- a/src/com/massivecraft/factions/iface/RelationParticipator.java +++ b/src/com/massivecraft/factions/iface/RelationParticipator.java @@ -5,11 +5,11 @@ import com.massivecraft.factions.struct.Rel; public interface RelationParticipator { - public String describeTo(RelationParticipator that); - public String describeTo(RelationParticipator that, boolean ucfirst); + public String describeTo(RelationParticipator observer); + public String describeTo(RelationParticipator observer, boolean ucfirst); - public Rel getRelationTo(RelationParticipator that); - public Rel getRelationTo(RelationParticipator that, boolean ignorePeaceful); + public Rel getRelationTo(RelationParticipator observer); + public Rel getRelationTo(RelationParticipator observer, boolean ignorePeaceful); - public ChatColor getColorTo(RelationParticipator to); + public ChatColor getColorTo(RelationParticipator observer); } diff --git a/src/com/massivecraft/factions/integration/capi/PluginCapiListener.java b/src/com/massivecraft/factions/integration/capi/PluginCapiListener.java index d0d5f0f9..a447c61b 100644 --- a/src/com/massivecraft/factions/integration/capi/PluginCapiListener.java +++ b/src/com/massivecraft/factions/integration/capi/PluginCapiListener.java @@ -81,7 +81,7 @@ public class PluginCapiListener extends CapiListener for (Player somePlayer : Bukkit.getServer().getOnlinePlayers()) { FPlayer someFPlayer = FPlayers.i.get(somePlayer); - if (someFPlayer.getRelationTo(fme).value >= Rel.ALLY.value) + if (someFPlayer.getRelationTo(fme).isAtLeast(Rel.ALLY)) { event.getThem().add(somePlayer); } diff --git a/src/com/massivecraft/factions/listeners/FactionsEntityListener.java b/src/com/massivecraft/factions/listeners/FactionsEntityListener.java index 2a29a028..1ec0071c 100644 --- a/src/com/massivecraft/factions/listeners/FactionsEntityListener.java +++ b/src/com/massivecraft/factions/listeners/FactionsEntityListener.java @@ -115,10 +115,7 @@ public class FactionsEntityListener extends EntityListener Entity damagee = sub.getEntity(); int damage = sub.getDamage(); - if ( ! (damagee instanceof Player)) - { - return true; - } + if ( ! (damagee instanceof Player)) return true; FPlayer defender = FPlayers.i.get((Player)damagee); @@ -206,23 +203,10 @@ public class FactionsEntityListener extends EntityListener Rel relation = defendFaction.getRelationTo(attackFaction); - // You can not hurt neutral factions - if (Conf.disablePVPBetweenNeutralFactions && relation == Rel.NEUTRAL) + // Check the relation + if (relation.isAtLeast(Conf.friendlyFireFromRel)) { - attacker.msg("You can't hurt neutral factions. Declare them as an enemy."); - return false; - } - - // Players without faction may be hurt anywhere - if (!defender.hasFaction()) - { - return true; - } - - // You can never hurt faction members or allies - if (relation == Rel.MEMBER || relation == Rel.ALLY) - { - attacker.msg("You can't hurt %s.", defender.describeTo(attacker)); + attacker.msg("You can't hurt %s.", relation.getDescPlayerMany()); return false; } diff --git a/src/com/massivecraft/factions/struct/FPerm.java b/src/com/massivecraft/factions/struct/FPerm.java index cffaae03..6668623b 100644 --- a/src/com/massivecraft/factions/struct/FPerm.java +++ b/src/com/massivecraft/factions/struct/FPerm.java @@ -79,7 +79,7 @@ public enum FPerm List rels = new ArrayList(); for (Rel rel : value) { - rels.add("

"+rel.nicename); + rels.add("

"+rel); } if (rels.size() > 0) { diff --git a/src/com/massivecraft/factions/struct/Rel.java b/src/com/massivecraft/factions/struct/Rel.java index e721e41b..7ca7f460 100644 --- a/src/com/massivecraft/factions/struct/Rel.java +++ b/src/com/massivecraft/factions/struct/Rel.java @@ -6,22 +6,35 @@ import com.massivecraft.factions.Conf; public enum Rel { - LEADER (70, "leader"), - OFFICER (60, "officer"), - MEMBER (50, "member"), - ALLY (40, "ally"), - TRUCE (30, "truce"), - NEUTRAL (20, "neutral"), - ENEMY (10, "enemy"), + LEADER (70, "your faction leader", "your faction leader", "", ""), + OFFICER (60, "an officer in your faction", "officers in your faction", "", ""), + MEMBER (50, "a member in your faction", "members in your faction", "your faction", "your factions"), + ALLY (40, "an ally", "allies", "an allied faction", "allied factions"), + TRUCE (30, "someone in truce with you", "those in truce with you", "a faction in truce", "factions in truce"), + NEUTRAL (20, "someone neutral to you", "those neutral to you", "a neutral faction", "neutral factions"), + ENEMY (10, "an enemy", "enemies", "an enemy faction", "enemy factions"), ; - public final int value; - public final String nicename; + private final int value; + private final String descPlayerOne; + public String getDescPlayerOne() { return this.descPlayerOne; } - private Rel(final int value, final String nicename) + private final String descPlayerMany; + public String getDescPlayerMany() { return this.descPlayerMany; } + + private final String descFactionOne; + public String getDescFactionOne() { return this.descFactionOne; } + + private final String descFactionMany; + public String getDescFactionMany() { return this.descFactionMany; } + + private Rel(final int value, final String descPlayerOne, final String descPlayerMany, final String descFactionOne, final String descFactionMany) { this.value = value; - this.nicename = nicename; + this.descPlayerOne = descPlayerOne; + this.descPlayerMany = descPlayerMany; + this.descFactionOne = descFactionOne; + this.descFactionMany = descFactionMany; } public static Rel parse(String str) @@ -58,12 +71,6 @@ public enum Rel return null; } - @Override - public String toString() - { - return this.nicename; - } - public boolean isAtLeast(Rel rel) { return this.value >= rel.value; @@ -79,6 +86,11 @@ public enum Rel return this.value < rel.value; } + public boolean isMoreThan(Rel rel) + { + return this.value > rel.value; + } + public ChatColor getColor() { if (this == MEMBER) @@ -116,6 +128,8 @@ public enum Rel return Conf.econCostEnemy; else if (this == ALLY) return Conf.econCostAlly; + else if (this == TRUCE) + return Conf.econCostTruce; else return Conf.econCostNeutral; } diff --git a/src/com/massivecraft/factions/util/RelationUtil.java b/src/com/massivecraft/factions/util/RelationUtil.java index 79aa4fd0..1c09837b 100644 --- a/src/com/massivecraft/factions/util/RelationUtil.java +++ b/src/com/massivecraft/factions/util/RelationUtil.java @@ -68,40 +68,46 @@ public class RelationUtil return describeThatToMe(that, me, false); } - public static Rel getRelationTo(RelationParticipator me, RelationParticipator that) + public static Rel getRelationOfThatToMe(RelationParticipator that, RelationParticipator me) { - return getRelationTo(that, me, false); + return getRelationOfThatToMe(me, that, false); } - public static Rel getRelationTo(RelationParticipator me, RelationParticipator that, boolean ignorePeaceful) + public static Rel getRelationOfThatToMe(RelationParticipator that, RelationParticipator me, boolean ignorePeaceful) { - Faction fthat = getFaction(that); + Rel ret = null; + + Faction fthat = getFaction(me); if (fthat == null) return Rel.NEUTRAL; // ERROR - Faction fme = getFaction(me); + Faction fme = getFaction(that); if (fme == null) return Rel.NEUTRAL; // ERROR - - if (!fthat.isNormal() || !fme.isNormal()) + + // The faction with the lowest wish "wins" + if (fme.getRelationWish(fthat).isLessThan(fthat.getRelationWish(fme))) { - return Rel.NEUTRAL; + ret = fme.getRelationWish(fthat); + } + else + { + ret = fthat.getRelationWish(fme); } if (fthat.equals(fme)) { - return Rel.MEMBER; + ret = Rel.MEMBER; + // Do officer and leader check + if (that instanceof FPlayer) + { + ret = ((FPlayer)that).getRole(); + } } - - if (!ignorePeaceful && (fme.getFlag(FFlag.PEACEFUL) || fthat.getFlag(FFlag.PEACEFUL))) + else if (!ignorePeaceful && (fme.getFlag(FFlag.PEACEFUL) || fthat.getFlag(FFlag.PEACEFUL))) { - return Rel.TRUCE; + ret = Rel.TRUCE; } - if (fme.getRelationWish(fthat).value >= fthat.getRelationWish(fme).value) - { - return fthat.getRelationWish(fme); - } - - return fme.getRelationWish(fthat); + return ret; } public static Faction getFaction(RelationParticipator rp) @@ -135,6 +141,6 @@ public class RelationUtil return Conf.colorNoPVP; } } - return getRelationTo(that, me).getColor(); + return getRelationOfThatToMe(that, me).getColor(); } }