From 08ee751b12fb4c5ca348a0618b8069105664451d Mon Sep 17 00:00:00 2001 From: Destro174 <40720638+destro174@users.noreply.github.com> Date: Sat, 12 Sep 2020 15:18:50 +0200 Subject: [PATCH] added version placeholder --- .gitignore | 39 ++++ pom.xml | 46 ++++ .../destro174/custommotd/CustomMOTD.java | 220 ++++++++++++++++++ .../destro174/custommotd/MotdWrapper.java | 50 ++++ .../commands/custommotdCommand.java | 103 ++++++++ .../custommotd/listener/PingListener.java | 79 +++++++ .../custommotd/storage/MOTDConfig.java | 101 ++++++++ src/main/resources/MOTD.yml | 2 + src/main/resources/config.yml | 31 +++ src/main/resources/plugin.yml | 12 + 10 files changed, 683 insertions(+) create mode 100644 .gitignore create mode 100644 pom.xml create mode 100644 src/main/java/com/alttd/destro174/custommotd/CustomMOTD.java create mode 100644 src/main/java/com/alttd/destro174/custommotd/MotdWrapper.java create mode 100644 src/main/java/com/alttd/destro174/custommotd/commands/custommotdCommand.java create mode 100644 src/main/java/com/alttd/destro174/custommotd/listener/PingListener.java create mode 100644 src/main/java/com/alttd/destro174/custommotd/storage/MOTDConfig.java create mode 100644 src/main/resources/MOTD.yml create mode 100644 src/main/resources/config.yml create mode 100644 src/main/resources/plugin.yml diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..6642ab8 --- /dev/null +++ b/.gitignore @@ -0,0 +1,39 @@ +# Plugin +.idea +testserver +run + +# Compiled class file +*.class + +# Log file +*.log + +# BlueJ files +*.ctxt + +# Mobile Tools for Java (J2ME) +.mtj.tmp/ + +# IntelliJ +*.iml +*.ipr +*.iws +.idea/ +out/ + +# Maven +target/ + +# Package Files # +*.jar +*.war +*.nar +*.ear +*.zip +*.tar.gz +*.rar + +# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml +hs_err_pid* + diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..b0ff161 --- /dev/null +++ b/pom.xml @@ -0,0 +1,46 @@ + + + 4.0.0 + + com.alttd.destro174 + CustomMOTD + 1.0 + + ${project.artifactId} + clean package + + + org.apache.maven.plugins + maven-compiler-plugin + 3.5.1 + + 1.8 + 1.8 + + + + + + src/main/resources + + plugin.yml + lang.yml + config.yml + + true + + + + + + + com.destroystokyo.paper + paper + 1.16.3-R0.1-SNAPSHOT + provided + + + + \ No newline at end of file diff --git a/src/main/java/com/alttd/destro174/custommotd/CustomMOTD.java b/src/main/java/com/alttd/destro174/custommotd/CustomMOTD.java new file mode 100644 index 0000000..415b672 --- /dev/null +++ b/src/main/java/com/alttd/destro174/custommotd/CustomMOTD.java @@ -0,0 +1,220 @@ +package com.alttd.destro174.custommotd; + +import java.util.ArrayList; +import java.util.List; + +import com.alttd.destro174.custommotd.commands.custommotdCommand; +import com.alttd.destro174.custommotd.listener.PingListener; +import com.alttd.destro174.custommotd.storage.MOTDConfig; +import org.bukkit.Bukkit; +import org.bukkit.ChatColor; +import org.bukkit.command.CommandSender; +import org.bukkit.plugin.PluginManager; +import org.bukkit.plugin.java.JavaPlugin; +import org.bukkit.util.CachedServerIcon; + +public class CustomMOTD extends JavaPlugin { + + public static CustomMOTD CustomMOTD; + + MOTDConfig motdfile, counterfile; + + public static String Options = ""; + public int counter; + public static List motd; + public static CachedServerIcon icon; + + + public List colorList(List list){ + List lines = new ArrayList(); + + for (String s : list) { + s = s.replace("%version%", Bukkit.getMinecraftVersion()); // 1.16 addon + lines.add(ChatColor.translateAlternateColorCodes('&', s)); + } + + return lines; + } + + public static CustomMOTD getInstance() { + return CustomMOTD; + } + + @Override + public void onEnable() { + // TODO Insert logic to be performed when the plugin is enabled + try { + CustomMOTD = this; + //getLogger().info("Altitude Custom MOTD starting"); + + loadConfig(); + reloadConfig(); + + registerEvents(); + + registerCommands(); + } catch (Throwable t) { + getLogger().severe("MOTD - An error has occured!"); + if (!(t instanceof ExceptionInInitializerError)) { + t.printStackTrace(); + } + getServer().getPluginManager().disablePlugin(this); + } + } + + private void loadConfig() { + // TODO load Config files + saveDefaultConfig(); + counter = 0; + motdfile = new MOTDConfig(this, "MOTD.yml", "MOTD.yml"); + counterfile = new MOTDConfig(this, "counter.yml", "counter.yml"); + counter = counterfile.getInt("counter"); + + //motd = motdfile.getStringList("MOTD"); + setmotd(motdfile.getString("MOTD")); + //getLogger().info("setting motd to - " + motdfile.getString("MOTD")); + //getLogger().info("setting Counter to - " + counter); + for(String line : this.getConfig().getKeys(false)) { + if(!line.equalsIgnoreCase("mcmmo")) { + Options += line + " "; + } + } + //getLogger().info(Options); + } + + public void reloadsettings() { + try { + reloadConfig(); + motdfile.reload(); + counterfile.reload(); + counter = 0; + counter = counterfile.getInt("counter"); + loadmotd(motdfile.getString("MOTD")); + //getLogger().info("setting MOTD to - " + motdfile.getString("MOTD")); + //getLogger().info("setting Counter to - " + counter); + Options = ""; + + for(String line : this.getConfig().getKeys(false)) { + Options += line + " "; + } + } + catch (Throwable t) { + getLogger().severe("MOTD - An error has occured!"); + if (!(t instanceof ExceptionInInitializerError)) { + t.printStackTrace(); + } + } + } + public boolean loadmotd(String enable) { + if(!enable.equalsIgnoreCase("mcmmo")) { + for(String line : this.getConfig().getKeys(false)) { + if(line.equalsIgnoreCase(enable)) { + try { + setmotd(enable); + } + catch (Throwable t) { + if (!(t instanceof ExceptionInInitializerError)) { + t.printStackTrace(); + } + } + break; + } + } + return true; + } + return false; + } + + public boolean loadmotd(CommandSender sender, String enable) { + if(!enable.equalsIgnoreCase("mcmmo")) { + for(String line : this.getConfig().getKeys(false)) { + if(line.equalsIgnoreCase(enable)) { + try { + setmotd(enable); + sender.sendMessage(ChatColor.YELLOW + "MOTD set to " + ChatColor.GOLD + enable + ChatColor.YELLOW + "." ); + } + catch (Throwable t) { + sender.sendMessage(ChatColor.RED + "MOTD - An error has occured!"); + if (!(t instanceof ExceptionInInitializerError)) { + t.printStackTrace(); + } + } + return true; + } + } + sender.sendMessage(ChatColor.RED + "MOTD " + ChatColor.GOLD + enable + ChatColor.RED + " not found!"); + return false; + } + return false; + } + + public void setmotd(String motdname) { + if(motdname.equalsIgnoreCase("default") || motdname.equalsIgnoreCase("mcmmo")) { + //getLogger().info("MOTD - Checking for MCMMO counter: " + counter); + if(counter > 0) { + getLogger().info(" - Activating mcmmo MOTD"); + List lines = this.colorList(this.getConfig().getStringList("mcmmo")); + //getLogger().info(lines.get(0) + "\n" + lines.get(1)); + motd = lines; + //motd = this.getConfig().getStringList(motd + ".LINES"); + motdfile.set("MOTD", "default"); + motdfile.save(); + motdfile.reload(); + return; + } + else { + List lines = this.colorList(this.getConfig().getStringList(motdname)); + //getLogger().info(lines.get(0) + "\n" + lines.get(1)); + motd = lines; + //motd = this.getConfig().getStringList(motd + ".LINES"); + motdfile.set("MOTD", "default"); + motdfile.save(); + motdfile.reload(); + return; + } + } + else { + List lines = this.colorList(this.getConfig().getStringList(motdname)); + //getLogger().info(lines.get(0) + "\n" + lines.get(1)); + motd = lines; + //motd = this.getConfig().getStringList(motd + ".LINES"); + motdfile.set("MOTD", motdname); + motdfile.save(); + motdfile.reload(); + return; + } + } + + public boolean counter(CommandSender sender, String args) { + if(args.equalsIgnoreCase("up")) { + counter += 1; + counterfile.set("counter", counter); + sender.sendMessage(ChatColor.YELLOW + "MOTD counter increased."); + counterfile.save(); + reloadsettings(); + return true; + } + else if(args.equalsIgnoreCase("down")) { + counter -= 1; + counterfile.set("counter", counter); + sender.sendMessage(ChatColor.YELLOW + "MOTD counter decreased."); + counterfile.save(); + reloadsettings(); + return true; + } + return false; + } + + private void registerEvents() { + // TODO add all events + PluginManager pm = getServer().getPluginManager(); + pm.registerEvents(new PingListener(), this); + //pm.registerEvents(blockListener, this); + } + + private void registerCommands() { + // TODO add all commands + getCommand("custommotd").setExecutor(new custommotdCommand()); + //getCommand("customicon").setExecutor(new customIconCommand()); + } +} \ No newline at end of file diff --git a/src/main/java/com/alttd/destro174/custommotd/MotdWrapper.java b/src/main/java/com/alttd/destro174/custommotd/MotdWrapper.java new file mode 100644 index 0000000..9b01991 --- /dev/null +++ b/src/main/java/com/alttd/destro174/custommotd/MotdWrapper.java @@ -0,0 +1,50 @@ +package com.alttd.destro174.custommotd; + +public class MotdWrapper { + + private String title; + + private String line1; + private String line2; + + private boolean restricted; + + public MotdWrapper(String Title, String Line1, String Line2, boolean Restricted) { + this.title = Title; + this.line1 = Line1; + this.line2 = Line2; + this.restricted = Restricted; + } + + public String getTitle() { + return title; + } + + public String getLine1() { + return line1; + } + + public String getLine2() { + return line2; + } + + public void setLine1(String line1) { + this.line1 = line1; + } + + public void setLine2(String line2) { + this.line2 = line2; + } + + public boolean isRestricted() { + return restricted; + } + + public void setRestricted(boolean restricted) { + this.restricted = restricted; + } + + public void setTitle(String title) { + this.title = title; + } +} diff --git a/src/main/java/com/alttd/destro174/custommotd/commands/custommotdCommand.java b/src/main/java/com/alttd/destro174/custommotd/commands/custommotdCommand.java new file mode 100644 index 0000000..bad16f2 --- /dev/null +++ b/src/main/java/com/alttd/destro174/custommotd/commands/custommotdCommand.java @@ -0,0 +1,103 @@ +package com.alttd.destro174.custommotd.commands; + +import com.alttd.destro174.custommotd.CustomMOTD; +import net.md_5.bungee.api.ChatColor; +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; + + +public class custommotdCommand implements CommandExecutor/*, TabCompleter*/ { + + public CustomMOTD instance = CustomMOTD.getInstance(); + + @Override + public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { + if (sender.hasPermission("custommotd.admin")) { + switch (args.length) { + case 0: + return help(sender); + case 1: + if (args[0].equalsIgnoreCase("help")) { + return help(sender); + } + if (args[0].equalsIgnoreCase("list")) { + sender.sendMessage(ChatColor.YELLOW + "Here's a list of all possible MOTD:"); + sender.sendMessage(ChatColor.GOLD + CustomMOTD.Options); + return true; + } + if (args[0].equalsIgnoreCase("reload")) { + instance.reloadsettings(); + sender.sendMessage("Custom MOTD reloaded!"); + return true; + } + case 2: + if (args[0].equalsIgnoreCase("set")) { + if(!args[1].isEmpty()) { + if(args[1].equalsIgnoreCase("mcmmo")) { + sender.sendMessage(ChatColor.RED + "You can't set this MOTD!"); + return false; + }else { + return CustomMOTD.getInstance().loadmotd(sender, args[1]); + } + } + } + if (args[0].equalsIgnoreCase("counter")) { + if(!args[1].isEmpty()) { + return CustomMOTD.getInstance().counter(sender, args[1]); + } + } + default: + return help(sender); + } + } + else { + sender.sendMessage(ChatColor.RED + "You do not have permission to use this command." ); + return false; + } + } + + private boolean help(CommandSender sender) { + sender.sendMessage("-----" + ChatColor.YELLOW + "Altitude Motd" + "-----"); + sender.sendMessage(ChatColor.BOLD + "" + ChatColor.YELLOW + "/custommotd help " + + ChatColor.RESET + "- Show this help."); + sender.sendMessage(ChatColor.BOLD + "" + ChatColor.YELLOW + "/custommotd list " + + ChatColor.RESET + "- List all the custom MOTD."); + sender.sendMessage(ChatColor.BOLD + "" + ChatColor.YELLOW + "/custommotd set motdname" + + ChatColor.RESET + "- Set the MOTD."); + sender.sendMessage(ChatColor.BOLD + "" + ChatColor.YELLOW + "/custommotd counter up " + + ChatColor.RESET + "- Add one to the counter."); + sender.sendMessage(ChatColor.BOLD + "" + ChatColor.YELLOW + "/custommotd counter down " + + ChatColor.RESET + "- Remove one from the counter."); + sender.sendMessage(ChatColor.BOLD + "" + ChatColor.YELLOW + "/custommotd reload " + + ChatColor.RESET + "- Reload the config."); + return false; + } + + /*@Override + public List onTabComplete(CommandSender sender, Command cmd, String label, String[] args) { + List completions = new ArrayList<>(); + List commands = new ArrayList<>(); + + if(args.length == 1) { + commands.add("set"); + commands.add("reload"); + commands.add("counter"); + StringUtil.copyPartialMatches(args[0], commands, completions); + } + if(args.length == 2) { + if (args[0].equals("set")) { + completions = CustomMOTD.getmotdlist().stream() + .filter(wrapper -> !wrapper.isRestricted()) + .map(wrapper -> wrapper.getTitle()) + .collect(Collectors.toList()); + } else if (args[0].equals("counter")) { + commands.add("up"); + commands.add("down"); + StringUtil.copyPartialMatches(args[1], commands, completions); + } + } + return completions; + }*/ + +} \ No newline at end of file diff --git a/src/main/java/com/alttd/destro174/custommotd/listener/PingListener.java b/src/main/java/com/alttd/destro174/custommotd/listener/PingListener.java new file mode 100644 index 0000000..76403bb --- /dev/null +++ b/src/main/java/com/alttd/destro174/custommotd/listener/PingListener.java @@ -0,0 +1,79 @@ +package com.alttd.destro174.custommotd.listener; + +import com.alttd.destro174.custommotd.CustomMOTD; +import com.alttd.destro174.custommotd.MotdWrapper; +import org.bukkit.ChatColor; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; +import org.bukkit.event.server.ServerListPingEvent; + +public class PingListener implements Listener { + + public CustomMOTD instance = CustomMOTD.getInstance(); + + @EventHandler + public void onPing(ServerListPingEvent event) throws IllegalArgumentException, UnsupportedOperationException, Exception { + event.setMotd(CustomMOTD.motd.get(0) + "\n" + CustomMOTD.motd.get(1)); + } + + /*private List colorList(List list){ + List lines = new ArrayList(); + + for (String s : list) { + lines.add(ChatColor.translateAlternateColorCodes('&', s)); + } + + return lines; + }*/ + + /*@EventHandler + public void onPing(ServerListPingEvent event) { + //event.setMotd(instance.getActiveMotd()); + for(MotdWrapper wrapper : CustomMOTD.getmotdlist()) { + if(wrapper.getTitle().equalsIgnoreCase("default")) { + event.setMotd(ChatColor.translateAlternateColorCodes('&', + centerText(wrapper.getLine1(), 60) + + "\n" + centerText(wrapper.getLine2(), 60) + )); + } + } + + }*/ + + // lineLength = 80 (Chat Length) + // lineLength = 45 (MOTD Length) + public String centerText(String text, int lineLength) { + char[] chars = text.toCharArray(); // Get a list of all characters in text + boolean isBold = false; + double length = 0; + ChatColor pholder = null; + for (int i = 0; i < chars.length; i++) { // Loop through all characters + // Check if the character is a ColorCode.. + if (chars[i] == '&' && chars.length != (i + 1) && (pholder = ChatColor.getByChar(chars[i + 1])) != null) { + if (pholder != ChatColor.UNDERLINE && pholder != ChatColor.ITALIC + && pholder != ChatColor.STRIKETHROUGH && pholder != ChatColor.MAGIC) { + isBold = (chars[i + 1] == 'l'); // Setting bold to true or false, depending on if the ChatColor is Bold. + length--; // Removing one from the length, of the string, because we don't wanna count color codes. + i += isBold ? 1 : 0; + } + } else { + // If the character is not a color code: + length++; // Adding a space + length += (isBold ? (chars[i] != ' ' ? 0.1555555555555556 : 0) : 0); // Adding 0.156 spaces if the character is bold. + } + } + + double spaces = (lineLength - length) / 3; // Getting the spaces to add by (max line length - length) / 2 + + // Adding the spaces + StringBuilder builder = new StringBuilder(); + for (int i = 0; i < spaces; i++) { + builder.append(' '); + } + String copy = builder.toString(); + builder.append(text).append(copy); + + return builder.toString(); + } + +} \ No newline at end of file diff --git a/src/main/java/com/alttd/destro174/custommotd/storage/MOTDConfig.java b/src/main/java/com/alttd/destro174/custommotd/storage/MOTDConfig.java new file mode 100644 index 0000000..08dcb48 --- /dev/null +++ b/src/main/java/com/alttd/destro174/custommotd/storage/MOTDConfig.java @@ -0,0 +1,101 @@ +package com.alttd.destro174.custommotd.storage; + +import java.io.File; +import java.io.IOException; +import java.io.InputStreamReader; + +import org.bukkit.configuration.InvalidConfigurationException; +import org.bukkit.configuration.file.FileConfiguration; +import org.bukkit.configuration.file.YamlConfiguration; +import org.bukkit.plugin.java.JavaPlugin; + +public class MOTDConfig extends YamlConfiguration { + + private File file; + private String defaults; + private JavaPlugin plugin; + + /** + * Creates new PluginFile, without defaults + * @param plugin - Your plugin + * @param fileName - Name of the file + */ + public MOTDConfig(JavaPlugin plugin, String fileName) { + this(plugin, fileName, null); + } + + /** + * Creates new PluginFile, with defaults + * @param plugin - Your plugin + * @param fileName - Name of the file + * @param defaultsName - Name of the defaults + */ + public MOTDConfig(JavaPlugin plugin, String fileName, String defaultsName) { + this.plugin = plugin; + this.defaults = defaultsName; + this.file = new File(plugin.getDataFolder(), fileName); + reload(); + } + + + /** + * Reload configuration + */ + public void reload() { + + if (!file.exists()) { + + try { + file.getParentFile().mkdirs(); + file.createNewFile(); + + } catch (IOException exception) { + exception.printStackTrace(); + plugin.getLogger().severe("Error while creating file " + file.getName()); + } + + } + + try { + load(file); + + if (defaults != null) { + InputStreamReader reader = new InputStreamReader(plugin.getResource(defaults)); + FileConfiguration defaultsConfig = YamlConfiguration.loadConfiguration(reader); + + setDefaults(defaultsConfig); + options().copyDefaults(true); + + reader.close(); + save(); + } + + } catch (IOException exception) { + exception.printStackTrace(); + plugin.getLogger().severe("Error while loading file " + file.getName()); + + } catch (InvalidConfigurationException exception) { + exception.printStackTrace(); + plugin.getLogger().severe("Error while loading file " + file.getName()); + + } + + } + + /** + * Save configuration + */ + public void save() { + + try { + options().indent(2); + save(file); + + } catch (IOException exception) { + exception.printStackTrace(); + plugin.getLogger().severe("Error while saving file " + file.getName()); + } + + } + +} \ No newline at end of file diff --git a/src/main/resources/MOTD.yml b/src/main/resources/MOTD.yml new file mode 100644 index 0000000..40698f9 --- /dev/null +++ b/src/main/resources/MOTD.yml @@ -0,0 +1,2 @@ +MOTD: + default \ No newline at end of file diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml new file mode 100644 index 0000000..7d3c297 --- /dev/null +++ b/src/main/resources/config.yml @@ -0,0 +1,31 @@ +# 1. If someone tries to activate a new custom MOTD when another one is already active, +# they are both set to active. I need it to either auto disable all the others, +# or notify the person who issued the command that another is already active, +# and which one it is. + +#2. I'd like a command that will list all the available custom motds, just their names + +#3. Whenever I add a custom motd it requires a reboot AND sometimes +# it resets to the default config if changes were made before the server shut down. +# I need it to not reset the config if I upload a new one before issuing a restart, +# and I'd also like a reload command for the plugin so we don't need to reboot. + +#### This plugin will not work if the default and MCMMO entry are removed +# The MCMMO motd will activate when the default motd is on and the counter is higher then 0 +# any new motd added to this list must all be lowercase, if a capital is used the plugin won't read them propperly + +default: +- ' &eThis is the first line' +- ' &4This is the second line!' + +mcmmo: +- ' &eThis is the first line' +- ' &4This is the second line!' + +custom1: +- ' &eThis is the first line' +- ' &4This is the second line!' + +justatest: +- ' &eThis is the first line' +- ' &4This is the second line!' \ No newline at end of file diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml new file mode 100644 index 0000000..ae44402 --- /dev/null +++ b/src/main/resources/plugin.yml @@ -0,0 +1,12 @@ +name: CustomMOTD +main: alttd.destro.custommotd.CustomMOTD +version: 1.0 +author: destro174 +commands: + custommotd: + aliases: [cmotd] + description: Set motd in-game or in console. + customicon: + aliases: [cicon] + description: Change the servericon in game or from console. + \ No newline at end of file