Add channel registry and remove custom channels

This commit is contained in:
Draycia committed 2020-07-13 07:05:50 -07:00
1 parent bbeaa3bb85
commit dceb47e160
19 files changed
+200 -565

No files matched your search

@@ -1,24 +1,28 @@
package net.draycia.simplechat.managers;
import co.aikar.commands.CommandManager;
import net.draycia.simplechat.SimpleChat;
import net.draycia.simplechat.channels.ChannelRegistry;
import net.draycia.simplechat.channels.ChatChannel;
import net.draycia.simplechat.channels.impls.*;
import net.draycia.simplechat.commands.AliasedChannelCommand;
import net.draycia.simplechat.events.ChannelRegisterEvent;
import net.draycia.simplechat.util.Registry;
import org.bukkit.Bukkit;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.event.Listener;
import java.util.ArrayList;
import java.util.List;
public class ChannelManager {
private ChannelRegistry registry;
private String defaultChannelKey = null;
public ChannelManager(SimpleChat simpleChat) {
boolean hasTownChat = false;
boolean hasNationChat = false;
boolean hasAllianceChat = false;
boolean hasPartyChat = false;
registry = new ChannelRegistry(simpleChat);
List<ChatChannel> channels = new ArrayList<>();
for (String key : simpleChat.getConfig().getConfigurationSection("channels").getKeys(false)) {
ChatChannel channel;
ChatChannel channel = new SimpleChatChannel(key, simpleChat);
ConfigurationSection section = simpleChat.getConfig().getConfigurationSection("channels").getConfigurationSection(key);
@@ -30,87 +34,41 @@ public class ChannelManager {
continue;
}
String type = section.getString("type");
channels.add(channel);
if (type == null) {
type = "normal";
if (channel.isDefault()) {
defaultChannelKey = channel.getKey();
}
switch (type.toLowerCase()) {
case "towny":
channel = new TownChatChannel(key, simpleChat);
hasTownChat = true;
break;
case "nation":
channel = new NationChatChannel(key, simpleChat);
hasNationChat = true;
break;
case "alliance":
channel = new AllianceChatChannel(key, simpleChat);
hasAllianceChat = true;
break;
case "party":
channel = new PartyChatChannel(key, simpleChat);
hasPartyChat = true;
break;
case "normal":
channel = new SimpleChatChannel(key, simpleChat);
break;
default:
simpleChat.getLogger().warning("Invalid channel type for channel [" + key + "]. Skipping channel.");
continue;
}
if (channel.isTownChat() || channel.isNationChat() || channel.isAllianceChat()) {
if (!Bukkit.getPluginManager().isPluginEnabled("Towny")) {
simpleChat.getLogger().warning("Towny related channel with key [" + key + "] found, but Towny isn't installed. Skipping channel.");
continue;
}
}
if (channel.isPartyChat()) {
if (!Bukkit.getPluginManager().isPluginEnabled("mcMMO")) {
simpleChat.getLogger().warning("mcMMO related channel with key [" + key + "] found, but mcMMO isn't installed. Skipping channel.");
continue;
}
}
simpleChat.getChannels().add(channel);
CommandManager commandManager = simpleChat.getCommandManager().getCommandManager();
String commandName = section.contains("aliases") ? section.getString("aliases") : key;
commandManager.getCommandReplacements().addReplacement("channelName", commandName);
commandManager.registerCommand(new AliasedChannelCommand(simpleChat, channel));
if (channel instanceof Listener) {
Bukkit.getPluginManager().registerEvents((Listener)channel, simpleChat);
}
}
boolean townyInstalled = Bukkit.getPluginManager().isPluginEnabled("Towny");
boolean mcmmoInstalled = Bukkit.getPluginManager().isPluginEnabled("mcMMO");
if (!hasTownChat && townyInstalled) {
simpleChat.getLogger().info("Towny installed but no Town channel is setup!");
}
if (!hasNationChat && townyInstalled) {
simpleChat.getLogger().info("Towny installed but no Nation channel is setup!");
}
if (!hasAllianceChat && townyInstalled) {
simpleChat.getLogger().info("Towny installed but no Alliance channel is setup!");
}
if (!hasPartyChat && mcmmoInstalled) {
simpleChat.getLogger().info("mcMMO installed but no Party channel is setup!");
}
Bukkit.getPluginManager().callEvent(new ChannelRegisterEvent(channels, getRegistry()));
}
public void reloadChannels() {
public Registry<ChatChannel> getRegistry() {
return registry;
}
public ChatChannel getDefaultChannel() {
if (defaultChannelKey != null) {
return registry.get(defaultChannelKey);
}
return null;
}
public ChatChannel getChannelOrDefault(String key) {
if (key == null) {
return getDefaultChannel();
}
ChatChannel channel = registry.get(key);
if (channel == null) {
return getDefaultChannel();
}
return channel;
}
}
@@ -3,10 +3,6 @@ package net.draycia.simplechat.managers;
import co.aikar.commands.*;
import net.draycia.simplechat.SimpleChat;
import net.draycia.simplechat.channels.ChatChannel;
import net.draycia.simplechat.channels.impls.AllianceChatChannel;
import net.draycia.simplechat.channels.impls.NationChatChannel;
import net.draycia.simplechat.channels.impls.PartyChatChannel;
import net.draycia.simplechat.channels.impls.TownChatChannel;
import net.draycia.simplechat.commands.*;
import net.draycia.simplechat.storage.ChatUser;
import org.bukkit.Bukkit;
@@ -30,7 +26,7 @@ public class CommandManager {
ArrayList<String> completions = new ArrayList<>();
for (ChatChannel chatChannel : simpleChat.getChannels()) {
for (ChatChannel chatChannel : simpleChat.getChannelManager().getRegistry().values()) {
if (chatChannel.canPlayerUse(user)) {
completions.add(chatChannel.getName());
}
@@ -42,7 +38,7 @@ public class CommandManager {
commandManager.getCommandCompletions().registerCompletion("channel", (context) -> {
ArrayList<String> completions = new ArrayList<>();
for (ChatChannel chatChannel : simpleChat.getChannels()) {
for (ChatChannel chatChannel : simpleChat.getChannelManager().getRegistry().values()) {
completions.add(chatChannel.getName());
}
@@ -52,7 +48,7 @@ public class CommandManager {
commandManager.getCommandContexts().registerContext(ChatChannel.class, (context) -> {
String name = context.popFirstArg();
for (ChatChannel chatChannel : simpleChat.getChannels()) {
for (ChatChannel chatChannel : simpleChat.getChannelManager().getRegistry().values()) {
if (chatChannel.getName().equalsIgnoreCase(name)) {
return chatChannel;
}
@@ -73,17 +69,7 @@ public class CommandManager {
ChatUser user = simpleChat.getUserService().wrap(context.getIssuer().getPlayer());
if (!value.canPlayerUse(user)) {
if (value.isTownChat() && !((TownChatChannel)value).isInTown(user)) {
throw new ConditionFailedException(simpleChat.getConfig().getString("language.town-cannot-use"));
} else if (value.isNationChat() && !((NationChatChannel)value).isInNation(user)) {
throw new ConditionFailedException(simpleChat.getConfig().getString("language.nation-cannot-use"));
} else if (value.isAllianceChat() && !((AllianceChatChannel)value).isInNation(user)) {
throw new ConditionFailedException(simpleChat.getConfig().getString("language.alliance-cannot-use"));
} else if (value.isPartyChat() && !((PartyChatChannel)value).isInParty(user)) {
throw new ConditionFailedException(simpleChat.getConfig().getString("language.party-cannot-use"));
} else {
throw new ConditionFailedException(simpleChat.getConfig().getString("language.cannot-use-channel"));
}
throw new ConditionFailedException(value.getCannotUseMessage());
}
});
@@ -57,7 +57,7 @@ public class PluginMessageManager implements PluginMessageListener {
private void onMessageReceived(ByteArrayDataInput in) {
String chatChannelName = in.readUTF();
ChatChannel chatChannel = simpleChat.getChannel(chatChannelName);
ChatChannel chatChannel = simpleChat.getChannelManager().getRegistry().get(chatChannelName);
if (chatChannel == null) {
return;
@@ -84,7 +84,7 @@ public class PluginMessageManager implements PluginMessageListener {
public void onComponentReceived(ByteArrayDataInput in) {
String chatChannelName = in.readUTF();
ChatChannel chatChannel = simpleChat.getChannel(chatChannelName);
ChatChannel chatChannel = simpleChat.getChannelManager().getRegistry().get(chatChannelName);
if (chatChannel == null) {
return;