Removes usage of SerializableUUID outside of serialization

This commit is contained in:
Kristian Knarvik 2023-03-29 02:07:00 +02:00
parent 21425f73a1
commit 45bdb3f2a6
3 changed files with 57 additions and 31 deletions

View File

@ -20,7 +20,7 @@ import java.util.UUID;
* @param playersCompleted <p>A list of all player that have completed this arena</p>
*/
public record DropperArenaData(@NotNull UUID arenaId, @NotNull DropperArenaRecordsRegistry recordsRegistry,
@NotNull Set<SerializableUUID> playersCompleted) implements ConfigurationSerializable {
@NotNull Set<UUID> playersCompleted) implements ConfigurationSerializable {
/**
* Instantiates a new dropper arena data object
@ -30,7 +30,7 @@ public record DropperArenaData(@NotNull UUID arenaId, @NotNull DropperArenaRecor
* @param playersCompleted <p>The set of ids for players that have cleared this data's arena</p>
*/
public DropperArenaData(@NotNull UUID arenaId, @NotNull DropperArenaRecordsRegistry recordsRegistry,
@NotNull Set<SerializableUUID> playersCompleted) {
@NotNull Set<UUID> playersCompleted) {
this.arenaId = arenaId;
this.recordsRegistry = recordsRegistry;
this.playersCompleted = new HashSet<>(playersCompleted);
@ -43,7 +43,7 @@ public record DropperArenaData(@NotNull UUID arenaId, @NotNull DropperArenaRecor
* @return <p>True if the player has cleared the arena this data belongs to</p>
*/
public boolean hasNotCompleted(@NotNull Player player) {
return !this.playersCompleted.contains(new SerializableUUID(player.getUniqueId()));
return !this.playersCompleted.contains(player.getUniqueId());
}
/**
@ -52,7 +52,7 @@ public record DropperArenaData(@NotNull UUID arenaId, @NotNull DropperArenaRecor
* @param player <p>The player that completed this data's arena</p>
*/
public boolean addCompleted(@NotNull Player player) {
boolean added = this.playersCompleted.add(new SerializableUUID(player.getUniqueId()));
boolean added = this.playersCompleted.add(player.getUniqueId());
// Persistently save the completion
if (added) {
Dropper.getInstance().getArenaHandler().saveData(this.arenaId);
@ -66,7 +66,12 @@ public record DropperArenaData(@NotNull UUID arenaId, @NotNull DropperArenaRecor
Map<String, Object> data = new HashMap<>();
data.put("arenaId", new SerializableUUID(this.arenaId));
data.put("recordsRegistry", this.recordsRegistry);
data.put("playersCompleted", this.playersCompleted);
Set<SerializableUUID> playersCompleted = new HashSet<>();
for (UUID playerCompleted : this.playersCompleted) {
playersCompleted.add(new SerializableUUID(playerCompleted));
}
data.put("playersCompleted", playersCompleted);
return data;
}
@ -80,7 +85,11 @@ public record DropperArenaData(@NotNull UUID arenaId, @NotNull DropperArenaRecor
public static @NotNull DropperArenaData deserialize(@NotNull Map<String, Object> data) {
SerializableUUID serializableUUID = (SerializableUUID) data.get("arenaId");
DropperArenaRecordsRegistry recordsRegistry = (DropperArenaRecordsRegistry) data.get("recordsRegistry");
Set<SerializableUUID> playersCompleted = (Set<SerializableUUID>) data.get("playersCompleted");
Set<SerializableUUID> playersCompletedData = (Set<SerializableUUID>) data.get("playersCompleted");
Set<UUID> playersCompleted = new HashSet<>();
for (SerializableUUID completedId : playersCompletedData) {
playersCompleted.add(completedId.uuid());
}
return new DropperArenaData(serializableUUID.uuid(), recordsRegistry, playersCompleted);
}

View File

@ -17,8 +17,8 @@ import java.util.stream.Stream;
public class DropperArenaRecordsRegistry implements ConfigurationSerializable {
private final UUID arenaId;
private final Map<SerializableUUID, Integer> leastDeaths;
private final Map<SerializableUUID, Long> shortestTimeMilliSeconds;
private final Map<UUID, Integer> leastDeaths;
private final Map<UUID, Long> shortestTimeMilliSeconds;
/**
* Instantiates a new empty records registry
@ -35,8 +35,8 @@ public class DropperArenaRecordsRegistry implements ConfigurationSerializable {
* @param leastDeaths <p>The existing least death records to use</p>
* @param shortestTimeMilliSeconds <p>The existing leash time records to use</p>
*/
public DropperArenaRecordsRegistry(@NotNull UUID arenaId, @NotNull Map<SerializableUUID, Integer> leastDeaths,
@NotNull Map<SerializableUUID, Long> shortestTimeMilliSeconds) {
private DropperArenaRecordsRegistry(@NotNull UUID arenaId, @NotNull Map<UUID, Integer> leastDeaths,
@NotNull Map<UUID, Long> shortestTimeMilliSeconds) {
this.arenaId = arenaId;
this.leastDeaths = new HashMap<>(leastDeaths);
this.shortestTimeMilliSeconds = new HashMap<>(shortestTimeMilliSeconds);
@ -47,7 +47,7 @@ public class DropperArenaRecordsRegistry implements ConfigurationSerializable {
*
* @return <p>Existing death records</p>
*/
public Map<SerializableUUID, Integer> getLeastDeathsRecords() {
public Map<UUID, Integer> getLeastDeathsRecords() {
return new HashMap<>(this.leastDeaths);
}
@ -56,7 +56,7 @@ public class DropperArenaRecordsRegistry implements ConfigurationSerializable {
*
* @return <p>Existing time records</p>
*/
public Map<SerializableUUID, Long> getShortestTimeMilliSecondsRecords() {
public Map<UUID, Long> getShortestTimeMilliSecondsRecords() {
return new HashMap<>(this.shortestTimeMilliSeconds);
}
@ -69,22 +69,21 @@ public class DropperArenaRecordsRegistry implements ConfigurationSerializable {
*/
public @NotNull RecordResult registerDeathRecord(@NotNull UUID playerId, int deaths) {
RecordResult result;
Stream<Map.Entry<SerializableUUID, Integer>> records = leastDeaths.entrySet().stream();
SerializableUUID serializableUUID = new SerializableUUID(playerId);
Stream<Map.Entry<UUID, Integer>> records = leastDeaths.entrySet().stream();
if (records.allMatch((entry) -> deaths < entry.getValue())) {
// If the given value is less than all other values, that's a world record!
result = RecordResult.WORLD_RECORD;
leastDeaths.put(serializableUUID, deaths);
leastDeaths.put(playerId, deaths);
save();
} else if (leastDeaths.containsKey(serializableUUID) && deaths < leastDeaths.get(serializableUUID)) {
} else if (leastDeaths.containsKey(playerId) && deaths < leastDeaths.get(playerId)) {
// If the given value is less than the player's previous value, that's a personal best!
result = RecordResult.PERSONAL_BEST;
leastDeaths.put(serializableUUID, deaths);
leastDeaths.put(playerId, deaths);
save();
} else {
// Make sure to save the record if this is the user's first attempt
if (!leastDeaths.containsKey(serializableUUID)) {
if (!leastDeaths.containsKey(playerId)) {
save();
}
result = RecordResult.NONE;
@ -102,23 +101,22 @@ public class DropperArenaRecordsRegistry implements ConfigurationSerializable {
*/
public @NotNull RecordResult registerTimeRecord(@NotNull UUID playerId, long milliseconds) {
RecordResult result;
Stream<Map.Entry<SerializableUUID, Long>> records = shortestTimeMilliSeconds.entrySet().stream();
SerializableUUID serializableUUID = new SerializableUUID(playerId);
Stream<Map.Entry<UUID, Long>> records = shortestTimeMilliSeconds.entrySet().stream();
if (records.allMatch((entry) -> milliseconds < entry.getValue())) {
//If the given value is less than all other values, that's a world record!
result = RecordResult.WORLD_RECORD;
shortestTimeMilliSeconds.put(serializableUUID, milliseconds);
shortestTimeMilliSeconds.put(playerId, milliseconds);
save();
} else if (shortestTimeMilliSeconds.containsKey(serializableUUID) &&
milliseconds < shortestTimeMilliSeconds.get(serializableUUID)) {
} else if (shortestTimeMilliSeconds.containsKey(playerId) &&
milliseconds < shortestTimeMilliSeconds.get(playerId)) {
//If the given value is less than the player's previous value, that's a personal best!
result = RecordResult.PERSONAL_BEST;
shortestTimeMilliSeconds.put(serializableUUID, milliseconds);
shortestTimeMilliSeconds.put(playerId, milliseconds);
save();
} else {
// Make sure to save the record if this is the user's first attempt
if (!shortestTimeMilliSeconds.containsKey(serializableUUID)) {
if (!shortestTimeMilliSeconds.containsKey(playerId)) {
save();
}
result = RecordResult.NONE;
@ -139,8 +137,18 @@ public class DropperArenaRecordsRegistry implements ConfigurationSerializable {
public Map<String, Object> serialize() {
Map<String, Object> data = new HashMap<>();
data.put("arenaId", new SerializableUUID(this.arenaId));
data.put("leastDeaths", this.leastDeaths);
data.put("shortestTime", this.shortestTimeMilliSeconds);
Map<SerializableUUID, Integer> leastDeaths = new HashMap<>();
for (Map.Entry<UUID, Integer> entry : this.leastDeaths.entrySet()) {
leastDeaths.put(new SerializableUUID(entry.getKey()), entry.getValue());
}
data.put("leastDeaths", leastDeaths);
Map<SerializableUUID, Long> shortestTimeMilliSeconds = new HashMap<>();
for (Map.Entry<UUID, Long> entry : this.shortestTimeMilliSeconds.entrySet()) {
shortestTimeMilliSeconds.put(new SerializableUUID(entry.getKey()), entry.getValue());
}
data.put("shortestTime", shortestTimeMilliSeconds);
return data;
}
@ -155,10 +163,19 @@ public class DropperArenaRecordsRegistry implements ConfigurationSerializable {
UUID arenaId = ((SerializableUUID) data.get("arenaId")).uuid();
Map<SerializableUUID, Integer> leastDeathsData =
(Map<SerializableUUID, Integer>) data.getOrDefault("leastDeaths", new HashMap<>());
Map<SerializableUUID, Long> shortestTimeMillisecondsData =
(Map<SerializableUUID, Long>) data.getOrDefault("shortestTime", new HashMap<>());
Map<UUID, Integer> leastDeaths = new HashMap<>();
for (Map.Entry<SerializableUUID, Integer> entry : leastDeathsData.entrySet()) {
leastDeaths.put(entry.getKey().uuid(), entry.getValue());
}
return new DropperArenaRecordsRegistry(arenaId, leastDeathsData, shortestTimeMillisecondsData);
Map<SerializableUUID, Number> shortestTimeMillisecondsData =
(Map<SerializableUUID, Number>) data.getOrDefault("shortestTime", new HashMap<>());
Map<UUID, Long> shortestTimeMilliseconds = new HashMap<>();
for (Map.Entry<SerializableUUID, Number> entry : shortestTimeMillisecondsData.entrySet()) {
shortestTimeMilliseconds.put(entry.getKey().uuid(), entry.getValue().longValue());
}
return new DropperArenaRecordsRegistry(arenaId, leastDeaths, shortestTimeMilliseconds);
}
}

View File

@ -25,7 +25,7 @@ public class CreateArenaCommand implements CommandExecutor {
if (arguments.length < 1) {
return false;
}
DropperArena existingArena = Dropper.getInstance().getArenaHandler().getArena(arguments[0]);
if (existingArena != null) {
commandSender.sendMessage("There already exists a dropper arena with that name!");