Skip to content

Commit

Permalink
Removed race condition of rewardonce
Browse files Browse the repository at this point in the history
Rewardonce check changed to use course-completions file
Fixed Course defaults
Fixed not resetting Players completed courses
  • Loading branch information
A5H73Y committed Jun 25, 2022
1 parent 26fc09c commit e153283
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@
import static io.github.a5h73y.parkour.type.course.CourseConfig.DIE_IN_VOID;
import static io.github.a5h73y.parkour.type.course.CourseConfig.HAS_FALL_DAMAGE;
import static io.github.a5h73y.parkour.type.course.CourseConfig.JOIN_ITEMS;
import static io.github.a5h73y.parkour.type.course.CourseConfig.MANUAL_CHECKPOINTS;
import static io.github.a5h73y.parkour.type.course.CourseConfig.MAX_DEATHS;
import static io.github.a5h73y.parkour.type.course.CourseConfig.MAX_FALL_TICKS;
import static io.github.a5h73y.parkour.type.course.CourseConfig.MAX_TIME;
import static io.github.a5h73y.parkour.type.course.CourseConfig.REWARD_DELAY;
import static io.github.a5h73y.parkour.type.course.CourseConfig.REWARD_LEVEL_ADD;
import static io.github.a5h73y.parkour.type.course.CourseConfig.REWARD_ONCE;
Expand Down Expand Up @@ -122,10 +125,14 @@ public DefaultConfig(File file) {

this.setDefault("OnServerRestart.KickPlayerFromCourse", false);

this.setDefault("CourseDefault.Settings." + HAS_FALL_DAMAGE, true);
this.setDefault("CourseDefault.Settings." + MAX_FALL_TICKS, 80);
this.setDefault("CourseDefault.Settings." + DIE_IN_LIQUID, false);
this.setDefault("CourseDefault.Settings." + DIE_IN_VOID, false);
this.setDefault("CourseDefault.Settings." + HAS_FALL_DAMAGE, true);
this.setDefault("CourseDefault.Settings." + MANUAL_CHECKPOINTS, false);
this.setDefault("CourseDefault.Settings." + MAX_FALL_TICKS, 80);
this.setDefault("CourseDefault.Settings." + MAX_DEATHS, 0);
this.setDefault("CourseDefault.Settings." + MAX_TIME, 0);

this.setDefault("CourseDefault.Settings." + REWARD_ONCE, false);
this.setDefault("CourseDefault.Settings." + REWARD_DELAY, 0);
this.setDefault("CourseDefault.Settings." + REWARD_LEVEL_ADD, 0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import de.leonhard.storage.Json;
import de.leonhard.storage.internal.FileType;
import de.leonhard.storage.internal.serialize.SimplixSerializer;
import de.leonhard.storage.util.ClassWrapper;
import io.github.a5h73y.parkour.Parkour;
import io.github.a5h73y.parkour.type.player.ParkourMode;
import io.github.a5h73y.parkour.utility.MaterialUtils;
Expand Down Expand Up @@ -62,14 +63,15 @@ public class CourseConfig extends Json {
public static final String REWARD_ONCE = "RewardOnce";
public static final String REWARD_PARKOINS = "RewardParkoins";

private static final String CHECKPOINTS = "Checkpoints";
private static final String COMMAND_PREFIX = "Command.";
private static final String MESSAGE_PREFIX = "Message.";
public static final String VIEWS = "Views";
public static final String PRIZE_MATERIAL = "Prize.Material";
public static final String PRIZE_AMOUNT = "Prize.Amount";

private String courseName;
private static final String CHECKPOINTS = "Checkpoints";
private static final String COMMAND_PREFIX = "Command.";
private static final String MESSAGE_PREFIX = "Message.";

private final String courseName;

public CourseConfig(String courseName, File file) {
super(file);
Expand Down Expand Up @@ -1086,4 +1088,11 @@ public static void displayCourseInfo(@NotNull CommandSender commandSender,
}
}
}

public <T> T getCourseSettingOrDefault(String key, T def) {
Object raw = this.get(key);
return raw == null
? Parkour.getDefaultConfig().get("CourseDefault.Settings." + key, def)
: ClassWrapper.getFromDef(raw, def);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -81,16 +81,16 @@ public CourseSettings getCourseSettings(String courseName) {
courseConfig.get(DIE_IN_LIQUID, defaultSection.getBoolean(DIE_IN_LIQUID)));
courseSettings.setDieInVoid(
courseConfig.get(DIE_IN_VOID, defaultSection.getBoolean(DIE_IN_VOID)));
courseSettings.setHasFallDamage(
courseConfig.get(HAS_FALL_DAMAGE, defaultSection.getBoolean(HAS_FALL_DAMAGE)));
courseSettings.setManualCheckpoints(
courseConfig.get(MANUAL_CHECKPOINTS, defaultSection.getBoolean(MANUAL_CHECKPOINTS)));
courseSettings.setMaxFallTicks(
courseConfig.get(MAX_FALL_TICKS, defaultSection.getInt(MAX_FALL_TICKS)));
courseSettings.setMaxDeaths(
courseConfig.get(MAX_DEATHS, defaultSection.getInt(MAX_DEATHS)));
courseSettings.setMaxFallTicks(
courseConfig.get(MAX_FALL_TICKS, defaultSection.getInt(MAX_FALL_TICKS)));
courseSettings.setMaxTime(
courseConfig.get(MAX_TIME, defaultSection.getInt(MAX_TIME)));
courseSettings.setHasFallDamage(
courseConfig.get(HAS_FALL_DAMAGE, defaultSection.getBoolean(HAS_FALL_DAMAGE)));

return courseSettings;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,13 @@
import io.github.a5h73y.parkour.utility.permission.Permission;
import io.github.a5h73y.parkour.utility.permission.PermissionUtils;
import io.github.a5h73y.parkour.utility.time.DateTimeUtils;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Effect;
Expand Down Expand Up @@ -522,6 +520,7 @@ public void finishCourse(final Player player) {
teleportCourseCompletion(player, courseName);
}
playerConfig.resetPlayerDataSnapshot();
parkour.getConfigManager().getCourseCompletionsConfig().addCompletedCourse(player, courseName);
}, parkour.getParkourConfig().getLong("OnFinish.TeleportDelay"));

playerConfig.setLastCompletedCourse(courseName);
Expand All @@ -531,8 +530,6 @@ public void finishCourse(final Player player) {
parkour.getParkourSessionManager().forceVisible(player);
parkour.getParkourSessionManager().deleteParkourSession(player, courseName);
parkour.getCourseManager().runEventCommands(player, session, FINISH);

parkour.getConfigManager().getCourseCompletionsConfig().addCompletedCourse(player, courseName);
parkour.getConfigManager().getCourseConfig(courseName).incrementCompletions();
}

Expand Down Expand Up @@ -592,18 +589,18 @@ public void rewardPrize(Player player, ParkourSession session) {
}

CourseConfig courseConfig = parkour.getConfigManager().getCourseConfig(courseName);
boolean rewardOnce = courseConfig.getCourseSettingOrDefault(CourseConfig.REWARD_ONCE, false);

if (courseConfig.getRewardOnce() && parkour.getDatabaseManager().hasPlayerAchievedTime(player, courseName)) {
if (rewardOnce && parkour.getConfigManager().getCourseCompletionsConfig().hasCompletedCourse(player, courseName)) {
parkour.getCourseManager().runEventCommands(player, session, NO_PRIZE);
return;
}

// check if the Course has a reward delay
if (courseConfig.hasRewardDelay()) {
// if the player has not exceeded the Course delay, no prize will be given
if (!hasPrizeCooldownDurationPassed(player, courseName, true)) {
return;
}
// if the player has not exceeded the Course delay, no prize will be given
if (!hasPrizeCooldownDurationPassed(player, courseName, true)) {
return;

} else {
// otherwise, make a note of last time rewarded, and let them continue
parkour.getConfigManager().getPlayerConfig(player)
.setLastRewardedTime(courseName, System.currentTimeMillis());
Expand Down Expand Up @@ -706,7 +703,8 @@ private void submitPlayerLeaderboard(Player player, ParkourSession session) {
* @return course prize cooldown passed
*/
public boolean hasPrizeCooldownDurationPassed(Player player, String courseName, boolean displayMessage) {
double rewardDelay = parkour.getConfigManager().getCourseConfig(courseName).getRewardDelay();
double rewardDelay = parkour.getConfigManager().getCourseConfig(courseName)
.getCourseSettingOrDefault(CourseConfig.REWARD_DELAY, 0);

if (rewardDelay <= 0) {
return true;
Expand Down Expand Up @@ -765,22 +763,22 @@ public void prepareParkourPlayer(Player player) {
* @param player player
*/
public void restoreInventoryArmor(Player player, PlayerConfig playerConfig) {
if (!parkour.getParkourConfig().getBoolean("Other.Parkour.InventoryManagement")) {
return;
}

ItemStack[] inventoryContents = playerConfig.getSnapshotInventory();
if (parkour.getParkourConfig().getBoolean("Other.Parkour.InventoryManagement")) {
ItemStack[] inventoryContents = playerConfig.getSnapshotInventory();

if (inventoryContents == null) {
TranslationUtils.sendMessage(player, "No saved inventory to load.");
return;
}
if (inventoryContents == null) {
TranslationUtils.sendMessage(player, "No saved inventory to load.");
return;
}

player.getInventory().clear();
player.getInventory().setContents(inventoryContents);
player.getInventory().clear();
player.getInventory().setContents(inventoryContents);

ItemStack[] armorContents = playerConfig.getSnapshotArmor();
player.getInventory().setArmorContents(armorContents);
ItemStack[] armorContents = playerConfig.getSnapshotArmor();
player.getInventory().setArmorContents(armorContents);
} else {
// TODO attempt to remove the Parkour Tools
}
player.updateInventory();
}

Expand Down Expand Up @@ -1049,6 +1047,7 @@ public void resetPlayer(CommandSender commandSender, String targetPlayerId) {
*/
public void resetPlayer(OfflinePlayer targetPlayer) {
PlayerConfig.deletePlayerData(targetPlayer);
parkour.getConfigManager().getCourseCompletionsConfig().removePlayer(targetPlayer);
parkour.getParkourSessionManager().deleteParkourSessions(targetPlayer);
parkour.getDatabaseManager().deletePlayerTimes(targetPlayer);
parkour.getPlaceholderApi().clearCache();
Expand Down Expand Up @@ -1386,7 +1385,7 @@ private void rewardParkourLevel(Player player, String courseName) {
}

// increase parkour level
int rewardAddLevel = courseConfig.getRewardParkourLevelIncrease();
int rewardAddLevel = courseConfig.getCourseSettingOrDefault(CourseConfig.REWARD_LEVEL_ADD, 0);
if (rewardAddLevel > 0) {
newParkourLevel = currentLevel + rewardAddLevel;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,12 @@ public void removeCompletedCourse(String courseName) {
}
}
}

/**
* Remove the Player's completed courses.
* @param targetPlayer target player
*/
public void removePlayer(OfflinePlayer targetPlayer) {
this.remove(Parkour.getDefaultConfig().getPlayerConfigName(targetPlayer));
}
}

0 comments on commit e153283

Please sign in to comment.