From 666e21ff2a6b7c7137dbc8e1b646a4afc80df4d9 Mon Sep 17 00:00:00 2001 From: Stealth2800 Date: Sun, 5 Apr 2015 11:03:34 -0700 Subject: [PATCH 01/16] Changed version to 4.1.0-SNAPSHOT --- pom.xml | 2 +- .../flexcore/command/FlexCommand.java | 7 +- .../flexcore/command/FlexCommandSettings.java | 41 ++++++- .../flexcore/command/FlexCommandWrapper.java | 6 +- .../flexcore/player/PlayerData.java | 110 ++++++++++++++++++ .../flexcore/player/PlayerManager.java | 83 ++++++++++++- 6 files changed, 238 insertions(+), 11 deletions(-) create mode 100644 src/main/java/me/st28/flexseries/flexcore/player/PlayerData.java diff --git a/pom.xml b/pom.xml index 3ef082d..35c541b 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ me.st28.flexseries flexcore - 4.0.0 + 4.1.0-SNAPSHOT FlexCore Powerful library for Bukkit plugins http://stealthyone.com/ diff --git a/src/main/java/me/st28/flexseries/flexcore/command/FlexCommand.java b/src/main/java/me/st28/flexseries/flexcore/command/FlexCommand.java index 2678ca0..ab6d472 100644 --- a/src/main/java/me/st28/flexseries/flexcore/command/FlexCommand.java +++ b/src/main/java/me/st28/flexseries/flexcore/command/FlexCommand.java @@ -51,11 +51,6 @@ public abstract class FlexCommand { */ private final List labels = new ArrayList<>(); - /** - * Label aliases that execute subcommands directly. - */ - private final Map> subcommandLabels = new HashMap<>(); - /** * The command that this command exists under. */ @@ -139,7 +134,7 @@ public final T getPlugin() { } /** - * @return a list containing the label(s) for this command. Element 0 is the main label. + * @return an unmodifiable list containing the label(s) for this command. Element 0 is the main label. */ public final List getLabels() { return Collections.unmodifiableList(labels); diff --git a/src/main/java/me/st28/flexseries/flexcore/command/FlexCommandSettings.java b/src/main/java/me/st28/flexseries/flexcore/command/FlexCommandSettings.java index ad72a48..9626fde 100644 --- a/src/main/java/me/st28/flexseries/flexcore/command/FlexCommandSettings.java +++ b/src/main/java/me/st28/flexseries/flexcore/command/FlexCommandSettings.java @@ -28,6 +28,7 @@ import me.st28.flexseries.flexcore.plugin.FlexPlugin; import org.apache.commons.lang.Validate; +import java.util.Collections; import java.util.HashMap; import java.util.Map; @@ -54,7 +55,7 @@ public final class FlexCommandSettings { /** * Label aliases that execute subcommands directly. */ - private final Map> subcommandAliases = new HashMap<>(); + private final Map subcommandAliases = new HashMap<>(); /** * If true, will use the default subcommand, regardless of argument count. @@ -152,6 +153,44 @@ public final String getDefaultSubcommand() { return defaultSubcommand; } + /** + * @return the aliases for labels that should execute subcommands directly. + */ + public final Map getSubcommandAliases() { + return Collections.unmodifiableMap(subcommandAliases); + } + + /** + * Sets a subcommand alias. + * + * @param alias The main label alias. + * @param subcommand The subcommand that should be executed directly for the given alias. + * @return The settings instance, for chaining. + */ + public final FlexCommandSettings subcommandAlias(String alias, FlexCommand subcommand) { + Validate.notNull(alias, "Alias cannot be null."); + Validate.notNull(subcommand, "Subcommand cannot be null."); + checkState(); + + return subcommandAlias(alias, subcommand.getLabels().get(0)); + } + + /** + * Sets a subcommand alias. + * + * @param alias The main label alias. + * @param subcommand The subcommand that should be executed directly for the given alias. + * @return The settings instance, for chaining. + */ + public final FlexCommandSettings subcommandAlias(String alias, String subcommand) { + Validate.notNull(alias, "Alias cannot be null."); + Validate.notNull(subcommand, "Subcommand cannot be null."); + checkState(); + + subcommandAliases.put(alias, subcommand); + return this; + } + /** * Makes this command a dummy command, meaning that it will use the default subcommand, regardless of argument count. * diff --git a/src/main/java/me/st28/flexseries/flexcore/command/FlexCommandWrapper.java b/src/main/java/me/st28/flexseries/flexcore/command/FlexCommandWrapper.java index 91debf4..3d19c00 100644 --- a/src/main/java/me/st28/flexseries/flexcore/command/FlexCommandWrapper.java +++ b/src/main/java/me/st28/flexseries/flexcore/command/FlexCommandWrapper.java @@ -105,7 +105,11 @@ public final boolean onCommand(CommandSender sender, Command command, String lab String cookieUserId = null; args = CommandUtils.fixArguments(args); - FlexCommand currentCommand = this.command.getSubcommands().get(label.toLowerCase());; + FlexCommand currentCommand = this.command.getSubcommands().get(((FlexCommandSettings) this.command.getSettings()).getSubcommandAliases().get(label.toLowerCase())); + + if (currentCommand == null) { + currentCommand = this.command.getSubcommands().get(label.toLowerCase()); + } // Set the cookie for this command label if (cookieManager != null) { diff --git a/src/main/java/me/st28/flexseries/flexcore/player/PlayerData.java b/src/main/java/me/st28/flexseries/flexcore/player/PlayerData.java new file mode 100644 index 0000000..b8a42e0 --- /dev/null +++ b/src/main/java/me/st28/flexseries/flexcore/player/PlayerData.java @@ -0,0 +1,110 @@ +/** + * FlexCore - Licensed under the MIT License (MIT) + * + * Copyright (c) Stealth2800 + * Copyright (c) contributors + * + * 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 me.st28.flexseries.flexcore.player; + +import org.bukkit.configuration.ConfigurationSection; +import org.bukkit.configuration.file.FileConfiguration; + +import java.sql.Timestamp; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.UUID; + +public final class PlayerData { + + private final UUID uuid; + + Long firstJoin; + Long lastLogin; + Long lastLogout; + + String lastIp; + final List ips = new ArrayList<>(); + + String lastName; + final List names = new ArrayList<>(); + + PlayerData(UUID uuid, FileConfiguration config) { + this.uuid = uuid; + + if (config.isSet("firstJoin")) { + firstJoin = config.getLong("firstJoin"); + } + + if (config.isSet("lastlogin")) { + lastLogin = config.getLong("lastlogin"); + } + + if (config.isSet("lastLogout")) { + lastLogout = config.getLong("lastLogout"); + } + + lastIp = config.getString("ip.last"); + ips.addAll(config.getStringList("ip.previous")); + + lastName = config.getString("name.last"); + names.addAll(config.getStringList("name.previous")); + } + + void save(ConfigurationSection config) { + config.set("firstJoin", firstJoin); + config.set("lastLogin", lastLogin); + config.set("lastLogout", lastLogout); + config.set("ip.last", lastIp); + config.set("ip.previous", ips); + config.set("name.last", lastName); + config.set("name.previous", names); + } + + public Timestamp getFirstJoin() { + return firstJoin == null ? null : new Timestamp(firstJoin); + } + + public Timestamp getLastLogin() { + return lastLogin == null ? null : new Timestamp(lastLogin); + } + + public Timestamp getLastLogout() { + return lastLogout == null ? null : new Timestamp(lastLogout); + } + + public String getLastIp() { + return lastIp; + } + + public List getIps() { + return Collections.unmodifiableList(ips); + } + + public String getLastName() { + return lastName; + } + + public List getNames() { + return Collections.unmodifiableList(names); + } + +} \ No newline at end of file diff --git a/src/main/java/me/st28/flexseries/flexcore/player/PlayerManager.java b/src/main/java/me/st28/flexseries/flexcore/player/PlayerManager.java index d696d7f..ded7747 100644 --- a/src/main/java/me/st28/flexseries/flexcore/player/PlayerManager.java +++ b/src/main/java/me/st28/flexseries/flexcore/player/PlayerManager.java @@ -29,8 +29,11 @@ import me.st28.flexseries.flexcore.events.PlayerLeaveEvent; import me.st28.flexseries.flexcore.message.MessageReference; import me.st28.flexseries.flexcore.player.loading.PlayerLoadCycle; +import me.st28.flexseries.flexcore.player.loading.PlayerLoader; import me.st28.flexseries.flexcore.plugin.module.FlexModule; +import me.st28.flexseries.flexcore.storage.flatfile.YamlFileManager; import org.apache.commons.lang.StringEscapeUtils; +import org.apache.commons.lang.Validate; import org.bukkit.Bukkit; import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.entity.Player; @@ -43,10 +46,11 @@ import org.bukkit.event.player.PlayerKickEvent; import org.bukkit.event.player.PlayerQuitEvent; +import java.io.File; import java.util.*; import java.util.Map.Entry; -public final class PlayerManager extends FlexModule implements Listener { +public final class PlayerManager extends FlexModule implements Listener, PlayerLoader { private String unloadedMessage; @@ -57,15 +61,26 @@ public final class PlayerManager extends FlexModule implements Listene private boolean enableJoinMessageChange; private boolean enableQuitMessageChange; - final Set handledPlayers = new HashSet<>(); + private final Set handledPlayers = new HashSet<>(); private final Map cachedCycles = new HashMap<>(); + private File playerDir; + private final Map playerData = new HashMap<>(); + public PlayerManager(FlexCore plugin) { super(plugin, "players", "Manages players", true); } + @Override + protected void handleLoad() { + playerDir = new File(getDataFolder() + File.separator + "userdata"); + playerDir.mkdirs(); + } + @Override protected void handleReload() { + playerDir.mkdirs(); + FileConfiguration config = getConfig(); loadTimeout = config.getLong("load timeout", 100L); @@ -78,7 +93,67 @@ protected void handleReload() { unloadedMessage = StringEscapeUtils.unescapeJava(config.getString("player join.unloaded message", "Unable to load your data.\nPlease try again.")); } + @Override + protected void handleSave(boolean async) { + for (UUID uuid : playerData.keySet()) { + savePlayer(uuid); + } + } + + private void savePlayer(UUID uuid) { + PlayerData data = getPlayerData(uuid); + if (data == null) return; + + YamlFileManager file = new YamlFileManager(playerDir + File.separator + uuid.toString() + ".yml"); + + data.save(file.getConfig()); + + file.save(); + } + + @Override + public boolean isPlayerLoadSync() { + return true; + } + + @Override + public boolean loadPlayer(UUID uuid, String name, PlayerLoadCycle cycle) { + if (!playerData.containsKey(uuid)) { + PlayerData data = new PlayerData(uuid, new YamlFileManager(playerDir + File.separator + uuid.toString() + ".yml").getConfig()); + playerData.put(uuid, data); + } + + PlayerLoadCycle.completedCycle(cycle, this); + return true; + } + + /** + * @return a {@link PlayerData} instance for a specified player. + */ + public PlayerData getPlayerData(UUID uuid) { + Validate.notNull(uuid, "UUID cannot be null."); + return playerData.get(uuid); + } + private void handleOfficialLogin(final Player p, final PlayerLoadCycle cycle, final String joinMessage) { + PlayerData playerData = getPlayerData(p.getUniqueId()); + if (playerData.firstJoin == null) { + playerData.firstJoin = System.currentTimeMillis(); + } + playerData.lastLogin = System.currentTimeMillis(); + + String curIp = p.getAddress().toString(); + playerData.lastIp = curIp; + if (!playerData.ips.contains(curIp)) { + playerData.ips.add(curIp); + } + + String curName = p.getName(); + playerData.lastName = curName; + if (!playerData.names.contains(curName)) { + playerData.names.add(curName); + } + PlayerJoinLoadedEvent newJoinEvent = new PlayerJoinLoadedEvent(p, cycle.getCustomData()); if (enableJoinMessageChange) { newJoinEvent.setJoinMessage(MessageReference.createPlain(joinMessage)); @@ -153,6 +228,8 @@ private void handlePlayerLeave(Player p, String message) { return; } + getPlayerData(p.getUniqueId()).lastLogout = System.currentTimeMillis(); + PlayerLeaveEvent newLeaveEvent = new PlayerLeaveEvent(p); if (enableQuitMessageChange) { newLeaveEvent.setLeaveMessage(MessageReference.createPlain(message)); @@ -169,6 +246,8 @@ private void handlePlayerLeave(Player p, String message) { } } } + + savePlayer(p.getUniqueId()); } @EventHandler(priority = EventPriority.HIGHEST) From e730178364778fe1b5947bdc0d711fa1f4657cb5 Mon Sep 17 00:00:00 2001 From: Stealth2800 Date: Sun, 5 Apr 2015 12:23:30 -0700 Subject: [PATCH 02/16] Adds the command label aliases defined the plugin.yml --- .../java/me/st28/flexseries/flexcore/command/FlexCommand.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main/java/me/st28/flexseries/flexcore/command/FlexCommand.java b/src/main/java/me/st28/flexseries/flexcore/command/FlexCommand.java index ab6d472..1e0f21e 100644 --- a/src/main/java/me/st28/flexseries/flexcore/command/FlexCommand.java +++ b/src/main/java/me/st28/flexseries/flexcore/command/FlexCommand.java @@ -85,6 +85,7 @@ public FlexCommand(T plugin, String label, List arguments, Flex this.plugin = plugin; this.labels.add(label.toLowerCase()); + this.labels.addAll(pluginCommand.getAliases()); this.parent = null; if (arguments != null) { Validate.noNullElements(arguments, "Arguments list cannot contain any null arguments."); From 7db91d6bfccb210d6a23704bce863fa298e93c67 Mon Sep 17 00:00:00 2001 From: Stealth2800 Date: Wed, 15 Apr 2015 16:30:06 -0700 Subject: [PATCH 03/16] Added a new collectionToString method that allows for a default value if the collection is empty. (Useful for one-line method calls) --- .../java/me/st28/flexseries/flexcore/util/StringUtils.java | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/main/java/me/st28/flexseries/flexcore/util/StringUtils.java b/src/main/java/me/st28/flexseries/flexcore/util/StringUtils.java index f3dc48d..b87e979 100644 --- a/src/main/java/me/st28/flexseries/flexcore/util/StringUtils.java +++ b/src/main/java/me/st28/flexseries/flexcore/util/StringUtils.java @@ -40,7 +40,12 @@ public static String collectionToString(Collection collection, StringConv } public static String collectionToString(Collection collection, StringConverter converter, String delimiter) { + return collectionToString(collection, converter, delimiter, null); + } + + public static String collectionToString(Collection collection, StringConverter converter, String delimiter, String defaultValue) { if (collection == null) return ""; + StringBuilder sb = new StringBuilder(); for (T item : collection) { if (sb.length() > 0) { @@ -48,7 +53,7 @@ public static String collectionToString(Collection collection, StringConv } sb.append(converter.toString(item)); } - return sb.toString(); + return sb.length() == 0 ? defaultValue : sb.toString(); } public static List collectionToStringList(Collection collection, StringConverter converter) { From 5642204fa6bbfeca3b00b8a44eee3c1251beb75a Mon Sep 17 00:00:00 2001 From: Stealth2800 Date: Wed, 15 Apr 2015 16:30:43 -0700 Subject: [PATCH 04/16] Added a method to retrieve an online or offline player's UUID using an exact name or partial name that is matched to the full name. --- .../flexcore/command/CommandUtils.java | 64 +++++++++++++++++++ 1 file changed, 64 insertions(+) diff --git a/src/main/java/me/st28/flexseries/flexcore/command/CommandUtils.java b/src/main/java/me/st28/flexseries/flexcore/command/CommandUtils.java index 74ef772..8aa19f8 100644 --- a/src/main/java/me/st28/flexseries/flexcore/command/CommandUtils.java +++ b/src/main/java/me/st28/flexseries/flexcore/command/CommandUtils.java @@ -29,6 +29,9 @@ import me.st28.flexseries.flexcore.message.MessageReference; import me.st28.flexseries.flexcore.message.ReplacementMap; import me.st28.flexseries.flexcore.permission.PermissionNode; +import me.st28.flexseries.flexcore.player.uuid_tracker.PlayerUuidTracker; +import me.st28.flexseries.flexcore.plugin.FlexPlugin; +import me.st28.flexseries.flexcore.util.QuickMap; import org.bukkit.Bukkit; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; @@ -168,4 +171,65 @@ public static Player getTargetPlayer(CommandSender sender, String name, boolean return player; } + /** + * Gets a player's UUID based on name. + * + * @param sender The sender of the command. + * @param name The name entered by the sender. + * @param notSender True to make sure the target is not the sender. + * @return The UUID of the matched player.
+ * Null if no matching player was found. + */ + public static UUID getPlayerUuid(CommandSender sender, String name, boolean notSender, boolean isOnline, boolean silent) { + UUID found = null; + + PlayerUuidTracker uuidTracker = FlexPlugin.getRegisteredModule(PlayerUuidTracker.class); + + // 1) Try getting by exact name + Player online = Bukkit.getPlayerExact(name); + if (online != null) { + found = online.getUniqueId(); + } + + if (found == null) { + // 2) Try getting by matching name + List matchedNames = new ArrayList<>(); + + for (String curName : uuidTracker.getNamesToUuids().keySet()) { + if (name.equalsIgnoreCase(curName)) { + // Exact match + matchedNames.add(name); + break; + } + + if (curName.toLowerCase().contains(name.toLowerCase())) { + // Partial match + matchedNames.add(curName); + } + } + + if (matchedNames.size() == 1) { + found = uuidTracker.getUuid(matchedNames.get(0)); + name = uuidTracker.getName(found); + } + } + + if (found != null && notSender && sender instanceof Player && ((Player) sender).getUniqueId().equals(found)) { + if (!silent) { + throw new CommandInterruptedException(MessageReference.create(FlexCore.class, "general.errors.players_cannot_be_self")); + } + return null; + } else if (found != null && isOnline && Bukkit.getPlayer(found) == null) { + if (!silent) { + throw new CommandInterruptedException(MessageReference.create(FlexCore.class, "general.errors.players_matched_offline", new QuickMap<>("{NAME}", name).getMap())); + } + return null; + } else if (found == null) { + if (!silent) { + throw new CommandInterruptedException(MessageReference.create(FlexCore.class, "general.errors.players_matched_none_offline", new QuickMap<>("{NAME}", name).getMap())); + } + } + return found; + } + } \ No newline at end of file From edaa7b09955b154656af14ea94d2fad593ebaa6c Mon Sep 17 00:00:00 2001 From: Stealth2800 Date: Wed, 15 Apr 2015 16:31:10 -0700 Subject: [PATCH 05/16] Fixed an NPE and made the subcommands map a LinkedHashMap in order to preserve ordering. --- .../java/me/st28/flexseries/flexcore/command/FlexCommand.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/me/st28/flexseries/flexcore/command/FlexCommand.java b/src/main/java/me/st28/flexseries/flexcore/command/FlexCommand.java index 1e0f21e..7dec0dc 100644 --- a/src/main/java/me/st28/flexseries/flexcore/command/FlexCommand.java +++ b/src/main/java/me/st28/flexseries/flexcore/command/FlexCommand.java @@ -64,7 +64,7 @@ public abstract class FlexCommand { /** * Subcommands under this command. */ - private final Map> subcommands = new HashMap<>(); + private final Map> subcommands = new LinkedHashMap<>(); /** * The settings for this command. @@ -170,7 +170,7 @@ public final List> getParents() { public final PermissionNode getPermission() { PermissionNode permission = settings.getPermission(); if (permission == null && settings.shouldInheritPermission()) { - return getParent().getPermission(); + return getParent() == null ? null : getParent().getPermission(); } return permission; } From 9518e832cca0dd0f8c7efffca07f60f5c30f423b Mon Sep 17 00:00:00 2001 From: Stealth2800 Date: Wed, 15 Apr 2015 16:32:46 -0700 Subject: [PATCH 06/16] Only use the help command as default if the command doesn't have a default subcommand set. --- .../st28/flexseries/flexcore/command/FlexCommandWrapper.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/me/st28/flexseries/flexcore/command/FlexCommandWrapper.java b/src/main/java/me/st28/flexseries/flexcore/command/FlexCommandWrapper.java index 3d19c00..602e6e3 100644 --- a/src/main/java/me/st28/flexseries/flexcore/command/FlexCommandWrapper.java +++ b/src/main/java/me/st28/flexseries/flexcore/command/FlexCommandWrapper.java @@ -72,7 +72,7 @@ public static void registerCommand(FlexPlugin plugin, Fle if (/*!command.getSubcommands().isEmpty() && */!command.getSubcommands().containsKey("help")) { command.registerSubcommand(new FlexHelpCommand<>(command)); - if (command.getSettings().isDummyCommand()) { + if (command.getSettings().isDummyCommand() && command.getSettings().getDefaultSubcommand() == null) { command.getSettings().defaultSubcommand("help"); } } @@ -80,7 +80,7 @@ public static void registerCommand(FlexPlugin plugin, Fle for (FlexSubcommand subcommand : command.getSubcommands().values()) { if (/*!subcommand.getSubcommands().isEmpty() && */!subcommand.getSubcommands().containsKey("help")) { subcommand.registerSubcommand(new FlexHelpCommand<>(subcommand)); - if (subcommand.getSettings().isDummyCommand()) { + if (subcommand.getSettings().isDummyCommand() && subcommand.getSettings().getDefaultSubcommand() == null) { subcommand.getSettings().defaultSubcommand("help"); } } From fff7c7c121fbc34dd74a666e2bef05938f8c5e68 Mon Sep 17 00:00:00 2001 From: Stealth2800 Date: Wed, 15 Apr 2015 16:33:03 -0700 Subject: [PATCH 07/16] Fixed exception on registering same permission multiple times. --- .../me/st28/flexseries/flexcore/command/FlexHelpCommand.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/main/java/me/st28/flexseries/flexcore/command/FlexHelpCommand.java b/src/main/java/me/st28/flexseries/flexcore/command/FlexHelpCommand.java index 397c246..b6e7ba2 100644 --- a/src/main/java/me/st28/flexseries/flexcore/command/FlexHelpCommand.java +++ b/src/main/java/me/st28/flexseries/flexcore/command/FlexHelpCommand.java @@ -46,7 +46,9 @@ public FlexHelpCommand(FlexCommand parent) { PermissionNode parentPerm = parent.getPermission(); if (parentPerm != null) { String helpPerm = parentPerm.getNode() + ".help"; - Bukkit.getPluginManager().addPermission(new Permission(helpPerm, PermissionDefault.TRUE)); + try { + Bukkit.getPluginManager().addPermission(new Permission(helpPerm, PermissionDefault.TRUE)); + } catch (IllegalArgumentException ex) {} getSettings().permission(new SimplePermissionNode(helpPerm)); } From 26b3198ddcc24d33a2124e49a068e3cff7e3bfc9 Mon Sep 17 00:00:00 2001 From: Stealth2800 Date: Wed, 15 Apr 2015 16:33:17 -0700 Subject: [PATCH 08/16] Added a utility class for Bukkit's Player class. --- .../flexseries/flexcore/util/PlayerUtils.java | 58 +++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 src/main/java/me/st28/flexseries/flexcore/util/PlayerUtils.java diff --git a/src/main/java/me/st28/flexseries/flexcore/util/PlayerUtils.java b/src/main/java/me/st28/flexseries/flexcore/util/PlayerUtils.java new file mode 100644 index 0000000..433cb5c --- /dev/null +++ b/src/main/java/me/st28/flexseries/flexcore/util/PlayerUtils.java @@ -0,0 +1,58 @@ +/** + * FlexCore - Licensed under the MIT License (MIT) + * + * Copyright (c) Stealth2800 + * Copyright (c) contributors + * + * 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 me.st28.flexseries.flexcore.util; + +import me.st28.flexseries.flexcore.player.PlayerProfile; +import org.apache.commons.lang.Validate; +import org.bukkit.Bukkit; +import org.bukkit.ChatColor; + +import java.util.UUID; + +/** + * Utility classes for Bukkit's Player class. + */ +public final class PlayerUtils { + + private PlayerUtils() {} + + /** + * @return the name for the given UUID. + */ + public static String getName(UUID uuid) { + Validate.notNull(uuid, "UUID cannot be null."); + return new PlayerProfile(uuid).getName(); + } + + /** + * @return the name for the given UUID in green if the player is offline or red if the player is offline. + */ + public static String getColoredName(UUID uuid) { + Validate.notNull(uuid, "UUID cannot be null."); + String name = new PlayerProfile(uuid).getName(); + return (Bukkit.getPlayer(uuid) != null ? ChatColor.GREEN : ChatColor.RED) + name; + } + +} \ No newline at end of file From a7128551945655911a3cfd6848aa89b736bc3ed4 Mon Sep 17 00:00:00 2001 From: Stealth2800 Date: Sat, 25 Apr 2015 23:53:09 -0700 Subject: [PATCH 09/16] Added a stringCollectionToString method that allows for a default value. --- .../flexseries/flexcore/util/StringUtils.java | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/src/main/java/me/st28/flexseries/flexcore/util/StringUtils.java b/src/main/java/me/st28/flexseries/flexcore/util/StringUtils.java index b87e979..14bbd16 100644 --- a/src/main/java/me/st28/flexseries/flexcore/util/StringUtils.java +++ b/src/main/java/me/st28/flexseries/flexcore/util/StringUtils.java @@ -83,15 +83,16 @@ public static String stringCollectionToString(Collection collection) { * @return A string containing the contents of the collection, separated by the given delimiter. */ public static String stringCollectionToString(Collection collection, String delimiter) { - if (collection == null) return ""; - StringBuilder sb = new StringBuilder(); - for (String item : collection) { - if (sb.length() > 0) { - sb.append(delimiter); + return stringCollectionToString(collection, delimiter, null); + } + + public static String stringCollectionToString(Collection collection, String delimiter, String defaultValue) { + return collectionToString(collection, new StringConverter() { + @Override + public String toString(String string) { + return string; } - sb.append(item); - } - return sb.toString(); + }, delimiter, defaultValue); } /** From 71441416bb886c4390acfbd23f53361093645b1e Mon Sep 17 00:00:00 2001 From: Stealth2800 Date: Sun, 26 Apr 2015 10:26:12 -0700 Subject: [PATCH 10/16] Added a utility class for Bukkit's Location class. --- .../flexcore/util/LocationUtils.java | 95 +++++++++++++++++++ 1 file changed, 95 insertions(+) create mode 100644 src/main/java/me/st28/flexseries/flexcore/util/LocationUtils.java diff --git a/src/main/java/me/st28/flexseries/flexcore/util/LocationUtils.java b/src/main/java/me/st28/flexseries/flexcore/util/LocationUtils.java new file mode 100644 index 0000000..d38496f --- /dev/null +++ b/src/main/java/me/st28/flexseries/flexcore/util/LocationUtils.java @@ -0,0 +1,95 @@ +package me.st28.flexseries.flexcore.util; + +import org.apache.commons.lang.Validate; +import org.bukkit.Bukkit; +import org.bukkit.Location; +import org.bukkit.World; +import org.bukkit.configuration.ConfigurationSection; + +/** + * Utility methods for Bukkit's Location class. + */ +public final class LocationUtils { + + private LocationUtils() {} + + /** + * Converts a location to a string. + * + * @param location The location to convert. + * @param useYawPitch True to use the yaw and pitch of the location.
+ * False to use 0F as the value for both yaw and pitch in the converted location. + * @return A string representing the location.
+ * Format: world;y;x;z;yaw;pitch + */ + public static String locationToString(Location location, boolean useYawPitch) { + Validate.notNull(location, "Location cannot be null."); + + String returnString = location.getWorld().getName() + ";" + location.getX() + ";" + location.getY() + ";" + location.getZ(); + + if (useYawPitch) { + return returnString + ";" + location.getYaw() + ";" + location.getPitch(); + } else { + return returnString + ";0;0"; + } + } + + /** + * Converts a string to a location. + * + * @param rawLocation The raw location.
+ * Format: world;x;y;z;yaw;pitch + * @return A new Location object that represents the input string. + */ + public static Location stringToLocation(String rawLocation) { + Validate.notNull(rawLocation, "Raw location cannot be null."); + + String[] split = rawLocation.split(";"); + + if (split.length != 6) { + throw new IllegalArgumentException("Invalid location '" + rawLocation + "'"); + } + + World world = Bukkit.getWorld(split[0]); + double x = Double.parseDouble(split[1]); + double y = Double.parseDouble(split[2]); + double z = Double.parseDouble(split[3]); + float yaw = Float.parseFloat(split[4]); + float pitch = Float.parseFloat(split[5]); + + return new Location(world, x, y, z, yaw, pitch); + } + + /** + * Saves a location to a configuration section. + * + * @param location The location to save. + * @param config The configuration section to save the location in. + */ + public static void saveLocationToConfiguration(Location location, ConfigurationSection config) { + config.set("world", location.getWorld().getName()); + config.set("x", location.getX()); + config.set("y", location.getY()); + config.set("z", location.getZ()); + config.set("yaw", location.getYaw()); + config.set("pitch", location.getPitch()); + } + + /** + * Reads a location from a configuration section. + * + * @param config The configuration to read the location from. + * @return A location representing the values found in the configuration. + */ + public static Location loadLocationFromConfiguration(ConfigurationSection config) { + World world = Bukkit.getWorld(config.getString("world")); + double x = config.getDouble("x"); + double y = config.getDouble("y"); + double z = config.getDouble("z"); + float yaw = (float) config.getDouble("yaw"); + float pitch = (float) config.getDouble("pitch"); + + return new Location(world, x, y, z, yaw, pitch); + } + +} \ No newline at end of file From d9818fdeb575a1d79181dafdd0454ea1b2186858 Mon Sep 17 00:00:00 2001 From: Stealth2800 Date: Sun, 26 Apr 2015 10:33:12 -0700 Subject: [PATCH 11/16] Added overloaded methods that take a Throwable argument to pass on to the actual log method call. --- .../flexcore/logging/LogHelper.java | 52 +++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/src/main/java/me/st28/flexseries/flexcore/logging/LogHelper.java b/src/main/java/me/st28/flexseries/flexcore/logging/LogHelper.java index 8db3a84..6458403 100644 --- a/src/main/java/me/st28/flexseries/flexcore/logging/LogHelper.java +++ b/src/main/java/me/st28/flexseries/flexcore/logging/LogHelper.java @@ -46,52 +46,104 @@ public static void debug(Class pluginClass, String message LogHelper.debug(JavaPlugin.getPlugin(pluginClass), message); } + public static void debug(Class pluginClass, String message, Throwable throwable) { + LogHelper.debug(JavaPlugin.getPlugin(pluginClass), message, throwable); + } + public static void debug(JavaPlugin plugin, String message) { if (plugin.getConfig().getBoolean("debug")) { Bukkit.getLogger().log(Level.INFO, String.format("[%s DEBUG] %s", plugin.getName(), message)); } } + public static void debug(JavaPlugin plugin, String message, Throwable throwable) { + if (plugin.getConfig().getBoolean("debug")) { + Bukkit.getLogger().log(Level.INFO, String.format("[%s DEBUG] %s", plugin.getName(), message), throwable); + } + } + public static void debug(FlexModule module, String message) { if (module.getPlugin().getConfig().getBoolean("debug")) { Bukkit.getLogger().log(Level.INFO, String.format("[%s/%s DEBUG] %s", module.getPlugin().getName(), module.getIdentifier(), message)); } } + public static void debug(FlexModule module, String message, Throwable throwable) { + if (module.getPlugin().getConfig().getBoolean("debug")) { + Bukkit.getLogger().log(Level.INFO, String.format("[%s/%s DEBUG] %s", module.getPlugin().getName(), module.getIdentifier(), message), throwable); + } + } + public static void info(Class pluginClass, String message) { LogHelper.info(JavaPlugin.getPlugin(pluginClass), message); } + public static void info(Class pluginClass, String message, Throwable throwable) { + LogHelper.info(JavaPlugin.getPlugin(pluginClass), message, throwable); + } + public static void info(JavaPlugin plugin, String message) { Bukkit.getLogger().log(Level.INFO, String.format("[%s] %s", plugin.getName(), message)); } + public static void info(JavaPlugin plugin, String message, Throwable throwable) { + Bukkit.getLogger().log(Level.INFO, String.format("[%s] %s", plugin.getName(), message), throwable); + } + public static void info(FlexModule module, String message) { Bukkit.getLogger().log(Level.INFO, String.format("[%s/%s] %s", module.getPlugin().getName(), module.getIdentifier(), message)); } + public static void info(FlexModule module, String message, Throwable throwable) { + Bukkit.getLogger().log(Level.INFO, String.format("[%s/%s] %s", module.getPlugin().getName(), module.getIdentifier(), message), throwable); + } + public static void warning(Class pluginClass, String message) { LogHelper.warning(JavaPlugin.getPlugin(pluginClass), message); } + public static void warning(Class pluginClass, String message, Throwable throwable) { + LogHelper.warning(JavaPlugin.getPlugin(pluginClass), message, throwable); + } + public static void warning(JavaPlugin plugin, String message) { Bukkit.getLogger().log(Level.WARNING, String.format("[%s] %s", plugin.getName(), message)); } + public static void warning(JavaPlugin plugin, String message, Throwable throwable) { + Bukkit.getLogger().log(Level.WARNING, String.format("[%s] %s", plugin.getName(), message), throwable); + } + public static void warning(FlexModule module, String message) { Bukkit.getLogger().log(Level.WARNING, String.format("[%s/%s] %s", module.getPlugin().getName(), module.getIdentifier(), message)); } + public static void warning(FlexModule module, String message, Throwable throwable) { + Bukkit.getLogger().log(Level.WARNING, String.format("[%s/%s] %s", module.getPlugin().getName(), module.getIdentifier(), message), throwable); + } + public static void severe(Class pluginClass, String message) { LogHelper.severe(JavaPlugin.getPlugin(pluginClass), message); } + public static void severe(Class pluginClass, String message, Throwable throwable) { + LogHelper.severe(JavaPlugin.getPlugin(pluginClass), message, throwable); + } + public static void severe(JavaPlugin plugin, String message) { Bukkit.getLogger().log(Level.SEVERE, String.format("[%s] %s", plugin.getName(), message)); } + public static void severe(JavaPlugin plugin, String message, Throwable throwable) { + Bukkit.getLogger().log(Level.SEVERE, String.format("[%s] %s", plugin.getName(), message), throwable); + } + public static void severe(FlexModule module, String message) { Bukkit.getLogger().log(Level.SEVERE, String.format("[%s/%s] %s", module.getPlugin().getName(), module.getIdentifier(), message)); } + public static void severe(FlexModule module, String message, Throwable throwable) { + Bukkit.getLogger().log(Level.SEVERE, String.format("[%s/%s] %s", module.getPlugin().getName(), module.getIdentifier(), message), throwable); + } + } \ No newline at end of file From b5a1969f2ca4dbebc7f5494f96a3d0b007ec9179 Mon Sep 17 00:00:00 2001 From: Stealth2800 Date: Tue, 28 Apr 2015 20:34:42 -0700 Subject: [PATCH 12/16] Added license header. --- .../flexcore/util/LocationUtils.java | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/main/java/me/st28/flexseries/flexcore/util/LocationUtils.java b/src/main/java/me/st28/flexseries/flexcore/util/LocationUtils.java index d38496f..1bccf4b 100644 --- a/src/main/java/me/st28/flexseries/flexcore/util/LocationUtils.java +++ b/src/main/java/me/st28/flexseries/flexcore/util/LocationUtils.java @@ -1,3 +1,27 @@ +/** + * FlexCore - Licensed under the MIT License (MIT) + * + * Copyright (c) Stealth2800 + * Copyright (c) contributors + * + * 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 me.st28.flexseries.flexcore.util; import org.apache.commons.lang.Validate; From 8c64bdf5cf0f866b86fe33fe6bc32f7b051efc59 Mon Sep 17 00:00:00 2001 From: Stealth2800 Date: Tue, 28 Apr 2015 20:50:38 -0700 Subject: [PATCH 13/16] Fixed join and quit messages not showing. --- .../me/st28/flexseries/flexcore/player/PlayerManager.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/main/java/me/st28/flexseries/flexcore/player/PlayerManager.java b/src/main/java/me/st28/flexseries/flexcore/player/PlayerManager.java index ded7747..9ea1c46 100644 --- a/src/main/java/me/st28/flexseries/flexcore/player/PlayerManager.java +++ b/src/main/java/me/st28/flexseries/flexcore/player/PlayerManager.java @@ -202,20 +202,20 @@ public void onPlayerJoinHighest(PlayerJoinEvent e) { return; } + handleOfficialLogin(p, cachedCycles.remove(uuid), e.getJoinMessage()); + if (enableJoinMessageChange) { e.setJoinMessage(null); } - - handleOfficialLogin(p, cachedCycles.remove(uuid), e.getJoinMessage()); } @EventHandler(priority = EventPriority.HIGHEST) public void onPlayerQuitHighest(PlayerQuitEvent e) { + handlePlayerLeave(e.getPlayer(), e.getQuitMessage()); + if (enableQuitMessageChange) { e.setQuitMessage(null); } - - handlePlayerLeave(e.getPlayer(), e.getQuitMessage()); } @EventHandler(ignoreCancelled = true, priority = EventPriority.MONITOR) From 4d2b6eb385ff68e7a5127d5b0ecbdefdbd46e882 Mon Sep 17 00:00:00 2001 From: Stealth2800 Date: Sat, 2 May 2015 15:26:45 -0700 Subject: [PATCH 14/16] Only save plugin configuration file if there actually is one. --- .../java/me/st28/flexseries/flexcore/plugin/FlexPlugin.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/main/java/me/st28/flexseries/flexcore/plugin/FlexPlugin.java b/src/main/java/me/st28/flexseries/flexcore/plugin/FlexPlugin.java index d4fbe25..3a37b98 100644 --- a/src/main/java/me/st28/flexseries/flexcore/plugin/FlexPlugin.java +++ b/src/main/java/me/st28/flexseries/flexcore/plugin/FlexPlugin.java @@ -336,7 +336,9 @@ public void run() { * @param async If true, should save asynchronously (where applicable). */ public final void saveAll(boolean async) { - saveConfig(); + if (hasConfig) { + saveConfig(); + } for (FlexModule module : modules.values()) { if (getModuleStatus(module.getClass()) == ModuleStatus.ENABLED) { From 912545b65f739c4d959b8614f2f9635273d1c718 Mon Sep 17 00:00:00 2001 From: Stealth2800 Date: Sun, 3 May 2015 11:29:00 -0700 Subject: [PATCH 15/16] Updated MCML dependency version. --- FlexCore.iml | 2 +- pom.xml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/FlexCore.iml b/FlexCore.iml index 832e95d..febdb7c 100644 --- a/FlexCore.iml +++ b/FlexCore.iml @@ -21,7 +21,7 @@ - + diff --git a/pom.xml b/pom.xml index 35c541b..a9ee7dd 100644 --- a/pom.xml +++ b/pom.xml @@ -118,7 +118,7 @@ com.stealthyone.mcb mcml - 1.5.2-SNAPSHOT + 1.5.3-SNAPSHOT mkremins From 05652285ae56d106f2eecd8c8f7404f62f6052d2 Mon Sep 17 00:00:00 2001 From: Stealth2800 Date: Sun, 3 May 2015 20:50:40 -0700 Subject: [PATCH 16/16] Changed version to 4.1.0 --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index a9ee7dd..b24a628 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ me.st28.flexseries flexcore - 4.1.0-SNAPSHOT + 4.1.0 FlexCore Powerful library for Bukkit plugins http://stealthyone.com/