diff --git a/pom.xml b/pom.xml index 56bfaf1..a37187d 100644 --- a/pom.xml +++ b/pom.xml @@ -7,7 +7,7 @@ me.oskar3123 staffchat - 1.0.0-SNAPSHOT + 1.1.0-SNAPSHOT diff --git a/src/main/java/me/oskar3123/staffchat/bungee/BungeeMain.java b/src/main/java/me/oskar3123/staffchat/bungee/BungeeMain.java index 4747fc9..ea402af 100644 --- a/src/main/java/me/oskar3123/staffchat/bungee/BungeeMain.java +++ b/src/main/java/me/oskar3123/staffchat/bungee/BungeeMain.java @@ -7,6 +7,7 @@ import net.md_5.bungee.api.plugin.Plugin; import net.md_5.bungee.config.Configuration; import net.md_5.bungee.config.ConfigurationProvider; import net.md_5.bungee.config.YamlConfiguration; +import org.yaml.snakeyaml.Yaml; import java.io.File; import java.io.IOException; @@ -27,7 +28,7 @@ public class BungeeMain extends Plugin saveDefaultConfig(); if (!reloadConfig()) { - getLogger().severe("Could not load config file, disabling plugin."); + getLogger().severe("Could not load config file, using defaults."); return; } registerCommands(); @@ -53,6 +54,7 @@ public class BungeeMain extends Plugin } catch (IOException e) { + config = ConfigurationProvider.getProvider(YamlConfiguration.class).load(getResourceAsStream("config.yml")); return false; } } @@ -66,6 +68,7 @@ public class BungeeMain extends Plugin { if (!getDataFolder().exists()) { + //noinspection ResultOfMethodCallIgnored getDataFolder().mkdir(); } diff --git a/src/main/java/me/oskar3123/staffchat/bungee/command/BungeeStaffChatCommand.java b/src/main/java/me/oskar3123/staffchat/bungee/command/BungeeStaffChatCommand.java index 4cdc7b2..026c3be 100644 --- a/src/main/java/me/oskar3123/staffchat/bungee/command/BungeeStaffChatCommand.java +++ b/src/main/java/me/oskar3123/staffchat/bungee/command/BungeeStaffChatCommand.java @@ -3,6 +3,7 @@ package me.oskar3123.staffchat.bungee.command; import me.oskar3123.staffchat.bungee.BungeeMain; import net.md_5.bungee.api.ChatColor; import net.md_5.bungee.api.CommandSender; +import net.md_5.bungee.api.chat.BaseComponent; import net.md_5.bungee.api.chat.TextComponent; import net.md_5.bungee.api.plugin.Command; import net.md_5.bungee.config.Configuration; @@ -10,33 +11,32 @@ import net.md_5.bungee.config.Configuration; public class BungeeStaffChatCommand extends Command { - private BungeeMain plugin; + private BungeeMain main; private Configuration config; - private static final String LABEL = "staffchat"; - public BungeeStaffChatCommand(BungeeMain plugin) + public BungeeStaffChatCommand(BungeeMain main) { - super(LABEL); - this.plugin = plugin; - config = plugin.getConfig(); + super("staffchat"); + this.main = main; + config = main.getConfig(); } public void execute(CommandSender sender, String[] args) { - String noPerm = clr(config.getString("messages.prefix") + config.getString("messages.nopermission")); - if (!sender.hasPermission(plugin.commandPerm)) + BaseComponent[] noPerm = txt(config.getString("messages.prefix") + config.getString("messages.nopermission")); + if (!sender.hasPermission(main.commandPerm)) { sender.sendMessage(noPerm); return; } if (args.length < 1) { - help(sender, LABEL); + help(sender, getName()); return; } if (args[0].equalsIgnoreCase("reload")) { - if (sender.hasPermission(plugin.reloadPerm)) + if (sender.hasPermission(main.reloadPerm)) { reload(sender); } @@ -46,30 +46,29 @@ public class BungeeStaffChatCommand extends Command } return; } - help(sender, LABEL); - return; + help(sender, getName()); } private void help(CommandSender sender, String label) { - String prefix = clr(config.getString("messages.prefix")); - sender.sendMessage(prefix + "Version " + plugin.getDescription().getVersion() + ", made by oskar3123"); - if (sender.hasPermission(plugin.reloadPerm)) + String prefix = config.getString("messages.prefix"); + sender.sendMessage(txt(prefix + "Version " + main.getDescription().getVersion() + ", made by oskar3123")); + if (sender.hasPermission(main.reloadPerm)) { - sender.sendMessage(TextComponent.fromLegacyText(prefix + "/" + label + " reload - Reloads the config file")); + sender.sendMessage(txt(prefix + "/" + label + " reload - Reloads the config file")); } } private void reload(CommandSender sender) { - plugin.reloadConfig(); - config = plugin.getConfig(); - sender.sendMessage(clr(config.getString("messages.prefix") + config.getString("messages.reloaded"))); + main.reloadConfig(); + config = main.getConfig(); + sender.sendMessage(txt(config.getString("messages.prefix") + config.getString("messages.reloaded"))); } - private String clr(String string) + private BaseComponent[] txt(String text) { - return ChatColor.translateAlternateColorCodes('&', string); + return TextComponent.fromLegacyText(ChatColor.translateAlternateColorCodes('&', text)); } } diff --git a/src/main/java/me/oskar3123/staffchat/bungee/event/BungeeChatListener.java b/src/main/java/me/oskar3123/staffchat/bungee/event/BungeeChatListener.java index 72c963c..1ee243d 100644 --- a/src/main/java/me/oskar3123/staffchat/bungee/event/BungeeChatListener.java +++ b/src/main/java/me/oskar3123/staffchat/bungee/event/BungeeChatListener.java @@ -1,24 +1,69 @@ package me.oskar3123.staffchat.bungee.event; import me.oskar3123.staffchat.bungee.BungeeMain; +import net.md_5.bungee.api.ChatColor; +import net.md_5.bungee.api.chat.BaseComponent; +import net.md_5.bungee.api.chat.TextComponent; +import net.md_5.bungee.api.connection.ProxiedPlayer; import net.md_5.bungee.api.event.ChatEvent; import net.md_5.bungee.api.plugin.Listener; +import net.md_5.bungee.config.Configuration; import net.md_5.bungee.event.EventHandler; public class BungeeChatListener implements Listener { - BungeeMain plugin; + private BungeeMain plugin; + private Configuration config; public BungeeChatListener(BungeeMain plugin) { this.plugin = plugin; + config = plugin.getConfig(); } @EventHandler public void chat(ChatEvent event) { + if (!(event.getSender() instanceof ProxiedPlayer)) + { + return; + } + ProxiedPlayer player = (ProxiedPlayer) event.getSender(); + if (!player.hasPermission(plugin.usePerm)) + { + return; + } + + Configuration config = plugin.getConfig(); + String character = config.getString("settings.character"); + if (!event.getMessage().startsWith(character)) + { + return; + } + + String format = config.getString("settings.format"); + format = format.replaceAll("\\{NAME\\}", player.getName()); + format = format.replaceAll("\\{MESSAGE\\}", event.getMessage().substring(character.length()).trim()); + final BaseComponent[] message = txt(format); + + plugin.getProxy().getPlayers().stream() + .filter(p -> p.hasPermission(plugin.seePerm)) + .forEach(p -> p.sendMessage(message)); + plugin.getLogger().info(ChatColor.stripColor(clr(format))); + + event.setCancelled(true); + } + + private String clr(String string) + { + return ChatColor.translateAlternateColorCodes('&', string); + } + + private BaseComponent[] txt(String text) + { + return TextComponent.fromLegacyText(clr(text)); } } diff --git a/src/main/resources/bungee.yml b/src/main/resources/bungee.yml index 6ee78cb..43381d5 100644 --- a/src/main/resources/bungee.yml +++ b/src/main/resources/bungee.yml @@ -1,4 +1,4 @@ name: StaffChat authors: [oskar3123] main: me.oskar3123.staffchat.bungee.BungeeMain -version: 1.0.0 +version: 1.1.0 diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index c84f2a9..9833583 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -1,7 +1,7 @@ name: StaffChat authors: [oskar3123] main: me.oskar3123.staffchat.spigot.Main -version: 1.0.0 +version: 1.1.0 commands: staffchat: