From 98c964fd5880c46af1c59180f6ee13b1a5ff2ed5 Mon Sep 17 00:00:00 2001 From: Esophose Date: Wed, 11 May 2016 19:45:33 -0600 Subject: [PATCH] :fireworks: Commenting Add comments to all methods and variables to explain what the heck is going on --- .../playerparticles/ConfigManager.java | 130 +++++++++++------- .../playerparticles/MessageManager.java | 24 ++++ .../ParticleCommandCompleter.java | 10 +- .../playerparticles/ParticleCreator.java | 102 +++++++++++++- .../playerparticles/ParticleStyle.java | 6 + .../playerparticles/PermissionHandler.java | 26 ++++ .../playerparticles/PlayerParticles.java | 60 +++++++- .../updater/PluginUpdateListener.java | 5 + 8 files changed, 303 insertions(+), 60 deletions(-) diff --git a/src/com/esophose/playerparticles/ConfigManager.java b/src/com/esophose/playerparticles/ConfigManager.java index 3707442..2cd642e 100644 --- a/src/com/esophose/playerparticles/ConfigManager.java +++ b/src/com/esophose/playerparticles/ConfigManager.java @@ -10,37 +10,53 @@ package com.esophose.playerparticles; import java.io.File; import java.io.IOException; -import java.io.InputStream; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.ArrayList; -import java.util.HashMap; -import java.util.Scanner; -import org.bukkit.configuration.InvalidConfigurationException; import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.entity.Player; -import org.bukkit.plugin.java.JavaPlugin; import com.esophose.playerparticles.libraries.particles.ParticleEffect.ParticleType; public class ConfigManager { + /** + * The instance of the ConfigManager used for effect data + */ private static ConfigManager instance = new ConfigManager("effectData"); + /** + * The instance of the ConfigManager used for style data + */ private static ConfigManager styleInstance = new ConfigManager("styleData"); + /** + * The file the data is located in for the instance + */ private File file; + /** + * The configuration used to edit the .yaml file + */ private FileConfiguration config; + /** + * @return The instance of the config for effects + */ public static ConfigManager getInstance() { return instance; } + /** + * @return The instance of the config for styles + */ public static ConfigManager getStyleInstance() { return styleInstance; } + /** + * @param fileName Will either be "effectData" or "styleData" + */ private ConfigManager(String fileName) { if (!PlayerParticles.getPlugin().getDataFolder().exists()) PlayerParticles.getPlugin().getDataFolder().mkdir(); @@ -54,6 +70,10 @@ public class ConfigManager { config = YamlConfiguration.loadConfiguration(file); } + /** + * Removes any data contained within the current config instance + * Never used + */ public void flushData() { for(String key : config.getKeys(false)) { config.set(key, null); @@ -62,6 +82,10 @@ public class ConfigManager { catch (IOException e) {e.printStackTrace();} } + /** + * Removes all the player, effect, and style data from the connected database + * Never used + */ public static void flushDatabase() { if(PlayerParticles.useMySQL) { Statement statement; @@ -74,6 +98,13 @@ public class ConfigManager { } } + /** + * Saves the particle effect to the player's name in either the database or config + * Should only be called from the effectData instance + * + * @param type The type of the particle + * @param player The player to save the particle to + */ public void setParticle(ParticleType type, Player player){ if(PlayerParticles.useMySQL) { Statement statement; @@ -90,6 +121,12 @@ public class ConfigManager { } } + /** + * Removes the particle effect from the player's name in either the database or config + * Should only be called from the effectData instance + * + * @param player The player to clear the particle effect from + */ public void resetParticle(Player player){ if(PlayerParticles.useMySQL) { Statement statement; @@ -106,6 +143,12 @@ public class ConfigManager { } } + /** + * Gets the particle effect saved in either the database or config for the player + * + * @param player The player to get the particle effect data for + * @return The particle effect for the player + */ public ParticleType getParticle(Player player){ if(PlayerParticles.useMySQL) { Statement statement; @@ -124,6 +167,13 @@ public class ConfigManager { return null; } + /** + * Saves the style effect to the player's name in either the database or config + * Should only be called from the effectData instance + * + * @param style The style to save for the player + * @param player The player to save the style to + */ public void setStyle(ParticleStyle style, Player player) { if(PlayerParticles.useMySQL) { Statement statement; @@ -140,6 +190,12 @@ public class ConfigManager { } } + /** + * Removes the particle effect from the player's name in either the database or config + * Should only be called from the effectData instance + * + * @param player The player to reset the style for + */ public void resetStyle(Player player) { if(PlayerParticles.useMySQL) { Statement statement; @@ -156,6 +212,12 @@ public class ConfigManager { } } + /** + * Gets the particle effect saved in either the database or config for the player + * + * @param player The player to get the particle style for + * @return The particle style for the player + */ public ParticleStyle getStyle(Player player) { if(PlayerParticles.useMySQL) { Statement statement; @@ -174,6 +236,12 @@ public class ConfigManager { return ParticleStyle.NONE; } + /** + * Checks if a world is disabled for particles to spawn in + * + * @param world The world name to check + * @return True if the world is disabled + */ @SuppressWarnings("unchecked") public boolean isWorldDisabled(String world) { if(PlayerParticles.getPlugin().getConfig().get("disabled-worlds") != null && ((ArrayList) PlayerParticles.getPlugin().getConfig().get("disabled-worlds")).contains(world)) { @@ -181,6 +249,11 @@ public class ConfigManager { }else return false; } + /** + * Gets all the worlds that are disabled + * + * @return All world names that are disabled + */ @SuppressWarnings("unchecked") public ArrayList getDisabledWorlds() { if(PlayerParticles.getPlugin().getConfig().get("disabled-worlds") != null) { @@ -188,51 +261,4 @@ public class ConfigManager { }else return null; } - public void updateConfig(JavaPlugin plugin) { - HashMap newConfig = getConfigVals(); - FileConfiguration c = plugin.getConfig(); - for (String var : c.getKeys(false)) { - newConfig.remove(var); - } - if (newConfig.size() != 0) { - for (String key : newConfig.keySet()) { - c.set(key, newConfig.get(key)); - } - try { - - c.set("version", getVersion()); - c.save(new File(plugin.getDataFolder(), "config.yml")); - } catch (IOException e) {} - } - } - - public HashMap getConfigVals() { - HashMap var = new HashMap<>(); - YamlConfiguration config = new YamlConfiguration(); - try { - config.loadFromString(stringFromInputStream(PlayerParticles.class.getResourceAsStream("/config.yml"))); - } catch (InvalidConfigurationException e) {} - for (String key : config.getKeys(false)) { - var.put(key, config.get(key)); - } - return var; - } - - public double getVersion() { - double version = -1; - try { - YamlConfiguration config = new YamlConfiguration(); - config.loadFromString(stringFromInputStream(PlayerParticles.class.getResourceAsStream("/config.yml"))); - version = config.getDouble("version"); - }catch(InvalidConfigurationException e) { } - return version; - } - - public String stringFromInputStream(InputStream in) { - Scanner scanner = new Scanner(in); - String string = scanner.useDelimiter("\\A").next(); - scanner.close(); - return string; - } - } diff --git a/src/com/esophose/playerparticles/MessageManager.java b/src/com/esophose/playerparticles/MessageManager.java index a214e58..610745d 100644 --- a/src/com/esophose/playerparticles/MessageManager.java +++ b/src/com/esophose/playerparticles/MessageManager.java @@ -13,10 +13,22 @@ import org.bukkit.entity.Player; public class MessageManager { + /** + * The instance of the MessageManager, we only need one of these + */ private static MessageManager instance = new MessageManager(); + /** + * Values contained in the config used for custom messages + */ private boolean messagesEnabled, prefix; + /** + * The prefix to place before all sent messages contained in the config + */ private String messagePrefix; + /** + * Sets up all the above variables with values from the plugin config + */ private MessageManager() { messagesEnabled = PlayerParticles.getPlugin().getConfig().getBoolean("messages-enabled"); prefix = PlayerParticles.getPlugin().getConfig().getBoolean("use-message-prefix"); @@ -24,10 +36,22 @@ public class MessageManager { messagePrefix = messagePrefix.replace("&", "§"); } + /** + * Gets the instance of the MessageManager + * + * @return The instance of the MessageManager + */ public static MessageManager getInstance() { return instance; } + /** + * Sends a message to a player + * + * @param player The player to send the message to + * @param message The message to send to the player + * @param color The chat color to put before the message + */ public void sendMessage(Player player, String message, ChatColor color) { if(!messagesEnabled) return; if(this.prefix){ diff --git a/src/com/esophose/playerparticles/ParticleCommandCompleter.java b/src/com/esophose/playerparticles/ParticleCommandCompleter.java index 6f28a77..3db3cc0 100644 --- a/src/com/esophose/playerparticles/ParticleCommandCompleter.java +++ b/src/com/esophose/playerparticles/ParticleCommandCompleter.java @@ -17,7 +17,15 @@ import org.bukkit.entity.Player; public class ParticleCommandCompleter implements TabCompleter { - @Override + /** + * Activated when a user pushes tab in chat prefixed with /pp + * + * @param sender The sender that hit tab, should always be a player + * @param cmd The command the player is executing + * @param alias The possible alias for the command + * @param args All arguments following the command + * @return A list of commands available to the sender + */ public List onTabComplete(CommandSender sender, Command cmd, String alias, String[] args) { if(cmd.getName().equalsIgnoreCase("pp")) { if(args.length == 1) { diff --git a/src/com/esophose/playerparticles/ParticleCreator.java b/src/com/esophose/playerparticles/ParticleCreator.java index 090c420..34086e2 100644 --- a/src/com/esophose/playerparticles/ParticleCreator.java +++ b/src/com/esophose/playerparticles/ParticleCreator.java @@ -28,16 +28,43 @@ import com.esophose.playerparticles.libraries.particles.ParticleEffect.ParticleT public class ParticleCreator extends BukkitRunnable implements Listener { + /** + * The map containing all the effects for players + */ private static HashMap map = new HashMap(); + /** + * The map containing all the styles for players + */ private static HashMap styleMap = new HashMap(); + + /** + * The timing system used for the styles HALO and SPIRAL + */ private double step = 0; + /** + * The timing system used for the styles QUAD_HELIX and ORB + */ private double helixStep = 0; private double helixYStep = 0; private boolean reverse = false; + /** + * Used to check for the database timing out + */ private double mysqltimer = 0; + /** + * First checks if the player is in the database (if it is enabled), if they are not then add them to the database + * Checks to see if that player has any effects or styles saved in either the database or config + * If so add the values to the map and/or styleMap + * + * Problematically clears the map and style map every time a player joins and refills the values + * Why does it do this? + * Figure out why or remove updateMap() and updateStyleMap() + * + * @param e The event + */ @EventHandler public void onPlayerJoin(PlayerJoinEvent e){ if(PlayerParticles.useMySQL) { @@ -50,7 +77,7 @@ public class ParticleCreator extends BukkitRunnable implements Listener { if(!res.next()) { statement = PlayerParticles.c.createStatement(); statement.executeUpdate("INSERT INTO playerparticles SET player_name = '" + e.getPlayer().getName() + "', particle = NULL, style = 'none';"); - System.out.println("New player added to PlayerParticles database: " + e.getPlayer().getName()); + PlayerParticles.getPlugin().getLogger().info("[PlayerParticles] New player added to database: " + e.getPlayer().getName()); } } catch (SQLException e2) { e2.printStackTrace(); @@ -67,6 +94,12 @@ public class ParticleCreator extends BukkitRunnable implements Listener { updateStyleMap(); } + /** + * Removes the player from the map and styleMap if they have any values in them + * Prevents spawning particles at a null location + * + * @param e The event + */ @EventHandler public void onPlayerQuit(PlayerQuitEvent e){ if(map.containsKey(e.getPlayer().getName())){ @@ -77,6 +110,11 @@ public class ParticleCreator extends BukkitRunnable implements Listener { } } + /** + * A somewhat costly solution to updating the MOVE style and displaying the appropriate particles + * + * @param e The event + */ @EventHandler public void onPlayerMove(PlayerMoveEvent e) { if(map.containsKey(e.getPlayer().getName()) && styleMap.get(e.getPlayer().getName()) == ParticleStyle.MOVE) { @@ -88,15 +126,30 @@ public class ParticleCreator extends BukkitRunnable implements Listener { } } + /** + * Adds the player with the given effect to the map + * + * @param player The player to add the effect to + * @param effect The effect + */ public static void addMap(Player player, ParticleType effect){ map.remove(player.getName()); map.put(player.getName(), effect); } + /** + * Removes the player from the map + * + * @param player The player to remove + */ public static void removeMap(Player player){ map.remove(player.getName()); } + /** + * Clears the map then adds everybody on the server if they have effects saved + * Used for when the server reloads and we can't rely on players rejoining + */ public static void updateMap(){ map.clear(); for(Player player : Bukkit.getOnlinePlayers()){ @@ -105,15 +158,30 @@ public class ParticleCreator extends BukkitRunnable implements Listener { } } + /** + * Adds the player with the given style to the styleMap + * + * @param player The player to add the style to + * @param style The style + */ public static void addStyleMap(Player player, ParticleStyle style) { styleMap.remove(player.getName()); styleMap.put(player.getName(), style); } + /** + * Removes the player from the styleMap + * + * @param player The player to remove + */ public static void removeStyleMap(Player player){ styleMap.remove(player.getName()); } + /** + * Clears the styleMap then adds everybody on the server if they have effects saved + * Used for when the server reloads and we can't rely on the players rejoining + */ public static void updateStyleMap(){ styleMap.clear(); for(Player player : Bukkit.getOnlinePlayers()){ @@ -121,14 +189,25 @@ public class ParticleCreator extends BukkitRunnable implements Listener { } } - public static ParticleType particleFromString(String particle){ + /** + * Gets a particle type from a string, used for getting ParticleType's from the saved data + * + * @param particle The name of the particle to check for + * @return The ParticleType with the given name, will be null if name was not found + */ + public static ParticleType particleFromString(String particle) { for(ParticleType effect : ParticleType.values()){ if(effect.toString().toLowerCase().replace("_", "").equals(particle)) return effect; } return null; } - @Override + /** + * The main loop to display all the particles + * Updates all the timing variables + * Refreshes the database connection if it is enabled and it has been 30 seconds since last refresh + * Displays the particles for all players on the server + */ public void run() { step++; if(step > 30) { @@ -152,7 +231,7 @@ public class ParticleCreator extends BukkitRunnable implements Listener { if(PlayerParticles.c != null && PlayerParticles.c.isClosed()) { PlayerParticles.c = PlayerParticles.mySQL.openConnection(); if(PlayerParticles.c.isClosed()) { - System.out.println("[PlayerParticles] Cannot connect to database! Is the database available and is your connection information correct?"); + PlayerParticles.getPlugin().getLogger().info("[PlayerParticles] Cannot connect to database! Is the database available and is your connection information correct?"); } } } catch (SQLException | ClassNotFoundException e) { @@ -172,6 +251,14 @@ public class ParticleCreator extends BukkitRunnable implements Listener { } } + /** + * Displays particles at the given player location with the effect and style given + * Checks all the effects and styles to make sure we display what is requested + * + * @param effect The effect to display + * @param style The style to display + * @param location The location to display at + */ private void displayParticle(ParticleType effect, ParticleStyle style, Location location){ if(style == null || style == ParticleStyle.NONE) { handleStyleNone(effect, location); @@ -246,6 +333,13 @@ public class ParticleCreator extends BukkitRunnable implements Listener { } } + /** + * Displays particles at the given location with the default spread out style, NONE + * Only check against every type to make sure they look nice, it isn't completely required + * + * @param effect The effect to display as + * @param location The locatio to display at + */ public void handleStyleNone(ParticleType effect, Location location) { if(effect == null || location == null) return; if(effect.equals(ParticleType.ANGRY_VILLAGER)){ diff --git a/src/com/esophose/playerparticles/ParticleStyle.java b/src/com/esophose/playerparticles/ParticleStyle.java index 5a53c1d..ee2362c 100644 --- a/src/com/esophose/playerparticles/ParticleStyle.java +++ b/src/com/esophose/playerparticles/ParticleStyle.java @@ -19,6 +19,12 @@ public enum ParticleStyle { QUADHELIX, ORB; + /** + * Gets the ParticleStyle with the name given, returns null if not found + * + * @param particle The string of the style to search for + * @return The ParticleStyle with the name requested + */ public static ParticleStyle styleFromString(String particle){ for(ParticleStyle style : ParticleStyle.values()){ if(style.toString().toLowerCase().replace("_", "").equals(particle)) return style; diff --git a/src/com/esophose/playerparticles/PermissionHandler.java b/src/com/esophose/playerparticles/PermissionHandler.java index 2fd8c54..5dfeb60 100644 --- a/src/com/esophose/playerparticles/PermissionHandler.java +++ b/src/com/esophose/playerparticles/PermissionHandler.java @@ -17,6 +17,13 @@ import com.esophose.playerparticles.libraries.particles.ParticleEffect.ParticleT public class PermissionHandler { + /** + * Checks if a player has permission to use an effect + * + * @param player The player to check the permission for + * @param effect The effect to check + * @return True if the player has permission to use the effect + */ public static boolean hasPermission(Player player, ParticleType effect) { if(player.hasPermission("playerparticles.*") || player.hasPermission("playerparticles.particles.*")) return true; if(effect.equals(ParticleType.RED_DUST) && player.hasPermission("playerparticles.reddust")) { @@ -33,12 +40,25 @@ public class PermissionHandler { return false; } + /** + * Checks if a player has permission to use a style + * + * @param player The player to check the permission for + * @param effect The style to check + * @return True if the player has permission to use the style + */ public static boolean hasStylePermission(Player player, ParticleStyle style) { if(player.hasPermission("playerparticles.*") || player.hasPermission("playerparticles.styles.*") || style == ParticleStyle.NONE) return true; if(player.hasPermission("playerparticles.style." + style.toString().toLowerCase().replace("_", ""))) return true; return false; } + /** + * Gets a List of all effect names a player has permission for + * + * @param p The player to get names for + * @return A List of all effect names the given player has permission for + */ public static List getParticlesUserHasPermissionFor(Player p) { List list = new ArrayList(); if(p.hasPermission("playerparticles.*") || p.hasPermission("playerparticles.particles.*")) { @@ -53,6 +73,12 @@ public class PermissionHandler { return list; } + /** + * Gets a List of all style names a player has permission for + * + * @param p The player to get names for + * @return A List of all style names the given player has permission for + */ public static List getStylesUserHasPermissionFor(Player p) { List list = new ArrayList(); if(p.hasPermission("playerparticles.*") || p.hasPermission("playerparticles.styles.*")) { diff --git a/src/com/esophose/playerparticles/PlayerParticles.java b/src/com/esophose/playerparticles/PlayerParticles.java index 9e2d349..c782083 100644 --- a/src/com/esophose/playerparticles/PlayerParticles.java +++ b/src/com/esophose/playerparticles/PlayerParticles.java @@ -31,13 +31,31 @@ import com.esophose.playerparticles.updater.Updater; public class PlayerParticles extends JavaPlugin { + /** + * The version a new update has, will be null if the config has it disabled or if there is no new version + */ public static String updateVersion = null; + /** + * The MySQL connection + */ public static MySQL mySQL = null; public static Connection c = null; + /** + * Whether or not to use MySQL as determined in the config + */ public static boolean useMySQL = false; + /** + * Saves the default config if it doesn't exist + * Registers the tab completer and the event listeners + * Checks if the config needs to be updated to the new version + * Makes sure the database is accessable + * Updates the map and styleMap @see ParticleCreator + * Starts the particle spawning task + * Checks for any updates if checking is enabled in the config + */ public void onEnable(){ saveDefaultConfig(); getCommand("pp").setTabCompleter(new ParticleCommandCompleter()); @@ -48,7 +66,7 @@ public class PlayerParticles extends JavaPlugin { configFile.delete(); saveDefaultConfig(); reloadConfig(); - getLogger().warning("config.yml has been updated!"); + getLogger().warning("[PlayerParticles] config.yml has been updated!"); } checkDatabase(); ParticleCreator.updateMap(); @@ -57,22 +75,41 @@ public class PlayerParticles extends JavaPlugin { // Check for an update if(shouldCheckUpdates()) { + getLogger().info("[PlayerParticles] Checking for an update..."); Updater updater = new Updater(this, 82823, this.getFile(), Updater.UpdateType.NO_DOWNLOAD, false); if(Double.parseDouble(updater.getLatestName().replaceAll("PlayerParticles v", "")) > Double.parseDouble(getPlugin().getDescription().getVersion())) { updateVersion = updater.getLatestName().replaceAll("PlayerParticles v", ""); getLogger().info("[PlayerParticles] An update (v" + updateVersion + ") is available! You are running v" + getPlugin().getDescription().getVersion()); + } else { + getLogger().info("[PlayerParticles] No update was found"); } } } + /** + * Gets the instance of the plugin running on the server + * + * @return The PlayerParticles plugin instance + */ public static Plugin getPlugin(){ return Bukkit.getPluginManager().getPlugin("PlayerParticles"); } + /** + * Checks the config if the plugin can look for updates + * + * @return True if check-updates is set to true in the config + */ public boolean shouldCheckUpdates() { return getConfig().getBoolean("check-updates"); } + /** + * Checks if database-enable is true in the config, if it is then continue + * Gets the database connection information from the config and tries to connect to the server + * Creates a new table if it doesn't exist called playerparticles + * Sets useMySQL to true if it connects successfully, and false if it fails or isn't enabled + */ private void checkDatabase() { if(getConfig().getBoolean("database-enable")) { String hostname = getConfig().getString("database-hostname"); @@ -88,7 +125,7 @@ public class PlayerParticles extends JavaPlugin { useMySQL = true; } catch (ClassNotFoundException | SQLException e) { e.printStackTrace(); - getLogger().info("Failed to connect to MySQL Database! Check to see if your config is correct!"); + getLogger().info("[PlayerParticles] Failed to connect to MySQL Database! Check to see if your config is correct!"); useMySQL = false; } }else{ @@ -97,6 +134,10 @@ public class PlayerParticles extends JavaPlugin { getLogger().info("[PlayerParticles] Using mySQL for data storage: " + useMySQL); } + /** + * Starts the task reponsible for spawning particles + * Starts two with 1 tick delay if ticks-per-particle is set to 0.5 + */ private void startTasks() { double ticks = getConfig().getDouble("ticks-per-particle"); if(ticks == 0.5){ @@ -106,8 +147,21 @@ public class PlayerParticles extends JavaPlugin { new ParticleCreator().runTaskTimer(this, 20, (long) ticks); } - @Override + /** + * Called when a player does a command and continues if the command is /pp + * Executes all the commands and methods + * Does some sorcery + * + * Needs to be rewritten as a separate CommandManager + * + * @param sender Who executed the command + * @param cmd The command + * @param label The command label + * @param args The arguments following the command + * @return True if everything went as planned (should always be true) + */ public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { + if(!(sender instanceof Player)) return true; Player p = (Player) sender; if(args.length == 1 && args[0].equalsIgnoreCase("worlds")) { String worlds = ""; diff --git a/src/com/esophose/playerparticles/updater/PluginUpdateListener.java b/src/com/esophose/playerparticles/updater/PluginUpdateListener.java index 4be0002..4d8fcbd 100644 --- a/src/com/esophose/playerparticles/updater/PluginUpdateListener.java +++ b/src/com/esophose/playerparticles/updater/PluginUpdateListener.java @@ -18,6 +18,11 @@ import com.esophose.playerparticles.PlayerParticles; public class PluginUpdateListener implements Listener { + /** + * Called when a player joins and notifies ops if an update is available + * + * @param e The event + */ @EventHandler public void onPlayerJoin(PlayerJoinEvent e) { if(e.getPlayer().isOp()) {