Mostly finishes rewards

This commit is contained in:
2023-07-10 17:51:59 +02:00
parent d7b1695cd9
commit 9dff407713
21 changed files with 762 additions and 31 deletions

View File

@@ -2,9 +2,12 @@ package net.knarcraft.minigames.arena;
import net.knarcraft.knarlib.formatting.StringFormatter;
import net.knarcraft.minigames.MiniGames;
import net.knarcraft.minigames.arena.reward.RewardCondition;
import net.knarcraft.minigames.config.MiniGameMessage;
import net.knarcraft.minigames.property.RecordResult;
import net.knarcraft.minigames.property.RecordType;
import net.knarcraft.minigames.util.PlayerTeleporter;
import net.knarcraft.minigames.util.RewardHelper;
import org.bukkit.Location;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
@@ -59,9 +62,9 @@ public abstract class AbstractArenaSession implements ArenaSession {
* Announces a record set by this player
*
* @param recordResult <p>The result of the record</p>
* @param type <p>The type of record set (time or deaths)</p>
* @param recordType <p>The type of record set (time or deaths)</p>
*/
protected void announceRecord(@NotNull RecordResult recordResult, @NotNull String type) {
protected void announceRecord(@NotNull RecordResult recordResult, @NotNull RecordType recordType) {
if (recordResult == RecordResult.NONE) {
return;
}
@@ -75,11 +78,15 @@ public abstract class AbstractArenaSession implements ArenaSession {
default -> throw new IllegalStateException("Unexpected value: " + recordResult);
};
StringFormatter stringFormatter = MiniGames.getInstance().getStringFormatter();
String recordInfo = stringFormatter.replacePlaceholder(recordInfoMiniGameMessage, "{recordType}", type);
String recordInfo = stringFormatter.replacePlaceholder(recordInfoMiniGameMessage, "{recordType}",
recordType.name().toLowerCase().replace("_", " "));
stringFormatter.displaySuccessMessage(player, stringFormatter.replacePlaceholders(
MiniGameMessage.SUCCESS_RECORD_ACHIEVED, new String[]{"{gameMode}", "{recordInfo}"},
new String[]{gameModeString, recordInfo}));
// Reward the player
rewardRecord(recordResult, recordType);
}
/**
@@ -88,8 +95,32 @@ public abstract class AbstractArenaSession implements ArenaSession {
protected void registerRecord() {
ArenaRecordsRegistry recordsRegistry = this.arena.getData().getRecordRegistries().get(this.gameMode);
long timeElapsed = System.currentTimeMillis() - this.startTime;
announceRecord(recordsRegistry.registerTimeRecord(this.player.getUniqueId(), timeElapsed), "time");
announceRecord(recordsRegistry.registerDeathRecord(this.player.getUniqueId(), this.deaths), "least deaths");
announceRecord(recordsRegistry.registerTimeRecord(this.player.getUniqueId(), timeElapsed), RecordType.TIME);
announceRecord(recordsRegistry.registerDeathRecord(this.player.getUniqueId(), this.deaths), RecordType.DEATHS);
}
/**
* Rewards the specified achieved record
*
* @param recordResult <p>The result of the record achieved</p>
* @param recordType <p>The type of record achieved</p>
*/
protected void rewardRecord(RecordResult recordResult, RecordType recordType) {
RewardCondition condition = null;
if (recordResult == RecordResult.WORLD_RECORD) {
if (recordType == RecordType.DEATHS) {
condition = RewardCondition.GLOBAL_DEATH_RECORD;
} else if (recordType == RecordType.TIME) {
condition = RewardCondition.GLOBAL_TIME_RECORD;
}
} else if (recordResult == RecordResult.PERSONAL_BEST) {
if (recordType == RecordType.DEATHS) {
condition = RewardCondition.PERSONAL_DEATH_RECORD;
} else if (recordType == RecordType.TIME) {
condition = RewardCondition.PERSONAL_TIME_RECORD;
}
}
RewardHelper.grantRewards(this.player, this.arena.getRewards(condition));
}
/**

View File

@@ -1,10 +1,13 @@
package net.knarcraft.minigames.arena;
import net.knarcraft.minigames.arena.reward.Reward;
import net.knarcraft.minigames.arena.reward.RewardCondition;
import org.bukkit.Location;
import org.bukkit.block.Block;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.Set;
import java.util.UUID;
/**
@@ -91,4 +94,27 @@ public interface Arena {
*/
@Nullable Location getExitLocation();
/**
* Adds a reward to this arena
*
* @param rewardCondition <p>The condition for the reward to be granted</p>
* @param reward <p>The reward to be granted</p>
*/
void addReward(@NotNull RewardCondition rewardCondition, @NotNull Reward reward);
/**
* Clears this arena's rewards for the given condition
*
* @param rewardCondition <p>The reward condition to clear all rewards for</p>
*/
void clearRewards(@NotNull RewardCondition rewardCondition);
/**
* Gets all rewards for the given reward condition
*
* @param rewardCondition <p>The condition to get the rewards for</p>
* @return <p>All rewards</p>
*/
@NotNull Set<Reward> getRewards(RewardCondition rewardCondition);
}

View File

@@ -4,6 +4,8 @@ import net.knarcraft.minigames.MiniGames;
import net.knarcraft.minigames.arena.Arena;
import net.knarcraft.minigames.arena.ArenaGameMode;
import net.knarcraft.minigames.arena.ArenaRecordsRegistry;
import net.knarcraft.minigames.arena.reward.Reward;
import net.knarcraft.minigames.arena.reward.RewardCondition;
import net.knarcraft.minigames.config.DropperConfiguration;
import net.knarcraft.minigames.util.DropperArenaStorageHelper;
import net.knarcraft.minigames.util.StringSanitizer;
@@ -15,7 +17,9 @@ import org.jetbrains.annotations.Nullable;
import java.io.IOException;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import static net.knarcraft.minigames.util.InputValidationHelper.isInvalid;
@@ -70,6 +74,8 @@ public class DropperArena implements Arena {
private final DropperArenaHandler dropperArenaHandler;
private Map<RewardCondition, Set<Reward>> rewards = new HashMap<>();
private static final DropperConfiguration dropperConfiguration = MiniGames.getInstance().getDropperConfiguration();
/**
@@ -82,13 +88,14 @@ public class DropperArena implements Arena {
* @param playerVerticalVelocity <p>The velocity to use for players' vertical velocity</p>
* @param playerHorizontalVelocity <p>The velocity to use for players' horizontal velocity (-1 to 1)</p>
* @param winBlockType <p>The material of the block players have to hit to win this dropper arena</p>
* @param rewards <p>The rewards given by this arena</p>
* @param dropperArenaData <p>The arena data keeping track of which players have done what in this arena</p>
* @param arenaHandler <p>The arena handler used for saving any changes</p>
*/
public DropperArena(@NotNull UUID arenaId, @NotNull String arenaName, @NotNull Location spawnLocation,
@Nullable Location exitLocation, double playerVerticalVelocity, float playerHorizontalVelocity,
@NotNull Material winBlockType, @NotNull DropperArenaData dropperArenaData,
@NotNull DropperArenaHandler arenaHandler) {
@NotNull Material winBlockType, @NotNull Map<RewardCondition, Set<Reward>> rewards,
@NotNull DropperArenaData dropperArenaData, @NotNull DropperArenaHandler arenaHandler) {
this.arenaId = arenaId;
this.arenaName = arenaName;
this.spawnLocation = spawnLocation;
@@ -98,6 +105,7 @@ public class DropperArena implements Arena {
this.winBlockType = winBlockType;
this.dropperArenaData = dropperArenaData;
this.dropperArenaHandler = arenaHandler;
this.rewards = rewards;
}
/**
@@ -155,6 +163,28 @@ public class DropperArena implements Arena {
return this.exitLocation != null ? this.exitLocation.clone() : null;
}
@Override
public void addReward(@NotNull RewardCondition rewardCondition, @NotNull Reward reward) {
this.rewards.computeIfAbsent(rewardCondition, k -> new HashSet<>());
this.rewards.get(rewardCondition).add(reward);
this.dropperArenaHandler.saveArenas();
}
@Override
public void clearRewards(@NotNull RewardCondition rewardCondition) {
this.rewards.remove(rewardCondition);
this.dropperArenaHandler.saveArenas();
}
@Override
public @NotNull Set<Reward> getRewards(RewardCondition rewardCondition) {
if (this.rewards.containsKey(rewardCondition) && this.rewards.get(rewardCondition) != null) {
return this.rewards.get(rewardCondition);
} else {
return new HashSet<>();
}
}
/**
* Gets the vertical velocity for players in this arena
*
@@ -237,7 +267,7 @@ public class DropperArena implements Arena {
return false;
} else {
this.spawnLocation = newLocation;
dropperArenaHandler.saveArenas();
this.dropperArenaHandler.saveArenas();
return true;
}
}
@@ -253,7 +283,7 @@ public class DropperArena implements Arena {
return false;
} else {
this.exitLocation = newLocation;
dropperArenaHandler.saveArenas();
this.dropperArenaHandler.saveArenas();
return true;
}
}
@@ -269,8 +299,8 @@ public class DropperArena implements Arena {
String oldName = this.getArenaNameSanitized();
this.arenaName = arenaName;
// Update the arena lookup map to make sure the new name can be used immediately
dropperArenaHandler.updateLookupName(oldName, this.getArenaNameSanitized());
dropperArenaHandler.saveArenas();
this.dropperArenaHandler.updateLookupName(oldName, this.getArenaNameSanitized());
this.dropperArenaHandler.saveArenas();
return true;
} else {
return false;
@@ -290,7 +320,7 @@ public class DropperArena implements Arena {
return false;
} else {
this.winBlockType = material;
dropperArenaHandler.saveArenas();
this.dropperArenaHandler.saveArenas();
return true;
}
}
@@ -308,7 +338,7 @@ public class DropperArena implements Arena {
return false;
} else {
this.playerHorizontalVelocity = horizontalVelocity;
dropperArenaHandler.saveArenas();
this.dropperArenaHandler.saveArenas();
return true;
}
}
@@ -324,7 +354,7 @@ public class DropperArena implements Arena {
return false;
} else {
this.playerVerticalVelocity = verticalVelocity;
dropperArenaHandler.saveArenas();
this.dropperArenaHandler.saveArenas();
return true;
}
}

View File

@@ -4,10 +4,12 @@ import net.knarcraft.knarlib.formatting.StringFormatter;
import net.knarcraft.minigames.MiniGames;
import net.knarcraft.minigames.arena.AbstractArenaSession;
import net.knarcraft.minigames.arena.PlayerEntryState;
import net.knarcraft.minigames.arena.reward.RewardCondition;
import net.knarcraft.minigames.config.MiniGameMessage;
import net.knarcraft.minigames.gui.ArenaGUI;
import net.knarcraft.minigames.gui.DropperGUI;
import net.knarcraft.minigames.util.PlayerTeleporter;
import net.knarcraft.minigames.util.RewardHelper;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
@@ -98,8 +100,10 @@ public class DropperArenaSession extends AbstractArenaSession {
// Mark the arena as cleared
if (this.arena.getData().setCompleted(this.gameMode, this.player)) {
stringFormatter.displaySuccessMessage(this.player, MiniGameMessage.SUCCESS_ARENA_FIRST_CLEAR);
RewardHelper.grantRewards(this.player, this.arena.getRewards(RewardCondition.FIRST_WIN));
}
stringFormatter.displaySuccessMessage(this.player, MiniGameMessage.SUCCESS_ARENA_WIN);
RewardHelper.grantRewards(this.player, this.arena.getRewards(RewardCondition.WIN));
// Teleport the player out of the arena
teleportToExit(false);

View File

@@ -43,9 +43,14 @@ public enum DropperArenaStorageKey {
WIN_BLOCK_TYPE("winBlockType"),
/**
* The hey for this arena's data
* The key for this arena's data
*/
DATA("arenaData"),
/**
* The key for this arena's rewards
*/
REWARDS("rewards"),
;
private final @NotNull String key;

View File

@@ -5,6 +5,8 @@ import net.knarcraft.minigames.MiniGames;
import net.knarcraft.minigames.arena.Arena;
import net.knarcraft.minigames.arena.ArenaGameMode;
import net.knarcraft.minigames.arena.ArenaRecordsRegistry;
import net.knarcraft.minigames.arena.reward.Reward;
import net.knarcraft.minigames.arena.reward.RewardCondition;
import net.knarcraft.minigames.util.ParkourArenaStorageHelper;
import net.knarcraft.minigames.util.StringSanitizer;
import org.bukkit.Location;
@@ -82,21 +84,27 @@ public class ParkourArena implements Arena {
private final @NotNull ParkourArenaHandler parkourArenaHandler;
private Map<RewardCondition, Set<Reward>> rewards = new HashMap<>();
/**
* Instantiates a new parkour arena
*
* @param arenaId <p>The id of the arena</p>
* @param arenaName <p>The name of the arena</p>
* @param spawnLocation <p>The location players spawn in when entering the arena</p>
* @param exitLocation <p>The location the players are teleported to when exiting the arena, or null</p>
* @param winBlockType <p>The material of the block players have to hit to win this parkour arena</p>
* @param winLocation <p>The location a player has to reach to win this arena</p>
* @param parkourArenaData <p>The arena data keeping track of which players have done what in this arena</p>
* @param arenaHandler <p>The arena handler used for saving any changes</p>
* @param arenaId <p>The id of the arena</p>
* @param arenaName <p>The name of the arena</p>
* @param spawnLocation <p>The location players spawn in when entering the arena</p>
* @param exitLocation <p>The location the players are teleported to when exiting the arena, or null</p>
* @param winBlockType <p>The material of the block players have to hit to win this parkour arena</p>
* @param winLocation <p>The location a player has to reach to win this arena</p>
* @param killPlaneBlockNames <p>The names of the type of blocks</p>
* @param checkpoints <p>The checkpoints set for this arena</p>
* @param rewards <p>The rewards given by this arena</p>
* @param parkourArenaData <p>The arena data keeping track of which players have done what in this arena</p>
* @param arenaHandler <p>The arena handler used for saving any changes</p>
*/
public ParkourArena(@NotNull UUID arenaId, @NotNull String arenaName, @NotNull Location spawnLocation,
@Nullable Location exitLocation, @NotNull Material winBlockType, @Nullable Location winLocation,
@Nullable Set<String> killPlaneBlockNames, @NotNull List<Location> checkpoints,
@NotNull Map<RewardCondition, Set<Reward>> rewards,
@NotNull ParkourArenaData parkourArenaData, @NotNull ParkourArenaHandler arenaHandler) {
this.arenaId = arenaId;
this.arenaName = arenaName;
@@ -110,6 +118,7 @@ public class ParkourArena implements Arena {
this.checkpoints = checkpoints;
this.parkourArenaData = parkourArenaData;
this.parkourArenaHandler = arenaHandler;
this.rewards = rewards;
}
/**
@@ -167,6 +176,28 @@ public class ParkourArena implements Arena {
return this.exitLocation;
}
@Override
public void addReward(@NotNull RewardCondition rewardCondition, @NotNull Reward reward) {
this.rewards.computeIfAbsent(rewardCondition, k -> new HashSet<>());
this.rewards.get(rewardCondition).add(reward);
this.parkourArenaHandler.saveArenas();
}
@Override
public void clearRewards(@NotNull RewardCondition rewardCondition) {
this.rewards.remove(rewardCondition);
this.parkourArenaHandler.saveArenas();
}
@Override
public @NotNull Set<Reward> getRewards(RewardCondition rewardCondition) {
if (this.rewards.containsKey(rewardCondition)) {
return this.rewards.get(rewardCondition);
} else {
return new HashSet<>();
}
}
/**
* Gets the type of block a player has to hit to win this arena
*

View File

@@ -1,12 +1,15 @@
package net.knarcraft.minigames.arena.parkour;
import net.knarcraft.knarlib.formatting.StringFormatter;
import net.knarcraft.minigames.MiniGames;
import net.knarcraft.minigames.arena.AbstractArenaSession;
import net.knarcraft.minigames.arena.PlayerEntryState;
import net.knarcraft.minigames.arena.reward.RewardCondition;
import net.knarcraft.minigames.config.MiniGameMessage;
import net.knarcraft.minigames.gui.ArenaGUI;
import net.knarcraft.minigames.gui.ParkourGUI;
import net.knarcraft.minigames.util.PlayerTeleporter;
import net.knarcraft.minigames.util.RewardHelper;
import org.bukkit.Location;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
@@ -87,13 +90,15 @@ public class ParkourArenaSession extends AbstractArenaSession {
registerRecord();
}
StringFormatter stringFormatter = MiniGames.getInstance().getStringFormatter();
// Mark the arena as cleared
if (this.arena.getData().setCompleted(this.gameMode, this.player)) {
MiniGames.getInstance().getStringFormatter().displaySuccessMessage(this.player,
MiniGameMessage.SUCCESS_ARENA_FIRST_CLEAR);
stringFormatter.displaySuccessMessage(this.player, MiniGameMessage.SUCCESS_ARENA_FIRST_CLEAR);
RewardHelper.grantRewards(this.player, this.arena.getRewards(RewardCondition.FIRST_WIN));
}
MiniGames.getInstance().getStringFormatter().displaySuccessMessage(this.player,
MiniGameMessage.SUCCESS_ARENA_WIN);
stringFormatter.displaySuccessMessage(this.player, MiniGameMessage.SUCCESS_ARENA_WIN);
RewardHelper.grantRewards(this.player, this.arena.getRewards(RewardCondition.WIN));
// Teleport the player out of the arena
teleportToExit(false);

View File

@@ -51,6 +51,11 @@ public enum ParkourArenaStorageKey {
* The hey for this arena's data
*/
DATA("arenaData"),
/**
* The key for this arena's rewards
*/
REWARDS("rewards"),
;
private final @NotNull String key;

View File

@@ -0,0 +1,83 @@
package net.knarcraft.minigames.arena.reward;
import org.bukkit.configuration.serialization.ConfigurationSerializable;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
/**
* The condition for granting a reward
*/
public enum RewardCondition implements ConfigurationSerializable {
/**
* The reward is granted each time the player wins/clears the arena
*/
WIN,
/**
* The reward is granted the first time the player wins/clears the arena
*/
FIRST_WIN,
/**
* The reward is granted if the player beats their personal least deaths record
*/
PERSONAL_DEATH_RECORD,
/**
* The reward is granted if the player beats their personal least time record
*/
PERSONAL_TIME_RECORD,
/**
* The reward is granted if the player beats the global least deaths record
*/
GLOBAL_DEATH_RECORD,
/**
* The reward is granted if the player beats the global least time record
*/
GLOBAL_TIME_RECORD,
;
/**
* Gets a reward condition from the given string
*
* @param condition <p>The string specifying a reward condition</p>
* @return <p>The matching reward condition, or null if not found</p>
*/
public static @Nullable RewardCondition getFromString(@NotNull String condition) {
for (RewardCondition rewardCondition : RewardCondition.values()) {
if (rewardCondition.name().equalsIgnoreCase(condition.replace("-", "_"))) {
return rewardCondition;
}
}
return null;
}
@NotNull
@Override
public Map<String, Object> serialize() {
Map<String, Object> data = new HashMap<>();
data.put("condition", this.name());
return data;
}
/**
* Deserializes a reward condition from the given data
*
* @param data <p>The data to deserialize</p>
* @return <p>The deserialized reward condition</p>
*/
@SuppressWarnings({"unused"})
public static @NotNull RewardCondition deserialize(@NotNull Map<String, Object> data) {
RewardCondition rewardCondition = getFromString(String.valueOf(data.get("condition")));
return Objects.requireNonNullElse(rewardCondition, RewardCondition.FIRST_WIN);
}
}

View File

@@ -0,0 +1,70 @@
package net.knarcraft.minigames.arena.reward;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
/**
* The type of a specific reward
*/
public enum RewardType {
/**
* A command reward
*/
COMMAND(CommandReward.class),
/**
* An economy reward
*/
ECONOMY(EconomyReward.class),
/**
* An item reward
*/
ITEM(ItemReward.class),
/**
* A permission reward
*/
PERMISSION(PermissionReward.class),
;
private final Class<?> classType;
RewardType(Class<?> classType) {
this.classType = classType;
}
/**
* Gets the type of reward the given object represents
*
* @param object <p>A reward object</p>
* @return <p>The reward type of the given object, or null if not recognized</p>
*/
public static <K extends Reward> @Nullable RewardType getFromObject(@NotNull K object) {
for (RewardType rewardType : RewardType.values()) {
if (object.getClass() == rewardType.classType) {
return rewardType;
}
}
return null;
}
/**
* Gets a reward type from the given string
*
* @param condition <p>The string specifying a reward type</p>
* @return <p>The matching reward type, or null if not found</p>
*/
public static RewardType getFromString(@NotNull String condition) {
for (RewardType rewardType : RewardType.values()) {
if (rewardType.name().equalsIgnoreCase(condition.replace("-", "_"))) {
return rewardType;
}
}
return null;
}
}