From a267261c7d8edbab704cb3345e9b54f2eec73ce2 Mon Sep 17 00:00:00 2001 From: Brianna Date: Tue, 20 Aug 2019 13:05:19 -0400 Subject: [PATCH] Added libraries --- SongodaCore.iml | 18 ++ pom.xml | 26 +- .../com/songoda/{update => core}/Module.java | 2 +- .../com/songoda/{update => core}/Plugin.java | 2 +- .../SongodaCore.java} | 21 +- .../command/AbstractCommand.java | 0 .../command/CommandManager.java | 0 .../{update => core}/command/TabManager.java | 0 .../command/commands/CommandDiag.java | 0 .../command/commands/CommandSongoda.java | 0 .../{update => core}/gui/GUIOverview.java | 0 .../core/library/economy/EconomyManager.java | 29 ++ .../library/economy/economies/Economy.java | 12 + .../economies/PlayerPointsEconomy.java | 38 +++ .../economy/economies/ReserveEconomy.java | 32 ++ .../economy/economies/VaultEconomy.java | 28 ++ .../songoda/core/library/locale/Locale.java | 302 ++++++++++++++++++ .../songoda/core/library/locale/Message.java | 128 ++++++++ .../listeners/LoginListener.java | 0 .../utils/AbstractChatConfirm.java | 8 +- .../{update => core}/utils/Methods.java | 2 +- .../{update => core}/utils/ServerVersion.java | 2 +- .../utils/gui/AbstractGUI.java | 0 .../{update => core}/utils/gui/Clickable.java | 0 .../{update => core}/utils/gui/OnClose.java | 0 .../{update => core}/utils/gui/Range.java | 0 26 files changed, 629 insertions(+), 21 deletions(-) create mode 100644 SongodaCore.iml rename src/main/java/com/songoda/{update => core}/Module.java (68%) rename src/main/java/com/songoda/{update => core}/Plugin.java (98%) rename src/main/java/com/songoda/{update/SongodaUpdate.java => core/SongodaCore.java} (88%) rename src/main/java/com/songoda/{update => core}/command/AbstractCommand.java (100%) rename src/main/java/com/songoda/{update => core}/command/CommandManager.java (100%) rename src/main/java/com/songoda/{update => core}/command/TabManager.java (100%) rename src/main/java/com/songoda/{update => core}/command/commands/CommandDiag.java (100%) rename src/main/java/com/songoda/{update => core}/command/commands/CommandSongoda.java (100%) rename src/main/java/com/songoda/{update => core}/gui/GUIOverview.java (100%) create mode 100644 src/main/java/com/songoda/core/library/economy/EconomyManager.java create mode 100644 src/main/java/com/songoda/core/library/economy/economies/Economy.java create mode 100644 src/main/java/com/songoda/core/library/economy/economies/PlayerPointsEconomy.java create mode 100644 src/main/java/com/songoda/core/library/economy/economies/ReserveEconomy.java create mode 100644 src/main/java/com/songoda/core/library/economy/economies/VaultEconomy.java create mode 100644 src/main/java/com/songoda/core/library/locale/Locale.java create mode 100644 src/main/java/com/songoda/core/library/locale/Message.java rename src/main/java/com/songoda/{update => core}/listeners/LoginListener.java (100%) rename src/main/java/com/songoda/{update => core}/utils/AbstractChatConfirm.java (93%) rename src/main/java/com/songoda/{update => core}/utils/Methods.java (93%) rename src/main/java/com/songoda/{update => core}/utils/ServerVersion.java (95%) rename src/main/java/com/songoda/{update => core}/utils/gui/AbstractGUI.java (100%) rename src/main/java/com/songoda/{update => core}/utils/gui/Clickable.java (100%) rename src/main/java/com/songoda/{update => core}/utils/gui/OnClose.java (100%) rename src/main/java/com/songoda/{update => core}/utils/gui/Range.java (100%) diff --git a/SongodaCore.iml b/SongodaCore.iml new file mode 100644 index 00000000..158fd46a --- /dev/null +++ b/SongodaCore.iml @@ -0,0 +1,18 @@ + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/pom.xml b/pom.xml index 2e77284f..26f19661 100644 --- a/pom.xml +++ b/pom.xml @@ -1,11 +1,11 @@ com.songoda - SongodaUpdater + SongodaCore 4.0.0 1 clean install - SongodaUpdater-${project.version} + SongodaCore-${project.version} org.apache.maven.plugins @@ -30,5 +30,27 @@ spigot 1.14 + + com.gmail.filoghost.holographicdisplays + holographicdisplays-api + 2.3.2 + + + net.tnemc + Reserve + 0.1.3.0 + provided + + + org.black_ixx + playerpoints + 2.1.4 + + + net.milkbowl + vault + 1.7.1 + + diff --git a/src/main/java/com/songoda/update/Module.java b/src/main/java/com/songoda/core/Module.java similarity index 68% rename from src/main/java/com/songoda/update/Module.java rename to src/main/java/com/songoda/core/Module.java index 0ac93f84..76708cd2 100644 --- a/src/main/java/com/songoda/update/Module.java +++ b/src/main/java/com/songoda/core/Module.java @@ -1,4 +1,4 @@ -package com.songoda.update; +package com.songoda.core; public interface Module { diff --git a/src/main/java/com/songoda/update/Plugin.java b/src/main/java/com/songoda/core/Plugin.java similarity index 98% rename from src/main/java/com/songoda/update/Plugin.java rename to src/main/java/com/songoda/core/Plugin.java index 1d3a79a5..3e783b6b 100644 --- a/src/main/java/com/songoda/update/Plugin.java +++ b/src/main/java/com/songoda/core/Plugin.java @@ -1,4 +1,4 @@ -package com.songoda.update; +package com.songoda.core; import org.bukkit.plugin.java.JavaPlugin; import org.json.simple.JSONObject; diff --git a/src/main/java/com/songoda/update/SongodaUpdate.java b/src/main/java/com/songoda/core/SongodaCore.java similarity index 88% rename from src/main/java/com/songoda/update/SongodaUpdate.java rename to src/main/java/com/songoda/core/SongodaCore.java index d4719171..57bac61c 100644 --- a/src/main/java/com/songoda/update/SongodaUpdate.java +++ b/src/main/java/com/songoda/core/SongodaCore.java @@ -1,8 +1,8 @@ -package com.songoda.update; +package com.songoda.core; -import com.songoda.update.command.CommandManager; -import com.songoda.update.listeners.LoginListener; -import com.songoda.update.utils.ServerVersion; +import com.songoda.core.command.CommandManager; +import com.songoda.core.listeners.LoginListener; +import com.songoda.core.utils.ServerVersion; import org.apache.commons.lang.ArrayUtils; import org.bukkit.Bukkit; import org.bukkit.plugin.java.JavaPlugin; @@ -15,13 +15,12 @@ import java.io.InputStream; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; -import java.net.URLConnection; import java.util.ArrayList; import java.util.List; -public class SongodaUpdate { +public class SongodaCore { - private static String prefix = "[SongodaUpdate] "; + private static String prefix = "[SongodaCore] "; private ServerVersion serverVersion = ServerVersion.fromPackageName(Bukkit.getServer().getClass().getPackage().getName()); @@ -30,11 +29,11 @@ public class SongodaUpdate { private static List registeredPlugins = new ArrayList<>(); - private static SongodaUpdate INSTANCE; + private static SongodaCore INSTANCE; private static JavaPlugin hijackedPlugin; - public SongodaUpdate() { + public SongodaCore() { hijackedPlugin = registeredPlugins.get(0).getJavaPlugin(); Bukkit.getPluginManager().registerEvents(new LoginListener(this), hijackedPlugin); @@ -80,7 +79,7 @@ public class SongodaUpdate { public static Plugin load(Plugin plugin) { registeredPlugins.add(plugin); System.out.println(prefix + "Hooked " + plugin.getJavaPlugin().getName() + "."); - if (INSTANCE == null) INSTANCE = new SongodaUpdate(); + if (INSTANCE == null) INSTANCE = new SongodaCore(); getInstance().update(plugin); return plugin; } @@ -116,7 +115,7 @@ public class SongodaUpdate { return hijackedPlugin; } - public static SongodaUpdate getInstance() { + public static SongodaCore getInstance() { return INSTANCE; } } diff --git a/src/main/java/com/songoda/update/command/AbstractCommand.java b/src/main/java/com/songoda/core/command/AbstractCommand.java similarity index 100% rename from src/main/java/com/songoda/update/command/AbstractCommand.java rename to src/main/java/com/songoda/core/command/AbstractCommand.java diff --git a/src/main/java/com/songoda/update/command/CommandManager.java b/src/main/java/com/songoda/core/command/CommandManager.java similarity index 100% rename from src/main/java/com/songoda/update/command/CommandManager.java rename to src/main/java/com/songoda/core/command/CommandManager.java diff --git a/src/main/java/com/songoda/update/command/TabManager.java b/src/main/java/com/songoda/core/command/TabManager.java similarity index 100% rename from src/main/java/com/songoda/update/command/TabManager.java rename to src/main/java/com/songoda/core/command/TabManager.java diff --git a/src/main/java/com/songoda/update/command/commands/CommandDiag.java b/src/main/java/com/songoda/core/command/commands/CommandDiag.java similarity index 100% rename from src/main/java/com/songoda/update/command/commands/CommandDiag.java rename to src/main/java/com/songoda/core/command/commands/CommandDiag.java diff --git a/src/main/java/com/songoda/update/command/commands/CommandSongoda.java b/src/main/java/com/songoda/core/command/commands/CommandSongoda.java similarity index 100% rename from src/main/java/com/songoda/update/command/commands/CommandSongoda.java rename to src/main/java/com/songoda/core/command/commands/CommandSongoda.java diff --git a/src/main/java/com/songoda/update/gui/GUIOverview.java b/src/main/java/com/songoda/core/gui/GUIOverview.java similarity index 100% rename from src/main/java/com/songoda/update/gui/GUIOverview.java rename to src/main/java/com/songoda/core/gui/GUIOverview.java diff --git a/src/main/java/com/songoda/core/library/economy/EconomyManager.java b/src/main/java/com/songoda/core/library/economy/EconomyManager.java new file mode 100644 index 00000000..148601b4 --- /dev/null +++ b/src/main/java/com/songoda/core/library/economy/EconomyManager.java @@ -0,0 +1,29 @@ +package com.songoda.core.library.economy; + +import com.songoda.core.library.economy.economies.Economy; +import com.songoda.core.library.economy.economies.PlayerPointsEconomy; +import com.songoda.core.library.economy.economies.ReserveEconomy; +import com.songoda.core.library.economy.economies.VaultEconomy; +import org.bukkit.Bukkit; +import org.bukkit.plugin.PluginManager; + +import java.util.HashMap; +import java.util.Map; + +public class EconomyManager { + + private static Map registeredEconomies = new HashMap(); + + public static void load() { + if (!registeredEconomies.isEmpty()) return; + + PluginManager pluginManager = Bukkit.getPluginManager(); + + if (pluginManager.isPluginEnabled("Vault")) + registeredEconomies.put(new VaultEconomy()); + if (pluginManager.isPluginEnabled("Reserve")) + registeredEconomies.add(new ReserveEconomy()); + if (pluginManager.isPluginEnabled("PlayerPoints")) + registeredEconomies.add(new PlayerPointsEconomy()); + } +} diff --git a/src/main/java/com/songoda/core/library/economy/economies/Economy.java b/src/main/java/com/songoda/core/library/economy/economies/Economy.java new file mode 100644 index 00000000..9d5a9141 --- /dev/null +++ b/src/main/java/com/songoda/core/library/economy/economies/Economy.java @@ -0,0 +1,12 @@ +package com.songoda.ultimateclaims.economy; + +import org.bukkit.OfflinePlayer; + +public interface Economy { + + boolean hasBalance(OfflinePlayer player, double cost); + + boolean withdrawBalance(OfflinePlayer player, double cost); + + boolean deposit(OfflinePlayer player, double amount); +} diff --git a/src/main/java/com/songoda/core/library/economy/economies/PlayerPointsEconomy.java b/src/main/java/com/songoda/core/library/economy/economies/PlayerPointsEconomy.java new file mode 100644 index 00000000..bc739556 --- /dev/null +++ b/src/main/java/com/songoda/core/library/economy/economies/PlayerPointsEconomy.java @@ -0,0 +1,38 @@ +package com.songoda.ultimateclaims.economy; + +import org.black_ixx.playerpoints.PlayerPoints; +import org.bukkit.Bukkit; +import org.bukkit.OfflinePlayer; + +public class PlayerPointsEconomy implements Economy { + + private final PlayerPoints playerPoints; + + public PlayerPointsEconomy() { + this.playerPoints = (PlayerPoints) Bukkit.getServer().getPluginManager().getPlugin("PlayerPoints"); + } + + private int convertAmount(double amount) { + return (int) Math.ceil(amount); + } + + @Override + public boolean hasBalance(OfflinePlayer player, double cost) { + int amount = convertAmount(cost); + return playerPoints.getAPI().look(player.getUniqueId()) >= amount; + + } + + @Override + public boolean withdrawBalance(OfflinePlayer player, double cost) { + int amount = convertAmount(cost); + return playerPoints.getAPI().take(player.getUniqueId(), amount); + + } + + @Override + public boolean deposit(OfflinePlayer player, double amount) { + int amt = convertAmount(amount); + return playerPoints.getAPI().give(player.getUniqueId(), amt); + } +} diff --git a/src/main/java/com/songoda/core/library/economy/economies/ReserveEconomy.java b/src/main/java/com/songoda/core/library/economy/economies/ReserveEconomy.java new file mode 100644 index 00000000..5fecdf3e --- /dev/null +++ b/src/main/java/com/songoda/core/library/economy/economies/ReserveEconomy.java @@ -0,0 +1,32 @@ +package com.songoda.ultimateclaims.economy; + +import net.tnemc.core.Reserve; +import net.tnemc.core.economy.EconomyAPI; +import org.bukkit.OfflinePlayer; + +import java.math.BigDecimal; + +public class ReserveEconomy implements Economy { + + EconomyAPI economyAPI; + + public ReserveEconomy() { + if (Reserve.instance().economyProvided()) + economyAPI = Reserve.instance().economy(); + } + + @Override + public boolean hasBalance(OfflinePlayer player, double cost) { + return economyAPI.hasHoldings(player.getUniqueId(), new BigDecimal(cost)); + } + + @Override + public boolean withdrawBalance(OfflinePlayer player, double cost) { + return economyAPI.removeHoldings(player.getUniqueId(), new BigDecimal(cost)); + } + + @Override + public boolean deposit(OfflinePlayer player, double amount) { + return economyAPI.addHoldings(player.getUniqueId(), new BigDecimal(amount)); + } +} diff --git a/src/main/java/com/songoda/core/library/economy/economies/VaultEconomy.java b/src/main/java/com/songoda/core/library/economy/economies/VaultEconomy.java new file mode 100644 index 00000000..dca7967c --- /dev/null +++ b/src/main/java/com/songoda/core/library/economy/economies/VaultEconomy.java @@ -0,0 +1,28 @@ +package com.songoda.ultimateclaims.economy; + +import org.bukkit.Bukkit; +import org.bukkit.OfflinePlayer; + +public class VaultEconomy implements Economy { + private final net.milkbowl.vault.economy.Economy vault; + + public VaultEconomy() { + this.vault = Bukkit.getServicesManager(). + getRegistration(net.milkbowl.vault.economy.Economy.class).getProvider(); + } + + @Override + public boolean hasBalance(OfflinePlayer player, double cost) { + return vault.has(player, cost); + } + + @Override + public boolean withdrawBalance(OfflinePlayer player, double cost) { + return vault.withdrawPlayer(player, cost).transactionSuccess(); + } + + @Override + public boolean deposit(OfflinePlayer player, double amount) { + return vault.depositPlayer(player, amount).transactionSuccess(); + } +} diff --git a/src/main/java/com/songoda/core/library/locale/Locale.java b/src/main/java/com/songoda/core/library/locale/Locale.java new file mode 100644 index 00000000..a12f68b0 --- /dev/null +++ b/src/main/java/com/songoda/core/library/locale/Locale.java @@ -0,0 +1,302 @@ +package com.songoda.ultimateclaims.utils.locale; + +import org.bukkit.plugin.java.JavaPlugin; + +import java.io.*; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.regex.Matcher; +import java.util.regex.Pattern; +import java.util.stream.Collectors; + +/** + * Assists in the utilization of localization files. + * Created to be used by the Songoda Team. + * + * @author Brianna O'Keefe - Songoda + */ +public class Locale { + + private static final List LOCALES = new ArrayList<>(); + private static final Pattern NODE_PATTERN = Pattern.compile("(\\w+(?:\\.{1}\\w+)*)\\s*=\\s*\"(.*)\""); + private static final String FILE_EXTENSION = ".lang"; + private static JavaPlugin plugin; + private static File localeFolder; + + private final Map nodes = new HashMap<>(); + + private static String defaultLocale; + + private File file; + private String name; + + /** + * Instantiate the Locale class for future use + * + * @param name the name of the instantiated language + */ + private Locale(String name) { + if (plugin == null) + return; + + this.name = name; + + String fileName = name + FILE_EXTENSION; + this.file = new File(localeFolder, fileName); + + if (!this.reloadMessages()) return; + + plugin.getLogger().info("Loaded locale \"" + fileName + "\""); + } + + /** + * Initialize the class to load all existing language files and update them. + * This must be called before any other methods in this class as otherwise + * the methods will fail to invoke + * + * @param plugin the plugin instance + * @param defaultLocale the default language + */ + public Locale(JavaPlugin plugin, String defaultLocale) { + + Locale.plugin = plugin; + Locale.localeFolder = new File(plugin.getDataFolder(), "locales/"); + + if (!localeFolder.exists()) localeFolder.mkdirs(); + + //Save the default locale file. + Locale.defaultLocale = defaultLocale; + saveLocale(defaultLocale); + + for (File file : localeFolder.listFiles()) { + String fileName = file.getName(); + if (!fileName.endsWith(FILE_EXTENSION)) continue; + + String name = fileName.substring(0, fileName.lastIndexOf('.')); + + if (name.split("_").length != 2) continue; + if (localeLoaded(name)) continue; + + LOCALES.add(new Locale(name)); + } + } + + /** + * Save a locale file from the InputStream, to the locale folder + * + * @param fileName the name of the file to save + * @return true if the operation was successful, false otherwise + */ + public static boolean saveLocale(String fileName) { + return saveLocale(plugin.getResource(defaultLocale + FILE_EXTENSION), fileName); + } + + + /** + * Save a locale file from the InputStream, to the locale folder + * + * @param in file to save + * @param fileName the name of the file to save + * @return true if the operation was successful, false otherwise + */ + public static boolean saveLocale(InputStream in, String fileName) { + if (!localeFolder.exists()) localeFolder.mkdirs(); + + if (!fileName.endsWith(FILE_EXTENSION)) + fileName = (fileName.lastIndexOf(".") == -1 ? fileName : fileName.substring(0, fileName.lastIndexOf('.'))) + FILE_EXTENSION; + + File destinationFile = new File(localeFolder, fileName); + if (destinationFile.exists()) + return compareFiles(in, destinationFile); + + try (OutputStream outputStream = new FileOutputStream(destinationFile)) { + copy(in, outputStream); + + fileName = fileName.substring(0, fileName.lastIndexOf('.')); + + if (fileName.split("_").length != 2) return false; + + LOCALES.add(new Locale(fileName)); + if (defaultLocale == null) defaultLocale = fileName; + return true; + } catch (IOException e) { + return false; + } + } + + // Write new changes to existing files, if any at all + private static boolean compareFiles(InputStream in, File existingFile) { + InputStream defaultFile = + in == null ? plugin.getResource((defaultLocale != null ? defaultLocale : "en_US") + FILE_EXTENSION) : in; + + boolean changed = false; + + List defaultLines, existingLines; + try (BufferedReader defaultReader = new BufferedReader(new InputStreamReader(defaultFile)); + BufferedReader existingReader = new BufferedReader(new FileReader(existingFile)); + BufferedWriter writer = new BufferedWriter(new FileWriter(existingFile, true))) { + defaultLines = defaultReader.lines().collect(Collectors.toList()); + existingLines = existingReader.lines().map(s -> s.split("\\s*=")[0]).collect(Collectors.toList()); + + for (String defaultValue : defaultLines) { + if (defaultValue.isEmpty() || defaultValue.startsWith("#")) continue; + + String key = defaultValue.split("\\s*=")[0]; + + if (!existingLines.contains(key)) { + if (!changed) { + writer.newLine(); + writer.newLine(); + // Leave a note alerting the user of the newly added messages. + writer.write("# New messages for " + plugin.getName() + " v" + plugin.getDescription().getVersion() + "."); + + // If changes were found outside of the default file leave a note explaining that. + if (in == null) { + writer.newLine(); + writer.write("# These translations were found untranslated, join"); + writer.newLine(); + writer.write("# our translation Discord https://discord.gg/f7fpZEf"); + writer.newLine(); + writer.write("# to request an official update!"); + } + } + + writer.newLine(); + writer.write(defaultValue); + + changed = true; + } + } + if (in != null && !changed) compareFiles(null, existingFile); + } catch (IOException e) { + return false; + } + + return changed; + } + + + /** + * Check whether a locale exists and is registered or not + * + * @param name the whole language tag (i.e. "en_US") + * @return true if it exists + */ + public static boolean localeLoaded(String name) { + for (Locale locale : LOCALES) + if (locale.getName().equals(name)) return true; + return false; + } + + + /** + * Get a locale by its entire proper name (i.e. "en_US") + * + * @param name the full name of the locale + * @return locale of the specified name + */ + public static Locale getLocale(String name) { + for (Locale locale : LOCALES) + if (locale.getName().equalsIgnoreCase(name)) return locale; + return null; + } + + /** + * Clear the previous message cache and load new messages directly from file + * + * @return reload messages from file + */ + public boolean reloadMessages() { + if (!this.file.exists()) { + plugin.getLogger().warning("Could not find file for locale \"" + this.name + "\""); + return false; + } + + this.nodes.clear(); // Clear previous data (if any) + + try (BufferedReader reader = new BufferedReader(new FileReader(file))) { + String line; + for (int lineNumber = 0; (line = reader.readLine()) != null; lineNumber++) { + if (line.trim().isEmpty() || line.startsWith("#") /* Comment */) continue; + + Matcher matcher = NODE_PATTERN.matcher(line); + if (!matcher.find()) { + System.err.println("Invalid locale syntax at (line=" + lineNumber + ")"); + continue; + } + + nodes.put(matcher.group(1), matcher.group(2)); + } + } catch (IOException e) { + e.printStackTrace(); + return false; + } + return true; + } + + /** + * Supply the Message object with the plugins prefix. + * + * @param message message to be applied + * @return applied message + */ + private Message supplyPrefix(Message message) { + return message.setPrefix(this.nodes.getOrDefault("general.nametag.prefix", "[Plugin]")); + } + + /** + * Create a new unsaved Message + * + * @param message the message to create + * @return the created message + */ + public Message newMessage(String message) { + return supplyPrefix(new Message(message)); + } + + /** + * Get a message set for a specific node. + * + * @param node the node to get + * @return the message for the specified node + */ + public Message getMessage(String node) { + return this.getMessageOrDefault(node, node); + } + + /** + * Get a message set for a specific node + * + * @param node the node to get + * @param defaultValue the default value given that a value for the node was not found + * @return the message for the specified node. Default if none found + */ + public Message getMessageOrDefault(String node, String defaultValue) { + return supplyPrefix(new Message(this.nodes.getOrDefault(node, defaultValue))); + } + + /** + * Return the locale name (i.e. "en_US") + * + * @return the locale name + */ + public String getName() { + return name; + } + + private static void copy(InputStream input, OutputStream output) { + int n; + byte[] buffer = new byte[1024 * 4]; + + try { + while ((n = input.read(buffer)) != -1) { + output.write(buffer, 0, n); + } + } catch (IOException e) { + e.printStackTrace(); + } + } + +} diff --git a/src/main/java/com/songoda/core/library/locale/Message.java b/src/main/java/com/songoda/core/library/locale/Message.java new file mode 100644 index 00000000..7ad2f6b2 --- /dev/null +++ b/src/main/java/com/songoda/core/library/locale/Message.java @@ -0,0 +1,128 @@ +package com.songoda.ultimateclaims.utils.locale; + +import org.bukkit.ChatColor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +/** + * The Message object. This holds the message to be sent + * as well as the plugins prefix so that they can both be + * easily manipulated then deployed + */ +public class Message { + + private String prefix = null; + private String message; + + /** + * create a new message + * + * @param message the message text + */ + public Message(String message) { + this.message = message; + } + + /** + * Format and send the held message to a player + * + * @param player player to send the message to + */ + public void sendMessage(Player player) { + player.sendMessage(this.getMessage()); + } + + /** + * Format and send the held message with the + * appended plugin prefix to a player + * + * @param player player to send the message to + */ + public void sendPrefixedMessage(Player player) { + player.sendMessage(this.getPrefixedMessage()); + } + + /** + * Format and send the held message to a player + * + * @param sender command sender to send the message to + */ + public void sendMessage(CommandSender sender) { + sender.sendMessage(this.getMessage()); + } + + /** + * Format and send the held message to a player as a title message + * + * @param sender command sender to send the message to + */ + public void sendTitle(CommandSender sender) { + if(sender instanceof Player) { + ((Player) sender).sendTitle("", this.getMessage(), 10, 20, 10); + } else { + sender.sendMessage(this.getMessage()); + } + } + + /** + * Format and send the held message with the + * appended plugin prefix to a command sender + * + * @param sender command sender to send the message to + */ + public void sendPrefixedMessage(CommandSender sender) { + sender.sendMessage(this.getPrefixedMessage()); + } + + /** + * Format the held message and append the plugins + * prefix + * + * @return the prefixed message + */ + public String getPrefixedMessage() { + return ChatColor.translateAlternateColorCodes('&',(prefix == null ? "" : this.prefix) + + " " + this.message); + } + + /** + * Get and format the held message + * + * @return the message + */ + public String getMessage() { + return ChatColor.translateAlternateColorCodes('&', this.message); + } + + /** + * Get the held message + * + * @return the message + */ + public String getUnformattedMessage() { + return this.message; + } + + /** + * Replace the provided placeholder with the + * provided object + * + * @param placeholder the placeholder to replace + * @param replacement the replacement object + * @return the modified Message + */ + public Message processPlaceholder(String placeholder, Object replacement) { + this.message = message.replace("%" + placeholder + "%", replacement.toString()); + return this; + } + + Message setPrefix(String prefix) { + this.prefix = prefix; + return this; + } + + @Override + public String toString() { + return this.message; + } +} \ No newline at end of file diff --git a/src/main/java/com/songoda/update/listeners/LoginListener.java b/src/main/java/com/songoda/core/listeners/LoginListener.java similarity index 100% rename from src/main/java/com/songoda/update/listeners/LoginListener.java rename to src/main/java/com/songoda/core/listeners/LoginListener.java diff --git a/src/main/java/com/songoda/update/utils/AbstractChatConfirm.java b/src/main/java/com/songoda/core/utils/AbstractChatConfirm.java similarity index 93% rename from src/main/java/com/songoda/update/utils/AbstractChatConfirm.java rename to src/main/java/com/songoda/core/utils/AbstractChatConfirm.java index 77212692..5360e8b4 100644 --- a/src/main/java/com/songoda/update/utils/AbstractChatConfirm.java +++ b/src/main/java/com/songoda/core/utils/AbstractChatConfirm.java @@ -1,6 +1,6 @@ -package com.songoda.update.utils; +package com.songoda.core.utils; -import com.songoda.update.SongodaUpdate; +import com.songoda.core.SongodaCore; import org.bukkit.Bukkit; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; @@ -27,7 +27,7 @@ public class AbstractChatConfirm implements Listener { this.player = player; this.handler = hander; player.closeInventory(); - initializeListeners(SongodaUpdate.getHijackedPlugin()); + initializeListeners(SongodaCore.getHijackedPlugin()); registered.add(player.getUniqueId()); } @@ -62,7 +62,7 @@ public class AbstractChatConfirm implements Listener { }; - Bukkit.getPluginManager().registerEvents(listener, SongodaUpdate.getHijackedPlugin()); + Bukkit.getPluginManager().registerEvents(listener, SongodaCore.getHijackedPlugin()); } public void setOnClose(OnClose onClose) { diff --git a/src/main/java/com/songoda/update/utils/Methods.java b/src/main/java/com/songoda/core/utils/Methods.java similarity index 93% rename from src/main/java/com/songoda/update/utils/Methods.java rename to src/main/java/com/songoda/core/utils/Methods.java index 3410cd93..30cedaa4 100644 --- a/src/main/java/com/songoda/update/utils/Methods.java +++ b/src/main/java/com/songoda/core/utils/Methods.java @@ -1,4 +1,4 @@ -package com.songoda.update.utils; +package com.songoda.core.utils; import org.bukkit.ChatColor; diff --git a/src/main/java/com/songoda/update/utils/ServerVersion.java b/src/main/java/com/songoda/core/utils/ServerVersion.java similarity index 95% rename from src/main/java/com/songoda/update/utils/ServerVersion.java rename to src/main/java/com/songoda/core/utils/ServerVersion.java index e5749c17..ed76419f 100644 --- a/src/main/java/com/songoda/update/utils/ServerVersion.java +++ b/src/main/java/com/songoda/core/utils/ServerVersion.java @@ -1,4 +1,4 @@ -package com.songoda.update.utils; +package com.songoda.core.utils; public enum ServerVersion { diff --git a/src/main/java/com/songoda/update/utils/gui/AbstractGUI.java b/src/main/java/com/songoda/core/utils/gui/AbstractGUI.java similarity index 100% rename from src/main/java/com/songoda/update/utils/gui/AbstractGUI.java rename to src/main/java/com/songoda/core/utils/gui/AbstractGUI.java diff --git a/src/main/java/com/songoda/update/utils/gui/Clickable.java b/src/main/java/com/songoda/core/utils/gui/Clickable.java similarity index 100% rename from src/main/java/com/songoda/update/utils/gui/Clickable.java rename to src/main/java/com/songoda/core/utils/gui/Clickable.java diff --git a/src/main/java/com/songoda/update/utils/gui/OnClose.java b/src/main/java/com/songoda/core/utils/gui/OnClose.java similarity index 100% rename from src/main/java/com/songoda/update/utils/gui/OnClose.java rename to src/main/java/com/songoda/core/utils/gui/OnClose.java diff --git a/src/main/java/com/songoda/update/utils/gui/Range.java b/src/main/java/com/songoda/core/utils/gui/Range.java similarity index 100% rename from src/main/java/com/songoda/update/utils/gui/Range.java rename to src/main/java/com/songoda/core/utils/gui/Range.java