Refactor ChatTag and ChatModifier to use Active.

This commit is contained in:
ulumulu1510 2017-03-01 12:07:28 +01:00
parent a18b6faf6f
commit f91cf135d4
20 changed files with 217 additions and 210 deletions

View File

@ -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()

View File

@ -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);
}
}

View File

@ -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<String, ChatTag> idToTag = new HashMap<String, ChatTag>();
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<String, ChatModifier> idToModifier = new HashMap<String, ChatModifier>();
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.

View File

@ -1,11 +1,55 @@
package com.massivecraft.factions.chat;
import java.util.Map;
import org.bukkit.command.CommandSender;
public interface ChatModifier
import com.massivecraft.massivecore.collections.MassiveMap;
public abstract class ChatModifier extends ChatActive
{
public String getId();
public String getModified(String subject, CommandSender sender, CommandSender recipient);
public boolean register();
public boolean unregister();
// -------------------------------------------- //
// MODIFIER REGISTER
// -------------------------------------------- //
private final static Map<String, ChatModifier> 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);
}

View File

@ -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();
}
}

View File

@ -1,11 +1,56 @@
package com.massivecraft.factions.chat;
import java.util.Map;
import org.bukkit.command.CommandSender;
public interface ChatTag
import com.massivecraft.massivecore.collections.MassiveMap;
public abstract class ChatTag extends ChatActive
{
public String getId();
public String getReplacement(CommandSender sender, CommandSender recipient);
public boolean register();
public boolean unregister();
// -------------------------------------------- //
// TAG REGISTER
// -------------------------------------------- //
private final static Map<String, ChatTag> 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);
}

View File

@ -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();
}
}

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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