From f91cf135d49ff4502019302e15c3f93993e89376 Mon Sep 17 00:00:00 2001 From: ulumulu1510 Date: Wed, 1 Mar 2017 12:07:28 +0100 Subject: [PATCH] Refactor ChatTag and ChatModifier to use Active. --- src/com/massivecraft/factions/Factions.java | 36 +++++----- .../factions/chat/ChatActive.java | 60 ++++++++++++++++ .../factions/chat/ChatFormatter.java | 72 +------------------ .../factions/chat/ChatModifier.java | 66 ++++++++++++++--- .../factions/chat/ChatModifierAbstract.java | 37 ---------- .../massivecraft/factions/chat/ChatTag.java | 67 ++++++++++++++--- .../factions/chat/ChatTagAbstract.java | 37 ---------- .../chat/modifier/ChatModifierLc.java | 4 +- .../chat/modifier/ChatModifierLp.java | 4 +- .../chat/modifier/ChatModifierParse.java | 4 +- .../chat/modifier/ChatModifierRp.java | 4 +- .../chat/modifier/ChatModifierUc.java | 4 +- .../chat/modifier/ChatModifierUcf.java | 4 +- .../factions/chat/tag/ChatTagName.java | 4 +- .../factions/chat/tag/ChatTagNameforce.java | 4 +- .../factions/chat/tag/ChatTagRelcolor.java | 4 +- .../factions/chat/tag/ChatTagRole.java | 4 +- .../factions/chat/tag/ChatTagRoleprefix.java | 4 +- .../chat/tag/ChatTagRoleprefixforce.java | 4 +- .../factions/chat/tag/ChatTagTitle.java | 4 +- 20 files changed, 217 insertions(+), 210 deletions(-) create mode 100644 src/com/massivecraft/factions/chat/ChatActive.java delete mode 100644 src/com/massivecraft/factions/chat/ChatModifierAbstract.java delete mode 100644 src/com/massivecraft/factions/chat/ChatTagAbstract.java diff --git a/src/com/massivecraft/factions/Factions.java b/src/com/massivecraft/factions/Factions.java index 779ddd8a..ddc355ae 100644 --- a/src/com/massivecraft/factions/Factions.java +++ b/src/com/massivecraft/factions/Factions.java @@ -207,25 +207,25 @@ public class Factions extends MassivePlugin TaskPlayerPowerUpdate.class, TaskPlayerDataRemove.class, TaskEconLandReward.class, - TaskFlagPermCreate.class + TaskFlagPermCreate.class, + + // ChatModifiers + ChatModifierLc.class, + ChatModifierLp.class, + ChatModifierParse.class, + ChatModifierRp.class, + ChatModifierUc.class, + ChatModifierUcf.class, + + // ChatTags, + ChatTagRelcolor.class, + ChatTagRole.class, + ChatTagRoleprefix.class, + ChatTagRoleprefixforce.class, + ChatTagName.class, + ChatTagNameforce.class, + ChatTagTitle.class ); - - // Register built in chat modifiers - ChatModifierLc.get().register(); - ChatModifierLp.get().register(); - ChatModifierParse.get().register(); - ChatModifierRp.get().register(); - ChatModifierUc.get().register(); - ChatModifierUcf.get().register(); - - // Register built in chat tags - ChatTagRelcolor.get().register(); - ChatTagRole.get().register(); - ChatTagRoleprefix.get().register(); - ChatTagRoleprefixforce.get().register(); - ChatTagName.get().register(); - ChatTagNameforce.get().register(); - ChatTagTitle.get().register(); } public GsonBuilder getGsonBuilderWithoutPreprocessors() diff --git a/src/com/massivecraft/factions/chat/ChatActive.java b/src/com/massivecraft/factions/chat/ChatActive.java new file mode 100644 index 00000000..2928df95 --- /dev/null +++ b/src/com/massivecraft/factions/chat/ChatActive.java @@ -0,0 +1,60 @@ +package com.massivecraft.factions.chat; + +import com.massivecraft.massivecore.Active; +import com.massivecraft.massivecore.Identified; +import com.massivecraft.massivecore.MassivePlugin; + +public abstract class ChatActive implements Active, Identified +{ + // -------------------------------------------- // + // FIELDS + // -------------------------------------------- // + + private MassivePlugin activePlugin; + + private final String id; + + // -------------------------------------------- // + // CONSTRUCT + // -------------------------------------------- // + + public ChatActive(final String id) + { + this.id = id.toLowerCase(); + } + + // -------------------------------------------- // + // OVERRIDE > IDENTIFIED + // -------------------------------------------- // + + @Override + public String getId() + { + return this.id; + } + + // -------------------------------------------- // + // OVERRIDE > ACTIVE + // -------------------------------------------- // + + @Override + public MassivePlugin setActivePlugin(MassivePlugin plugin) + { + this.activePlugin = plugin; + return plugin; + } + + @Override + public MassivePlugin getActivePlugin() + { + return this.activePlugin; + } + + @Override + public void setActive(MassivePlugin plugin) + { + this.setActive(plugin != null); + this.setActivePlugin(plugin); + } + +} diff --git a/src/com/massivecraft/factions/chat/ChatFormatter.java b/src/com/massivecraft/factions/chat/ChatFormatter.java index b473ba77..c3869ba5 100644 --- a/src/com/massivecraft/factions/chat/ChatFormatter.java +++ b/src/com/massivecraft/factions/chat/ChatFormatter.java @@ -2,9 +2,7 @@ package com.massivecraft.factions.chat; import java.util.ArrayList; import java.util.Arrays; -import java.util.HashMap; import java.util.List; -import java.util.Map; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -32,72 +30,6 @@ public class ChatFormatter public final static Pattern pattern = Pattern.compile(ESC_START+"([^"+ESC_START+ESC_END+"]+)"+ESC_END); - // -------------------------------------------- // - // TAG REGISTER - // -------------------------------------------- // - - private final static Map idToTag = new HashMap(); - public static ChatTag getTag(String tagId) { return idToTag.get(tagId); } - public static boolean registerTag(ChatTag tag) - { - if (tag == null) throw new NullPointerException("tag"); - - String id = tag.getId(); - if (id == null) throw new NullPointerException("tag id"); - if (!id.equals(id.toLowerCase())) - { - throw new IllegalArgumentException("tag id must be lowercase"); - } - - ChatTag current = idToTag.get(id); - if (current != null) - { - return current.equals(tag); - } - - idToTag.put(id, tag); - return true; - } - - public static boolean unregisterTag(ChatTag tag) - { - if (tag == null) return false; - return idToTag.remove(tag.getId()) != null; - } - - // -------------------------------------------- // - // MODIFIER REGISTER - // -------------------------------------------- // - - private final static Map idToModifier = new HashMap(); - public static ChatModifier getModifier(String modifierId) { return idToModifier.get(modifierId); } - public static boolean registerModifier(ChatModifier modifier) - { - if (modifier == null) throw new NullPointerException("modifier"); - - String id = modifier.getId(); - if (id == null) throw new NullPointerException("modifier id"); - if (!id.equals(id.toLowerCase())) - { - throw new IllegalArgumentException("modifier id must be lowercase"); - } - - ChatModifier current = idToModifier.get(id); - if (current != null) - { - return current.equals(modifier); - } - - idToModifier.put(id, modifier); - return true; - } - - public static boolean unregisterModifier(ChatModifier modifier) - { - if (modifier == null) return false; - return idToModifier.remove(modifier.getId()) != null; - } - // -------------------------------------------- // // FORMAT // -------------------------------------------- // @@ -127,7 +59,7 @@ public class ChatFormatter String tagId = modifierIds.remove(0); // Fetch tag for the id - ChatTag tag = getTag(tagId); + ChatTag tag = ChatTag.getTag(tagId); String replacement; if (tag == null) @@ -167,7 +99,7 @@ public class ChatFormatter for (String modifierId : modifierIds) { // Find the modifier or skip - ChatModifier modifier = getModifier(modifierId); + ChatModifier modifier = ChatModifier.getModifier(modifierId); if (modifier == null) continue; // Modify and ignore change if null. diff --git a/src/com/massivecraft/factions/chat/ChatModifier.java b/src/com/massivecraft/factions/chat/ChatModifier.java index 8340a5d6..9e10bad3 100644 --- a/src/com/massivecraft/factions/chat/ChatModifier.java +++ b/src/com/massivecraft/factions/chat/ChatModifier.java @@ -1,11 +1,55 @@ -package com.massivecraft.factions.chat; - -import org.bukkit.command.CommandSender; - -public interface ChatModifier -{ - public String getId(); - public String getModified(String subject, CommandSender sender, CommandSender recipient); - public boolean register(); - public boolean unregister(); -} +package com.massivecraft.factions.chat; + +import java.util.Map; + +import org.bukkit.command.CommandSender; + +import com.massivecraft.massivecore.collections.MassiveMap; + +public abstract class ChatModifier extends ChatActive +{ + // -------------------------------------------- // + // MODIFIER REGISTER + // -------------------------------------------- // + + private final static Map idToModifier = new MassiveMap<>(); + public static ChatModifier getModifier(String modifierId) { return idToModifier.get(modifierId); } + + // -------------------------------------------- // + // CONSTRUCT + // -------------------------------------------- // + + public ChatModifier(final String id) + { + super(id); + } + + // -------------------------------------------- // + // OVERRIDE + // -------------------------------------------- // + + @Override + public boolean isActive() + { + return idToModifier.containsKey(this.getId()); + } + + @Override + public void setActive(boolean active) + { + if (active) + { + idToModifier.put(this.getId(), this); + } + else + { + idToModifier.remove(this.getId()); + } + } + + // -------------------------------------------- // + // ABSTRACT + // -------------------------------------------- // + + public abstract String getModified(String subject, CommandSender sender, CommandSender recipient); +} diff --git a/src/com/massivecraft/factions/chat/ChatModifierAbstract.java b/src/com/massivecraft/factions/chat/ChatModifierAbstract.java deleted file mode 100644 index f368bb31..00000000 --- a/src/com/massivecraft/factions/chat/ChatModifierAbstract.java +++ /dev/null @@ -1,37 +0,0 @@ -package com.massivecraft.factions.chat; - -public abstract class ChatModifierAbstract implements ChatModifier -{ - // -------------------------------------------- // - // FIELDS & RAWDATA GET/SET - // -------------------------------------------- // - - private final String id; - @Override public String getId() { return this.id; } - - // -------------------------------------------- // - // OVERRIDES - // -------------------------------------------- // - - @Override - public boolean register() - { - return ChatFormatter.registerModifier(this); - } - - @Override - public boolean unregister() - { - return ChatFormatter.unregisterModifier(this); - } - - // -------------------------------------------- // - // CONSTRUCT - // -------------------------------------------- // - - public ChatModifierAbstract(final String id) - { - this.id = id.toLowerCase(); - } - -} diff --git a/src/com/massivecraft/factions/chat/ChatTag.java b/src/com/massivecraft/factions/chat/ChatTag.java index fa5732a0..4bdfe873 100644 --- a/src/com/massivecraft/factions/chat/ChatTag.java +++ b/src/com/massivecraft/factions/chat/ChatTag.java @@ -1,11 +1,56 @@ -package com.massivecraft.factions.chat; - -import org.bukkit.command.CommandSender; - -public interface ChatTag -{ - public String getId(); - public String getReplacement(CommandSender sender, CommandSender recipient); - public boolean register(); - public boolean unregister(); -} +package com.massivecraft.factions.chat; + +import java.util.Map; + +import org.bukkit.command.CommandSender; + +import com.massivecraft.massivecore.collections.MassiveMap; + +public abstract class ChatTag extends ChatActive +{ + // -------------------------------------------- // + // TAG REGISTER + // -------------------------------------------- // + + private final static Map idToTag = new MassiveMap<>(); + public static ChatTag getTag(String tagId) { return (ChatTag) idToTag.get(tagId); } + + // -------------------------------------------- // + // CONSTRUCT + // -------------------------------------------- // + + public ChatTag(final String id) + { + super(id); + } + + // -------------------------------------------- // + // OVERRIDE + // -------------------------------------------- // + + @Override + public boolean isActive() + { + return idToTag.containsKey(this.getId()); + } + + @Override + public void setActive(boolean active) + { + if (active) + { + idToTag.put(this.getId(), this); + } + else + { + idToTag.remove(this.getId()); + } + } + + // -------------------------------------------- // + // ABSTRACT + // -------------------------------------------- // + + public abstract String getReplacement(CommandSender sender, CommandSender recipient); + +} diff --git a/src/com/massivecraft/factions/chat/ChatTagAbstract.java b/src/com/massivecraft/factions/chat/ChatTagAbstract.java deleted file mode 100644 index 5949453e..00000000 --- a/src/com/massivecraft/factions/chat/ChatTagAbstract.java +++ /dev/null @@ -1,37 +0,0 @@ -package com.massivecraft.factions.chat; - -public abstract class ChatTagAbstract implements ChatTag -{ - // -------------------------------------------- // - // FIELDS & RAWDATA GET/SET - // -------------------------------------------- // - - private final String id; - @Override public String getId() { return this.id; } - - // -------------------------------------------- // - // OVERRIDES - // -------------------------------------------- // - - @Override - public boolean register() - { - return ChatFormatter.registerTag(this); - } - - @Override - public boolean unregister() - { - return ChatFormatter.unregisterTag(this); - } - - // -------------------------------------------- // - // CONSTRUCT - // -------------------------------------------- // - - public ChatTagAbstract(final String id) - { - this.id = id.toLowerCase(); - } - -} diff --git a/src/com/massivecraft/factions/chat/modifier/ChatModifierLc.java b/src/com/massivecraft/factions/chat/modifier/ChatModifierLc.java index f84df1df..74804e77 100644 --- a/src/com/massivecraft/factions/chat/modifier/ChatModifierLc.java +++ b/src/com/massivecraft/factions/chat/modifier/ChatModifierLc.java @@ -2,9 +2,9 @@ package com.massivecraft.factions.chat.modifier; import org.bukkit.command.CommandSender; -import com.massivecraft.factions.chat.ChatModifierAbstract; +import com.massivecraft.factions.chat.ChatModifier; -public class ChatModifierLc extends ChatModifierAbstract +public class ChatModifierLc extends ChatModifier { // -------------------------------------------- // // INSTANCE & CONSTRUCT diff --git a/src/com/massivecraft/factions/chat/modifier/ChatModifierLp.java b/src/com/massivecraft/factions/chat/modifier/ChatModifierLp.java index d6bbd4dd..6d105714 100644 --- a/src/com/massivecraft/factions/chat/modifier/ChatModifierLp.java +++ b/src/com/massivecraft/factions/chat/modifier/ChatModifierLp.java @@ -2,10 +2,10 @@ package com.massivecraft.factions.chat.modifier; import org.bukkit.command.CommandSender; -import com.massivecraft.factions.chat.ChatModifierAbstract; +import com.massivecraft.factions.chat.ChatModifier; -public class ChatModifierLp extends ChatModifierAbstract +public class ChatModifierLp extends ChatModifier { // -------------------------------------------- // // INSTANCE & CONSTRUCT diff --git a/src/com/massivecraft/factions/chat/modifier/ChatModifierParse.java b/src/com/massivecraft/factions/chat/modifier/ChatModifierParse.java index af5cbec7..6c896bdc 100644 --- a/src/com/massivecraft/factions/chat/modifier/ChatModifierParse.java +++ b/src/com/massivecraft/factions/chat/modifier/ChatModifierParse.java @@ -2,10 +2,10 @@ package com.massivecraft.factions.chat.modifier; import org.bukkit.command.CommandSender; -import com.massivecraft.factions.chat.ChatModifierAbstract; +import com.massivecraft.factions.chat.ChatModifier; import com.massivecraft.massivecore.util.Txt; -public class ChatModifierParse extends ChatModifierAbstract +public class ChatModifierParse extends ChatModifier { // -------------------------------------------- // // INSTANCE & CONSTRUCT diff --git a/src/com/massivecraft/factions/chat/modifier/ChatModifierRp.java b/src/com/massivecraft/factions/chat/modifier/ChatModifierRp.java index e9afddf1..5038f3ca 100644 --- a/src/com/massivecraft/factions/chat/modifier/ChatModifierRp.java +++ b/src/com/massivecraft/factions/chat/modifier/ChatModifierRp.java @@ -2,9 +2,9 @@ package com.massivecraft.factions.chat.modifier; import org.bukkit.command.CommandSender; -import com.massivecraft.factions.chat.ChatModifierAbstract; +import com.massivecraft.factions.chat.ChatModifier; -public class ChatModifierRp extends ChatModifierAbstract +public class ChatModifierRp extends ChatModifier { // -------------------------------------------- // // INSTANCE & CONSTRUCT diff --git a/src/com/massivecraft/factions/chat/modifier/ChatModifierUc.java b/src/com/massivecraft/factions/chat/modifier/ChatModifierUc.java index 43f0de03..c482ea79 100644 --- a/src/com/massivecraft/factions/chat/modifier/ChatModifierUc.java +++ b/src/com/massivecraft/factions/chat/modifier/ChatModifierUc.java @@ -2,9 +2,9 @@ package com.massivecraft.factions.chat.modifier; import org.bukkit.command.CommandSender; -import com.massivecraft.factions.chat.ChatModifierAbstract; +import com.massivecraft.factions.chat.ChatModifier; -public class ChatModifierUc extends ChatModifierAbstract +public class ChatModifierUc extends ChatModifier { // -------------------------------------------- // // INSTANCE & CONSTRUCT diff --git a/src/com/massivecraft/factions/chat/modifier/ChatModifierUcf.java b/src/com/massivecraft/factions/chat/modifier/ChatModifierUcf.java index 9b0cb4e1..82a0f001 100644 --- a/src/com/massivecraft/factions/chat/modifier/ChatModifierUcf.java +++ b/src/com/massivecraft/factions/chat/modifier/ChatModifierUcf.java @@ -2,10 +2,10 @@ package com.massivecraft.factions.chat.modifier; import org.bukkit.command.CommandSender; -import com.massivecraft.factions.chat.ChatModifierAbstract; +import com.massivecraft.factions.chat.ChatModifier; import com.massivecraft.massivecore.util.Txt; -public class ChatModifierUcf extends ChatModifierAbstract +public class ChatModifierUcf extends ChatModifier { // -------------------------------------------- // // INSTANCE & CONSTRUCT diff --git a/src/com/massivecraft/factions/chat/tag/ChatTagName.java b/src/com/massivecraft/factions/chat/tag/ChatTagName.java index 97717004..0adc2350 100644 --- a/src/com/massivecraft/factions/chat/tag/ChatTagName.java +++ b/src/com/massivecraft/factions/chat/tag/ChatTagName.java @@ -2,11 +2,11 @@ package com.massivecraft.factions.chat.tag; import org.bukkit.command.CommandSender; -import com.massivecraft.factions.chat.ChatTagAbstract; +import com.massivecraft.factions.chat.ChatTag; import com.massivecraft.factions.entity.Faction; import com.massivecraft.factions.entity.MPlayer; -public class ChatTagName extends ChatTagAbstract +public class ChatTagName extends ChatTag { // -------------------------------------------- // // INSTANCE & CONSTRUCT diff --git a/src/com/massivecraft/factions/chat/tag/ChatTagNameforce.java b/src/com/massivecraft/factions/chat/tag/ChatTagNameforce.java index c25c2156..708c5370 100644 --- a/src/com/massivecraft/factions/chat/tag/ChatTagNameforce.java +++ b/src/com/massivecraft/factions/chat/tag/ChatTagNameforce.java @@ -2,11 +2,11 @@ package com.massivecraft.factions.chat.tag; import org.bukkit.command.CommandSender; -import com.massivecraft.factions.chat.ChatTagAbstract; +import com.massivecraft.factions.chat.ChatTag; import com.massivecraft.factions.entity.Faction; import com.massivecraft.factions.entity.MPlayer; -public class ChatTagNameforce extends ChatTagAbstract +public class ChatTagNameforce extends ChatTag { // -------------------------------------------- // // INSTANCE & CONSTRUCT diff --git a/src/com/massivecraft/factions/chat/tag/ChatTagRelcolor.java b/src/com/massivecraft/factions/chat/tag/ChatTagRelcolor.java index 1b112ab5..50b2024b 100644 --- a/src/com/massivecraft/factions/chat/tag/ChatTagRelcolor.java +++ b/src/com/massivecraft/factions/chat/tag/ChatTagRelcolor.java @@ -2,10 +2,10 @@ package com.massivecraft.factions.chat.tag; import org.bukkit.command.CommandSender; -import com.massivecraft.factions.chat.ChatTagAbstract; +import com.massivecraft.factions.chat.ChatTag; import com.massivecraft.factions.entity.MPlayer; -public class ChatTagRelcolor extends ChatTagAbstract +public class ChatTagRelcolor extends ChatTag { // -------------------------------------------- // // INSTANCE & CONSTRUCT diff --git a/src/com/massivecraft/factions/chat/tag/ChatTagRole.java b/src/com/massivecraft/factions/chat/tag/ChatTagRole.java index 09aa4dc2..290c5cf2 100644 --- a/src/com/massivecraft/factions/chat/tag/ChatTagRole.java +++ b/src/com/massivecraft/factions/chat/tag/ChatTagRole.java @@ -2,11 +2,11 @@ package com.massivecraft.factions.chat.tag; import org.bukkit.command.CommandSender; -import com.massivecraft.factions.chat.ChatTagAbstract; +import com.massivecraft.factions.chat.ChatTag; import com.massivecraft.factions.entity.MPlayer; import com.massivecraft.massivecore.util.Txt; -public class ChatTagRole extends ChatTagAbstract +public class ChatTagRole extends ChatTag { // -------------------------------------------- // // INSTANCE & CONSTRUCT diff --git a/src/com/massivecraft/factions/chat/tag/ChatTagRoleprefix.java b/src/com/massivecraft/factions/chat/tag/ChatTagRoleprefix.java index e6bb5527..ae761997 100644 --- a/src/com/massivecraft/factions/chat/tag/ChatTagRoleprefix.java +++ b/src/com/massivecraft/factions/chat/tag/ChatTagRoleprefix.java @@ -2,11 +2,11 @@ package com.massivecraft.factions.chat.tag; import org.bukkit.command.CommandSender; -import com.massivecraft.factions.chat.ChatTagAbstract; +import com.massivecraft.factions.chat.ChatTag; import com.massivecraft.factions.entity.Faction; import com.massivecraft.factions.entity.MPlayer; -public class ChatTagRoleprefix extends ChatTagAbstract +public class ChatTagRoleprefix extends ChatTag { // -------------------------------------------- // // INSTANCE & CONSTRUCT diff --git a/src/com/massivecraft/factions/chat/tag/ChatTagRoleprefixforce.java b/src/com/massivecraft/factions/chat/tag/ChatTagRoleprefixforce.java index dd4a9db6..59c9cf0c 100644 --- a/src/com/massivecraft/factions/chat/tag/ChatTagRoleprefixforce.java +++ b/src/com/massivecraft/factions/chat/tag/ChatTagRoleprefixforce.java @@ -2,10 +2,10 @@ package com.massivecraft.factions.chat.tag; import org.bukkit.command.CommandSender; -import com.massivecraft.factions.chat.ChatTagAbstract; +import com.massivecraft.factions.chat.ChatTag; import com.massivecraft.factions.entity.MPlayer; -public class ChatTagRoleprefixforce extends ChatTagAbstract +public class ChatTagRoleprefixforce extends ChatTag { // -------------------------------------------- // // INSTANCE & CONSTRUCT diff --git a/src/com/massivecraft/factions/chat/tag/ChatTagTitle.java b/src/com/massivecraft/factions/chat/tag/ChatTagTitle.java index b68a58ef..b6eede37 100644 --- a/src/com/massivecraft/factions/chat/tag/ChatTagTitle.java +++ b/src/com/massivecraft/factions/chat/tag/ChatTagTitle.java @@ -2,10 +2,10 @@ package com.massivecraft.factions.chat.tag; import org.bukkit.command.CommandSender; -import com.massivecraft.factions.chat.ChatTagAbstract; +import com.massivecraft.factions.chat.ChatTag; import com.massivecraft.factions.entity.MPlayer; -public class ChatTagTitle extends ChatTagAbstract +public class ChatTagTitle extends ChatTag { // -------------------------------------------- // // INSTANCE & CONSTRUCT