Finish mcMMO addon

This commit is contained in:
Draycia committed 2020-07-18 08:14:38 -07:00
1 parent f54a71a25b
commit 0bb3c5b232
5 files changed
+75 -51

No files matched your search

@@ -5,14 +5,15 @@ import com.gmail.nossr50.events.party.McMMOPartyChangeEvent;
import net.draycia.simplechat.SimpleChat; import net.draycia.simplechat.SimpleChat;
import net.draycia.simplechat.channels.impls.SimpleChatChannel; import net.draycia.simplechat.channels.impls.SimpleChatChannel;
import net.draycia.simplechat.storage.ChatUser; import net.draycia.simplechat.storage.ChatUser;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener; import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent; import org.bukkit.event.player.PlayerJoinEvent;
public class PartyChatChannel extends SimpleChatChannel implements Listener { public class PartyChatChannel extends SimpleChatChannel implements Listener {
public PartyChatChannel(String name, SimpleChat simpleChat) { public PartyChatChannel(String name, SimpleChat simpleChat, ConfigurationSection config) {
super(name, simpleChat); super(name, simpleChat, config);
} }
@Override @Override
@@ -1,25 +1,44 @@
package net.draycia.simplechatmcmmo; package net.draycia.simplechatmcmmo;
import net.draycia.simplechat.events.ChannelRegisterEvent; import net.draycia.simplechat.SimpleChat;
import org.bukkit.event.EventHandler; import net.draycia.simplechat.channels.ChatChannel;
import org.bukkit.Bukkit;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPlugin;
public final class SimpleChatMCMMO extends JavaPlugin { public final class SimpleChatMCMMO extends JavaPlugin {
private SimpleChat simpleChat;
@Override @Override
public void onEnable() { 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 ChatChannel loadChannel(String key, ConfigurationSection section) {
public void onDisable() { ChatChannel channel = new PartyChatChannel(key, simpleChat, section);
// Plugin shutdown logic
}
@EventHandler String name = section.getString("name");
public void onChannelRegister(ChannelRegisterEvent event) {
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;
} }
} }
@@ -0,0 +1 @@
channels:
@@ -22,12 +22,14 @@ public class SimpleChatChannel extends ChatChannel {
private String key; private String key;
private SimpleChat simpleChat; private SimpleChat simpleChat;
private ConfigurationSection config;
private SimpleChatChannel() { } private SimpleChatChannel() { }
public SimpleChatChannel(String key, SimpleChat simpleChat) { public SimpleChatChannel(String key, SimpleChat simpleChat, ConfigurationSection config) {
this.key = key; this.key = key;
this.simpleChat = simpleChat; this.simpleChat = simpleChat;
this.config = config;
} }
public SimpleChat getSimpleChat() { public SimpleChat getSimpleChat() {
@@ -244,9 +246,7 @@ public class SimpleChatChannel extends ChatChannel {
public Map<String, String> getFormats() { public Map<String, String> getFormats() {
HashMap<String, String> formats = new HashMap<>(); HashMap<String, String> formats = new HashMap<>();
ConfigurationSection channelsSection = simpleChat.getConfig().getConfigurationSection("channels"); ConfigurationSection formatSection = config.getConfigurationSection("formats");
ConfigurationSection section = channelsSection.getConfigurationSection(getKey());
ConfigurationSection formatSection = section.getConfigurationSection("formats");
if (formatSection == null) { if (formatSection == null) {
formatSection = simpleChat.getConfig().getConfigurationSection("default").getConfigurationSection("formats"); formatSection = simpleChat.getConfig().getConfigurationSection("default").getConfigurationSection("formats");
@@ -353,11 +353,8 @@ public class SimpleChatChannel extends ChatChannel {
} }
private String getString(String key) { private String getString(String key) {
ConfigurationSection channelsSection = simpleChat.getConfig().getConfigurationSection("channels"); if (config != null && config.contains(key)) {
ConfigurationSection section = channelsSection.getConfigurationSection(getKey()); return config.getString(key);
if (section != null && section.contains(key)) {
return section.getString(key);
} }
ConfigurationSection defaultSection = simpleChat.getConfig().getConfigurationSection("default"); ConfigurationSection defaultSection = simpleChat.getConfig().getConfigurationSection("default");
@@ -370,11 +367,8 @@ public class SimpleChatChannel extends ChatChannel {
} }
private boolean getBoolean(String key) { private boolean getBoolean(String key) {
ConfigurationSection channelsSection = simpleChat.getConfig().getConfigurationSection("channels"); if (config != null && config.contains(key)) {
ConfigurationSection section = channelsSection.getConfigurationSection(getKey()); return config.getBoolean(key);
if (section != null && section.contains(key)) {
return section.getBoolean(key);
} }
ConfigurationSection defaultSection = simpleChat.getConfig().getConfigurationSection("default"); ConfigurationSection defaultSection = simpleChat.getConfig().getConfigurationSection("default");
@@ -387,11 +381,8 @@ public class SimpleChatChannel extends ChatChannel {
} }
private double getDouble(String key) { private double getDouble(String key) {
ConfigurationSection channelsSection = simpleChat.getConfig().getConfigurationSection("channels"); if (config != null && config.contains(key)) {
ConfigurationSection section = channelsSection.getConfigurationSection(getKey()); return config.getDouble(key);
if (section != null && section.contains(key)) {
return section.getDouble(key);
} }
ConfigurationSection defaultSection = simpleChat.getConfig().getConfigurationSection("default"); ConfigurationSection defaultSection = simpleChat.getConfig().getConfigurationSection("default");
@@ -9,46 +9,58 @@ import net.draycia.simplechat.util.Registry;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.configuration.ConfigurationSection; import org.bukkit.configuration.ConfigurationSection;
import java.util.ArrayList; import java.util.Collections;
import java.util.List;
public class ChannelManager { public class ChannelManager {
private ChannelRegistry registry; private ChannelRegistry registry;
private String defaultChannelKey = null; private String defaultChannelKey = null;
private SimpleChat simpleChat;
public ChannelManager(SimpleChat simpleChat) { public ChannelManager(SimpleChat simpleChat) {
registry = new ChannelRegistry(simpleChat); this.simpleChat = simpleChat;
List<ChatChannel> channels = new ArrayList<>(); this.registry = new ChannelRegistry(simpleChat);
for (String key : simpleChat.getConfig().getConfigurationSection("channels").getKeys(false)) { for (String key : simpleChat.getConfig().getConfigurationSection("channels").getKeys(false)) {
ChatChannel channel = new SimpleChatChannel(key, simpleChat);
ConfigurationSection section = simpleChat.getConfig().getConfigurationSection("channels").getConfigurationSection(key); ConfigurationSection section = simpleChat.getConfig().getConfigurationSection("channels").getConfigurationSection(key);
String name = section.getString("name"); ChatChannel channel = loadChannel(key, section);
if (name != null && name.length() > 16) { if (channel != null) {
simpleChat.getLogger().warning("Channel name [" + name + "] too long! Max length: 16."); if (registerChannel(channel)) {
simpleChat.getLogger().warning("Skipping channel, please check your settings!"); simpleChat.getLogger().info("Registering channel: " + channel.getName());
continue; }
} }
}
}
channels.add(channel); public ChatChannel loadChannel(String key, ConfigurationSection section) {
ChatChannel channel = new SimpleChatChannel(key, simpleChat, section);
if (channel.isDefault()) { String name = section.getString("name");
simpleChat.getLogger().info("Default channel found: " + channel.getName());
defaultChannelKey = channel.getKey();
}
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) { return channel;
simpleChat.getLogger().info("Registering channel: " + channel.getName()); }
getRegistry().register(channel.getKey(), 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<ChatChannel> getRegistry() { public Registry<ChatChannel> getRegistry() {