From 3e0241cb374d754a5c25c2c6c162ade7f0f55eb6 Mon Sep 17 00:00:00 2001 From: A5H73Y Date: Tue, 29 Dec 2020 23:21:34 +0000 Subject: [PATCH] Fixed ParkourLevel and ParkourRank format Added option to not teleport Player on Leave Treat AutoStart as a Restart if already on Course to prevent exploitation --- docs/developer.md | 2 - docs/tutorials/rewarding-player.md | 2 - pom.xml | 2 +- .../io/github/a5h73y/parkour/Parkour.java | 2 +- .../configuration/impl/DefaultConfig.java | 1 + .../listener/PlayerInteractListener.java | 5 +- .../parkour/type/player/PlayerInfo.java | 4 +- .../parkour/type/player/PlayerManager.java | 25 ++++-- .../parkour/upgrade/ParkourUpgrader.java | 53 +++++++++-- .../{ => major}/CourseInfoUpgradeTask.java | 7 +- .../{ => major}/DatabaseUpgradeTask.java | 12 ++- .../{ => major}/DefaultConfigUpgradeTask.java | 6 +- .../{ => major}/PlayerInfoUpgradeTask.java | 7 +- .../{ => major}/StringsConfigUpgradeTask.java | 4 +- .../upgrade/minor/PartialUpgradeTask.java | 88 +++++++++++++++++++ 15 files changed, 186 insertions(+), 34 deletions(-) rename src/main/java/io/github/a5h73y/parkour/upgrade/{ => major}/CourseInfoUpgradeTask.java (92%) rename src/main/java/io/github/a5h73y/parkour/upgrade/{ => major}/DatabaseUpgradeTask.java (94%) rename src/main/java/io/github/a5h73y/parkour/upgrade/{ => major}/DefaultConfigUpgradeTask.java (95%) rename src/main/java/io/github/a5h73y/parkour/upgrade/{ => major}/PlayerInfoUpgradeTask.java (89%) rename src/main/java/io/github/a5h73y/parkour/upgrade/{ => major}/StringsConfigUpgradeTask.java (93%) create mode 100644 src/main/java/io/github/a5h73y/parkour/upgrade/minor/PartialUpgradeTask.java diff --git a/docs/developer.md b/docs/developer.md index 54535118..42d0cf3a 100644 --- a/docs/developer.md +++ b/docs/developer.md @@ -1,8 +1,6 @@ Developer Tutorials ====== -## Description - Are you a developer? Great! You can make the most of out Parkour's functionality to create the perfect experience for your server. I've made the plugin super easy to expand and integrate into, first we will import Parkour project to begin working with it. ## Importing Parkour into your Project diff --git a/docs/tutorials/rewarding-player.md b/docs/tutorials/rewarding-player.md index 926d07eb..4d053290 100644 --- a/docs/tutorials/rewarding-player.md +++ b/docs/tutorials/rewarding-player.md @@ -1,8 +1,6 @@ Rewarding the Player ====== -## Description - There are several ways we can reward the player for completing a Course. We are able to reward the Player with ParkourLevels that allow them to unlock new courses, or achieve a ParkourRank that showcases how far they've progressed. All can be configured to match any server requirements, and even allow developers to natively add their own functionality. ## Course Prizes diff --git a/pom.xml b/pom.xml index 3de7a972..91398917 100644 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ 4.0.0 io.github.a5h73y Parkour - 6.1 + 6.2 jar Parkour diff --git a/src/main/java/io/github/a5h73y/parkour/Parkour.java b/src/main/java/io/github/a5h73y/parkour/Parkour.java index 48c94f15..42857fa3 100644 --- a/src/main/java/io/github/a5h73y/parkour/Parkour.java +++ b/src/main/java/io/github/a5h73y/parkour/Parkour.java @@ -300,7 +300,7 @@ private void checkForUpdates() { private boolean parkourNeedsUpgrading() { if (super.getConfig().contains("Version")) { Version existingVersion = new Version(super.getConfig().getString("Version")); - return existingVersion.isLowerThan("6.0"); + return existingVersion.isLowerThan(this.getDescription().getVersion()); } return false; } diff --git a/src/main/java/io/github/a5h73y/parkour/configuration/impl/DefaultConfig.java b/src/main/java/io/github/a5h73y/parkour/configuration/impl/DefaultConfig.java index 10bf6e29..c18de64f 100644 --- a/src/main/java/io/github/a5h73y/parkour/configuration/impl/DefaultConfig.java +++ b/src/main/java/io/github/a5h73y/parkour/configuration/impl/DefaultConfig.java @@ -91,6 +91,7 @@ protected void initializeConfig() { this.addDefault("OnLeave.TeleportToLinkedLobby", false); this.addDefault("OnLeave.DestroyCourseProgress", true); + this.addDefault("OnLeave.TeleportAway", true); this.addDefault("OnDie.ResetTimeWithNoCheckpoint", false); this.addDefault("OnDie.SetXPBarToDeathCount", false); diff --git a/src/main/java/io/github/a5h73y/parkour/listener/PlayerInteractListener.java b/src/main/java/io/github/a5h73y/parkour/listener/PlayerInteractListener.java index a8b41efd..f83d8455 100644 --- a/src/main/java/io/github/a5h73y/parkour/listener/PlayerInteractListener.java +++ b/src/main/java/io/github/a5h73y/parkour/listener/PlayerInteractListener.java @@ -237,9 +237,8 @@ public void onAutoStartEvent(PlayerInteractEvent event) { if (session != null) { // we only want to do something if the names match if (session.getCourseName().equals(courseName)) { - session.resetTime(); - parkour.getBountifulApi().sendActionBar(event.getPlayer(), - TranslationUtils.getTranslation("Parkour.TimerStarted", false), true); + Bukkit.getScheduler().runTask(parkour, () -> + parkour.getPlayerManager().restartCourse(event.getPlayer(), true)); } } else { parkour.getPlayerManager().joinCourseButDelayed( diff --git a/src/main/java/io/github/a5h73y/parkour/type/player/PlayerInfo.java b/src/main/java/io/github/a5h73y/parkour/type/player/PlayerInfo.java index f83b2a4e..ac157cbb 100644 --- a/src/main/java/io/github/a5h73y/parkour/type/player/PlayerInfo.java +++ b/src/main/java/io/github/a5h73y/parkour/type/player/PlayerInfo.java @@ -225,7 +225,7 @@ public static void setParkourLevel(OfflinePlayer player, int level) { * @return player's parkour rank */ public static String getParkourRank(OfflinePlayer player) { - return getPlayersConfig().getString(player.getUniqueId() + ".Rank", + return getPlayersConfig().getString(player.getUniqueId() + ".ParkourRank", TranslationUtils.getTranslation("Event.DefaultRank", false)); } @@ -235,7 +235,7 @@ public static String getParkourRank(OfflinePlayer player) { * @param parkourRank parkour rank value */ public static void setParkourRank(OfflinePlayer player, String parkourRank) { - getPlayersConfig().set(player.getUniqueId() + ".Rank", parkourRank); + getPlayersConfig().set(player.getUniqueId() + ".ParkourRank", parkourRank); persistChanges(); } diff --git a/src/main/java/io/github/a5h73y/parkour/type/player/PlayerManager.java b/src/main/java/io/github/a5h73y/parkour/type/player/PlayerManager.java index 36bd4213..c206767d 100644 --- a/src/main/java/io/github/a5h73y/parkour/type/player/PlayerManager.java +++ b/src/main/java/io/github/a5h73y/parkour/type/player/PlayerManager.java @@ -335,11 +335,13 @@ public void leaveCourse(Player player, boolean silent) { session.getCourse().getName(), LEAVE, "Parkour.Leave"), parkour.getConfig().getBoolean("DisplayTitle.Leave")); - if (parkour.getConfig().isTeleportToJoinLocation() - && PlayerInfo.hasJoinLocation(player)) { - player.teleport(PlayerInfo.getJoinLocation(player)); - } else { - parkour.getLobbyManager().teleportToLeaveDestination(player, session); + if (parkour.getConfig().getBoolean("OnLeave.TeleportAway")) { + if (parkour.getConfig().isTeleportToJoinLocation() + && PlayerInfo.hasJoinLocation(player)) { + player.teleport(PlayerInfo.getJoinLocation(player)); + } else { + parkour.getLobbyManager().teleportToLeaveDestination(player, session); + } } parkour.getCourseManager().runEventCommands(player, session.getCourseName(), LEAVE); } @@ -586,6 +588,17 @@ public void finishCourse(final Player player) { * @param player requesting player */ public void restartCourse(Player player) { + restartCourse(player, false); + } + + /** + * Restart Course progress. + * Will trigger a silent leave and rejoin of the Course. + * + * @param player requesting player + * @param doNotTeleport do not teleport the player manually + */ + public void restartCourse(Player player, boolean doNotTeleport) { if (!isPlaying(player)) { return; } @@ -595,7 +608,7 @@ public void restartCourse(Player player) { deleteParkourSession(player); joinCourse(player, course, true); // if they are restarting the Course, we need to teleport them back - if (!parkour.getConfig().getBoolean("OnJoin.TeleportPlayer")) { + if (!doNotTeleport && !parkour.getConfig().getBoolean("OnJoin.TeleportPlayer")) { player.teleport(course.getCheckpoints().get(0).getLocation()); } TranslationUtils.sendTranslation("Parkour.Restarting", player); diff --git a/src/main/java/io/github/a5h73y/parkour/upgrade/ParkourUpgrader.java b/src/main/java/io/github/a5h73y/parkour/upgrade/ParkourUpgrader.java index 1506e96d..6bb67fa5 100644 --- a/src/main/java/io/github/a5h73y/parkour/upgrade/ParkourUpgrader.java +++ b/src/main/java/io/github/a5h73y/parkour/upgrade/ParkourUpgrader.java @@ -1,8 +1,15 @@ package io.github.a5h73y.parkour.upgrade; +import com.g00fy2.versioncompare.Version; import io.github.a5h73y.parkour.Parkour; import io.github.a5h73y.parkour.other.AbstractPluginReceiver; import io.github.a5h73y.parkour.other.Backup; +import io.github.a5h73y.parkour.upgrade.major.CourseInfoUpgradeTask; +import io.github.a5h73y.parkour.upgrade.major.DatabaseUpgradeTask; +import io.github.a5h73y.parkour.upgrade.major.DefaultConfigUpgradeTask; +import io.github.a5h73y.parkour.upgrade.major.PlayerInfoUpgradeTask; +import io.github.a5h73y.parkour.upgrade.major.StringsConfigUpgradeTask; +import io.github.a5h73y.parkour.upgrade.minor.PartialUpgradeTask; import java.io.File; import java.io.IOException; import java.util.function.BooleanSupplier; @@ -57,6 +64,39 @@ public boolean getAsBoolean() { parkour.getLogger().info("Creating backup of current install..."); Backup.backupNow(true); + boolean success = true; + Version existingVersion = new Version(defaultConfig.getString("Version")); + + if (existingVersion.isLowerThan("5.0")) { + parkour.getLogger().warning("This version is too outdated."); + success = false; + + } else if (existingVersion.isLowerThan("6.0")) { + success = performFullUpgrade(); + + } else { + success = performPartialUpgrade(); + } + + if (success) { + try { + defaultConfig.set("Version", parkour.getDescription().getVersion()); + saveDefaultConfig(); + parkour.reloadConfig(); + parkour.getLogger().info("Parkour successfully upgraded to v" + + parkour.getDescription().getVersion()); + parkour.getLogger().info("The plugin will now start up..."); + + } catch (IOException e) { + parkour.getLogger().severe("An error occurred during upgrade: " + e.getMessage()); + e.printStackTrace(); + success = false; + } + } + return success; + } + + private boolean performFullUpgrade() { if (!new PlayerInfoUpgradeTask(this).start()) { return false; } @@ -83,16 +123,11 @@ public boolean getAsBoolean() { parkour.getLogger().info("Setting up Configs and Database..."); parkour.registerEssentialManagers(); + return databaseUpgrade.doMoreWork(); + } - if (!databaseUpgrade.doMoreWork()) { - return false; - } - - parkour.getLogger().info("Parkour successfully upgraded to " + parkour.getDescription().getVersion()); - parkour.getLogger().info("The plugin will now start up..."); - parkour.reloadConfig(); - parkour.onEnable(); - return true; + private boolean performPartialUpgrade() { + return new PartialUpgradeTask(this, defaultConfig.getString("Version")).start(); } public FileConfiguration getDefaultConfig() { diff --git a/src/main/java/io/github/a5h73y/parkour/upgrade/CourseInfoUpgradeTask.java b/src/main/java/io/github/a5h73y/parkour/upgrade/major/CourseInfoUpgradeTask.java similarity index 92% rename from src/main/java/io/github/a5h73y/parkour/upgrade/CourseInfoUpgradeTask.java rename to src/main/java/io/github/a5h73y/parkour/upgrade/major/CourseInfoUpgradeTask.java index aede318a..c2e582e6 100644 --- a/src/main/java/io/github/a5h73y/parkour/upgrade/CourseInfoUpgradeTask.java +++ b/src/main/java/io/github/a5h73y/parkour/upgrade/major/CourseInfoUpgradeTask.java @@ -1,5 +1,7 @@ -package io.github.a5h73y.parkour.upgrade; +package io.github.a5h73y.parkour.upgrade.major; +import io.github.a5h73y.parkour.upgrade.ParkourUpgrader; +import io.github.a5h73y.parkour.upgrade.TimedConfigUpgradeTask; import java.io.IOException; import java.util.Collections; import java.util.List; @@ -33,6 +35,9 @@ protected boolean doWork() { } transferAndDelete(courseName + ".Finished", courseName + ".Ready"); + transferAndDelete(courseName + ".Level", courseName + ".RewardLevel"); + transferAndDelete(courseName + ".LevelAdd", courseName + ".RewardLevelAdd"); + if (getConfig().contains(courseName + ".RewardDelay")) { getConfig().set(courseName + ".RewardDelay", getConfig().getInt(courseName + ".RewardDelay") * 24); diff --git a/src/main/java/io/github/a5h73y/parkour/upgrade/DatabaseUpgradeTask.java b/src/main/java/io/github/a5h73y/parkour/upgrade/major/DatabaseUpgradeTask.java similarity index 94% rename from src/main/java/io/github/a5h73y/parkour/upgrade/DatabaseUpgradeTask.java rename to src/main/java/io/github/a5h73y/parkour/upgrade/major/DatabaseUpgradeTask.java index 625aa66e..b553dba5 100644 --- a/src/main/java/io/github/a5h73y/parkour/upgrade/DatabaseUpgradeTask.java +++ b/src/main/java/io/github/a5h73y/parkour/upgrade/major/DatabaseUpgradeTask.java @@ -1,9 +1,11 @@ -package io.github.a5h73y.parkour.upgrade; +package io.github.a5h73y.parkour.upgrade.major; import io.github.a5h73y.parkour.Parkour; import io.github.a5h73y.parkour.database.ParkourDatabase; import io.github.a5h73y.parkour.database.SQLite; import io.github.a5h73y.parkour.database.TimeEntry; +import io.github.a5h73y.parkour.upgrade.ParkourUpgrader; +import io.github.a5h73y.parkour.upgrade.TimedUpgradeTask; import java.io.File; import java.sql.ResultSet; import java.sql.SQLException; @@ -101,7 +103,13 @@ protected boolean doWork() { return success; } - protected boolean doMoreWork() { + /** + * Proceed to do the additional Database work required. + * Reinserts the times and cleans up the temporary tables. + * + * @return success + */ + public boolean doMoreWork() { boolean success = true; // create a proper Parkour connection // which also setups up the new tables diff --git a/src/main/java/io/github/a5h73y/parkour/upgrade/DefaultConfigUpgradeTask.java b/src/main/java/io/github/a5h73y/parkour/upgrade/major/DefaultConfigUpgradeTask.java similarity index 95% rename from src/main/java/io/github/a5h73y/parkour/upgrade/DefaultConfigUpgradeTask.java rename to src/main/java/io/github/a5h73y/parkour/upgrade/major/DefaultConfigUpgradeTask.java index cb47109e..d9988cb9 100644 --- a/src/main/java/io/github/a5h73y/parkour/upgrade/DefaultConfigUpgradeTask.java +++ b/src/main/java/io/github/a5h73y/parkour/upgrade/major/DefaultConfigUpgradeTask.java @@ -1,7 +1,8 @@ -package io.github.a5h73y.parkour.upgrade; +package io.github.a5h73y.parkour.upgrade.major; import com.cryptomorin.xseries.XMaterial; -import io.github.a5h73y.parkour.Parkour; +import io.github.a5h73y.parkour.upgrade.ParkourUpgrader; +import io.github.a5h73y.parkour.upgrade.TimedConfigUpgradeTask; import java.io.IOException; import java.util.Arrays; import java.util.List; @@ -53,7 +54,6 @@ protected boolean doWork() { // miscellaneous getConfig().set("OnJoin.Item.HideAllEnabled.Material", getConfig().getString("OnJoin.Item.HideAll.Material")); - getConfig().set("Version", Double.valueOf(Parkour.getInstance().getDescription().getVersion())); if ("SAPLING".equals(getConfig().getString("OnJoin.Item.Leave.Material"))) { getConfig().set("OnJoin.Item.Leave.Material", XMaterial.OAK_SAPLING.parseMaterial().name()); } diff --git a/src/main/java/io/github/a5h73y/parkour/upgrade/PlayerInfoUpgradeTask.java b/src/main/java/io/github/a5h73y/parkour/upgrade/major/PlayerInfoUpgradeTask.java similarity index 89% rename from src/main/java/io/github/a5h73y/parkour/upgrade/PlayerInfoUpgradeTask.java rename to src/main/java/io/github/a5h73y/parkour/upgrade/major/PlayerInfoUpgradeTask.java index 3508ce2b..29ce60d0 100644 --- a/src/main/java/io/github/a5h73y/parkour/upgrade/PlayerInfoUpgradeTask.java +++ b/src/main/java/io/github/a5h73y/parkour/upgrade/major/PlayerInfoUpgradeTask.java @@ -1,5 +1,7 @@ -package io.github.a5h73y.parkour.upgrade; +package io.github.a5h73y.parkour.upgrade.major; +import io.github.a5h73y.parkour.upgrade.ParkourUpgrader; +import io.github.a5h73y.parkour.upgrade.TimedConfigUpgradeTask; import java.io.IOException; import java.util.Arrays; import java.util.List; @@ -55,6 +57,9 @@ protected boolean doWork() { convertPlayerInventory(playerName, targetPlayer); } + transferAndDelete(targetPlayer.getUniqueId() + ".Level", targetPlayer.getUniqueId() + ".ParkourLevel"); + transferAndDelete(targetPlayer.getUniqueId() + ".Rank", targetPlayer.getUniqueId() + ".ParkourRank"); + count++; } diff --git a/src/main/java/io/github/a5h73y/parkour/upgrade/StringsConfigUpgradeTask.java b/src/main/java/io/github/a5h73y/parkour/upgrade/major/StringsConfigUpgradeTask.java similarity index 93% rename from src/main/java/io/github/a5h73y/parkour/upgrade/StringsConfigUpgradeTask.java rename to src/main/java/io/github/a5h73y/parkour/upgrade/major/StringsConfigUpgradeTask.java index a33671cd..55976b82 100644 --- a/src/main/java/io/github/a5h73y/parkour/upgrade/StringsConfigUpgradeTask.java +++ b/src/main/java/io/github/a5h73y/parkour/upgrade/major/StringsConfigUpgradeTask.java @@ -1,5 +1,7 @@ -package io.github.a5h73y.parkour.upgrade; +package io.github.a5h73y.parkour.upgrade.major; +import io.github.a5h73y.parkour.upgrade.ParkourUpgrader; +import io.github.a5h73y.parkour.upgrade.TimedConfigUpgradeTask; import java.io.IOException; import java.util.Set; import java.util.regex.Matcher; diff --git a/src/main/java/io/github/a5h73y/parkour/upgrade/minor/PartialUpgradeTask.java b/src/main/java/io/github/a5h73y/parkour/upgrade/minor/PartialUpgradeTask.java new file mode 100644 index 00000000..a055f2e2 --- /dev/null +++ b/src/main/java/io/github/a5h73y/parkour/upgrade/minor/PartialUpgradeTask.java @@ -0,0 +1,88 @@ +package io.github.a5h73y.parkour.upgrade.minor; + +import io.github.a5h73y.parkour.upgrade.ParkourUpgrader; +import io.github.a5h73y.parkour.upgrade.TimedUpgradeTask; +import java.io.IOException; +import java.util.Set; +import org.bukkit.configuration.ConfigurationSection; +import org.bukkit.configuration.file.FileConfiguration; + +public class PartialUpgradeTask extends TimedUpgradeTask { + + private final String previousVersion; + + public PartialUpgradeTask(ParkourUpgrader parkourUpgrader, String previousVersion) { + super(parkourUpgrader); + this.previousVersion = previousVersion; + } + + @Override + protected String getTitle() { + return previousVersion + " Config"; + } + + @Override + protected boolean doWork() { + switch (previousVersion) { + case "6.0": + case "6.1": + convertPlayerParkourLevelConfig(); + convertCourseParkourLevelConfig(); + break; + + default: + break; + } + + return true; + } + + private void convertPlayerParkourLevelConfig() { + FileConfiguration playerConfig = getParkourUpgrader().getPlayerConfig(); + ConfigurationSection playerSection = playerConfig.getConfigurationSection(""); + + if (playerSection != null) { + Set playerIds = playerSection.getKeys(false); + for (String playerId : playerIds) { + if (playerConfig.contains(playerId + ".Level")) { + playerConfig.set(playerId + ".ParkourLevel", playerConfig.getInt(playerId + ".Level")); + playerConfig.set(playerId + ".Level", null); + } + if (playerConfig.contains(playerId + ".Rank")) { + playerConfig.set(playerId + ".ParkourRank", playerConfig.getString(playerId + ".Rank")); + playerConfig.set(playerId + ".Rank", null); + } + } + + try { + getParkourUpgrader().savePlayerConfig(); + } catch (IOException e) { + getParkourUpgrader().getLogger().severe("An error occurred during upgrade: " + e.getMessage()); + e.printStackTrace(); + } + } + } + + private void convertCourseParkourLevelConfig() { + FileConfiguration coursesConfig = getParkourUpgrader().getCoursesConfig(); + + for (String courseName : coursesConfig.getStringList("Courses")) { + if (coursesConfig.contains(courseName + ".Level")) { + coursesConfig.set(courseName + ".RewardLevel", coursesConfig.getInt(courseName + ".Level")); + coursesConfig.set(courseName + ".Level", null); + } + + if (coursesConfig.contains(courseName + ".LevelAdd")) { + coursesConfig.set(courseName + ".RewardLevelAdd", coursesConfig.getInt(courseName + ".LevelAdd")); + coursesConfig.set(courseName + ".LevelAdd", null); + } + } + + try { + getParkourUpgrader().saveCoursesConfig(); + } catch (IOException e) { + getParkourUpgrader().getLogger().severe("An error occurred during upgrade: " + e.getMessage()); + e.printStackTrace(); + } + } +}