Improves display of time records

This commit is contained in:
Kristian Knarvik 2024-04-04 15:50:38 +02:00
parent c8fbdec64f
commit d7e950c53e
7 changed files with 42 additions and 14 deletions

View File

@ -105,12 +105,6 @@
<version>24.0.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.9.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>me.clip</groupId>
<artifactId>placeholderapi</artifactId>

View File

@ -104,7 +104,7 @@ public abstract class AbstractArenaPlayerRegistry<K extends Arena> implements Ar
for (PlayerEntryState entryState : entryStates) {
this.entryStates.put(entryState.getPlayerId(), entryState);
}
if (this.entryStates.size() > 0) {
if (!this.entryStates.isEmpty()) {
MiniGames.log(Level.WARNING, entryStates.size() + " un-exited sessions found. This happens if " +
"players leave in the middle of a game, or if the server crashes. MiniGames will do its best " +
"to fix the players' states.");

View File

@ -44,6 +44,13 @@ public abstract class ArenaRecord<K extends Comparable<K>> implements Comparable
return record;
}
/**
* Gets this as a string that should be printed on a sign
*
* @return <p>This as a string</p>
*/
public abstract String getAsString();
@Override
public boolean equals(Object other) {
return other instanceof ArenaRecord<?> && userId.equals(((ArenaRecord<?>) other).userId);
@ -70,7 +77,7 @@ public abstract class ArenaRecord<K extends Comparable<K>> implements Comparable
@Override
public String toString() {
return userId + ": " + record;
return userId + ":" + record;
}
}

View File

@ -19,6 +19,11 @@ public class IntegerRecord extends SummableArenaRecord<Integer> {
super(userId, record);
}
@Override
public String getAsString() {
return String.valueOf(this.getRecord());
}
@Override
public SummableArenaRecord<Integer> sum(Integer value) {
return new IntegerRecord(this.getUserId(), this.getRecord() + value);

View File

@ -7,7 +7,7 @@ import java.util.Map;
import java.util.UUID;
/**
* A record storing a Long
* A record storing a Long time
*/
public class LongRecord extends SummableArenaRecord<Long> {
@ -29,6 +29,22 @@ public class LongRecord extends SummableArenaRecord<Long> {
return new LongRecord(this.getUserId(), this.getRecord() + value);
}
@Override
public String getAsString() {
int seconds = (int) Math.floor(getRecord() / 1000.0);
int minutes = 0;
if (seconds > 60) {
minutes = (int) Math.floor(seconds / 60.0);
seconds = seconds % 60;
}
if (minutes > 0) {
return minutes + "m" + seconds + "s";
} else {
return seconds + "s";
}
}
/**
* Deserializes the saved arena record
*

View File

@ -313,8 +313,8 @@ public abstract class RecordExpansion extends PlaceholderExpansion {
private String getRecordData(@NotNull InfoType infoType, @NotNull ArenaRecord<?> arenaRecord) {
return switch (infoType) {
case PLAYER -> getPlayerName(arenaRecord.getUserId());
case VALUE -> arenaRecord.getRecord().toString();
case COMBINED -> getPlayerName(arenaRecord.getUserId()) + ": " + arenaRecord.getRecord().toString();
case VALUE -> arenaRecord.getAsString();
case COMBINED -> getPlayerName(arenaRecord.getUserId()) + ": " + arenaRecord.getAsString();
};
}

View File

@ -20,6 +20,7 @@ import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.YamlConfiguration;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.yaml.snakeyaml.error.YAMLException;
import java.io.File;
import java.io.IOException;
@ -249,9 +250,14 @@ public final class DropperArenaStorageHelper {
* @return <p>The loaded arena data</p>
*/
private static @Nullable DropperArenaData loadDropperArenaData(@NotNull UUID arenaId) {
File arenaDataFile = getDropperArenaDataFile(arenaId);
YamlConfiguration configuration = YamlConfiguration.loadConfiguration(arenaDataFile);
return (DropperArenaData) configuration.get(DropperArenaStorageKey.DATA.getKey());
try {
File arenaDataFile = getDropperArenaDataFile(arenaId);
YamlConfiguration configuration = YamlConfiguration.loadConfiguration(arenaDataFile);
return (DropperArenaData) configuration.get(DropperArenaStorageKey.DATA.getKey());
} catch (YAMLException exception) {
MiniGames.log(Level.SEVERE, "Unable to load arena data from arena " + arenaId);
return null;
}
}
/**