From 147a051feef436e759c30dd7f1781dd5174d8946 Mon Sep 17 00:00:00 2001 From: drtshock Date: Tue, 27 Jan 2015 09:33:47 -0600 Subject: [PATCH] Add max relation type. Adds HCF feature from #169. If used, max relation should be set to -1 for the default relation. --- .../com/massivecraft/factions/Faction.java | 2 + .../factions/cmd/FRelationCommand.java | 19 ++++++++- .../factions/struct/Relation.java | 13 ++++++- .../factions/zcore/persist/MemoryFaction.java | 10 +++++ .../massivecraft/factions/zcore/util/TL.java | 39 +++++++++++-------- src/main/resources/config.yml | 17 ++++++++ 6 files changed, 80 insertions(+), 20 deletions(-) diff --git a/src/main/java/com/massivecraft/factions/Faction.java b/src/main/java/com/massivecraft/factions/Faction.java index 11ff53e0..7ed6ab80 100644 --- a/src/main/java/com/massivecraft/factions/Faction.java +++ b/src/main/java/com/massivecraft/factions/Faction.java @@ -134,6 +134,8 @@ public interface Faction extends EconomyParticipator { public void setRelationWish(Faction otherFaction, Relation relation); + public int getRelationCount(Relation relation); + // ----------------------------------------------// // Power // ----------------------------------------------// diff --git a/src/main/java/com/massivecraft/factions/cmd/FRelationCommand.java b/src/main/java/com/massivecraft/factions/cmd/FRelationCommand.java index cd3cb846..6bff7650 100644 --- a/src/main/java/com/massivecraft/factions/cmd/FRelationCommand.java +++ b/src/main/java/com/massivecraft/factions/cmd/FRelationCommand.java @@ -2,13 +2,13 @@ package com.massivecraft.factions.cmd; import com.massivecraft.factions.Conf; import com.massivecraft.factions.Faction; +import com.massivecraft.factions.P; import com.massivecraft.factions.event.FactionRelationEvent; import com.massivecraft.factions.event.FactionRelationWishEvent; import com.massivecraft.factions.scoreboards.FTeamWrapper; import com.massivecraft.factions.struct.Permission; import com.massivecraft.factions.struct.Relation; import com.massivecraft.factions.zcore.util.TL; - import org.bukkit.Bukkit; import org.bukkit.ChatColor; @@ -52,6 +52,10 @@ public abstract class FRelationCommand extends FCommand { return; } + if (exceedsMaxRelations(targetRelation)) { + // We message them down there with the count. + return; + } Relation oldRelation = myFaction.getRelationTo(them, true); FactionRelationWishEvent wishEvent = new FactionRelationWishEvent(fme, myFaction, them, oldRelation, targetRelation); Bukkit.getPluginManager().callEvent(wishEvent); @@ -98,6 +102,19 @@ public abstract class FRelationCommand extends FCommand { FTeamWrapper.updatePrefixes(them); } + private boolean exceedsMaxRelations(Relation targetRelation) { + if (P.p.getConfig().getBoolean("max-relations.enabled", false)) { + int max = P.p.getConfig().getInt("max-relations." + targetRelation.toString(), -1); + // -1 means don't care. + if (max != -1 && myFaction.getRelationCount(targetRelation) >= max) { + // Message them now as long as we have the count. + msg(TL.COMMAND_RELATIONS_EXCEEDS_MAX, max, targetRelation.getPluralTranslation()); + return true; + } + } + return false; + } + @Override public TL getUsageTranslation() { return TL.COMMAND_RELATIONS_DESCRIPTION; diff --git a/src/main/java/com/massivecraft/factions/struct/Relation.java b/src/main/java/com/massivecraft/factions/struct/Relation.java index 8401e9fc..4234fc75 100644 --- a/src/main/java/com/massivecraft/factions/struct/Relation.java +++ b/src/main/java/com/massivecraft/factions/struct/Relation.java @@ -31,7 +31,7 @@ public enum Relation { return MEMBER; } else if (s.equalsIgnoreCase("ally")) { return ALLY; - } else if(s.equalsIgnoreCase("truce")) { + } else if (s.equalsIgnoreCase("truce")) { return TRUCE; } else if (s.equalsIgnoreCase("enemy")) { return ENEMY; @@ -42,7 +42,16 @@ public enum Relation { public String getTranslation() { for (TL t : TL.values()) { - if (t.name().equals("RELATION_" + name())) { + if (t.name().equalsIgnoreCase("RELATION_" + name())) { + return t.toString(); + } + } + return toString(); + } + + public String getPluralTranslation() { + for (TL t : TL.values()) { + if (t.name().equalsIgnoreCase("RELATION_" + name() + "_PLURAL")) { return t.toString(); } } diff --git a/src/main/java/com/massivecraft/factions/zcore/persist/MemoryFaction.java b/src/main/java/com/massivecraft/factions/zcore/persist/MemoryFaction.java index 7ed6667d..b12c5ebb 100644 --- a/src/main/java/com/massivecraft/factions/zcore/persist/MemoryFaction.java +++ b/src/main/java/com/massivecraft/factions/zcore/persist/MemoryFaction.java @@ -365,6 +365,16 @@ public abstract class MemoryFaction implements Faction, EconomyParticipator { } } + public int getRelationCount(Relation relation) { + int count = 0; + for (Faction faction : Factions.getInstance().getAllFactions()) { + if (faction.getRelationTo(this) == relation) { + count++; + } + } + return count; + } + // ----------------------------------------------// // Power // ----------------------------------------------// diff --git a/src/main/java/com/massivecraft/factions/zcore/util/TL.java b/src/main/java/com/massivecraft/factions/zcore/util/TL.java index da2e2b0a..f3c3e1ff 100644 --- a/src/main/java/com/massivecraft/factions/zcore/util/TL.java +++ b/src/main/java/com/massivecraft/factions/zcore/util/TL.java @@ -383,6 +383,21 @@ public enum TL { COMMAND_POWERBOOST_BOOSTLOG("%1$s has set the power bonus/penalty for %2$s to %3$d."), COMMAND_POWERBOOST_DESCRIPTION("Apply permanent power bonus/penalty to specified player or faction"), + COMMAND_RELATIONS_ALLTHENOPE("Nope! You can't."), + COMMAND_RELATIONS_MORENOPE("Nope! You can't declare a relation to yourself :)"), + COMMAND_RELATIONS_ALREADYINRELATIONSHIP("You already have that relation wish set with %1$s."), + COMMAND_RELATIONS_TOMARRY("to change a relation wish"), + COMMAND_RELATIONS_FORMARRY("for changing a relation wish"), + COMMAND_RELATIONS_MUTUAL("Your faction is now %1$s to %2$s"), + COMMAND_RELATIONS_PEACEFUL("This will have no effect while your faction is peaceful."), + COMMAND_RELATIONS_PEACEFULOTHER("This will have no effect while their faction is peaceful."), + COMMAND_RELATIONS_DESCRIPTION("Set relation wish to another faction"), + COMMAND_RELATIONS_EXCEEDS_MAX("Failed to set relation wish. You can only have %1$d %2%s."), + + COMMAND_RELATIONS_PROPOSAL_1("%1$s wishes to be your %2$s"), + COMMAND_RELATIONS_PROPOSAL_2("Type /%1$s %2$s %3$s to accept."), + COMMAND_RELATIONS_PROPOSAL_SENT("%1$s were informed that you wish to be %2$s"), + COMMAND_RELOAD_TIME("Reloaded conf.json from disk, took %1$d ms."), COMMAND_RELOAD_DESCRIPTION("Reload data file(s) from disk"), @@ -484,20 +499,6 @@ public enum TL { COMMAND_WARUNCLAIMALL_SUCCESS("You unclaimed ALL war zone land."), COMMAND_WARUNCLAIMALL_LOG("%1$s unclaimed all war zones."), - COMMAND_RELATIONS_ALLTHENOPE("Nope! You can't."), - COMMAND_RELATIONS_MORENOPE("Nope! You can't declare a relation to yourself :)"), - COMMAND_RELATIONS_ALREADYINRELATIONSHIP("You already have that relation wish set with %1$s."), - COMMAND_RELATIONS_TOMARRY("to change a relation wish"), - COMMAND_RELATIONS_FORMARRY("for changing a relation wish"), - COMMAND_RELATIONS_MUTUAL("Your faction is now %1$s to %2$s"), - COMMAND_RELATIONS_PEACEFUL("This will have no effect while your faction is peaceful."), - COMMAND_RELATIONS_PEACEFULOTHER("This will have no effect while their faction is peaceful."), - COMMAND_RELATIONS_DESCRIPTION("Set relation wish to another faction"), - - COMMAND_RELATIONS_PROPOSAL_1("%1$s wishes to be your %2$s"), - COMMAND_RELATIONS_PROPOSAL_2("Type /%1$s %2$s %3$s to accept."), - COMMAND_RELATIONS_PROPOSAL_SENT("%1$s were informed that you wish to be %2$s"), - /** * Leaving - This is accessed through a command, and so it MAY need a COMMAND_* slug :s */ @@ -511,7 +512,7 @@ public enum TL { LEAVE_DESCRIPTION("Leave your faction"), /** - * Claiming - Same as above basically. No COMMAND_* because it's not in a command class, but... + * Claiming - Same as above basically. No COMMAND_* because it's not in a command class, but... */ CLAIM_PROTECTED("This land is protected"), CLAIM_DISABLED("Sorry, this world has land claiming disabled."), @@ -583,10 +584,15 @@ public enum TL { * Relations */ RELATION_MEMBER("member"), + RELATION_MEMBER_PURAL("members"), RELATION_ALLY("ally"), + RELATION_ALLY_PURAL("allies"), RELATION_TRUCE("truce"), + RELATION_TRUCE_PLURAL("truces"), RELATION_NEUTRAL("neutral"), + RELATION_NEUTRAL_PURAL("neutrals"), RELATION_ENEMY("enemy"), + RELATION_ENEMY_PLURAL("enemies"), /** * Roles @@ -657,8 +663,7 @@ public enum TL { /** * Warmups */ - WARMUPS_NOTIFY_TELEPORT("&eYou will teleport to &d%1$s &ein &d%2$d &eseconds.") - ; + WARMUPS_NOTIFY_TELEPORT("&eYou will teleport to &d%1$s &ein &d%2$d &eseconds."); private String path; private String def; diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index 7ca5b584..adfb6bfe 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -149,3 +149,20 @@ warmups: f-home: 0 # Delay for /f warp f-warp: 0 + +# HCF Features +# These features were requested as part of Hardcore Factions or something. +# All of them are disabled by default. + +# Max Relation Types +# Limits factions to having a max number of each relation. +# Setting to 0 means none allowed. -1 for disabled. +# This will have no effect on default or existing relations, only when relations are changed. +# It is advised that you set the default relation to -1 so they can always go back to that. +# Otherwise Factions could be stuck with not being able to unenemy other Factions. +max-relations: + enabled: false + ally: 10 + truce: 10 + neutral: -1 + enemy: 10