From 46abcadf938e74dd0f582bfa689824ebb455c53b Mon Sep 17 00:00:00 2001 From: Harry Jeffery Date: Sat, 24 Sep 2011 11:04:49 +0100 Subject: [PATCH] Added alliance only chat mode. --- src/com/massivecraft/factions/Conf.java | 1 + src/com/massivecraft/factions/FPlayer.java | 19 ++++----- src/com/massivecraft/factions/Factions.java | 3 +- .../factions/commands/FCommandChat.java | 40 ++++++++++++++----- .../listeners/FactionsChatEarlyListener.java | 22 +++++++++- .../factions/struct/ChatMode.java | 28 +++++++++++++ 6 files changed, 93 insertions(+), 20 deletions(-) create mode 100644 src/com/massivecraft/factions/struct/ChatMode.java diff --git a/src/com/massivecraft/factions/Conf.java b/src/com/massivecraft/factions/Conf.java index e1d78b70..0b4ba0e5 100644 --- a/src/com/massivecraft/factions/Conf.java +++ b/src/com/massivecraft/factions/Conf.java @@ -65,6 +65,7 @@ public class Conf { public static boolean chatTagPadAfter = true; public static String chatTagFormat = "%s"+ChatColor.WHITE; public static String factionChatFormat = "%s"+ChatColor.WHITE+" %s"; + public static String allianceChatFormat = "%s"+colorAlly+" %s"; public static boolean allowNoSlashCommand = true; diff --git a/src/com/massivecraft/factions/FPlayer.java b/src/com/massivecraft/factions/FPlayer.java index 17336fdf..63722dca 100644 --- a/src/com/massivecraft/factions/FPlayer.java +++ b/src/com/massivecraft/factions/FPlayer.java @@ -11,6 +11,7 @@ import org.bukkit.Location; import org.bukkit.entity.Player; import com.google.gson.reflect.TypeToken; +import com.massivecraft.factions.struct.ChatMode; import com.massivecraft.factions.struct.Relation; import com.massivecraft.factions.struct.Role; import com.massivecraft.factions.util.DiscUtil; @@ -49,8 +50,8 @@ public class FPlayer { private transient boolean autoClaimEnabled; private transient boolean autoSafeZoneEnabled; private transient boolean autoWarZoneEnabled; - private transient boolean loginPvpDisabled; - private boolean factionChatting; + private transient boolean loginPvpDisabled; + private ChatMode chatMode; // -------------------------------------------- // // Construct @@ -80,7 +81,7 @@ public class FPlayer { } this.factionId = 0; // The default neutral faction - this.factionChatting = false; + this.chatMode = ChatMode.PUBLIC; this.role = Role.NORMAL; this.title = ""; @@ -139,15 +140,15 @@ public class FPlayer { SpoutFeatures.updateAppearances(this.getPlayer()); } - public boolean isFactionChatting() { - if (this.factionId == 0) { - return false; + public ChatMode getChatMode() { + if(this.factionId == 0 ) { + return ChatMode.PUBLIC; } - return factionChatting; + return chatMode; } - public void setFactionChatting(boolean factionChatting) { - this.factionChatting = factionChatting; + public void setChatMode(ChatMode chatMode) { + this.chatMode = chatMode; } public long getLastLoginTime() { diff --git a/src/com/massivecraft/factions/Factions.java b/src/com/massivecraft/factions/Factions.java index d61eb460..ff5f9e41 100644 --- a/src/com/massivecraft/factions/Factions.java +++ b/src/com/massivecraft/factions/Factions.java @@ -27,6 +27,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.struct.ChatMode; import com.massivecraft.factions.util.JarLoader; import com.massivecraft.factions.util.MapFLocToStringSetTypeAdapter; import com.massivecraft.factions.util.MyLocationTypeAdapter; @@ -285,7 +286,7 @@ public class Factions extends JavaPlugin { FPlayer me = FPlayer.get(player); if (me == null) return false; - return me.isFactionChatting(); + return me.getChatMode().isAtLeast(ChatMode.ALLIANCE); } // Is this chat message actually a Factions command, and thus should be left alone by other plugins? diff --git a/src/com/massivecraft/factions/commands/FCommandChat.java b/src/com/massivecraft/factions/commands/FCommandChat.java index ebf96a61..8f9d708c 100644 --- a/src/com/massivecraft/factions/commands/FCommandChat.java +++ b/src/com/massivecraft/factions/commands/FCommandChat.java @@ -1,6 +1,7 @@ package com.massivecraft.factions.commands; import com.massivecraft.factions.Conf; +import com.massivecraft.factions.struct.ChatMode; public class FCommandChat extends FBaseCommand { @@ -8,7 +9,9 @@ public class FCommandChat extends FBaseCommand { aliases.add("chat"); aliases.add("c"); - helpDescription = "Switch faction only chat on and off"; + optionalParameters.add("mode"); + + helpDescription = "Change chat mode"; } @Override @@ -21,15 +24,34 @@ public class FCommandChat extends FBaseCommand { return; } - if ( ! me.isFactionChatting()) { - // Turn on - me.setFactionChatting(true); - sendMessage("Faction-only chat ENABLED."); + if( this.parameters.size() >= 1 ) { + String mode = this.parameters.get(0); + + if(mode.startsWith("p")) { + me.setChatMode(ChatMode.PUBLIC); + sendMessage("Public chat mode."); + } else if(mode.startsWith("a")) { + me.setChatMode(ChatMode.ALLIANCE); + sendMessage("Alliance only chat mode."); + } else if(mode.startsWith("f")) { + me.setChatMode(ChatMode.FACTION); + sendMessage("Faction only chat mode."); + } else { + sendMessage("Unrecognised chat mode. Please enter either 'a','f' or 'p'"); + } + } else { - // Turn off - me.setFactionChatting(false); - sendMessage("Faction-only chat DISABLED."); + + if(me.getChatMode() == ChatMode.PUBLIC) { + me.setChatMode(ChatMode.ALLIANCE); + sendMessage("Alliance only chat mode."); + } else if (me.getChatMode() == ChatMode.ALLIANCE ) { + me.setChatMode(ChatMode.FACTION); + sendMessage("Faction only chat mode."); + } else { + me.setChatMode(ChatMode.PUBLIC); + sendMessage("Public chat mode."); + } } } - } diff --git a/src/com/massivecraft/factions/listeners/FactionsChatEarlyListener.java b/src/com/massivecraft/factions/listeners/FactionsChatEarlyListener.java index 07bcd569..778bf31d 100644 --- a/src/com/massivecraft/factions/listeners/FactionsChatEarlyListener.java +++ b/src/com/massivecraft/factions/listeners/FactionsChatEarlyListener.java @@ -11,7 +11,10 @@ import org.bukkit.event.player.PlayerListener; import com.massivecraft.factions.Conf; import com.massivecraft.factions.FPlayer; +import com.massivecraft.factions.Faction; import com.massivecraft.factions.Factions; +import com.massivecraft.factions.struct.ChatMode; +import com.massivecraft.factions.struct.Relation; import com.massivecraft.factions.util.TextUtil; @@ -45,12 +48,29 @@ public class FactionsChatEarlyListener extends PlayerListener{ FPlayer me = FPlayer.get(talkingPlayer); // Is it a faction chat message? - if (me.isFactionChatting()) { + if (me.getChatMode() == ChatMode.FACTION) { + String message = String.format(Conf.factionChatFormat, me.getNameAndRelevant(me), msg); me.getFaction().sendMessage(message); Logger.getLogger("Minecraft").info(ChatColor.stripColor("FactionChat "+me.getFaction().getTag()+": "+message)); event.setCancelled(true); return; + + } else if (me.getChatMode() == ChatMode.ALLIANCE ) { + String message = String.format(Conf.allianceChatFormat, me.getNameAndRelevant(me), msg); + Faction myFaction = me.getFaction(); + + //Send message to our own faction + myFaction.sendMessage(message); + for (FPlayer fplayer : FPlayer.getAllOnline()) { + if(myFaction.getRelation(fplayer) == Relation.ALLY) { + //Send to all our allies + fplayer.sendMessage(message); + } + } + Logger.getLogger("Minecraft").info(ChatColor.stripColor("AllianceChat "+me.getFaction().getTag()+": "+message)); + event.setCancelled(true); + return; } } } diff --git a/src/com/massivecraft/factions/struct/ChatMode.java b/src/com/massivecraft/factions/struct/ChatMode.java new file mode 100644 index 00000000..86fb9fd7 --- /dev/null +++ b/src/com/massivecraft/factions/struct/ChatMode.java @@ -0,0 +1,28 @@ +package com.massivecraft.factions.struct; + +public enum ChatMode { + FACTION(2, "faction chat"), + ALLIANCE(1, "alliance chat"), + PUBLIC(0, "public chat"); + + public final int value; + public final String nicename; + + private ChatMode(final int value, final String nicename) { + this.value = value; + this.nicename = nicename; + } + + public boolean isAtLeast(ChatMode role) { + return this.value >= role.value; + } + + public boolean isAtMost(ChatMode role) { + return this.value <= role.value; + } + + @Override + public String toString() { + return this.nicename; + } +}