aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/world/anhgelus
diff options
context:
space:
mode:
authorAnhgelus Morhtuuzh <william@herges.fr>2026-03-17 13:53:59 +0100
committerAnhgelus Morhtuuzh <william@herges.fr>2026-03-17 18:06:17 +0100
commit66856d971b331b3986a17306b9812ac52f0fa3bf (patch)
treef724cee44afde57681e824c5b56c6b6362ad7fcb /src/main/java/world/anhgelus
parent97be59a7b6b990e8a68b69424904ed465a1faabe (diff)
fix(game): invalid time set
Diffstat (limited to 'src/main/java/world/anhgelus')
-rw-r--r--src/main/java/world/anhgelus/molehunt/config/ConfigPayload.java2
-rw-r--r--src/main/java/world/anhgelus/molehunt/config/SimpleConfig.java201
-rw-r--r--src/main/java/world/anhgelus/molehunt/game/Game.java10
-rw-r--r--src/main/java/world/anhgelus/molehunt/game/GamePayload.java2
-rw-r--r--src/main/java/world/anhgelus/molehunt/utils/TimeUtils.java31
5 files changed, 113 insertions, 133 deletions
diff --git a/src/main/java/world/anhgelus/molehunt/config/ConfigPayload.java b/src/main/java/world/anhgelus/molehunt/config/ConfigPayload.java
index 4c8fcdb..2388d09 100644
--- a/src/main/java/world/anhgelus/molehunt/config/ConfigPayload.java
+++ b/src/main/java/world/anhgelus/molehunt/config/ConfigPayload.java
@@ -5,6 +5,7 @@ import net.minecraft.network.codec.ByteBufCodecs;
import net.minecraft.network.codec.StreamCodec;
import net.minecraft.network.protocol.common.custom.CustomPacketPayload;
import net.minecraft.resources.Identifier;
+import org.jetbrains.annotations.NotNull;
import world.anhgelus.molehunt.Molehunt;
public record ConfigPayload(boolean showNametags, boolean showSkins, boolean showTab) implements CustomPacketPayload {
@@ -19,6 +20,7 @@ public record ConfigPayload(boolean showNametags, boolean showSkins, boolean sho
);
@Override
+ @NotNull
public Type<? extends CustomPacketPayload> type() {
return ID;
}
diff --git a/src/main/java/world/anhgelus/molehunt/config/SimpleConfig.java b/src/main/java/world/anhgelus/molehunt/config/SimpleConfig.java
index 2b65829..a44ffaa 100644
--- a/src/main/java/world/anhgelus/molehunt/config/SimpleConfig.java
+++ b/src/main/java/world/anhgelus/molehunt/config/SimpleConfig.java
@@ -43,51 +43,30 @@ public class SimpleConfig {
private final ConfigRequest request;
private boolean broken = false;
- public interface DefaultConfig {
- String get( String namespace );
-
- static String empty( String namespace ) {
- return "";
- }
- }
-
- public static class ConfigRequest {
-
- private final File file;
- private final String filename;
- private DefaultConfig provider;
-
- private ConfigRequest(File file, String filename ) {
- this.file = file;
- this.filename = filename;
- this.provider = DefaultConfig::empty;
- }
+ private SimpleConfig(ConfigRequest request) {
+ this.request = request;
+ String identifier = "Config '" + request.filename + "'";
- /**
- * Sets the default config provider, used to generate the
- * config if it's missing.
- *
- * @param provider default config provider
- * @return current config request object
- * @see DefaultConfig
- */
- public ConfigRequest provider( DefaultConfig provider ) {
- this.provider = provider;
- return this;
- }
+ if (!request.file.exists()) {
+ LOGGER.info("{} is missing, generating default one...", identifier);
- /**
- * Loads the config from the filesystem.
- *
- * @return config object
- * @see SimpleConfig
- */
- public SimpleConfig request() {
- return new SimpleConfig( this );
+ try {
+ createConfig();
+ } catch (IOException e) {
+ LOGGER.error("{} failed to generate!", identifier);
+ LOGGER.trace(e);
+ broken = true;
+ }
}
- private String getConfig() {
- return provider.get( filename ) + "\n";
+ if (!broken) {
+ try {
+ loadConfig();
+ } catch (Exception e) {
+ LOGGER.error("{} failed to load!", identifier);
+ LOGGER.trace(e);
+ broken = true;
+ }
}
}
@@ -99,89 +78,54 @@ public class SimpleConfig {
* @param filename - name of the config file
* @return new config request object
*/
- public static ConfigRequest of( String filename ) {
+ public static ConfigRequest of(String filename) {
Path path = FabricLoader.getInstance().getConfigDir();
- return new ConfigRequest( path.resolve( filename + ".properties" ).toFile(), filename );
+ return new ConfigRequest(path.resolve(filename + ".properties").toFile(), filename);
}
private void createConfig() throws IOException {
-
// try creating missing files
- request.file.getParentFile().mkdirs();
- Files.createFile( request.file.toPath() );
+ final var parent = request.file.getParentFile();
+ if (parent == null) throw new IOException("Cannot get parent file of the config");
+ parent.mkdirs();
+ Files.createFile(request.file.toPath());
// write default config data
PrintWriter writer = new PrintWriter(request.file, StandardCharsets.UTF_8);
- writer.write( request.getConfig() );
+ writer.write(request.getConfig());
writer.close();
}
private void loadConfig() throws IOException {
- Scanner reader = new Scanner( request.file );
- for( int line = 1; reader.hasNextLine(); line ++ ) {
- parseConfigEntry( reader.nextLine(), line );
+ Scanner reader = new Scanner(request.file);
+ for (int line = 1; reader.hasNextLine(); line++) {
+ parseConfigEntry(reader.nextLine(), line);
}
}
- private void parseConfigEntry( String entry, int line ) {
- if( !entry.isEmpty() && !entry.startsWith( "#" ) ) {
+ private void parseConfigEntry(String entry, int line) {
+ if (!entry.isEmpty() && !entry.startsWith("#")) {
String[] parts = entry.split("=", 2);
- if( parts.length == 2 ) {
- config.put( parts[0].stripTrailing(), parts[1].strip() );
- }else{
+ if (parts.length == 2) {
+ config.put(parts[0].stripTrailing(), parts[1].strip());
+ } else {
throw new RuntimeException("Syntax error in config file on line " + line + "!");
}
}
}
- private SimpleConfig( ConfigRequest request ) {
- this.request = request;
- String identifier = "Config '" + request.filename + "'";
-
- if( !request.file.exists() ) {
- LOGGER.info("{} is missing, generating default one...", identifier);
-
- try {
- createConfig();
- } catch (IOException e) {
- LOGGER.error("{} failed to generate!", identifier);
- LOGGER.trace( e );
- broken = true;
- }
- }
-
- if( !broken ) {
- try {
- loadConfig();
- } catch (Exception e) {
- LOGGER.error("{} failed to load!", identifier);
- LOGGER.trace( e );
- broken = true;
- }
- }
-
- }
-
- /**
- * Queries a value from config, returns `null` if the
- * key does not exist.
- *
- * @return value corresponding to the given key
- * @see SimpleConfig#getOrDefault
- */
- @Deprecated
- public String get( String key ) {
- return config.get( key );
+ private String get(String key) {
+ return config.get(key);
}
/**
* Returns string value from config corresponding to the given
* key, or the default string if the key is missing.
*
- * @return value corresponding to the given key, or the default value
+ * @return value corresponding to the given key, or the default value
*/
- public String getOrDefault( String key, String def ) {
+ public String getOrDefault(String key, String def) {
String val = get(key);
return val == null ? def : val;
}
@@ -190,11 +134,11 @@ public class SimpleConfig {
* Returns integer value from config corresponding to the given
* key, or the default integer if the key is missing or invalid.
*
- * @return value corresponding to the given key, or the default value
+ * @return value corresponding to the given key, or the default value
*/
- public int getOrDefault( String key, int def ) {
+ public int getOrDefault(String key, int def) {
try {
- return Integer.parseInt( get(key) );
+ return Integer.parseInt(get(key));
} catch (Exception e) {
return def;
}
@@ -204,11 +148,11 @@ public class SimpleConfig {
* Returns boolean value from config corresponding to the given
* key, or the default boolean if the key is missing.
*
- * @return value corresponding to the given key, or the default value
+ * @return value corresponding to the given key, or the default value
*/
- public boolean getOrDefault( String key, boolean def ) {
+ public boolean getOrDefault(String key, boolean def) {
String val = get(key);
- if( val != null ) {
+ if (val != null) {
return val.equalsIgnoreCase("true");
}
@@ -219,11 +163,11 @@ public class SimpleConfig {
* Returns double value from config corresponding to the given
* key, or the default string if the key is missing or invalid.
*
- * @return value corresponding to the given key, or the default value
+ * @return value corresponding to the given key, or the default value
*/
- public double getOrDefault( String key, double def ) {
+ public double getOrDefault(String key, double def) {
try {
- return Double.parseDouble( get(key) );
+ return Double.parseDouble(get(key));
} catch (Exception e) {
return def;
}
@@ -250,4 +194,53 @@ public class SimpleConfig {
return request.file.delete();
}
+ public interface DefaultConfig {
+ static String empty(String namespace) {
+ return "";
+ }
+
+ String get(String namespace);
+ }
+
+ public static class ConfigRequest {
+
+ private final File file;
+ private final String filename;
+ private DefaultConfig provider;
+
+ private ConfigRequest(File file, String filename) {
+ this.file = file;
+ this.filename = filename;
+ this.provider = DefaultConfig::empty;
+ }
+
+ /**
+ * Sets the default config provider, used to generate the
+ * config if it's missing.
+ *
+ * @param provider default config provider
+ * @return current config request object
+ * @see DefaultConfig
+ */
+ public ConfigRequest provider(DefaultConfig provider) {
+ this.provider = provider;
+ return this;
+ }
+
+ /**
+ * Loads the config from the filesystem.
+ *
+ * @return config object
+ * @see SimpleConfig
+ */
+ public SimpleConfig request() {
+ return new SimpleConfig(this);
+ }
+
+ private String getConfig() {
+ return provider.get(filename) + "\n";
+ }
+
+ }
+
} \ No newline at end of file
diff --git a/src/main/java/world/anhgelus/molehunt/game/Game.java b/src/main/java/world/anhgelus/molehunt/game/Game.java
index d95d211..5f4513d 100644
--- a/src/main/java/world/anhgelus/molehunt/game/Game.java
+++ b/src/main/java/world/anhgelus/molehunt/game/Game.java
@@ -156,7 +156,7 @@ public class Game {
}
public Component getRemainingText() {
- return Component.nullToEmpty("§c" + TimeUtils.generateShortString(remaining));
+ return Component.nullToEmpty("§c" + TimeUtils.toTime(remaining));
}
public List<ServerPlayer> getMoles() {
@@ -180,12 +180,10 @@ public class Game {
public boolean wonByMoles() {
return new HashSet<>(moles).containsAll(
- server.getPlayerList()
- .getPlayers()
- .stream()
- .filter(p -> !p.isSpectator() && !p.isCreative())
+ server.getPlayerList().getPlayers().stream()
+ .filter(p -> p.gameMode.isSurvival())
.map(Entity::getUUID)
- .toList()
+ .collect(Collectors.toSet())
);
}
diff --git a/src/main/java/world/anhgelus/molehunt/game/GamePayload.java b/src/main/java/world/anhgelus/molehunt/game/GamePayload.java
index f611385..3a32e1c 100644
--- a/src/main/java/world/anhgelus/molehunt/game/GamePayload.java
+++ b/src/main/java/world/anhgelus/molehunt/game/GamePayload.java
@@ -5,6 +5,7 @@ import net.minecraft.network.codec.ByteBufCodecs;
import net.minecraft.network.codec.StreamCodec;
import net.minecraft.network.protocol.common.custom.CustomPacketPayload;
import net.minecraft.resources.Identifier;
+import org.jetbrains.annotations.NotNull;
import world.anhgelus.molehunt.Molehunt;
public record GamePayload(boolean gameLaunched) implements CustomPacketPayload {
@@ -17,6 +18,7 @@ public record GamePayload(boolean gameLaunched) implements CustomPacketPayload {
);
@Override
+ @NotNull
public Type<? extends CustomPacketPayload> type() {
return ID;
}
diff --git a/src/main/java/world/anhgelus/molehunt/utils/TimeUtils.java b/src/main/java/world/anhgelus/molehunt/utils/TimeUtils.java
index 2b96606..ac3c116 100644
--- a/src/main/java/world/anhgelus/molehunt/utils/TimeUtils.java
+++ b/src/main/java/world/anhgelus/molehunt/utils/TimeUtils.java
@@ -1,33 +1,18 @@
package world.anhgelus.molehunt.utils;
-public class TimeUtils {
-
- private record Time(long hours, long minutes, long seconds) {}
+import org.jetbrains.annotations.NotNull;
- public static String generateString(long time) {
- final var pt = generateTime(time);
+public class TimeUtils {
- StringBuilder sb = new StringBuilder();
- if (pt.hours != 0) {
- sb.append(pt.hours).append(" hours ");
- }
- if (pt.minutes != 0 || pt.hours != 0) {
- sb.append(pt.minutes).append(" minutes ");
+ public record Time(long hours, long minutes, long seconds) {
+ public @NotNull String toString() {
+ return padLeft(hours) + ":" +
+ padLeft(minutes) + ":" +
+ padLeft(seconds);
}
- sb.append(pt.seconds).append(" seconds");
-
- return sb.toString();
- }
-
- public static String generateShortString(long time) {
- final var pt = generateTime(time);
-
- return padLeft(pt.hours) + ":" +
- padLeft(pt.minutes) + ":" +
- padLeft(pt.seconds);
}
- private static Time generateTime(long time) {
+ public static Time toTime(long time) {
long hours = 0;
if (time > 3600) {
hours = Math.floorDiv(time, 3600);