diff --git a/README.md b/README.md index e6ecf63..b160abb 100644 --- a/README.md +++ b/README.md @@ -23,10 +23,11 @@ MP Utils is a simple library for helping on the developing of minecraft spigot p ## Installation Developers: Add the following maven dependecy to your pom.xml file: + ```xml - com.github.alfonsoLeandro + com.github.alfonsoleandrocom.github.alfonsoleandro MPUtils VERSION provided diff --git a/pom.xml b/pom.xml index 626a74e..0312432 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ com.github.alfonsoLeandro MPUtils - 1.4.4 + 1.5.0 jar MPUtils @@ -40,7 +40,7 @@ Leandro Alfonso leandroalfonsoporley@gmail.com - com.github.alfonsoLeandro + com.github.alfonsoleandro https://github.com/alfonsoLeandro/MPUtils diff --git a/src/main/java/com/github/alfonsoLeandro/mpUtils/events/JoinEvent.java b/src/main/java/com/github/alfonsoLeandro/mpUtils/events/JoinEvent.java deleted file mode 100644 index 8c1ed8c..0000000 --- a/src/main/java/com/github/alfonsoLeandro/mpUtils/events/JoinEvent.java +++ /dev/null @@ -1,30 +0,0 @@ -package com.github.alfonsoLeandro.mpUtils.events; - -import com.github.alfonsoLeandro.mpUtils.Main; -import org.bukkit.ChatColor; -import org.bukkit.entity.Player; -import org.bukkit.event.EventHandler; -import org.bukkit.event.Listener; -import org.bukkit.event.player.PlayerJoinEvent; - -public class JoinEvent implements Listener { - - final private Main plugin; - - public JoinEvent(Main plugin){ - this.plugin = plugin; - } - - @EventHandler - public void onJoin(PlayerJoinEvent event){ - final Player player = event.getPlayer(); - final String exclamation = "&e&l(&4&l!&e&l)"; - final String prefix = "&f[&aMPUtils&f]"; - - if(player.isOp() && !plugin.getVersion().equals(plugin.getLatestVersion())) { - player.sendMessage(ChatColor.translateAlternateColorCodes('&', prefix+" "+exclamation+" &4New version available &7(&e"+plugin.getLatestVersion()+"&7)")); - player.sendMessage(ChatColor.translateAlternateColorCodes('&', prefix+" "+exclamation+" &fhttp://bit.ly/2If90hb") ); - } - } - -} diff --git a/src/main/java/com/github/alfonsoLeandro/mpUtils/guis/GUIAttributes.java b/src/main/java/com/github/alfonsoLeandro/mpUtils/guis/GUIAttributes.java deleted file mode 100644 index fa0f799..0000000 --- a/src/main/java/com/github/alfonsoLeandro/mpUtils/guis/GUIAttributes.java +++ /dev/null @@ -1,34 +0,0 @@ -package com.github.alfonsoLeandro.mpUtils.guis; - -class GUIAttributes { - - - final private int page; - final private GUIType guiType; - final private String guiTags; - final private GUI gui; - - public GUIAttributes(int page, GUIType guiType, String guiTags, GUI gui){ - this.page = page; - this.guiType = guiType; - this.guiTags = guiTags; - this.gui = gui; - } - - public int getPage() { - return page; - } - - public GUIType getGuiType() { - return guiType; - } - - public String getGuiTags(){ - return guiTags; - } - - public GUI getGui(){ - return gui; - } - -} diff --git a/src/main/java/com/github/alfonsoLeandro/mpUtils/guis/GUIType.java b/src/main/java/com/github/alfonsoLeandro/mpUtils/guis/GUIType.java deleted file mode 100644 index 808d835..0000000 --- a/src/main/java/com/github/alfonsoLeandro/mpUtils/guis/GUIType.java +++ /dev/null @@ -1,6 +0,0 @@ -package com.github.alfonsoLeandro.mpUtils.guis; - -public enum GUIType { - PAGINATED, - SIMPLE -} diff --git a/src/main/java/com/github/alfonsoLeandro/mpUtils/time/Cooldown.java b/src/main/java/com/github/alfonsoLeandro/mpUtils/time/Cooldown.java deleted file mode 100644 index 6029f1b..0000000 --- a/src/main/java/com/github/alfonsoLeandro/mpUtils/time/Cooldown.java +++ /dev/null @@ -1,85 +0,0 @@ -package com.github.alfonsoLeandro.mpUtils.time; - -import java.util.HashMap; - -/** - * Class used for creating cooldowns - */ -public class Cooldown { - - /** - * HashMap used for storing each player name with their corresponding cooldown ending time. - */ - private final HashMap playersOnCooldown; - - /** - * Creates a cooldown object, you can store more than one player per cooldown. - * When creating a new cooldown the HashMap is also new so players in another cooldown do not - * have nothing to do with the new cooldown. - */ - public Cooldown(){ - this.playersOnCooldown = new HashMap<>(); - } - - /** - * Adds a player to the cooldown for x amount of seconds. - * - * @param playerName The player to add. - * @param seconds The time in seconds left for this player's cooldown to end. - */ - public void addToCooldown(String playerName, int seconds){ - playersOnCooldown.put(playerName, System.currentTimeMillis()+(seconds*1000)); - } - - /** - * Manually removes a player from the cooldown even if it was not finished yet. - * - * @param playerName The player to remove from the cooldown. - */ - public void removeFromCooldown(String playerName){ - playersOnCooldown.remove(playerName); - } - - /** - * Checks if a player is on cooldown. - * - * @param playerName The player to look for. - * @return true if the player is on cooldown and it has not finished. - */ - public boolean isOnCooldown(String playerName){ - if(playersOnCooldown.containsKey(playerName)){ - if(playersOnCooldown.get(playerName) > System.currentTimeMillis()){ - return true; - }else{ - playersOnCooldown.remove(playerName); - return false; - } - }else{ - return false; - } - } - - /** - * Gets the time left for a player to leave cooldown. - * - * @param playerName The player to look for. - * @return The time left for the player to leave the cooldown. - */ - public long getTimeLeft(String playerName){ - - if(playersOnCooldown.containsKey(playerName)) return 0; - - final long timeLeft = playersOnCooldown.get(playerName) - System.currentTimeMillis(); - - if(timeLeft <= 0){ - playersOnCooldown.remove(playerName); - return 0; - }else{ - return timeLeft/1000; - } - - - } - - -} diff --git a/src/main/java/com/github/alfonsoLeandro/mpUtils/Main.java b/src/main/java/com/github/alfonsoleandro/mputils/MPUtils.java similarity index 65% rename from src/main/java/com/github/alfonsoLeandro/mpUtils/Main.java rename to src/main/java/com/github/alfonsoleandro/mputils/MPUtils.java index 6aec364..90c9a4f 100644 --- a/src/main/java/com/github/alfonsoLeandro/mpUtils/Main.java +++ b/src/main/java/com/github/alfonsoleandro/mputils/MPUtils.java @@ -1,10 +1,32 @@ -package com.github.alfonsoLeandro.mpUtils; +/* +Copyright (c) 2020 Leandro Alfonso -import com.github.alfonsoLeandro.mpUtils.events.JoinEvent; -import com.github.alfonsoLeandro.mpUtils.files.YamlFile; -import com.github.alfonsoLeandro.mpUtils.guis.Events; -import com.github.alfonsoLeandro.mpUtils.metrics.Metrics; -import com.github.alfonsoLeandro.mpUtils.string.StringUtils; +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + */ +package com.github.alfonsoleandro.mputils; + +import com.github.alfonsoleandro.mputils.events.JoinEvent; +import com.github.alfonsoleandro.mputils.files.YamlFile; +import com.github.alfonsoleandro.mputils.guis.Events; +import com.github.alfonsoleandro.mputils.metrics.Metrics; +import com.github.alfonsoleandro.mputils.string.StringUtils; +import com.github.alfonsoleandro.mputils.time.Cooldown; import org.bukkit.Bukkit; import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.plugin.PluginDescriptionFile; @@ -16,7 +38,10 @@ import java.net.HttpURLConnection; import java.net.URL; -public final class Main extends JavaPlugin { +/** + * MPUtils' main class. + */ +public final class MPUtils extends JavaPlugin { /** * This plugin's plugin.yml file. @@ -34,6 +59,10 @@ public final class Main extends JavaPlugin { * YamlFile object used for storing the {@link FileConfiguration} object for the config file and the file itself. */ private YamlFile configYaml; + /** + * YamlFile object used for storing the {@link FileConfiguration} object for the cooldown file and the file itself. + */ + private YamlFile cooldownYaml; /** * Sends a message to the console with colors and prefix. @@ -43,7 +72,9 @@ private void send(String msg){ Bukkit.getConsoleSender().sendMessage(StringUtils.colorizeString('&', "&f[&aMPUtils&f] "+msg)); } - + /** + * Plugin enable logic. + */ @Override public void onEnable() { send("&aEnabled&f. Version: &e" + version); @@ -51,11 +82,15 @@ public void onEnable() { send("&fJoin my discord server at &chttps://discordapp.com/invite/ZznhQud"); send("Please consider subscribing to my yt channel: &c" + pdfFile.getWebsite()); registerConfig(); + registerCooldown(); registerEvents(); updateChecker(); startMetrics(); } + /** + * Plugin disable logic. + */ @Override public void onDisable() { send("&cDisabled&f. Version: &e" + version); @@ -71,6 +106,13 @@ private void registerConfig(){ configYaml = new YamlFile(this, "config.yml"); } + /** + * Registers the cooldown file for this plugin. + */ + private void registerCooldown(){ + cooldownYaml = new YamlFile(this, "cooldown.yml"); + } + /** * Tries to start the metrics system. */ @@ -130,4 +172,12 @@ public String getVersion() { public String getLatestVersion(){ return latestVersion; } + + /** + * Gets the cooldown YAMLFile object. + * @return The YAMLFile object containing the cooldown's File and FileConfiguration objects. + */ + public YamlFile getCooldownYaml(){ + return cooldownYaml; + } } diff --git a/src/main/java/com/github/alfonsoleandro/mputils/events/JoinEvent.java b/src/main/java/com/github/alfonsoleandro/mputils/events/JoinEvent.java new file mode 100644 index 0000000..2ccb0ad --- /dev/null +++ b/src/main/java/com/github/alfonsoleandro/mputils/events/JoinEvent.java @@ -0,0 +1,55 @@ +/* +Copyright (c) 2020 Leandro Alfonso + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + */ +package com.github.alfonsoleandro.mputils.events; + +import com.github.alfonsoleandro.mputils.MPUtils; +import org.bukkit.ChatColor; +import org.bukkit.entity.Player; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; +import org.bukkit.event.player.PlayerJoinEvent; + +/** + * {@link PlayerJoinEvent} listener used for notifying server operators whenever there + * is a new MPUtils version available. + */ +public class JoinEvent implements Listener { + + final private MPUtils plugin; + + public JoinEvent(MPUtils plugin){ + this.plugin = plugin; + } + + @EventHandler + public void onJoin(PlayerJoinEvent event){ + final Player player = event.getPlayer(); + final String exclamation = "&e&l(&4&l!&e&l)"; + final String prefix = "&f[&aMPUtils&f]"; + + if(player.isOp() && !plugin.getVersion().equals(plugin.getLatestVersion())) { + player.sendMessage(ChatColor.translateAlternateColorCodes('&', prefix+" "+exclamation+" &4New version available &7(&e"+plugin.getLatestVersion()+"&7)")); + player.sendMessage(ChatColor.translateAlternateColorCodes('&', prefix+" "+exclamation+" &fhttp://bit.ly/2If90hb") ); + } + } + +} diff --git a/src/main/java/com/github/alfonsoLeandro/mpUtils/files/YamlFile.java b/src/main/java/com/github/alfonsoleandro/mputils/files/YamlFile.java similarity index 98% rename from src/main/java/com/github/alfonsoLeandro/mpUtils/files/YamlFile.java rename to src/main/java/com/github/alfonsoleandro/mputils/files/YamlFile.java index 095d79a..45ed66d 100644 --- a/src/main/java/com/github/alfonsoLeandro/mpUtils/files/YamlFile.java +++ b/src/main/java/com/github/alfonsoleandro/mputils/files/YamlFile.java @@ -1,4 +1,4 @@ -package com.github.alfonsoLeandro.mpUtils.files; +package com.github.alfonsoleandro.mputils.files; import com.google.common.base.Strings; import org.bukkit.configuration.InvalidConfigurationException; diff --git a/src/main/java/com/github/alfonsoLeandro/mpUtils/guis/Events.java b/src/main/java/com/github/alfonsoleandro/mputils/guis/Events.java similarity index 50% rename from src/main/java/com/github/alfonsoLeandro/mpUtils/guis/Events.java rename to src/main/java/com/github/alfonsoleandro/mputils/guis/Events.java index 3d77dd6..5b4fee0 100644 --- a/src/main/java/com/github/alfonsoLeandro/mpUtils/guis/Events.java +++ b/src/main/java/com/github/alfonsoleandro/mputils/guis/Events.java @@ -1,4 +1,25 @@ -package com.github.alfonsoLeandro.mpUtils.guis; +/* +Copyright (c) 2020 Leandro Alfonso + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + */ +package com.github.alfonsoleandro.mputils.guis; import org.bukkit.Bukkit; import org.bukkit.entity.Player; @@ -17,7 +38,7 @@ public class Events implements Listener { public void onClick(InventoryClickEvent event){ if(event.getWhoClicked() instanceof Player && PlayersOnGUIsManager.isInGUI(event.getWhoClicked().getName())){ GUIAttributes attributes = PlayersOnGUIsManager.getAttributesByPlayer(event.getWhoClicked().getName()); - Bukkit.getPluginManager().callEvent(new GUIClickEvent((Player)event.getWhoClicked(), attributes.getGuiType(), attributes.getPage(), event, attributes.getGuiTags(), attributes.getGui())); + Bukkit.getPluginManager().callEvent(new GUIClickEvent((Player)event.getWhoClicked(), attributes.getGuiType(), attributes.getPage(), event, attributes.getGui().getGuiTags(), attributes.getGui())); } } @@ -25,7 +46,7 @@ public void onClick(InventoryClickEvent event){ public void onClose(InventoryCloseEvent event){ if(PlayersOnGUIsManager.isInGUI(event.getPlayer().getName())) { GUIAttributes attributes = PlayersOnGUIsManager.getAttributesByPlayer(event.getPlayer().getName()); - Bukkit.getPluginManager().callEvent(new GUICloseEvent((Player) event.getPlayer(), attributes.getGuiType(), attributes.getPage(), event, attributes.getGuiTags(), attributes.getGui())); + Bukkit.getPluginManager().callEvent(new GUICloseEvent((Player) event.getPlayer(), attributes.getGuiType(), attributes.getPage(), event, attributes.getGui().getGuiTags(), attributes.getGui())); PlayersOnGUIsManager.removePlayer(event.getPlayer().getName()); } } diff --git a/src/main/java/com/github/alfonsoLeandro/mpUtils/guis/GUI.java b/src/main/java/com/github/alfonsoleandro/mputils/guis/GUI.java similarity index 61% rename from src/main/java/com/github/alfonsoLeandro/mpUtils/guis/GUI.java rename to src/main/java/com/github/alfonsoleandro/mputils/guis/GUI.java index 8726b45..7e05ed0 100644 --- a/src/main/java/com/github/alfonsoLeandro/mpUtils/guis/GUI.java +++ b/src/main/java/com/github/alfonsoleandro/mputils/guis/GUI.java @@ -1,10 +1,34 @@ -package com.github.alfonsoLeandro.mpUtils.guis; +/* +Copyright (c) 2020 Leandro Alfonso + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + */ +package com.github.alfonsoleandro.mputils.guis; import org.bukkit.Bukkit; import org.bukkit.entity.Player; import org.bukkit.inventory.Inventory; import org.bukkit.inventory.ItemStack; +/** + * Abstract class inherited by {@link PaginatedGUI} and {@link SimpleGUI}. + */ public abstract class GUI { /** diff --git a/src/main/java/com/github/alfonsoleandro/mputils/guis/GUIAttributes.java b/src/main/java/com/github/alfonsoleandro/mputils/guis/GUIAttributes.java new file mode 100644 index 0000000..b2292de --- /dev/null +++ b/src/main/java/com/github/alfonsoleandro/mputils/guis/GUIAttributes.java @@ -0,0 +1,72 @@ +/* +Copyright (c) 2020 Leandro Alfonso + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + */ +package com.github.alfonsoleandro.mputils.guis; + +/** + * Class containing various GUI's details for using on MPUtils' custom events. + */ +class GUIAttributes { + + /** + * The page number the player is currently on. + */ + final private int page; + /** + * The type of GUI the player has open. + */ + final private GUIType guiType; + /** + * The actual GUI object. + */ + final private GUI gui; + + public GUIAttributes(int page, GUIType guiType, GUI gui){ + this.page = page; + this.guiType = guiType; + this.gui = gui; + } + + /** + * Gets the page number the player was last seen on. + * @return The GUI page number or -1 if the {@link GUIType} is equal to {@link SimpleGUI}. + */ + public int getPage() { + return page; + } + + /** + * Gets the GUI type. + * @return Either {@link SimpleGUI} or {@link PaginatedGUI}. + */ + public GUIType getGuiType() { + return guiType; + } + + /** + * Gets the GUI object. + * @return The GUI object. + */ + public GUI getGui(){ + return gui; + } + +} diff --git a/src/main/java/com/github/alfonsoLeandro/mpUtils/guis/GUIClickEvent.java b/src/main/java/com/github/alfonsoleandro/mputils/guis/GUIClickEvent.java similarity index 74% rename from src/main/java/com/github/alfonsoLeandro/mpUtils/guis/GUIClickEvent.java rename to src/main/java/com/github/alfonsoleandro/mputils/guis/GUIClickEvent.java index cd92df5..5a25c9a 100644 --- a/src/main/java/com/github/alfonsoLeandro/mpUtils/guis/GUIClickEvent.java +++ b/src/main/java/com/github/alfonsoleandro/mputils/guis/GUIClickEvent.java @@ -1,4 +1,25 @@ -package com.github.alfonsoLeandro.mpUtils.guis; +/* +Copyright (c) 2020 Leandro Alfonso + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + */ +package com.github.alfonsoleandro.mputils.guis; import org.bukkit.entity.Player; import org.bukkit.event.Event; diff --git a/src/main/java/com/github/alfonsoLeandro/mpUtils/guis/GUICloseEvent.java b/src/main/java/com/github/alfonsoleandro/mputils/guis/GUICloseEvent.java similarity index 70% rename from src/main/java/com/github/alfonsoLeandro/mpUtils/guis/GUICloseEvent.java rename to src/main/java/com/github/alfonsoleandro/mputils/guis/GUICloseEvent.java index 7830f3a..99fa48a 100644 --- a/src/main/java/com/github/alfonsoLeandro/mpUtils/guis/GUICloseEvent.java +++ b/src/main/java/com/github/alfonsoleandro/mputils/guis/GUICloseEvent.java @@ -1,4 +1,25 @@ -package com.github.alfonsoLeandro.mpUtils.guis; +/* +Copyright (c) 2020 Leandro Alfonso + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + */ +package com.github.alfonsoleandro.mputils.guis; import org.bukkit.entity.Player; @@ -6,6 +27,9 @@ import org.bukkit.event.HandlerList; import org.bukkit.event.inventory.InventoryCloseEvent; +/** + * Custom event for when a GUI closes, called when a player who is being GUI managed by MPUtils closes a GUI. + */ public class GUICloseEvent extends Event { private static final HandlerList HANDLERS = new HandlerList(); diff --git a/src/main/java/com/github/alfonsoleandro/mputils/guis/GUIType.java b/src/main/java/com/github/alfonsoleandro/mputils/guis/GUIType.java new file mode 100644 index 0000000..1c01d87 --- /dev/null +++ b/src/main/java/com/github/alfonsoleandro/mputils/guis/GUIType.java @@ -0,0 +1,36 @@ +/* +Copyright (c) 2020 Leandro Alfonso + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + */ +package com.github.alfonsoleandro.mputils.guis; + +/** + * Enum for representing the available types of GUI objects. + */ +public enum GUIType { + /** + * A GUI that has more than one page. + */ + PAGINATED, + /** + * A GUI made up of a single page. + */ + SIMPLE +} diff --git a/src/main/java/com/github/alfonsoLeandro/mpUtils/guis/PaginatedGUI.java b/src/main/java/com/github/alfonsoleandro/mputils/guis/PaginatedGUI.java similarity index 90% rename from src/main/java/com/github/alfonsoLeandro/mpUtils/guis/PaginatedGUI.java rename to src/main/java/com/github/alfonsoleandro/mputils/guis/PaginatedGUI.java index 839b679..cc3cc52 100644 --- a/src/main/java/com/github/alfonsoLeandro/mpUtils/guis/PaginatedGUI.java +++ b/src/main/java/com/github/alfonsoleandro/mputils/guis/PaginatedGUI.java @@ -1,7 +1,28 @@ -package com.github.alfonsoLeandro.mpUtils.guis; +/* +Copyright (c) 2020 Leandro Alfonso + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + */ +package com.github.alfonsoleandro.mputils.guis; -import com.github.alfonsoLeandro.mpUtils.itemStacks.MPItemStacks; -import com.github.alfonsoLeandro.mpUtils.string.StringUtils; +import com.github.alfonsoleandro.mputils.itemstacks.MPItemStacks; +import com.github.alfonsoleandro.mputils.string.StringUtils; import org.bukkit.Material; import org.bukkit.entity.Player; import org.bukkit.inventory.ItemStack; @@ -301,7 +322,7 @@ public void openGUI(Player player, int page) { setNavBar(page); setItemsForPage(page); player.openInventory(inventory); - PlayersOnGUIsManager.addPlayer(player.getName(), page, GUIType.PAGINATED, guiTags, this); + PlayersOnGUIsManager.addPlayer(player.getName(), page, GUIType.PAGINATED, getGuiTags(), this); } /** diff --git a/src/main/java/com/github/alfonsoLeandro/mpUtils/guis/PlayersOnGUIsManager.java b/src/main/java/com/github/alfonsoleandro/mputils/guis/PlayersOnGUIsManager.java similarity index 63% rename from src/main/java/com/github/alfonsoLeandro/mpUtils/guis/PlayersOnGUIsManager.java rename to src/main/java/com/github/alfonsoleandro/mputils/guis/PlayersOnGUIsManager.java index a856dd8..8bd34d9 100644 --- a/src/main/java/com/github/alfonsoLeandro/mpUtils/guis/PlayersOnGUIsManager.java +++ b/src/main/java/com/github/alfonsoleandro/mputils/guis/PlayersOnGUIsManager.java @@ -1,4 +1,25 @@ -package com.github.alfonsoLeandro.mpUtils.guis; +/* +Copyright (c) 2020 Leandro Alfonso + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + */ +package com.github.alfonsoleandro.mputils.guis; import java.util.HashMap; @@ -32,7 +53,7 @@ public static GUIAttributes getAttributesByPlayer(String playerName){ */ public static void addPlayer(String playerName, int pageNumber, GUIType guiType, String guiTags, GUI gui){ players.remove(playerName); - players.put(playerName, new GUIAttributes(pageNumber, guiType, guiTags, gui)); + players.put(playerName, new GUIAttributes(pageNumber, guiType, gui)); } /** diff --git a/src/main/java/com/github/alfonsoLeandro/mpUtils/guis/SimpleGUI.java b/src/main/java/com/github/alfonsoleandro/mputils/guis/SimpleGUI.java similarity index 70% rename from src/main/java/com/github/alfonsoLeandro/mpUtils/guis/SimpleGUI.java rename to src/main/java/com/github/alfonsoleandro/mputils/guis/SimpleGUI.java index b3b242f..861c6e7 100644 --- a/src/main/java/com/github/alfonsoLeandro/mpUtils/guis/SimpleGUI.java +++ b/src/main/java/com/github/alfonsoleandro/mputils/guis/SimpleGUI.java @@ -1,4 +1,25 @@ -package com.github.alfonsoLeandro.mpUtils.guis; +/* +Copyright (c) 2020 Leandro Alfonso + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + */ +package com.github.alfonsoleandro.mputils.guis; import org.bukkit.Bukkit; import org.bukkit.event.inventory.InventoryType; diff --git a/src/main/java/com/github/alfonsoLeandro/mpUtils/itemStacks/MPItemStacks.java b/src/main/java/com/github/alfonsoleandro/mputils/itemstacks/MPItemStacks.java similarity index 72% rename from src/main/java/com/github/alfonsoLeandro/mpUtils/itemStacks/MPItemStacks.java rename to src/main/java/com/github/alfonsoleandro/mputils/itemstacks/MPItemStacks.java index b2231f4..41ba179 100644 --- a/src/main/java/com/github/alfonsoLeandro/mpUtils/itemStacks/MPItemStacks.java +++ b/src/main/java/com/github/alfonsoleandro/mputils/itemstacks/MPItemStacks.java @@ -1,6 +1,27 @@ -package com.github.alfonsoLeandro.mpUtils.itemStacks; +/* +Copyright (c) 2020 Leandro Alfonso + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + */ +package com.github.alfonsoleandro.mputils.itemstacks; -import com.github.alfonsoLeandro.mpUtils.string.StringUtils; +import com.github.alfonsoleandro.mputils.string.StringUtils; import org.bukkit.Material; import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.meta.*; diff --git a/src/main/java/com/github/alfonsoLeandro/mpUtils/metrics/Metrics.java b/src/main/java/com/github/alfonsoleandro/mputils/metrics/Metrics.java similarity index 99% rename from src/main/java/com/github/alfonsoLeandro/mpUtils/metrics/Metrics.java rename to src/main/java/com/github/alfonsoleandro/mputils/metrics/Metrics.java index 794cfaf..ca78d72 100644 --- a/src/main/java/com/github/alfonsoLeandro/mpUtils/metrics/Metrics.java +++ b/src/main/java/com/github/alfonsoleandro/mputils/metrics/Metrics.java @@ -1,4 +1,4 @@ -package com.github.alfonsoLeandro.mpUtils.metrics; +package com.github.alfonsoleandro.mputils.metrics; import com.google.gson.JsonArray; import com.google.gson.JsonObject; diff --git a/src/main/java/com/github/alfonsoLeandro/mpUtils/string/StringUtils.java b/src/main/java/com/github/alfonsoleandro/mputils/string/StringUtils.java similarity index 68% rename from src/main/java/com/github/alfonsoLeandro/mpUtils/string/StringUtils.java rename to src/main/java/com/github/alfonsoleandro/mputils/string/StringUtils.java index 0c5f55f..cfddac3 100644 --- a/src/main/java/com/github/alfonsoLeandro/mpUtils/string/StringUtils.java +++ b/src/main/java/com/github/alfonsoleandro/mputils/string/StringUtils.java @@ -1,4 +1,25 @@ -package com.github.alfonsoLeandro.mpUtils.string; +/* +Copyright (c) 2020 Leandro Alfonso + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + */ +package com.github.alfonsoleandro.mputils.string; import org.bukkit.Bukkit; import org.bukkit.ChatColor; @@ -7,7 +28,7 @@ import java.util.List; /** - * Class loaded with useful string tools + * Utility class loaded with useful string tools */ public class StringUtils { diff --git a/src/main/java/com/github/alfonsoleandro/mputils/time/Cooldown.java b/src/main/java/com/github/alfonsoleandro/mputils/time/Cooldown.java new file mode 100644 index 0000000..440ee8f --- /dev/null +++ b/src/main/java/com/github/alfonsoleandro/mputils/time/Cooldown.java @@ -0,0 +1,116 @@ +/* +Copyright (c) 2020 Leandro Alfonso + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + */ +package com.github.alfonsoleandro.mputils.time; + +import com.github.alfonsoleandro.mputils.MPUtils; +import com.github.alfonsoleandro.mputils.files.YamlFile; +import org.bukkit.plugin.java.JavaPlugin; + + +/** + * Class used for creating cooldowns + */ +public class Cooldown { + + /** + * YAMLFile object containing cooldowns. + */ + private final YamlFile cooldownYaml; + /** + * Cooldown name for this object, used for creating more than one cooldown, for different purposes. + * i.e: send a message cooldown and open a chest cooldown. + */ + private final String cooldownName; + + + /** + * Creates a cooldown object. + * When creating a new cooldown the HashMap is also new so players in another cooldown do not + * have nothing to do with the new cooldown. + */ + public Cooldown(String cooldownName){ + this.cooldownName = cooldownName; + this.cooldownYaml = JavaPlugin.getPlugin(MPUtils.class).getCooldownYaml(); + } + + /** + * Adds a player to the cooldown for a given amount of time. + * + * @param playerName The player to add. + * @param amount The amount of time to add the player to the cooldown for. + * @param timeUnit The timeunit that the amount represents. See {@link TimeUnit}. + */ + public void addToCooldown(String playerName, int amount, TimeUnit timeUnit){ + cooldownYaml.getAccess().set("cooldowns."+cooldownName+"."+playerName, + System.currentTimeMillis() + + java.util.concurrent.TimeUnit.SECONDS.toMillis(TimeUtils.getTotalSeconds((long) amount *timeUnit.getMultiplier()))); + cooldownYaml.save(); + } + + /** + * Manually removes a player from the cooldown even if it was not finished yet. + * + * @param playerName The player to remove from the cooldown. + */ + public void removeFromCooldown(String playerName){ + cooldownYaml.getAccess().set("cooldowns."+cooldownName+"."+playerName, null); + cooldownYaml.save(); + } + + /** + * Checks if a player is on cooldown. + * This method will be removed from MPUtils. Please use "{@link #getTimeLeft(String)} > 0" instead for + * checking if a player is on cooldown. + * + * @param playerName The player to look for. + * @return true if the player is on cooldown and it has not finished. + */ + @Deprecated + public boolean isInCooldown(String playerName){ + return getTimeLeft(playerName) > 0; + } + + /** + * Gets the time left for a player to leave cooldown (in ticks). + * As a suggestion, this can be later used on {@link TimeUtils#getTimeString(long)}. + * + * @param playerName The player to look for. + * @return The time left for the player to leave the cooldown or 0 if the player was not in cooldown. + */ + public long getTimeLeft(String playerName){ + if(!cooldownYaml.getAccess().contains(playerName)) return 0; + + final long timeLeft = cooldownYaml.getAccess().getLong("cooldowns."+cooldownName+"."+playerName) - System.currentTimeMillis(); + + if(timeLeft <= 0){ + removeFromCooldown(playerName); + return 0; + }else{ + return timeLeft*20000; + } + + + } + + + +} diff --git a/src/main/java/com/github/alfonsoleandro/mputils/time/TimeUnit.java b/src/main/java/com/github/alfonsoleandro/mputils/time/TimeUnit.java new file mode 100644 index 0000000..ba0640d --- /dev/null +++ b/src/main/java/com/github/alfonsoleandro/mputils/time/TimeUnit.java @@ -0,0 +1,99 @@ +/* +Copyright (c) 2020 Leandro Alfonso + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + */ +package com.github.alfonsoleandro.mputils.time; + +public enum TimeUnit { + + /** + * Seconds, conformed by 20 ticks. + */ + SECONDS(20), + /** + * Minutes, conformed by 60 seconds, 1,200 ticks. + */ + MINUTES(SECONDS.multiplier*60), + /** + * Hours, conformed by 60 minutes, 1,200 seconds, 72,000 ticks. + */ + HOURS(MINUTES.multiplier*60), + /** + * Days, conformed by 24 hours, 3,600 minutes, 216,000 seconds, 1,728,000 ticks. + */ + DAYS(HOURS.multiplier*24), + /** + * Weeks, conformed by 7 days 168 hours, 25,200 minutes, 1,512,000 seconds, 12,096,000 ticks. + */ + WEEKS(DAYS.multiplier*7); + + + + /** + * The value that a value needs to be multiplied to translate to ticks. + */ + private final int multiplier; + + + /** + * Represents a conventional time unit. + * + * @param multiplier The value that a value needs to be multiplied to transform to ticks. + */ + TimeUnit(int multiplier) { + this.multiplier = multiplier; + } + + + /** + * Gets the value that a value needs to be multiplied to transform to ticks. + * + * @return The value that a value needs to be multiplied to transform to ticks. + */ + public int getMultiplier() { + return this.multiplier; + } + + + /** + * Gets a timeUnit by its alias. + * @param alias The alias the timeunit is known by. + * @return The timeUnit + */ + public static TimeUnit getByAlias(char alias){ + switch (alias){ + case 'm': + case 'M': + return MINUTES; + case 'h': + case 'H': + return HOURS; + case 'd': + case 'D': + return DAYS; + case 'w': + case 'W': + return WEEKS; + default: + return SECONDS; + } + } +} + diff --git a/src/main/java/com/github/alfonsoleandro/mputils/time/TimeUtils.java b/src/main/java/com/github/alfonsoleandro/mputils/time/TimeUtils.java new file mode 100644 index 0000000..1723c0b --- /dev/null +++ b/src/main/java/com/github/alfonsoleandro/mputils/time/TimeUtils.java @@ -0,0 +1,173 @@ +/* +Copyright (c) 2020 Leandro Alfonso + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + */ +package com.github.alfonsoleandro.mputils.time; + +import java.util.ArrayList; +import java.util.List; + +/** + * Class containing various useful time related methods. + * + * @author lelesape + */ +public class TimeUtils { + + + /** + * Gets the amount of ticks a given amount of time of the given unit represents. + * @param amount The amount of time of the given time unit. + * @param timeUnit The timeunit for the given amount. + * @return The value in ticks of the given time amount. + */ + public static int getTicks(int amount, TimeUnit timeUnit){ + return amount * timeUnit.getMultiplier(); + } + + /** + * Gets the amount of ticks a given amount of time of the given unit represents. + * @param amount The amount of time of the given time unit. + * @param timeUnit The char representing the timeunit for the given amount. + * @return The value in ticks of the given time amount. + */ + public static int getTicks(int amount, char timeUnit){ + return getTicks(amount, TimeUnit.getByAlias(timeUnit)); + } + + /** + * Translates and amount of ticks into weeks, days, hours, minutes and seconds. + * From this string you will need to replace %weeks%, %week%, %days%, %day%, %hours%, %hour%, %minutes%, + * %minute%, %seconds%, %second% and %and% placeholders. + * @param ticks The amount of ticks to translate + * @return A string with an w,d,h,m and s format. + */ + public static String getTimeString(long ticks){ + List args = new ArrayList<>(); + StringBuilder sb = new StringBuilder(); + + final long weeks = getWeeks(ticks); + final long days = getDays(ticks); + final long hours = getHours(ticks); + final long minutes = getMinutes(ticks); + final long seconds = getSeconds(ticks); + + + if(weeks > 0){ + String s = weeks + " " + + (weeks > 1 ? "%weeks%" : "%week%"); + + args.add(s); + } + + if(days > 0){ + String s = days + " " + + (days > 1 ? "%days%" : "%day%"); + + args.add(s); + } + + if(hours > 0){ + String s = hours + " " + + (hours > 1 ? "%hours%" : "%hour%"); + + args.add(s); + } + + if(minutes > 0){ + String s = minutes + " " + + (minutes > 1 ? "%minutes%" : "%minute%"); + args.add(s); + } + + String s = (seconds >= 0 ? seconds : 0) + " " + + (seconds == 0 || seconds > 1 ? "%seconds%" : "%second%"); + args.add(s); + + + for (int i = 0; i < args.size(); i++) { + + if(args.size() > 1 && i != 0) { + sb.append(i == args.size()-1 ? " "+"%and%"+" " : ", "); + } + + sb.append(args.get(i)); + } + + return sb.toString(); + } + + /** + * Gets the total amount of seconds a given amount of ticks represents. + * @param ticks The ticks to translate to seconds. + * @return The amount of seconds the given amount of ticks represent. + */ + public static long getTotalSeconds(long ticks){ + return ticks/20; + } + + /** + * Gets only the amount of seconds (between 0 and 59) an amount of ticks represent. + * @param ticks The amount of ticks. + * @return A number between 0 and 59 representing the seconds for the given amount of ticks. + */ + public static long getSeconds(long ticks){ + return java.util.concurrent.TimeUnit.SECONDS.toSeconds(getTotalSeconds(ticks)) - java.util.concurrent.TimeUnit.MINUTES.toSeconds(getMinutes(ticks)) - java.util.concurrent.TimeUnit.HOURS.toSeconds(getHours(ticks)) - java.util.concurrent.TimeUnit.DAYS.toSeconds(getDays(ticks)); + } + + /** + * Gets only the amount of minutes (between 0 and 59) an amount of ticks represent. + * @param ticks The amount of ticks. + * @return A number between 0 and 59 representing the minutes for the given amount of ticks. + */ + public static long getMinutes(long ticks){ + return java.util.concurrent.TimeUnit.SECONDS.toMinutes(getTotalSeconds(ticks)) - java.util.concurrent.TimeUnit.HOURS.toMinutes(getHours(ticks)) - java.util.concurrent.TimeUnit.DAYS.toMinutes(getDays(ticks)); + } + + /** + * Gets only the amount of hours (between 0 and 23) an amount of ticks represent. + * @param ticks The amount of ticks. + * @return A number between 0 and 23 representing the hours for the given amount of ticks. + */ + public static long getHours(long ticks){ + return java.util.concurrent.TimeUnit.SECONDS.toHours(getTotalSeconds(ticks)) - java.util.concurrent.TimeUnit.DAYS.toHours(getDays(ticks)); + } + + /** + * Gets only the amount of days (between 0 and 6) an amount of ticks represent. + * @param ticks The amount of ticks. + * @return A number between 0 and 6 representing the days for the given amount of ticks. + */ + public static long getDays(long ticks){ + return java.util.concurrent.TimeUnit.SECONDS.toDays(getTotalSeconds(ticks)) - getWeeks(ticks)*7; + } + + /** + * Gets only the amount of weeks an amount of ticks represent. + * @param ticks The amount of ticks. + * @return A number representing the weeks for the given amount of ticks. + */ + public static long getWeeks(long ticks){ + return java.util.concurrent.TimeUnit.SECONDS.toDays(getTotalSeconds(ticks))/7; + } + + + +} diff --git a/src/main/resources/cooldown.yml b/src/main/resources/cooldown.yml new file mode 100644 index 0000000..f69ddb4 --- /dev/null +++ b/src/main/resources/cooldown.yml @@ -0,0 +1,3 @@ +# File used for storing different cooldowns and players inside those cooldowns. +# Stores cooldowns in a "player: milliseconds" format. +cooldowns: [] \ No newline at end of file diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index 3026c5a..c339be4 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -1,6 +1,6 @@ name: MPUtils version: ${project.version} -main: com.github.alfonsoLeandro.mpUtils.Main +main: com.github.alfonsoleandro.mputils.MPUtils api-version: "1.13" authors: [lelesape] description: Library for minecraft plugins development