diff --git a/SimpleChatMCMMO/src/main/java/net/draycia/simplechatmcmmo/PartyChatChannel.java b/SimpleChatMCMMO/src/main/java/net/draycia/simplechatmcmmo/PartyChatChannel.java index e39fc1ab..da38563c 100644 --- a/SimpleChatMCMMO/src/main/java/net/draycia/simplechatmcmmo/PartyChatChannel.java +++ b/SimpleChatMCMMO/src/main/java/net/draycia/simplechatmcmmo/PartyChatChannel.java @@ -5,14 +5,15 @@ import com.gmail.nossr50.events.party.McMMOPartyChangeEvent; import net.draycia.simplechat.SimpleChat; import net.draycia.simplechat.channels.impls.SimpleChatChannel; import net.draycia.simplechat.storage.ChatUser; +import org.bukkit.configuration.ConfigurationSection; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; import org.bukkit.event.player.PlayerJoinEvent; public class PartyChatChannel extends SimpleChatChannel implements Listener { - public PartyChatChannel(String name, SimpleChat simpleChat) { - super(name, simpleChat); + public PartyChatChannel(String name, SimpleChat simpleChat, ConfigurationSection config) { + super(name, simpleChat, config); } @Override diff --git a/SimpleChatMCMMO/src/main/java/net/draycia/simplechatmcmmo/SimpleChatMCMMO.java b/SimpleChatMCMMO/src/main/java/net/draycia/simplechatmcmmo/SimpleChatMCMMO.java index 39f2e1e4..400e7f96 100644 --- a/SimpleChatMCMMO/src/main/java/net/draycia/simplechatmcmmo/SimpleChatMCMMO.java +++ b/SimpleChatMCMMO/src/main/java/net/draycia/simplechatmcmmo/SimpleChatMCMMO.java @@ -1,25 +1,44 @@ package net.draycia.simplechatmcmmo; -import net.draycia.simplechat.events.ChannelRegisterEvent; -import org.bukkit.event.EventHandler; +import net.draycia.simplechat.SimpleChat; +import net.draycia.simplechat.channels.ChatChannel; +import org.bukkit.Bukkit; +import org.bukkit.configuration.ConfigurationSection; import org.bukkit.plugin.java.JavaPlugin; public final class SimpleChatMCMMO extends JavaPlugin { + private SimpleChat simpleChat; + @Override public void onEnable() { - // Plugin startup logic + simpleChat = (SimpleChat) Bukkit.getPluginManager().getPlugin("SimpleChat"); + for (String key : getConfig().getConfigurationSection("channels").getKeys(false)) { + ConfigurationSection section = getConfig().getConfigurationSection("channels").getConfigurationSection(key); + + ChatChannel channel = loadChannel(key, section); + + if (channel != null) { + if (simpleChat.getChannelManager().registerChannel(channel)) { + getLogger().info("Registering channel: " + channel.getName()); + } + } + } } - @Override - public void onDisable() { - // Plugin shutdown logic - } + public ChatChannel loadChannel(String key, ConfigurationSection section) { + ChatChannel channel = new PartyChatChannel(key, simpleChat, section); - @EventHandler - public void onChannelRegister(ChannelRegisterEvent event) { + String name = section.getString("name"); + if (name != null && name.length() > 16) { + getLogger().warning("Channel name [" + name + "] too long! Max length: 16."); + getLogger().warning("Skipping channel, please check your settings!"); + return null; + } + + return channel; } } diff --git a/SimpleChatMCMMO/src/main/resources/config.yml b/SimpleChatMCMMO/src/main/resources/config.yml new file mode 100644 index 00000000..91595e60 --- /dev/null +++ b/SimpleChatMCMMO/src/main/resources/config.yml @@ -0,0 +1 @@ +channels: diff --git a/SimpleChatPlugin/src/main/java/net/draycia/simplechat/channels/impls/SimpleChatChannel.java b/SimpleChatPlugin/src/main/java/net/draycia/simplechat/channels/impls/SimpleChatChannel.java index 85a91ece..b3771da3 100644 --- a/SimpleChatPlugin/src/main/java/net/draycia/simplechat/channels/impls/SimpleChatChannel.java +++ b/SimpleChatPlugin/src/main/java/net/draycia/simplechat/channels/impls/SimpleChatChannel.java @@ -22,12 +22,14 @@ public class SimpleChatChannel extends ChatChannel { private String key; private SimpleChat simpleChat; + private ConfigurationSection config; private SimpleChatChannel() { } - public SimpleChatChannel(String key, SimpleChat simpleChat) { + public SimpleChatChannel(String key, SimpleChat simpleChat, ConfigurationSection config) { this.key = key; this.simpleChat = simpleChat; + this.config = config; } public SimpleChat getSimpleChat() { @@ -244,9 +246,7 @@ public class SimpleChatChannel extends ChatChannel { public Map getFormats() { HashMap formats = new HashMap<>(); - ConfigurationSection channelsSection = simpleChat.getConfig().getConfigurationSection("channels"); - ConfigurationSection section = channelsSection.getConfigurationSection(getKey()); - ConfigurationSection formatSection = section.getConfigurationSection("formats"); + ConfigurationSection formatSection = config.getConfigurationSection("formats"); if (formatSection == null) { formatSection = simpleChat.getConfig().getConfigurationSection("default").getConfigurationSection("formats"); @@ -353,11 +353,8 @@ public class SimpleChatChannel extends ChatChannel { } private String getString(String key) { - ConfigurationSection channelsSection = simpleChat.getConfig().getConfigurationSection("channels"); - ConfigurationSection section = channelsSection.getConfigurationSection(getKey()); - - if (section != null && section.contains(key)) { - return section.getString(key); + if (config != null && config.contains(key)) { + return config.getString(key); } ConfigurationSection defaultSection = simpleChat.getConfig().getConfigurationSection("default"); @@ -370,11 +367,8 @@ public class SimpleChatChannel extends ChatChannel { } private boolean getBoolean(String key) { - ConfigurationSection channelsSection = simpleChat.getConfig().getConfigurationSection("channels"); - ConfigurationSection section = channelsSection.getConfigurationSection(getKey()); - - if (section != null && section.contains(key)) { - return section.getBoolean(key); + if (config != null && config.contains(key)) { + return config.getBoolean(key); } ConfigurationSection defaultSection = simpleChat.getConfig().getConfigurationSection("default"); @@ -387,11 +381,8 @@ public class SimpleChatChannel extends ChatChannel { } private double getDouble(String key) { - ConfigurationSection channelsSection = simpleChat.getConfig().getConfigurationSection("channels"); - ConfigurationSection section = channelsSection.getConfigurationSection(getKey()); - - if (section != null && section.contains(key)) { - return section.getDouble(key); + if (config != null && config.contains(key)) { + return config.getDouble(key); } ConfigurationSection defaultSection = simpleChat.getConfig().getConfigurationSection("default"); diff --git a/SimpleChatPlugin/src/main/java/net/draycia/simplechat/managers/ChannelManager.java b/SimpleChatPlugin/src/main/java/net/draycia/simplechat/managers/ChannelManager.java index 689409a8..1c533b5d 100644 --- a/SimpleChatPlugin/src/main/java/net/draycia/simplechat/managers/ChannelManager.java +++ b/SimpleChatPlugin/src/main/java/net/draycia/simplechat/managers/ChannelManager.java @@ -9,46 +9,58 @@ import net.draycia.simplechat.util.Registry; import org.bukkit.Bukkit; import org.bukkit.configuration.ConfigurationSection; -import java.util.ArrayList; -import java.util.List; +import java.util.Collections; public class ChannelManager { private ChannelRegistry registry; private String defaultChannelKey = null; + private SimpleChat simpleChat; public ChannelManager(SimpleChat simpleChat) { - registry = new ChannelRegistry(simpleChat); - List channels = new ArrayList<>(); + this.simpleChat = simpleChat; + this.registry = new ChannelRegistry(simpleChat); for (String key : simpleChat.getConfig().getConfigurationSection("channels").getKeys(false)) { - ChatChannel channel = new SimpleChatChannel(key, simpleChat); - ConfigurationSection section = simpleChat.getConfig().getConfigurationSection("channels").getConfigurationSection(key); - String name = section.getString("name"); + ChatChannel channel = loadChannel(key, section); - if (name != null && name.length() > 16) { - simpleChat.getLogger().warning("Channel name [" + name + "] too long! Max length: 16."); - simpleChat.getLogger().warning("Skipping channel, please check your settings!"); - continue; + if (channel != null) { + if (registerChannel(channel)) { + simpleChat.getLogger().info("Registering channel: " + channel.getName()); + } } + } + } - channels.add(channel); + public ChatChannel loadChannel(String key, ConfigurationSection section) { + ChatChannel channel = new SimpleChatChannel(key, simpleChat, section); - if (channel.isDefault()) { - simpleChat.getLogger().info("Default channel found: " + channel.getName()); - defaultChannelKey = channel.getKey(); - } + String name = section.getString("name"); + if (name != null && name.length() > 16) { + simpleChat.getLogger().warning("Channel name [" + name + "] too long! Max length: 16."); + simpleChat.getLogger().warning("Skipping channel, please check your settings!"); + return null; } - for (ChatChannel channel : channels) { - simpleChat.getLogger().info("Registering channel: " + channel.getName()); - getRegistry().register(channel.getKey(), channel); - } + return channel; + } - Bukkit.getPluginManager().callEvent(new ChannelRegisterEvent(channels, getRegistry())); + public boolean registerChannel(ChatChannel channel) { + boolean success = getRegistry().register(channel.getKey(), channel); + + if (success) { + if (channel.isDefault() && defaultChannelKey == null) { + simpleChat.getLogger().info("Default channel registered: " + channel.getName()); + defaultChannelKey = channel.getKey(); + } + + Bukkit.getPluginManager().callEvent(new ChannelRegisterEvent(Collections.singletonList(channel), getRegistry())); + } + + return success; } public Registry getRegistry() {