From 207a43135be96bec8f55f81125bea54224028dfd Mon Sep 17 00:00:00 2001 From: LB2345 <163643124+LovelyBunny11@users.noreply.github.com> Date: Wed, 21 May 2025 02:51:40 +0300 Subject: [PATCH 1/8] Added some methods from the 1.8.9 update. --- .../zombiesutils/handlers/Location.java | 4 +- .../client/ClientPlayNetworkHandlerMixin.java | 17 +++-- .../xyz/stachel/zombiesutils/game/Game.java | 27 +++++++- .../zombiesutils/game/GameManager.java | 66 +++++++++++++++++-- .../stachel/zombiesutils/game/GameMode.java | 4 ++ 5 files changed, 99 insertions(+), 19 deletions(-) diff --git a/src/client/java/xyz/stachel/zombiesutils/handlers/Location.java b/src/client/java/xyz/stachel/zombiesutils/handlers/Location.java index 791f8bd..8685add 100644 --- a/src/client/java/xyz/stachel/zombiesutils/handlers/Location.java +++ b/src/client/java/xyz/stachel/zombiesutils/handlers/Location.java @@ -1,9 +1,9 @@ package xyz.stachel.zombiesutils.handlers; -import java.util.Optional; - import net.hypixel.modapi.packet.impl.clientbound.event.ClientboundLocationPacket; +import java.util.Optional; + public class Location { private static String serverNumber; diff --git a/src/client/java/xyz/stachel/zombiesutils/mixin/client/ClientPlayNetworkHandlerMixin.java b/src/client/java/xyz/stachel/zombiesutils/mixin/client/ClientPlayNetworkHandlerMixin.java index 9fa8faf..aba33bb 100644 --- a/src/client/java/xyz/stachel/zombiesutils/mixin/client/ClientPlayNetworkHandlerMixin.java +++ b/src/client/java/xyz/stachel/zombiesutils/mixin/client/ClientPlayNetworkHandlerMixin.java @@ -1,17 +1,16 @@ package xyz.stachel.zombiesutils.mixin.client; -import java.util.regex.Matcher; -import java.util.regex.Pattern; - -import org.spongepowered.asm.mixin.Mixin; -import org.spongepowered.asm.mixin.injection.Inject; -import org.spongepowered.asm.mixin.injection.At; -import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; - import net.minecraft.client.network.ClientPlayNetworkHandler; import net.minecraft.network.packet.s2c.play.TitleS2CPacket; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; import xyz.stachel.zombiesutils.game.GameManager; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + @Mixin(ClientPlayNetworkHandler.class) public class ClientPlayNetworkHandlerMixin { private static final Pattern pattern = Pattern.compile("Round (\\d{1,3})"); @@ -23,7 +22,7 @@ public class ClientPlayNetworkHandlerMixin { if (matcher.find()) { int number = Integer.parseInt(matcher.group(1)); if (number >= 1 && number <= 105) { - GameManager.onRound(number); + new GameManager().onRound(number); } } } diff --git a/src/main/java/xyz/stachel/zombiesutils/game/Game.java b/src/main/java/xyz/stachel/zombiesutils/game/Game.java index 1b27c72..52f7714 100644 --- a/src/main/java/xyz/stachel/zombiesutils/game/Game.java +++ b/src/main/java/xyz/stachel/zombiesutils/game/Game.java @@ -1,8 +1,12 @@ package xyz.stachel.zombiesutils.game; -import java.io.File; - +import net.minecraft.client.MinecraftClient; +import net.minecraft.text.Text; +import org.apache.commons.lang3.exception.ExceptionUtils; import org.jetbrains.annotations.NotNull; +import xyz.stachel.zombiesutils.ZombiesUtils; + +import java.io.File; class Game extends Timer { private final boolean joinedRoundOne; @@ -27,8 +31,25 @@ class Game extends Timer { this.mode = mode; } + public void pass(int round) { + if ((round == 0) || (this.round == round + 1) || (this.roundTime() < 100)) { + ZombiesUtils.LOGGER.debug("SPLIT CANCELLED"); + return; + } + try { +// record(); + } catch (Exception e) { + ZombiesUtils.LOGGER.error(ExceptionUtils.getStackTrace(e)); + MinecraftClient.getInstance().player.sendMessage(Text.of("Error recording splits"), false); + } + this.split(); + this.round = round + 1; + } + public long split(final int round) { - if (round ) + if (round == 1) { + + } return super.split(); } } diff --git a/src/main/java/xyz/stachel/zombiesutils/game/GameManager.java b/src/main/java/xyz/stachel/zombiesutils/game/GameManager.java index 2e7c52a..09e64d8 100644 --- a/src/main/java/xyz/stachel/zombiesutils/game/GameManager.java +++ b/src/main/java/xyz/stachel/zombiesutils/game/GameManager.java @@ -1,14 +1,23 @@ package xyz.stachel.zombiesutils.game; -import java.util.HashMap; - +import org.jetbrains.annotations.NotNull; import xyz.stachel.zombiesutils.game.GameMode.Map; import xyz.stachel.zombiesutils.handlers.Location; +import java.util.HashMap; +import java.util.Optional; +import java.util.Set; + public class GameManager { private final HashMap games = new HashMap<>(); + private Optional queuedDifficulty = Optional.empty(); + private String queuedDifficultyServer = "INVALID"; private void addGame(final String serverNumber, final GameMode mode, final int round) { + if (serverNumber.equals(queuedDifficultyServer)) { + this.queuedDifficulty.ifPresent(mode::changeDifficulty); + } + this.queuedDifficulty = Optional.empty(); this.games.put(serverNumber, new Game(new GameFile(serverNumber), mode)); } @@ -17,11 +26,58 @@ public class GameManager { final String mode = Location.getMode(); if (sn == null || mode == null || !mode.startsWith("ZOMBIES")) return; - if (!games.containsKey(sn)) addGame(sn, new GameMode(Map.DEAD_END), round); - else games.get(sn).split(round); + if (!this.games.containsKey(sn)) { + addGame(sn, new GameMode(Map.DEAD_END), round); + } else { + this.games.get(sn).split(round); + } } public Game getGame() { - + final String sn = Location.getServerNumber(); + return this.games.get(sn); } + + public void endGame(final String serverNumber, final boolean isWin) { + if (!games.containsKey(serverNumber)) return; + final Game game = games.get(serverNumber); + if (isWin) { + switch (game.mode.getMap()) { + case DEAD_END: + case BAD_BLOOD: + case PRISON: + game.pass(30); + break; + case ALIEN_ARCADIUM: + game.pass(105); + break; + } + } + games.remove(serverNumber); + + } + + public void splitOrNew(GameMode mode, int round) { + final String serverNumber = Location.getServerNumber(); + if (games.containsKey(serverNumber)) { + if (round == 0) addGame(serverNumber, mode, round); + else games.get(serverNumber).pass(round); + } else addGame(serverNumber, mode, round); + } + + public void setDifficulty(@NotNull GameMode.Difficulty difficulty) { + this.queuedDifficultyServer = Location.getServerNumber(); + if (this.games.containsKey(this.queuedDifficultyServer)) { + this.games.get(this.queuedDifficultyServer).mode.changeDifficulty(difficulty); + } else this.queuedDifficulty = Optional.of(difficulty); + } + + public Set getGames() { + return this.games.keySet(); + } + + public void killAll() { + games.clear(); + } + } diff --git a/src/main/java/xyz/stachel/zombiesutils/game/GameMode.java b/src/main/java/xyz/stachel/zombiesutils/game/GameMode.java index ac8525a..a6ebae0 100644 --- a/src/main/java/xyz/stachel/zombiesutils/game/GameMode.java +++ b/src/main/java/xyz/stachel/zombiesutils/game/GameMode.java @@ -19,6 +19,10 @@ class GameMode { this.difficulty = map != Map.ALIEN_ARCADIUM ? difficulty : Difficulty.NORMAL; } + public Map getMap() { + return this.map; + } + enum Map { DEAD_END, BAD_BLOOD, PRISON, ALIEN_ARCADIUM; } -- 2.39.5 From 047dfcab72911ab7fdeb72f766724dae78d4a26e Mon Sep 17 00:00:00 2001 From: LB2345 <163643124+LovelyBunny11@users.noreply.github.com> Date: Wed, 21 May 2025 06:06:25 +0300 Subject: [PATCH 2/8] Added Player Visibility and Utils. --- build.gradle | 8 ++--- .../zombiesutils/ZombiesUtilsClient.java | 20 ------------- .../zombies-utils.client.mixins.json | 11 ------- .../stachel/zombiesutils/ZombiesUtils.java | 13 +++++++-- .../stachel/zombiesutils/game/GameFile.java | 4 +-- .../zombiesutils/game/GameManager.java | 5 ++-- .../stachel/zombiesutils/game/GameMode.java | 6 ++-- .../zombiesutils/handlers/Location.java | 0 .../zombiesutils/handlers/Renderer.java | 0 .../mixin}/ClientPlayNetworkHandlerMixin.java | 4 +-- .../mixin/OtherClientPlayerEntityMixin.java | 29 +++++++++++++++++++ .../xyz/stachel/zombiesutils/util/Utils.java | 26 +++++++++++++++++ src/main/resources/fabric.mod.json | 7 ++--- src/main/resources/zombies-utils.mixins.json | 13 +++++++++ 14 files changed, 93 insertions(+), 53 deletions(-) delete mode 100644 src/client/java/xyz/stachel/zombiesutils/ZombiesUtilsClient.java delete mode 100644 src/client/resources/zombies-utils.client.mixins.json rename src/{client => main}/java/xyz/stachel/zombiesutils/handlers/Location.java (100%) rename src/{client => main}/java/xyz/stachel/zombiesutils/handlers/Renderer.java (100%) rename src/{client/java/xyz/stachel/zombiesutils/mixin/client => main/java/xyz/stachel/zombiesutils/mixin}/ClientPlayNetworkHandlerMixin.java (91%) create mode 100644 src/main/java/xyz/stachel/zombiesutils/mixin/OtherClientPlayerEntityMixin.java create mode 100644 src/main/java/xyz/stachel/zombiesutils/util/Utils.java create mode 100644 src/main/resources/zombies-utils.mixins.json diff --git a/build.gradle b/build.gradle index df8e22b..9af532f 100644 --- a/build.gradle +++ b/build.gradle @@ -11,16 +11,14 @@ base { } repositories { - maven { url 'https://repo.hypixel.net/repository/Hypixel/' } + maven { url 'https://repo.hypixel.net/repository/Hypixel/' } } loom { - splitEnvironmentSourceSets() mods { "zombies-utils" { sourceSet sourceSets.main - sourceSet sourceSets.client } } @@ -34,7 +32,7 @@ dependencies { // Fabric API. This is technically optional, but you probably want it anyway. modImplementation "net.fabricmc.fabric-api:fabric-api:${project.fabric_version}" - modImplementation 'net.hypixel:mod-api:1.0.1' + modImplementation 'net.hypixel:mod-api:1.0.1' } processResources { @@ -83,4 +81,4 @@ publishing { // The repositories here will be used for publishing your artifact, not for // retrieving dependencies. } -} +} \ No newline at end of file diff --git a/src/client/java/xyz/stachel/zombiesutils/ZombiesUtilsClient.java b/src/client/java/xyz/stachel/zombiesutils/ZombiesUtilsClient.java deleted file mode 100644 index 8e87530..0000000 --- a/src/client/java/xyz/stachel/zombiesutils/ZombiesUtilsClient.java +++ /dev/null @@ -1,20 +0,0 @@ -package xyz.stachel.zombiesutils; - -import net.fabricmc.api.ClientModInitializer; -import net.fabricmc.fabric.api.client.rendering.v1.HudLayerRegistrationCallback; -import net.hypixel.modapi.HypixelModAPI; -import net.hypixel.modapi.packet.impl.clientbound.event.ClientboundLocationPacket; -import xyz.stachel.zombiesutils.handlers.Location; -import xyz.stachel.zombiesutils.handlers.Renderer; - -public class ZombiesUtilsClient implements ClientModInitializer { - - @Override - public void onInitializeClient() { - HypixelModAPI.getInstance().subscribeToEventPacket(ClientboundLocationPacket.class); - HypixelModAPI.getInstance().createHandler(ClientboundLocationPacket.class, Location::onLocation); - ZombiesUtils.LOGGER.info("initializing..."); - HudLayerRegistrationCallback.EVENT.register(new Renderer()); - } - -} diff --git a/src/client/resources/zombies-utils.client.mixins.json b/src/client/resources/zombies-utils.client.mixins.json deleted file mode 100644 index 72e38c2..0000000 --- a/src/client/resources/zombies-utils.client.mixins.json +++ /dev/null @@ -1,11 +0,0 @@ -{ - "required": true, - "package": "xyz.stachel.zombiesutils.mixin.client", - "compatibilityLevel": "JAVA_21", - "client": [ - "ClientPlayNetworkHandlerMixin" - ], - "injectors": { - "defaultRequire": 1 - } -} diff --git a/src/main/java/xyz/stachel/zombiesutils/ZombiesUtils.java b/src/main/java/xyz/stachel/zombiesutils/ZombiesUtils.java index d049a78..fa9cc87 100644 --- a/src/main/java/xyz/stachel/zombiesutils/ZombiesUtils.java +++ b/src/main/java/xyz/stachel/zombiesutils/ZombiesUtils.java @@ -1,9 +1,13 @@ package xyz.stachel.zombiesutils; +import net.fabricmc.api.ModInitializer; +import net.fabricmc.fabric.api.client.rendering.v1.HudLayerRegistrationCallback; +import net.hypixel.modapi.HypixelModAPI; +import net.hypixel.modapi.packet.impl.clientbound.event.ClientboundLocationPacket; import org.slf4j.Logger; import org.slf4j.LoggerFactory; - -import net.fabricmc.api.ModInitializer; +import xyz.stachel.zombiesutils.handlers.Location; +import xyz.stachel.zombiesutils.handlers.Renderer; public class ZombiesUtils implements ModInitializer { @@ -12,6 +16,9 @@ public class ZombiesUtils implements ModInitializer { @Override public void onInitialize() { + HypixelModAPI.getInstance().subscribeToEventPacket(ClientboundLocationPacket.class); + HypixelModAPI.getInstance().createHandler(ClientboundLocationPacket.class, Location::onLocation); + ZombiesUtils.LOGGER.info("initializing..."); + HudLayerRegistrationCallback.EVENT.register(new Renderer()); } - } diff --git a/src/main/java/xyz/stachel/zombiesutils/game/GameFile.java b/src/main/java/xyz/stachel/zombiesutils/game/GameFile.java index fc01920..c1088a1 100644 --- a/src/main/java/xyz/stachel/zombiesutils/game/GameFile.java +++ b/src/main/java/xyz/stachel/zombiesutils/game/GameFile.java @@ -1,5 +1,7 @@ package xyz.stachel.zombiesutils.game; +import org.jetbrains.annotations.NotNull; + import java.io.File; import java.io.FileWriter; import java.io.IOException; @@ -7,8 +9,6 @@ import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; import java.time.temporal.ChronoUnit; -import org.jetbrains.annotations.NotNull; - class GameFile extends File { private FileWriter writer; diff --git a/src/main/java/xyz/stachel/zombiesutils/game/GameManager.java b/src/main/java/xyz/stachel/zombiesutils/game/GameManager.java index 09e64d8..9356906 100644 --- a/src/main/java/xyz/stachel/zombiesutils/game/GameManager.java +++ b/src/main/java/xyz/stachel/zombiesutils/game/GameManager.java @@ -1,8 +1,9 @@ package xyz.stachel.zombiesutils.game; import org.jetbrains.annotations.NotNull; -import xyz.stachel.zombiesutils.game.GameMode.Map; import xyz.stachel.zombiesutils.handlers.Location; +import xyz.stachel.zombiesutils.game.GameMode.Map; +import xyz.stachel.zombiesutils.util.Utils; import java.util.HashMap; import java.util.Optional; @@ -24,7 +25,7 @@ public class GameManager { public void onRound(final int round) { final String sn = Location.getServerNumber(); final String mode = Location.getMode(); - if (sn == null || mode == null || !mode.startsWith("ZOMBIES")) return; + if (sn == null || mode == null || !Utils.isZombies()) return; if (!this.games.containsKey(sn)) { addGame(sn, new GameMode(Map.DEAD_END), round); diff --git a/src/main/java/xyz/stachel/zombiesutils/game/GameMode.java b/src/main/java/xyz/stachel/zombiesutils/game/GameMode.java index a6ebae0..27589cc 100644 --- a/src/main/java/xyz/stachel/zombiesutils/game/GameMode.java +++ b/src/main/java/xyz/stachel/zombiesutils/game/GameMode.java @@ -2,7 +2,7 @@ package xyz.stachel.zombiesutils.game; import org.jetbrains.annotations.NotNull; -class GameMode { +public class GameMode { private final Map map; private Difficulty difficulty; @@ -23,11 +23,11 @@ class GameMode { return this.map; } - enum Map { + public enum Map { DEAD_END, BAD_BLOOD, PRISON, ALIEN_ARCADIUM; } - enum Difficulty { + public enum Difficulty { NORMAL, HARD, RIP; } diff --git a/src/client/java/xyz/stachel/zombiesutils/handlers/Location.java b/src/main/java/xyz/stachel/zombiesutils/handlers/Location.java similarity index 100% rename from src/client/java/xyz/stachel/zombiesutils/handlers/Location.java rename to src/main/java/xyz/stachel/zombiesutils/handlers/Location.java diff --git a/src/client/java/xyz/stachel/zombiesutils/handlers/Renderer.java b/src/main/java/xyz/stachel/zombiesutils/handlers/Renderer.java similarity index 100% rename from src/client/java/xyz/stachel/zombiesutils/handlers/Renderer.java rename to src/main/java/xyz/stachel/zombiesutils/handlers/Renderer.java diff --git a/src/client/java/xyz/stachel/zombiesutils/mixin/client/ClientPlayNetworkHandlerMixin.java b/src/main/java/xyz/stachel/zombiesutils/mixin/ClientPlayNetworkHandlerMixin.java similarity index 91% rename from src/client/java/xyz/stachel/zombiesutils/mixin/client/ClientPlayNetworkHandlerMixin.java rename to src/main/java/xyz/stachel/zombiesutils/mixin/ClientPlayNetworkHandlerMixin.java index aba33bb..78ddda8 100644 --- a/src/client/java/xyz/stachel/zombiesutils/mixin/client/ClientPlayNetworkHandlerMixin.java +++ b/src/main/java/xyz/stachel/zombiesutils/mixin/ClientPlayNetworkHandlerMixin.java @@ -1,4 +1,4 @@ -package xyz.stachel.zombiesutils.mixin.client; +package xyz.stachel.zombiesutils.mixin; import net.minecraft.client.network.ClientPlayNetworkHandler; import net.minecraft.network.packet.s2c.play.TitleS2CPacket; @@ -12,7 +12,7 @@ import java.util.regex.Matcher; import java.util.regex.Pattern; @Mixin(ClientPlayNetworkHandler.class) -public class ClientPlayNetworkHandlerMixin { +public abstract class ClientPlayNetworkHandlerMixin { private static final Pattern pattern = Pattern.compile("Round (\\d{1,3})"); @Inject(at = @At("HEAD"), method = "onTitle") diff --git a/src/main/java/xyz/stachel/zombiesutils/mixin/OtherClientPlayerEntityMixin.java b/src/main/java/xyz/stachel/zombiesutils/mixin/OtherClientPlayerEntityMixin.java new file mode 100644 index 0000000..8fb5962 --- /dev/null +++ b/src/main/java/xyz/stachel/zombiesutils/mixin/OtherClientPlayerEntityMixin.java @@ -0,0 +1,29 @@ +package xyz.stachel.zombiesutils.mixin; + +import com.llamalad7.mixinextras.injector.ModifyReturnValue; +import com.mojang.authlib.GameProfile; +import net.minecraft.client.network.AbstractClientPlayerEntity; +import net.minecraft.client.network.OtherClientPlayerEntity; +import net.minecraft.client.world.ClientWorld; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.injection.At; + +@Mixin(OtherClientPlayerEntity.class) +public abstract class OtherClientPlayerEntityMixin extends AbstractClientPlayerEntity { + + public OtherClientPlayerEntityMixin(ClientWorld world, GameProfile profile) { + super(world, profile); + } + + @ModifyReturnValue(method = "shouldRender", at = @At(value = "RETURN")) + public boolean shouldRender(boolean original, double distance) { + int dist = 1; + double d = this.getBoundingBox().getAverageSideLength() * 1.75 + ((double) dist * 2.5D); + if (Double.isNaN(d)) { + d = 1.0; + } + + d *= 1.0; + return (distance >= d * d) && original; + } +} diff --git a/src/main/java/xyz/stachel/zombiesutils/util/Utils.java b/src/main/java/xyz/stachel/zombiesutils/util/Utils.java new file mode 100644 index 0000000..011379f --- /dev/null +++ b/src/main/java/xyz/stachel/zombiesutils/util/Utils.java @@ -0,0 +1,26 @@ +package xyz.stachel.zombiesutils.util; + +import xyz.stachel.zombiesutils.game.GameMode; +import xyz.stachel.zombiesutils.handlers.Location; + +import java.util.Optional; + +import static xyz.stachel.zombiesutils.game.GameMode.Map.*; + +public class Utils { + + public static Optional getMap() { + return switch (Location.getMode()) { + case "ZOMBIES_DEAD_END" -> Optional.of(DEAD_END); + case "ZOMBIES_BAD_BLOOD" -> Optional.of(BAD_BLOOD); + case "ZOMBIES_ALIEN_ARCADIUM" -> Optional.of(ALIEN_ARCADIUM); + case "ZOMBIES_PRISON" -> Optional.of(PRISON); + default -> Optional.empty(); + }; + } + + public static boolean isZombies() { + return Location.getMode().startsWith("ZOMBIES"); + } + +} diff --git a/src/main/resources/fabric.mod.json b/src/main/resources/fabric.mod.json index 480c9c6..ca2e0a2 100644 --- a/src/main/resources/fabric.mod.json +++ b/src/main/resources/fabric.mod.json @@ -17,14 +17,11 @@ "entrypoints": { "main": [ "xyz.stachel.zombiesutils.ZombiesUtils" - ], - "client": [ - "xyz.stachel.zombiesutils.ZombiesUtilsClient" - ] + ] }, "mixins": [ { - "config": "zombies-utils.client.mixins.json", + "config": "zombies-utils.mixins.json", "environment": "client" } ], diff --git a/src/main/resources/zombies-utils.mixins.json b/src/main/resources/zombies-utils.mixins.json new file mode 100644 index 0000000..4d05fdd --- /dev/null +++ b/src/main/resources/zombies-utils.mixins.json @@ -0,0 +1,13 @@ +{ + "required": true, + "package": "xyz.stachel.zombiesutils.mixin", + "compatibilityLevel": "JAVA_21", + "client": [ + "ClientPlayNetworkHandlerMixin", + "OtherClientPlayerEntityMixin" + ], + "injectors": { + "defaultRequire": 1 + }, + "mixins": [] +} -- 2.39.5 From c7af5d198f6a57851368a42f221154fbf7ffb3f2 Mon Sep 17 00:00:00 2001 From: LB2345 <163643124+LovelyBunny11@users.noreply.github.com> Date: Wed, 21 May 2025 07:56:41 +0300 Subject: [PATCH 3/8] Added Config and made the distance players no longer render configurable. --- .../stachel/zombiesutils/ZombiesUtils.java | 2 + .../zombiesutils/config/AbstractConfig.java | 45 ++++++++++++++++ .../stachel/zombiesutils/config/Config.java | 53 +++++++++++++++++++ .../zombiesutils/config/IntConfig.java | 39 ++++++++++++++ .../mixin/OtherClientPlayerEntityMixin.java | 5 +- 5 files changed, 142 insertions(+), 2 deletions(-) create mode 100644 src/main/java/xyz/stachel/zombiesutils/config/AbstractConfig.java create mode 100644 src/main/java/xyz/stachel/zombiesutils/config/Config.java create mode 100644 src/main/java/xyz/stachel/zombiesutils/config/IntConfig.java diff --git a/src/main/java/xyz/stachel/zombiesutils/ZombiesUtils.java b/src/main/java/xyz/stachel/zombiesutils/ZombiesUtils.java index fa9cc87..67ae4ff 100644 --- a/src/main/java/xyz/stachel/zombiesutils/ZombiesUtils.java +++ b/src/main/java/xyz/stachel/zombiesutils/ZombiesUtils.java @@ -6,6 +6,7 @@ import net.hypixel.modapi.HypixelModAPI; import net.hypixel.modapi.packet.impl.clientbound.event.ClientboundLocationPacket; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import xyz.stachel.zombiesutils.config.Config; import xyz.stachel.zombiesutils.handlers.Location; import xyz.stachel.zombiesutils.handlers.Renderer; @@ -20,5 +21,6 @@ public class ZombiesUtils implements ModInitializer { HypixelModAPI.getInstance().createHandler(ClientboundLocationPacket.class, Location::onLocation); ZombiesUtils.LOGGER.info("initializing..."); HudLayerRegistrationCallback.EVENT.register(new Renderer()); + Config.readFile(); } } diff --git a/src/main/java/xyz/stachel/zombiesutils/config/AbstractConfig.java b/src/main/java/xyz/stachel/zombiesutils/config/AbstractConfig.java new file mode 100644 index 0000000..8d97cec --- /dev/null +++ b/src/main/java/xyz/stachel/zombiesutils/config/AbstractConfig.java @@ -0,0 +1,45 @@ +package xyz.stachel.zombiesutils.config; + +import com.google.common.base.CaseFormat; +import net.minecraft.client.gui.tooltip.Tooltip; +import net.minecraft.client.gui.widget.ClickableWidget; +import net.minecraft.text.Text; + +public abstract class AbstractConfig { + private final Text message; + private final Tooltip tooltip; + private T value; + + public AbstractConfig(String key, T defaultValue) { + value = defaultValue; + message = Text.translatable(String.format("hardcover.config.%s", toSnakeCase(key))); + tooltip = Tooltip.of(Text.translatable(String.format("hardcover.config.tooltip.%s", toSnakeCase(key)))); + } + + private static String toSnakeCase(String text) { + return CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, text); + } + + public abstract ClickableWidget createWidget(Runnable callback); + + public ClickableWidget createWidget() { + return createWidget(() -> {}); + } + + public Text getMessage() { + return message; + } + + public Tooltip getTooltip() { + return tooltip; + } + + public T getValue() { + return value; + } + + public void setValue(T value) { + this.value = value; + Config.writeFile(); + } +} \ No newline at end of file diff --git a/src/main/java/xyz/stachel/zombiesutils/config/Config.java b/src/main/java/xyz/stachel/zombiesutils/config/Config.java new file mode 100644 index 0000000..ef298f6 --- /dev/null +++ b/src/main/java/xyz/stachel/zombiesutils/config/Config.java @@ -0,0 +1,53 @@ +package xyz.stachel.zombiesutils.config; + +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; +import com.google.gson.JsonParser; +import net.fabricmc.loader.api.FabricLoader; +import xyz.stachel.zombiesutils.ZombiesUtils; + +import java.lang.reflect.Modifier; +import java.nio.file.Files; +import java.nio.file.Path; + +public final class Config { + private static final Gson GSON = new GsonBuilder() + .registerTypeAdapter(IntConfig.class, new IntConfig.Serializer()) + .excludeFieldsWithModifiers(Modifier.TRANSIENT) + .setPrettyPrinting() + .create(); + private static final Path PATH = FabricLoader.getInstance().getConfigDir().resolve(String.format("%s.json", ZombiesUtils.MOD_ID)); + + public static void readFile() { + if (Files.exists(PATH)) { + try (var reader = Files.newBufferedReader(PATH)) { + var jsonElement = JsonParser.parseReader(reader); + + for (var field : Configs.class.getDeclaredFields()) { + var value = jsonElement.getAsJsonObject().get(field.getName()); + + if (value != null) { + var fieldType = field.getType(); + + if (IntConfig.class.isAssignableFrom(fieldType)) { + var intValue = value.getAsInt(); + ((IntConfig) field.get(null)).setValue(intValue); + } + } + } + } catch (Exception exception) { + ZombiesUtils.LOGGER.error("Failed to load configs at '{}'. Using default values.", PATH, exception); + } + } else { + writeFile(); + } + } + + public static void writeFile() { + try (var writer = Files.newBufferedWriter(PATH)) { + GSON.toJson(new Configs(), writer); + } catch (Exception exception) { + ZombiesUtils.LOGGER.error("Failed to save configs to '{}'.", PATH, exception); + } + } +} \ No newline at end of file diff --git a/src/main/java/xyz/stachel/zombiesutils/config/IntConfig.java b/src/main/java/xyz/stachel/zombiesutils/config/IntConfig.java new file mode 100644 index 0000000..60534a0 --- /dev/null +++ b/src/main/java/xyz/stachel/zombiesutils/config/IntConfig.java @@ -0,0 +1,39 @@ +package xyz.stachel.zombiesutils.config; + +import com.google.gson.JsonElement; +import com.google.gson.JsonPrimitive; +import com.google.gson.JsonSerializationContext; +import com.google.gson.JsonSerializer; +import net.minecraft.client.gui.widget.ClickableWidget; +import net.minecraft.client.gui.widget.CyclingButtonWidget; +import net.minecraft.text.Text; + +import java.lang.reflect.Type; + +public class IntConfig extends AbstractConfig { + + public IntConfig(String key, Integer defaultValue) { + super(key, defaultValue); + } + + public static IntConfig of(String key, int defaultValue) { + return new IntConfig(key, defaultValue); + } + + @Override + public ClickableWidget createWidget(Runnable callback) { + return CyclingButtonWidget.builder((value) -> Text.of(getValue().toString())).initially(getValue()) + .tooltip(value -> getTooltip()) + .build(getMessage(), (button, value) -> { + setValue(value); + callback.run(); + }); + } + + public static class Serializer implements JsonSerializer { + @Override + public JsonElement serialize(IntConfig value, Type type, JsonSerializationContext context) { + return new JsonPrimitive(value.getValue()); + } + } +} diff --git a/src/main/java/xyz/stachel/zombiesutils/mixin/OtherClientPlayerEntityMixin.java b/src/main/java/xyz/stachel/zombiesutils/mixin/OtherClientPlayerEntityMixin.java index 8fb5962..556d837 100644 --- a/src/main/java/xyz/stachel/zombiesutils/mixin/OtherClientPlayerEntityMixin.java +++ b/src/main/java/xyz/stachel/zombiesutils/mixin/OtherClientPlayerEntityMixin.java @@ -7,6 +7,7 @@ import net.minecraft.client.network.OtherClientPlayerEntity; import net.minecraft.client.world.ClientWorld; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.injection.At; +import xyz.stachel.zombiesutils.config.Configs; @Mixin(OtherClientPlayerEntity.class) public abstract class OtherClientPlayerEntityMixin extends AbstractClientPlayerEntity { @@ -17,8 +18,8 @@ public abstract class OtherClientPlayerEntityMixin extends AbstractClientPlayerE @ModifyReturnValue(method = "shouldRender", at = @At(value = "RETURN")) public boolean shouldRender(boolean original, double distance) { - int dist = 1; - double d = this.getBoundingBox().getAverageSideLength() * 1.75 + ((double) dist * 2.5D); + int dist = Configs.playerVisibleDistance.getValue(); + double d = this.getBoundingBox().getAverageSideLength() * 1.75 + (double) dist; if (Double.isNaN(d)) { d = 1.0; } -- 2.39.5 From a118708202877e0bff8549a396b95ee9fabc3631 Mon Sep 17 00:00:00 2001 From: LB2345 <163643124+LovelyBunny11@users.noreply.github.com> Date: Wed, 21 May 2025 07:58:03 +0300 Subject: [PATCH 4/8] Added Config and made the distance players no longer render configurable. --- src/main/java/xyz/stachel/zombiesutils/config/Configs.java | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 src/main/java/xyz/stachel/zombiesutils/config/Configs.java diff --git a/src/main/java/xyz/stachel/zombiesutils/config/Configs.java b/src/main/java/xyz/stachel/zombiesutils/config/Configs.java new file mode 100644 index 0000000..bf72d08 --- /dev/null +++ b/src/main/java/xyz/stachel/zombiesutils/config/Configs.java @@ -0,0 +1,5 @@ +package xyz.stachel.zombiesutils.config; + +public class Configs { + public static final IntConfig playerVisibleDistance = IntConfig.of("playerVisibilityDistance", 0); +} -- 2.39.5 From 3f065e114f8b085078d68b7b99abe87e20d40e19 Mon Sep 17 00:00:00 2001 From: LB2345 <163643124+LovelyBunny11@users.noreply.github.com> Date: Fri, 23 May 2025 01:09:31 +0300 Subject: [PATCH 5/8] Added Player Visibility Keybinds and Other Cool Stuff! --- .../stachel/zombiesutils/ZombiesUtils.java | 7 ++ .../zombiesutils/ZombiesUtilsClient.java | 39 ++++++++++ .../stachel/zombiesutils/config/Config.java | 6 ++ .../stachel/zombiesutils/config/Configs.java | 2 +- .../zombiesutils/config/DoubleConfig.java | 39 ++++++++++ .../xyz/stachel/zombiesutils/game/Game.java | 62 +++++++++++++-- .../stachel/zombiesutils/game/GameFile.java | 23 +++++- .../zombiesutils/game/GameManager.java | 12 ++- .../stachel/zombiesutils/game/GameMode.java | 8 ++ .../xyz/stachel/zombiesutils/game/Prefix.java | 36 +++++++++ .../zombiesutils/game/recorder/Category.java | 77 +++++++++++++++++++ .../game/recorder/FileManager.java | 58 ++++++++++++++ .../game/recorder/ISplitsData.java | 5 ++ .../game/recorder/RecordMessageSender.java | 54 +++++++++++++ .../game/recorder/data/CategoryData.java | 58 ++++++++++++++ .../game/recorder/data/GameData.java | 41 ++++++++++ .../game/recorder/files/CategoryFile.java | 66 ++++++++++++++++ .../mixin/ClientPlayNetworkHandlerMixin.java | 3 +- .../mixin/OtherClientPlayerEntityMixin.java | 15 ++-- .../xyz/stachel/zombiesutils/util/Utils.java | 21 +++-- .../assets/zombies-utils/lang/en_us.json | 6 ++ .../assets/zombies-utils/lang/ru_ru.json | 6 ++ src/main/resources/fabric.mod.json | 5 +- 23 files changed, 621 insertions(+), 28 deletions(-) create mode 100644 src/main/java/xyz/stachel/zombiesutils/ZombiesUtilsClient.java create mode 100644 src/main/java/xyz/stachel/zombiesutils/config/DoubleConfig.java create mode 100644 src/main/java/xyz/stachel/zombiesutils/game/Prefix.java create mode 100644 src/main/java/xyz/stachel/zombiesutils/game/recorder/Category.java create mode 100644 src/main/java/xyz/stachel/zombiesutils/game/recorder/FileManager.java create mode 100644 src/main/java/xyz/stachel/zombiesutils/game/recorder/ISplitsData.java create mode 100644 src/main/java/xyz/stachel/zombiesutils/game/recorder/RecordMessageSender.java create mode 100644 src/main/java/xyz/stachel/zombiesutils/game/recorder/data/CategoryData.java create mode 100644 src/main/java/xyz/stachel/zombiesutils/game/recorder/data/GameData.java create mode 100644 src/main/java/xyz/stachel/zombiesutils/game/recorder/files/CategoryFile.java create mode 100644 src/main/resources/assets/zombies-utils/lang/en_us.json create mode 100644 src/main/resources/assets/zombies-utils/lang/ru_ru.json diff --git a/src/main/java/xyz/stachel/zombiesutils/ZombiesUtils.java b/src/main/java/xyz/stachel/zombiesutils/ZombiesUtils.java index 67ae4ff..0e7201a 100644 --- a/src/main/java/xyz/stachel/zombiesutils/ZombiesUtils.java +++ b/src/main/java/xyz/stachel/zombiesutils/ZombiesUtils.java @@ -7,11 +7,18 @@ import net.hypixel.modapi.packet.impl.clientbound.event.ClientboundLocationPacke import org.slf4j.Logger; import org.slf4j.LoggerFactory; import xyz.stachel.zombiesutils.config.Config; +import xyz.stachel.zombiesutils.game.GameManager; import xyz.stachel.zombiesutils.handlers.Location; import xyz.stachel.zombiesutils.handlers.Renderer; public class ZombiesUtils implements ModInitializer { + private static final GameManager gameManager = new GameManager(); + + public static GameManager getGameManager() { + return gameManager; + } + public static final String MOD_ID = "zombies-utils"; public static final Logger LOGGER = LoggerFactory.getLogger(MOD_ID); diff --git a/src/main/java/xyz/stachel/zombiesutils/ZombiesUtilsClient.java b/src/main/java/xyz/stachel/zombiesutils/ZombiesUtilsClient.java new file mode 100644 index 0000000..b44b6de --- /dev/null +++ b/src/main/java/xyz/stachel/zombiesutils/ZombiesUtilsClient.java @@ -0,0 +1,39 @@ +package xyz.stachel.zombiesutils; + +import net.fabricmc.api.ClientModInitializer; +import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents; +import net.fabricmc.fabric.api.client.keybinding.v1.KeyBindingHelper; +import net.minecraft.client.option.KeyBinding; +import net.minecraft.client.util.InputUtil; +import net.minecraft.text.MutableText; +import net.minecraft.text.Style; +import net.minecraft.text.Text; +import net.minecraft.text.TextColor; +import net.minecraft.util.Formatting; +import org.lwjgl.glfw.GLFW; +import xyz.stachel.zombiesutils.config.Configs; + +public class ZombiesUtilsClient implements ClientModInitializer { + public static boolean PLAYER_VISIBILITY_SWITCH; + + private static KeyBinding PLAYER_VISIBILITY_KEY; + + @Override + public void onInitializeClient() { + PLAYER_VISIBILITY_KEY = KeyBindingHelper.registerKeyBinding(new KeyBinding( + "key.zombies-utils.toggle_player_visibility", + InputUtil.Type.KEYSYM, + GLFW.GLFW_KEY_V, + "category.zombies-utils" + )); + ClientTickEvents.END_CLIENT_TICK.register(client -> { + while (PLAYER_VISIBILITY_KEY.wasPressed()) { + PLAYER_VISIBILITY_SWITCH = !PLAYER_VISIBILITY_SWITCH; + MutableText message = PLAYER_VISIBILITY_SWITCH ? + Text.translatable("key.zombies-utils.toggle_player_visibility.toggle_on").setStyle(Style.EMPTY.withColor(Formatting.GREEN).withBold(true)) : + Text.translatable("key.zombies-utils.toggle_player_visibility.toggle_off").setStyle(Style.EMPTY.withColor(Formatting.RED).withBold(true)); + client.player.sendMessage(message, false); + } + }); + } +} diff --git a/src/main/java/xyz/stachel/zombiesutils/config/Config.java b/src/main/java/xyz/stachel/zombiesutils/config/Config.java index ef298f6..2432c0b 100644 --- a/src/main/java/xyz/stachel/zombiesutils/config/Config.java +++ b/src/main/java/xyz/stachel/zombiesutils/config/Config.java @@ -13,6 +13,7 @@ import java.nio.file.Path; public final class Config { private static final Gson GSON = new GsonBuilder() .registerTypeAdapter(IntConfig.class, new IntConfig.Serializer()) + .registerTypeAdapter(DoubleConfig.class, new DoubleConfig.Serializer()) .excludeFieldsWithModifiers(Modifier.TRANSIENT) .setPrettyPrinting() .create(); @@ -33,6 +34,11 @@ public final class Config { var intValue = value.getAsInt(); ((IntConfig) field.get(null)).setValue(intValue); } + + if (DoubleConfig.class.isAssignableFrom(fieldType)) { + var doubleValue = value.getAsDouble(); + ((DoubleConfig) field.get(null)).setValue(doubleValue); + } } } } catch (Exception exception) { diff --git a/src/main/java/xyz/stachel/zombiesutils/config/Configs.java b/src/main/java/xyz/stachel/zombiesutils/config/Configs.java index bf72d08..65d3856 100644 --- a/src/main/java/xyz/stachel/zombiesutils/config/Configs.java +++ b/src/main/java/xyz/stachel/zombiesutils/config/Configs.java @@ -1,5 +1,5 @@ package xyz.stachel.zombiesutils.config; public class Configs { - public static final IntConfig playerVisibleDistance = IntConfig.of("playerVisibilityDistance", 0); + public static final DoubleConfig playerVisibleDistance = DoubleConfig.of("playerInvisibilityDistance", 0.8D); } diff --git a/src/main/java/xyz/stachel/zombiesutils/config/DoubleConfig.java b/src/main/java/xyz/stachel/zombiesutils/config/DoubleConfig.java new file mode 100644 index 0000000..74e6421 --- /dev/null +++ b/src/main/java/xyz/stachel/zombiesutils/config/DoubleConfig.java @@ -0,0 +1,39 @@ +package xyz.stachel.zombiesutils.config; + +import com.google.gson.JsonElement; +import com.google.gson.JsonPrimitive; +import com.google.gson.JsonSerializationContext; +import com.google.gson.JsonSerializer; +import net.minecraft.client.gui.widget.ClickableWidget; +import net.minecraft.client.gui.widget.CyclingButtonWidget; +import net.minecraft.text.Text; + +import java.lang.reflect.Type; + +public class DoubleConfig extends AbstractConfig { + + public DoubleConfig(String key, Double defaultValue) { + super(key, defaultValue); + } + + public static DoubleConfig of(String key, double defaultValue) { + return new DoubleConfig(key, defaultValue); + } + + @Override + public ClickableWidget createWidget(Runnable callback) { + return CyclingButtonWidget.builder((value) -> Text.of(getValue().toString())).initially(getValue()) + .tooltip(value -> getTooltip()) + .build(getMessage(), (button, value) -> { + setValue(value); + callback.run(); + }); + } + + public static class Serializer implements JsonSerializer { + @Override + public JsonElement serialize(DoubleConfig value, Type type, JsonSerializationContext context) { + return new JsonPrimitive(value.getValue()); + } + } +} diff --git a/src/main/java/xyz/stachel/zombiesutils/game/Game.java b/src/main/java/xyz/stachel/zombiesutils/game/Game.java index 52f7714..53575b1 100644 --- a/src/main/java/xyz/stachel/zombiesutils/game/Game.java +++ b/src/main/java/xyz/stachel/zombiesutils/game/Game.java @@ -5,15 +5,18 @@ import net.minecraft.text.Text; import org.apache.commons.lang3.exception.ExceptionUtils; import org.jetbrains.annotations.NotNull; import xyz.stachel.zombiesutils.ZombiesUtils; - -import java.io.File; +import xyz.stachel.zombiesutils.game.recorder.Category; +import xyz.stachel.zombiesutils.game.recorder.RecordMessageSender; +import xyz.stachel.zombiesutils.game.recorder.files.CategoryFile; class Game extends Timer { private final boolean joinedRoundOne; private int round; - private final File gameFile; - private String category; + private final GameFile gameFile; + private final boolean roundOneRecorded; + private Category category; public final GameMode mode; + private boolean escaping; public Game(@NotNull final GameFile gameFile, @NotNull final GameMode mode) { super(); @@ -21,6 +24,7 @@ class Game extends Timer { this.round = 1; this.joinedRoundOne = true; this.mode = mode; + this.roundOneRecorded = true; } public Game(@NotNull final GameFile gameFile, @NotNull final GameMode mode, final int round) { @@ -29,6 +33,11 @@ class Game extends Timer { this.round = round; this.joinedRoundOne = round == 1; this.mode = mode; + this.roundOneRecorded = round == 1; + } + + public void setCategory(Category category) { + this.category = category; } public void pass(int round) { @@ -37,7 +46,7 @@ class Game extends Timer { return; } try { -// record(); + this.record(); } catch (Exception e) { ZombiesUtils.LOGGER.error(ExceptionUtils.getStackTrace(e)); MinecraftClient.getInstance().player.sendMessage(Text.of("Error recording splits"), false); @@ -52,4 +61,47 @@ class Game extends Timer { } return super.split(); } + + public void helicopter() { + if (!this.mode.isMap(GameMode.Map.PRISON)) { + MinecraftClient.getInstance().player.sendMessage(Text.of("You are not imprisoned. Yet."), false); + ZombiesUtils.LOGGER.error(Thread.currentThread().getStackTrace().toString()); + return; + } + this.escaping = true; + this.pass(30); + } + + private void record() { + this.compareSegment(); + if (this.roundOneRecorded) this.compareBest(); + this.gameFile.setSegment(this.round, (int) this.roundTime()); + } + + public void compareSegment() throws IndexOutOfBoundsException { + if (this.escaping) return; + final CategoryFile categoryFile = this.category.getByGameMode(this.mode); + final short bestSegment = categoryFile.getBestSegment(round); + final int roundTime = (int) this.roundTime(); + + if (bestSegment == (short) 0) categoryFile.setBestSegment(round, roundTime); + else if (roundTime < bestSegment) categoryFile.setBestSegment(round, roundTime); + final RecordMessageSender recordMessageSender = new RecordMessageSender(this.category.getName(), round, roundTime, bestSegment); + recordMessageSender.roundSplit(); + recordMessageSender.sendRecordMessage(); + } + + public void compareBest() throws IndexOutOfBoundsException { + final CategoryFile categoryFile = this.category.getByGameMode(this.mode); + final int round = this.escaping ? 31 : this.round; + final int personalBest = categoryFile.getPersonalBest(round); + final int gameTime = (int) this.gameTime(); + + if (personalBest == 0) categoryFile.setPersonalBest(round, gameTime); + else if (gameTime < personalBest) categoryFile.setPersonalBest(round, gameTime); + final RecordMessageSender recordMessageSender = new RecordMessageSender(category.getName(), round, gameTime, personalBest); + if (!escaping) recordMessageSender.gameSplit(); + else recordMessageSender.helicopterSplit(); + recordMessageSender.sendRecordMessage(); + } } diff --git a/src/main/java/xyz/stachel/zombiesutils/game/GameFile.java b/src/main/java/xyz/stachel/zombiesutils/game/GameFile.java index c1088a1..d055473 100644 --- a/src/main/java/xyz/stachel/zombiesutils/game/GameFile.java +++ b/src/main/java/xyz/stachel/zombiesutils/game/GameFile.java @@ -1,6 +1,12 @@ package xyz.stachel.zombiesutils.game; +import net.minecraft.client.MinecraftClient; +import net.minecraft.text.Text; +import org.apache.commons.lang3.exception.ExceptionUtils; import org.jetbrains.annotations.NotNull; +import xyz.stachel.zombiesutils.ZombiesUtils; +import xyz.stachel.zombiesutils.game.recorder.FileManager; +import xyz.stachel.zombiesutils.game.recorder.data.GameData; import java.io.File; import java.io.FileWriter; @@ -10,10 +16,14 @@ import java.time.format.DateTimeFormatter; import java.time.temporal.ChronoUnit; class GameFile extends File { - private FileWriter writer; + private final GameData data; + + private FileWriter writer; GameFile(@NotNull final String serverNumber) { super(new File("zombies", "runs"), formattedTime() + "_" + serverNumber + ".seg2"); + this.data = new GameData(ZombiesUtils.getGameManager().getGame(serverNumber).mode.getMap()); + FileManager.createDataFile(this, this.data); } private static String formattedTime() { @@ -39,4 +49,15 @@ class GameFile extends File { void clean() throws IOException { this.writer.close(); } + + public void setSegment(int round, int ticks) { + this.data.setSegment(round - 1, ticks); + + try { + FileManager.writeDataToFile(this, this.data); + } catch (Exception e) { + ZombiesUtils.LOGGER.error(ExceptionUtils.getStackTrace(e)); + MinecraftClient.getInstance().player.sendMessage(Text.of("Error saving segment to run-file. Please Contact Stachelbeere1248."), false); + } + } } diff --git a/src/main/java/xyz/stachel/zombiesutils/game/GameManager.java b/src/main/java/xyz/stachel/zombiesutils/game/GameManager.java index 9356906..1c84e99 100644 --- a/src/main/java/xyz/stachel/zombiesutils/game/GameManager.java +++ b/src/main/java/xyz/stachel/zombiesutils/game/GameManager.java @@ -34,14 +34,18 @@ public class GameManager { } } - public Game getGame() { + public Optional getGame() { final String sn = Location.getServerNumber(); + return Optional.of(this.games.get(sn)); + } + + public Game getGame(String sn) { return this.games.get(sn); } public void endGame(final String serverNumber, final boolean isWin) { if (!games.containsKey(serverNumber)) return; - final Game game = games.get(serverNumber); + final Game game = this.getGame(serverNumber); if (isWin) { switch (game.mode.getMap()) { case DEAD_END: @@ -62,14 +66,14 @@ public class GameManager { final String serverNumber = Location.getServerNumber(); if (games.containsKey(serverNumber)) { if (round == 0) addGame(serverNumber, mode, round); - else games.get(serverNumber).pass(round); + else this.getGame(serverNumber).pass(round); } else addGame(serverNumber, mode, round); } public void setDifficulty(@NotNull GameMode.Difficulty difficulty) { this.queuedDifficultyServer = Location.getServerNumber(); if (this.games.containsKey(this.queuedDifficultyServer)) { - this.games.get(this.queuedDifficultyServer).mode.changeDifficulty(difficulty); + this.getGame(this.queuedDifficultyServer).mode.changeDifficulty(difficulty); } else this.queuedDifficulty = Optional.of(difficulty); } diff --git a/src/main/java/xyz/stachel/zombiesutils/game/GameMode.java b/src/main/java/xyz/stachel/zombiesutils/game/GameMode.java index 27589cc..9f665ce 100644 --- a/src/main/java/xyz/stachel/zombiesutils/game/GameMode.java +++ b/src/main/java/xyz/stachel/zombiesutils/game/GameMode.java @@ -23,6 +23,14 @@ public class GameMode { return this.map; } + public Difficulty getDifficulty() { + return this.difficulty; + } + + public boolean isMap(Map map) { + return map == this.map; + } + public enum Map { DEAD_END, BAD_BLOOD, PRISON, ALIEN_ARCADIUM; } diff --git a/src/main/java/xyz/stachel/zombiesutils/game/Prefix.java b/src/main/java/xyz/stachel/zombiesutils/game/Prefix.java new file mode 100644 index 0000000..8b25f58 --- /dev/null +++ b/src/main/java/xyz/stachel/zombiesutils/game/Prefix.java @@ -0,0 +1,36 @@ +package xyz.stachel.zombiesutils.game; + +public enum Prefix { + BOSS(0xCC5555, "B", 0x7A3333), + BLAZES(0xEFB61F, "BL", 0x8F6D0F), + SLIME(0x88FF88, "S", 0x51A951), + HBM(0x2A415F, "HBM", 0x193241), + WITHER_SKELETON(0x888888, "WS", 0x515151), + OLD_ONE(0x55AA55, "O1", 0x336633), + GIANT(0x00FFFF, "G", 0x009999), + POLICE(0x16537E, "P", 0x0E324D), + CELL(0xFF8234, "C", 0x99501F), + WINDOW(0xAAAAAA, "W", 0x666666); + + private final int color; + private final int fadedColor; + private final String prefix; + + Prefix(final int color, final String prefix, final int fadedColor) { + this.color = color; + this.prefix = prefix; + this.fadedColor = fadedColor; + } + + public int getColor() { + return color; + } + + public String getPrefix() { + return prefix; + } + + public int getFadedColor() { + return fadedColor; + } +} diff --git a/src/main/java/xyz/stachel/zombiesutils/game/recorder/Category.java b/src/main/java/xyz/stachel/zombiesutils/game/recorder/Category.java new file mode 100644 index 0000000..cda11a3 --- /dev/null +++ b/src/main/java/xyz/stachel/zombiesutils/game/recorder/Category.java @@ -0,0 +1,77 @@ +package xyz.stachel.zombiesutils.game.recorder; + +import org.jetbrains.annotations.NotNull; +import xyz.stachel.zombiesutils.ZombiesUtils; +import xyz.stachel.zombiesutils.game.GameMode; +import xyz.stachel.zombiesutils.game.recorder.files.CategoryFile; +import xyz.stachel.zombiesutils.util.Utils; + +import java.io.File; + +import static xyz.stachel.zombiesutils.game.GameMode.Map.*; + +public class Category { + private static String selectedCategory /*= ZombiesUtils.getInstance().getConfig().getDefaultCategory()*/; + public final CategoryFile[] categoryFiles = new CategoryFile[10]; + private final String name; + + public Category() { + final File category; + if (Utils.isHypixel()) category = new File(new File("zombies", "splits"), selectedCategory); + else category = new File(new File("zombies", "practise-splits"), selectedCategory); + categoryFiles[0] = new CategoryFile(category, DEAD_END, GameMode.Difficulty.NORMAL); + categoryFiles[1] = new CategoryFile(category, DEAD_END, GameMode.Difficulty.HARD); + categoryFiles[2] = new CategoryFile(category, DEAD_END, GameMode.Difficulty.RIP); + + categoryFiles[3] = new CategoryFile(category, BAD_BLOOD, GameMode.Difficulty.NORMAL); + categoryFiles[4] = new CategoryFile(category, BAD_BLOOD, GameMode.Difficulty.HARD); + categoryFiles[5] = new CategoryFile(category, BAD_BLOOD, GameMode.Difficulty.RIP); + + categoryFiles[6] = new CategoryFile(category, ALIEN_ARCADIUM, GameMode.Difficulty.NORMAL); + + categoryFiles[7] = new CategoryFile(category, PRISON, GameMode.Difficulty.NORMAL); + categoryFiles[8] = new CategoryFile(category, PRISON, GameMode.Difficulty.HARD); + categoryFiles[9] = new CategoryFile(category, PRISON, GameMode.Difficulty.RIP); + + this.name = Category.selectedCategory; + } + +// public static void setSelectedCategory(String selectedCategory) { +// Category.selectedCategory = selectedCategory; +// ZombiesUtils.getGameManager().getGame().ifPresent(game -> game.setCategory(new Category())); +// } + + public static String[] getCategories() { + File dir; + if (Utils.isHypixel()) dir = new File("zombies" + File.separator + "splits"); + else dir = new File("zombies" + File.separator + "practise-splits"); + if (dir.isDirectory()) return dir.list(); + else return new String[0]; + } + + public CategoryFile getByGameMode(@NotNull GameMode gameMode) { + return switch (gameMode.getMap()) { + case DEAD_END -> switch (gameMode.getDifficulty()) { + case NORMAL -> categoryFiles[0]; + case HARD -> categoryFiles[1]; + case RIP -> categoryFiles[2]; + }; + case BAD_BLOOD -> switch (gameMode.getDifficulty()) { + case NORMAL -> categoryFiles[3]; + case HARD -> categoryFiles[4]; + case RIP -> categoryFiles[5]; + }; + case ALIEN_ARCADIUM -> categoryFiles[6]; + case PRISON -> switch (gameMode.getDifficulty()) { + case NORMAL -> categoryFiles[7]; + case HARD -> categoryFiles[8]; + case RIP -> categoryFiles[9]; + }; + default -> throw new IllegalStateException("Unexpected value: " + gameMode); + }; + } + + public String getName() { + return name; + } +} \ No newline at end of file diff --git a/src/main/java/xyz/stachel/zombiesutils/game/recorder/FileManager.java b/src/main/java/xyz/stachel/zombiesutils/game/recorder/FileManager.java new file mode 100644 index 0000000..fd5e184 --- /dev/null +++ b/src/main/java/xyz/stachel/zombiesutils/game/recorder/FileManager.java @@ -0,0 +1,58 @@ +package xyz.stachel.zombiesutils.game.recorder; + +import com.google.gson.Gson; +import com.google.gson.JsonSyntaxException; +import org.apache.commons.io.FileUtils; +import org.jetbrains.annotations.NotNull; +import xyz.stachel.zombiesutils.game.recorder.data.CategoryData; +import xyz.stachel.zombiesutils.game.recorder.files.CategoryFile; + +import java.io.File; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.nio.charset.StandardCharsets; + +public class FileManager { + private static CategoryData readDataFromFile(@NotNull File file) throws FileNotFoundException, JsonSyntaxException { + if (!file.exists()) throw new FileNotFoundException(); + + String dataJson; + Gson gson = new Gson(); + try { + dataJson = FileUtils.readFileToString(file, StandardCharsets.UTF_16); + } catch (IOException e) { + throw new RuntimeException(e); + } + if (dataJson == null || dataJson.trim().isEmpty()) throw new JsonSyntaxException("File empty"); + + return gson.fromJson(dataJson, CategoryData.class); + } + + public static void createDataFile(@NotNull File file, ISplitsData data) { + try { + //noinspection ResultOfMethodCallIgnored + file.getParentFile().mkdirs(); + //noinspection ResultOfMethodCallIgnored + file.createNewFile(); + writeDataToFile(file, data); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + + public static void writeDataToFile(File file, @NotNull ISplitsData data) throws IOException { + FileUtils.writeStringToFile(file, data.toJSON(), StandardCharsets.UTF_16); + } + + @NotNull + public static CategoryData categoryReadOrCreate(CategoryFile file) { + CategoryData data; + try { + data = FileManager.readDataFromFile(file); + } catch (FileNotFoundException | JsonSyntaxException ignored) { + data = new CategoryData(file.getMap()); + FileManager.createDataFile(file, data); + } + return data; + } +} \ No newline at end of file diff --git a/src/main/java/xyz/stachel/zombiesutils/game/recorder/ISplitsData.java b/src/main/java/xyz/stachel/zombiesutils/game/recorder/ISplitsData.java new file mode 100644 index 0000000..28027fd --- /dev/null +++ b/src/main/java/xyz/stachel/zombiesutils/game/recorder/ISplitsData.java @@ -0,0 +1,5 @@ +package xyz.stachel.zombiesutils.game.recorder; + +public interface ISplitsData { + String toJSON(); +} \ No newline at end of file diff --git a/src/main/java/xyz/stachel/zombiesutils/game/recorder/RecordMessageSender.java b/src/main/java/xyz/stachel/zombiesutils/game/recorder/RecordMessageSender.java new file mode 100644 index 0000000..38dbfda --- /dev/null +++ b/src/main/java/xyz/stachel/zombiesutils/game/recorder/RecordMessageSender.java @@ -0,0 +1,54 @@ +package xyz.stachel.zombiesutils.game.recorder; + +import org.jetbrains.annotations.Contract; + +public class RecordMessageSender { + + private final StringBuilder recordMessage; + private final int newTime; + private final int oldTime; + private final int round; + private final String deltaString; + private final String timeString; + private String copyString; + + public RecordMessageSender(final String categoryName, final int round, final int newTime, final int oldTime) { + this.recordMessage = new StringBuilder( + "§l§a▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬\n§e Category: §d" + categoryName + ); + this.newTime = newTime; + this.oldTime = oldTime; + this.deltaString = oldTime != 0 ? " " + formattedDelta(newTime, oldTime) : ""; + this.timeString = formattedTime(newTime); + this.round = round; + } + + public void sendRecordMessage() { + } + + public void gameSplit() { + } + + public void roundSplit() { + } + + public void helicopterSplit() { + } + + @Contract(pure = true) + private String formattedTime(int time) { + time *= 50; + return String.format("%d:%02d.%d%d", + time / 60000, + (time % 60000) / 1000, + (time % 1000) / 100, + (time % 100) / 10 + ); + } + + @Contract(pure = true) + private String formattedDelta(int newTime, int oldTime) { + final double delta = (double) (newTime - oldTime) / 20; + return String.format("%+.2f", delta); + } +} diff --git a/src/main/java/xyz/stachel/zombiesutils/game/recorder/data/CategoryData.java b/src/main/java/xyz/stachel/zombiesutils/game/recorder/data/CategoryData.java new file mode 100644 index 0000000..10379fb --- /dev/null +++ b/src/main/java/xyz/stachel/zombiesutils/game/recorder/data/CategoryData.java @@ -0,0 +1,58 @@ +package xyz.stachel.zombiesutils.game.recorder.data; + +import com.google.gson.Gson; +import org.jetbrains.annotations.NotNull; +import xyz.stachel.zombiesutils.game.GameMode; +import xyz.stachel.zombiesutils.game.recorder.ISplitsData; + +import java.util.Arrays; + +public class CategoryData implements ISplitsData { + private final short[] bestSegments; //in ticks, max ~27 min + private final int[] personalBests; //in ticks, + + public CategoryData(@NotNull GameMode.Map map) throws IllegalStateException { + switch (map) { + case ALIEN_ARCADIUM: + bestSegments = new short[105]; + personalBests = new int[105]; + break; + case DEAD_END: + case BAD_BLOOD: + bestSegments = new short[30]; + personalBests = new int[30]; + break; + case PRISON: + bestSegments = new short[30]; + personalBests = new int[31]; + break; + default: + throw new IllegalStateException("Not a map: " + map); + } + Arrays.fill(bestSegments, (short) 0); + Arrays.fill(personalBests, 0); + } + + @Override + @NotNull + public String toJSON() { + Gson gson = new Gson(); + return gson.toJson(this, CategoryData.class); + } + + public short getBestSegment(int index) { + return bestSegments[index]; + } + + public int getPersonalBest(int index) { + return personalBests[index]; + } + + public void setBestSegment(int index, int ticks) { + bestSegments[index] = (short) ticks; + } + + public void setPersonalBest(int index, int ticks) { + personalBests[index] = ticks; + } +} diff --git a/src/main/java/xyz/stachel/zombiesutils/game/recorder/data/GameData.java b/src/main/java/xyz/stachel/zombiesutils/game/recorder/data/GameData.java new file mode 100644 index 0000000..ebf996f --- /dev/null +++ b/src/main/java/xyz/stachel/zombiesutils/game/recorder/data/GameData.java @@ -0,0 +1,41 @@ +package xyz.stachel.zombiesutils.game.recorder.data; + +import com.google.gson.Gson; +import org.jetbrains.annotations.NotNull; +import xyz.stachel.zombiesutils.game.GameMode; +import xyz.stachel.zombiesutils.game.recorder.ISplitsData; + +import java.util.Arrays; + +public class GameData implements ISplitsData { + private final short[] segments; + + public GameData(@NotNull GameMode.Map map) throws IllegalStateException { + switch (map) { + case ALIEN_ARCADIUM: + segments = new short[105]; + break; + case DEAD_END: + case BAD_BLOOD: + segments = new short[30]; + break; + case PRISON: + segments = new short[31]; + break; + default: + throw new IllegalStateException("Not a map: " + map); + } + Arrays.fill(segments, (short) 0); + } + + @Override + @NotNull + public String toJSON() { + Gson gson = new Gson(); + return gson.toJson(this.segments); + } + + public void setSegment(int index, int ticks) { + segments[index] = (short) ticks; + } +} diff --git a/src/main/java/xyz/stachel/zombiesutils/game/recorder/files/CategoryFile.java b/src/main/java/xyz/stachel/zombiesutils/game/recorder/files/CategoryFile.java new file mode 100644 index 0000000..a573c6b --- /dev/null +++ b/src/main/java/xyz/stachel/zombiesutils/game/recorder/files/CategoryFile.java @@ -0,0 +1,66 @@ +package xyz.stachel.zombiesutils.game.recorder.files; + +import net.minecraft.client.MinecraftClient; +import net.minecraft.text.Text; +import org.apache.commons.lang3.exception.ExceptionUtils; +import org.jetbrains.annotations.NotNull; +import xyz.stachel.zombiesutils.ZombiesUtils; +import xyz.stachel.zombiesutils.game.GameMode; +import xyz.stachel.zombiesutils.game.recorder.FileManager; +import xyz.stachel.zombiesutils.game.recorder.data.CategoryData; + +import java.io.File; + +public class CategoryFile extends File { + private final CategoryData data; + private final GameMode.Map map; + private final GameMode.Difficulty difficulty; + + public CategoryFile(File category, @NotNull GameMode.Map map, GameMode.Difficulty difficulty) { + // Game-directory -> custom category -> file named "MAP_DIFFICULTY.times" + // Content encoded in StandardCharsets.UTF_16 + super(category, map + "_" + difficulty + ".times"); + this.map = map; + this.difficulty = difficulty; + this.data = FileManager.categoryReadOrCreate(this); + } + + public short getBestSegment(int round) { + return this.data.getBestSegment(round - 1); + } + + public void setBestSegment(int round, int ticks) { + this.data.setBestSegment(round - 1, ticks); + + try { + FileManager.writeDataToFile(this, this.data); + } catch (Exception e) { + ZombiesUtils.LOGGER.error(ExceptionUtils.getStackTrace(e)); + MinecraftClient.getInstance().player.sendMessage(Text.of("Error saving segment to splits-file. Please Contact Stachelbeere1248."), false); + } + } + + public int getPersonalBest(int round) { + return data.getPersonalBest(round - 1); + } + + public void setPersonalBest(int round, int ticks) { + this.data.setPersonalBest(round - 1, ticks); + + try { + FileManager.writeDataToFile(this, this.data); + } catch (Exception e) { + ZombiesUtils.LOGGER.error(ExceptionUtils.getStackTrace(e)); + MinecraftClient.getInstance().player.sendMessage(Text.of("Error saving pb to splits-file. Please Contact Stachelbeere1248."), false); + } + } + + public GameMode.Map getMap() { + return this.map; + } + + public GameMode.Difficulty getDifficulty() { + return this.difficulty; + } + +} \ No newline at end of file diff --git a/src/main/java/xyz/stachel/zombiesutils/mixin/ClientPlayNetworkHandlerMixin.java b/src/main/java/xyz/stachel/zombiesutils/mixin/ClientPlayNetworkHandlerMixin.java index 78ddda8..a60ac8d 100644 --- a/src/main/java/xyz/stachel/zombiesutils/mixin/ClientPlayNetworkHandlerMixin.java +++ b/src/main/java/xyz/stachel/zombiesutils/mixin/ClientPlayNetworkHandlerMixin.java @@ -6,6 +6,7 @@ import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.injection.At; import org.spongepowered.asm.mixin.injection.Inject; import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; +import xyz.stachel.zombiesutils.ZombiesUtils; import xyz.stachel.zombiesutils.game.GameManager; import java.util.regex.Matcher; @@ -22,7 +23,7 @@ public abstract class ClientPlayNetworkHandlerMixin { if (matcher.find()) { int number = Integer.parseInt(matcher.group(1)); if (number >= 1 && number <= 105) { - new GameManager().onRound(number); + ZombiesUtils.getGameManager().onRound(number); } } } diff --git a/src/main/java/xyz/stachel/zombiesutils/mixin/OtherClientPlayerEntityMixin.java b/src/main/java/xyz/stachel/zombiesutils/mixin/OtherClientPlayerEntityMixin.java index 556d837..5cd713a 100644 --- a/src/main/java/xyz/stachel/zombiesutils/mixin/OtherClientPlayerEntityMixin.java +++ b/src/main/java/xyz/stachel/zombiesutils/mixin/OtherClientPlayerEntityMixin.java @@ -7,6 +7,7 @@ import net.minecraft.client.network.OtherClientPlayerEntity; import net.minecraft.client.world.ClientWorld; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.injection.At; +import xyz.stachel.zombiesutils.ZombiesUtilsClient; import xyz.stachel.zombiesutils.config.Configs; @Mixin(OtherClientPlayerEntity.class) @@ -18,13 +19,13 @@ public abstract class OtherClientPlayerEntityMixin extends AbstractClientPlayerE @ModifyReturnValue(method = "shouldRender", at = @At(value = "RETURN")) public boolean shouldRender(boolean original, double distance) { - int dist = Configs.playerVisibleDistance.getValue(); - double d = this.getBoundingBox().getAverageSideLength() * 1.75 + (double) dist; - if (Double.isNaN(d)) { - d = 1.0; - } + if (ZombiesUtilsClient.PLAYER_VISIBILITY_SWITCH) { + double dist = Configs.playerVisibleDistance.getValue(); + double d = this.getBoundingBox().getAverageSideLength() + dist; - d *= 1.0; - return (distance >= d * d) && original; + return (distance >= d * d) && original; + } else { + return original; + } } } diff --git a/src/main/java/xyz/stachel/zombiesutils/util/Utils.java b/src/main/java/xyz/stachel/zombiesutils/util/Utils.java index 011379f..1e0df06 100644 --- a/src/main/java/xyz/stachel/zombiesutils/util/Utils.java +++ b/src/main/java/xyz/stachel/zombiesutils/util/Utils.java @@ -1,21 +1,22 @@ package xyz.stachel.zombiesutils.util; +import net.minecraft.client.MinecraftClient; +import org.jetbrains.annotations.Nullable; import xyz.stachel.zombiesutils.game.GameMode; import xyz.stachel.zombiesutils.handlers.Location; -import java.util.Optional; - import static xyz.stachel.zombiesutils.game.GameMode.Map.*; public class Utils { - public static Optional getMap() { + @Nullable + public static GameMode.Map getMap() { return switch (Location.getMode()) { - case "ZOMBIES_DEAD_END" -> Optional.of(DEAD_END); - case "ZOMBIES_BAD_BLOOD" -> Optional.of(BAD_BLOOD); - case "ZOMBIES_ALIEN_ARCADIUM" -> Optional.of(ALIEN_ARCADIUM); - case "ZOMBIES_PRISON" -> Optional.of(PRISON); - default -> Optional.empty(); + case "ZOMBIES_DEAD_END" -> DEAD_END; + case "ZOMBIES_BAD_BLOOD" -> BAD_BLOOD; + case "ZOMBIES_ALIEN_ARCADIUM" -> ALIEN_ARCADIUM; + case "ZOMBIES_PRISON" -> PRISON; + default -> null; }; } @@ -23,4 +24,8 @@ public class Utils { return Location.getMode().startsWith("ZOMBIES"); } + public static boolean isHypixel() { + return true; + } + } diff --git a/src/main/resources/assets/zombies-utils/lang/en_us.json b/src/main/resources/assets/zombies-utils/lang/en_us.json new file mode 100644 index 0000000..a407743 --- /dev/null +++ b/src/main/resources/assets/zombies-utils/lang/en_us.json @@ -0,0 +1,6 @@ +{ + "category.zombies-utils": "Zombies Utils", + "key.zombies-utils.toggle_player_visibility": "Toggle Nearest Players", + "key.zombies-utils.toggle_player_visibility.toggle_on": "Nearest Players Are Now Invisible", + "key.zombies-utils.toggle_player_visibility.toggle_off": "Nearest Players Are Now Visible" +} \ No newline at end of file diff --git a/src/main/resources/assets/zombies-utils/lang/ru_ru.json b/src/main/resources/assets/zombies-utils/lang/ru_ru.json new file mode 100644 index 0000000..9566dab --- /dev/null +++ b/src/main/resources/assets/zombies-utils/lang/ru_ru.json @@ -0,0 +1,6 @@ +{ + "category.zombies-utils": "Zombies Utils", + "key.zombies-utils.toggle_player_visibility": "Видимость ближайших игроков", + "key.zombies-utils.toggle_player_visibility.toggle_on": "Ближайшие игроки к вам теперь невидимы.", + "key.zombies-utils.toggle_player_visibility.toggle_off": "Ближайшие игроки к вам теперь видимы." +} \ No newline at end of file diff --git a/src/main/resources/fabric.mod.json b/src/main/resources/fabric.mod.json index ca2e0a2..332d8b0 100644 --- a/src/main/resources/fabric.mod.json +++ b/src/main/resources/fabric.mod.json @@ -17,7 +17,10 @@ "entrypoints": { "main": [ "xyz.stachel.zombiesutils.ZombiesUtils" - ] + ], + "client": [ + "xyz.stachel.zombiesutils.ZombiesUtilsClient" + ] }, "mixins": [ { -- 2.39.5 From fdb9acb04a2d2528a48d15f130c0c48d447d75a8 Mon Sep 17 00:00:00 2001 From: LB2345 <163643124+LovelyBunny11@users.noreply.github.com> Date: Sun, 25 May 2025 18:47:50 +0300 Subject: [PATCH 6/8] Fixed crashing on round. --- .../zombiesutils/ZombiesUtilsClient.java | 2 - .../xyz/stachel/zombiesutils/game/Game.java | 6 +- .../stachel/zombiesutils/game/GameFile.java | 10 ++- .../zombiesutils/game/GameManager.java | 13 +--- .../zombiesutils/game/recorder/Category.java | 9 +-- .../game/recorder/files/CategoryFile.java | 6 +- .../zombiesutils/handlers/Location.java | 4 +- .../mixin/ClientPlayNetworkHandlerMixin.java | 18 +++-- .../zombiesutils/util/LanguageSupport.java | 78 +++++++++++++++++++ .../xyz/stachel/zombiesutils/util/Utils.java | 5 +- 10 files changed, 114 insertions(+), 37 deletions(-) create mode 100644 src/main/java/xyz/stachel/zombiesutils/util/LanguageSupport.java diff --git a/src/main/java/xyz/stachel/zombiesutils/ZombiesUtilsClient.java b/src/main/java/xyz/stachel/zombiesutils/ZombiesUtilsClient.java index b44b6de..2f71267 100644 --- a/src/main/java/xyz/stachel/zombiesutils/ZombiesUtilsClient.java +++ b/src/main/java/xyz/stachel/zombiesutils/ZombiesUtilsClient.java @@ -8,10 +8,8 @@ import net.minecraft.client.util.InputUtil; import net.minecraft.text.MutableText; import net.minecraft.text.Style; import net.minecraft.text.Text; -import net.minecraft.text.TextColor; import net.minecraft.util.Formatting; import org.lwjgl.glfw.GLFW; -import xyz.stachel.zombiesutils.config.Configs; public class ZombiesUtilsClient implements ClientModInitializer { public static boolean PLAYER_VISIBILITY_SWITCH; diff --git a/src/main/java/xyz/stachel/zombiesutils/game/Game.java b/src/main/java/xyz/stachel/zombiesutils/game/Game.java index 53575b1..1e2cd98 100644 --- a/src/main/java/xyz/stachel/zombiesutils/game/Game.java +++ b/src/main/java/xyz/stachel/zombiesutils/game/Game.java @@ -1,7 +1,9 @@ package xyz.stachel.zombiesutils.game; import net.minecraft.client.MinecraftClient; +import net.minecraft.text.Style; import net.minecraft.text.Text; +import net.minecraft.util.Formatting; import org.apache.commons.lang3.exception.ExceptionUtils; import org.jetbrains.annotations.NotNull; import xyz.stachel.zombiesutils.ZombiesUtils; @@ -49,7 +51,7 @@ class Game extends Timer { this.record(); } catch (Exception e) { ZombiesUtils.LOGGER.error(ExceptionUtils.getStackTrace(e)); - MinecraftClient.getInstance().player.sendMessage(Text.of("Error recording splits"), false); + MinecraftClient.getInstance().player.sendMessage(Text.literal("Error recording splits").setStyle(Style.EMPTY.withColor(Formatting.RED)), false); } this.split(); this.round = round + 1; @@ -64,7 +66,7 @@ class Game extends Timer { public void helicopter() { if (!this.mode.isMap(GameMode.Map.PRISON)) { - MinecraftClient.getInstance().player.sendMessage(Text.of("You are not imprisoned. Yet."), false); + MinecraftClient.getInstance().player.sendMessage(Text.literal("You are not imprisoned. Yet.").setStyle(Style.EMPTY.withColor(Formatting.RED)), false); ZombiesUtils.LOGGER.error(Thread.currentThread().getStackTrace().toString()); return; } diff --git a/src/main/java/xyz/stachel/zombiesutils/game/GameFile.java b/src/main/java/xyz/stachel/zombiesutils/game/GameFile.java index d055473..5d398e6 100644 --- a/src/main/java/xyz/stachel/zombiesutils/game/GameFile.java +++ b/src/main/java/xyz/stachel/zombiesutils/game/GameFile.java @@ -1,7 +1,9 @@ package xyz.stachel.zombiesutils.game; import net.minecraft.client.MinecraftClient; +import net.minecraft.text.Style; import net.minecraft.text.Text; +import net.minecraft.util.Formatting; import org.apache.commons.lang3.exception.ExceptionUtils; import org.jetbrains.annotations.NotNull; import xyz.stachel.zombiesutils.ZombiesUtils; @@ -20,9 +22,9 @@ class GameFile extends File { private FileWriter writer; - GameFile(@NotNull final String serverNumber) { - super(new File("zombies", "runs"), formattedTime() + "_" + serverNumber + ".seg2"); - this.data = new GameData(ZombiesUtils.getGameManager().getGame(serverNumber).mode.getMap()); + GameFile(@NotNull final String serverNumber, @NotNull final GameMode mode) { + super(new File("zombies", "runs"), formattedTime() + "_" + serverNumber + ".seg2"); + this.data = new GameData(mode.getMap()); FileManager.createDataFile(this, this.data); } @@ -57,7 +59,7 @@ class GameFile extends File { FileManager.writeDataToFile(this, this.data); } catch (Exception e) { ZombiesUtils.LOGGER.error(ExceptionUtils.getStackTrace(e)); - MinecraftClient.getInstance().player.sendMessage(Text.of("Error saving segment to run-file. Please Contact Stachelbeere1248."), false); + MinecraftClient.getInstance().player.sendMessage(Text.literal("Error saving segment to run-file. Please Contact Stachelbeere1248.").setStyle(Style.EMPTY.withColor(Formatting.RED)), false); } } } diff --git a/src/main/java/xyz/stachel/zombiesutils/game/GameManager.java b/src/main/java/xyz/stachel/zombiesutils/game/GameManager.java index 1c84e99..6016950 100644 --- a/src/main/java/xyz/stachel/zombiesutils/game/GameManager.java +++ b/src/main/java/xyz/stachel/zombiesutils/game/GameManager.java @@ -19,7 +19,7 @@ public class GameManager { this.queuedDifficulty.ifPresent(mode::changeDifficulty); } this.queuedDifficulty = Optional.empty(); - this.games.put(serverNumber, new Game(new GameFile(serverNumber), mode)); + this.games.put(serverNumber, new Game(new GameFile(serverNumber, mode), mode, round)); } public void onRound(final int round) { @@ -27,18 +27,13 @@ public class GameManager { final String mode = Location.getMode(); if (sn == null || mode == null || !Utils.isZombies()) return; - if (!this.games.containsKey(sn)) { - addGame(sn, new GameMode(Map.DEAD_END), round); + if (this.getGame(sn) == null) { + this.addGame(sn, new GameMode(Utils.getMap(mode)), round); } else { - this.games.get(sn).split(round); + this.getGame(sn).split(round); } } - public Optional getGame() { - final String sn = Location.getServerNumber(); - return Optional.of(this.games.get(sn)); - } - public Game getGame(String sn) { return this.games.get(sn); } diff --git a/src/main/java/xyz/stachel/zombiesutils/game/recorder/Category.java b/src/main/java/xyz/stachel/zombiesutils/game/recorder/Category.java index cda11a3..dd58ed1 100644 --- a/src/main/java/xyz/stachel/zombiesutils/game/recorder/Category.java +++ b/src/main/java/xyz/stachel/zombiesutils/game/recorder/Category.java @@ -36,10 +36,10 @@ public class Category { this.name = Category.selectedCategory; } -// public static void setSelectedCategory(String selectedCategory) { -// Category.selectedCategory = selectedCategory; -// ZombiesUtils.getGameManager().getGame().ifPresent(game -> game.setCategory(new Category())); -// } + public static void setSelectedCategory(String selectedCategory) { + Category.selectedCategory = selectedCategory; + // ZombiesUtils.getGameManager().getGame().ifPresent(game -> game.setCategory(new Category())); + } public static String[] getCategories() { File dir; @@ -67,7 +67,6 @@ public class Category { case HARD -> categoryFiles[8]; case RIP -> categoryFiles[9]; }; - default -> throw new IllegalStateException("Unexpected value: " + gameMode); }; } diff --git a/src/main/java/xyz/stachel/zombiesutils/game/recorder/files/CategoryFile.java b/src/main/java/xyz/stachel/zombiesutils/game/recorder/files/CategoryFile.java index a573c6b..6adcbb2 100644 --- a/src/main/java/xyz/stachel/zombiesutils/game/recorder/files/CategoryFile.java +++ b/src/main/java/xyz/stachel/zombiesutils/game/recorder/files/CategoryFile.java @@ -1,7 +1,9 @@ package xyz.stachel.zombiesutils.game.recorder.files; import net.minecraft.client.MinecraftClient; +import net.minecraft.text.Style; import net.minecraft.text.Text; +import net.minecraft.util.Formatting; import org.apache.commons.lang3.exception.ExceptionUtils; import org.jetbrains.annotations.NotNull; import xyz.stachel.zombiesutils.ZombiesUtils; @@ -36,7 +38,7 @@ public class CategoryFile extends File { FileManager.writeDataToFile(this, this.data); } catch (Exception e) { ZombiesUtils.LOGGER.error(ExceptionUtils.getStackTrace(e)); - MinecraftClient.getInstance().player.sendMessage(Text.of("Error saving segment to splits-file. Please Contact Stachelbeere1248."), false); + MinecraftClient.getInstance().player.sendMessage(Text.literal("Error saving segment to splits-file. Please Contact Stachelbeere1248.").setStyle(Style.EMPTY.withColor(Formatting.RED)), false); } } @@ -51,7 +53,7 @@ public class CategoryFile extends File { FileManager.writeDataToFile(this, this.data); } catch (Exception e) { ZombiesUtils.LOGGER.error(ExceptionUtils.getStackTrace(e)); - MinecraftClient.getInstance().player.sendMessage(Text.of("Error saving pb to splits-file. Please Contact Stachelbeere1248."), false); + MinecraftClient.getInstance().player.sendMessage(Text.literal("Error saving pb to splits-file. Please Contact Stachelbeere1248.").setStyle(Style.EMPTY.withColor(Formatting.RED)), false); } } diff --git a/src/main/java/xyz/stachel/zombiesutils/handlers/Location.java b/src/main/java/xyz/stachel/zombiesutils/handlers/Location.java index 8685add..b98d7ad 100644 --- a/src/main/java/xyz/stachel/zombiesutils/handlers/Location.java +++ b/src/main/java/xyz/stachel/zombiesutils/handlers/Location.java @@ -10,9 +10,9 @@ public class Location { private static String mode; public static void onLocation(ClientboundLocationPacket p) { - Location.serverNumber = p.getServerName(); + serverNumber = p.getServerName(); Optional m = p.getMode(); - if (m.isPresent()) Location.mode = m.get(); + m.ifPresent(s -> mode = s); } public static String getServerNumber() { diff --git a/src/main/java/xyz/stachel/zombiesutils/mixin/ClientPlayNetworkHandlerMixin.java b/src/main/java/xyz/stachel/zombiesutils/mixin/ClientPlayNetworkHandlerMixin.java index a60ac8d..d61182f 100644 --- a/src/main/java/xyz/stachel/zombiesutils/mixin/ClientPlayNetworkHandlerMixin.java +++ b/src/main/java/xyz/stachel/zombiesutils/mixin/ClientPlayNetworkHandlerMixin.java @@ -3,6 +3,7 @@ package xyz.stachel.zombiesutils.mixin; import net.minecraft.client.network.ClientPlayNetworkHandler; import net.minecraft.network.packet.s2c.play.TitleS2CPacket; import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Unique; import org.spongepowered.asm.mixin.injection.At; import org.spongepowered.asm.mixin.injection.Inject; import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; @@ -14,17 +15,18 @@ import java.util.regex.Pattern; @Mixin(ClientPlayNetworkHandler.class) public abstract class ClientPlayNetworkHandlerMixin { + @Unique private static final Pattern pattern = Pattern.compile("Round (\\d{1,3})"); @Inject(at = @At("HEAD"), method = "onTitle") - private void zombiesUtils_onTitle(TitleS2CPacket p, CallbackInfo info) { - final String msg = p.text().getString(); - final Matcher matcher = pattern.matcher(msg); - if (matcher.find()) { - int number = Integer.parseInt(matcher.group(1)); - if (number >= 1 && number <= 105) { - ZombiesUtils.getGameManager().onRound(number); + private void zombiesUtils_onTitle(TitleS2CPacket p, CallbackInfo info) { + final String msg = p.text().getString(); + final Matcher matcher = pattern.matcher(msg); + if (matcher.find()) { + int number = Integer.parseInt(matcher.group(1)); + if (number >= 1 && number <= 105) { + ZombiesUtils.getGameManager().onRound(number); + } } } - } } diff --git a/src/main/java/xyz/stachel/zombiesutils/util/LanguageSupport.java b/src/main/java/xyz/stachel/zombiesutils/util/LanguageSupport.java new file mode 100644 index 0000000..0506073 --- /dev/null +++ b/src/main/java/xyz/stachel/zombiesutils/util/LanguageSupport.java @@ -0,0 +1,78 @@ +package xyz.stachel.zombiesutils.util; + +import org.jetbrains.annotations.NotNull; + +import java.util.Arrays; +import java.util.regex.Pattern; + +@SuppressWarnings("SpellCheckingInspection") +public class LanguageSupport { + private static final String[] LANGUAGES = { + "EN", + "FR", + "DE" + }; + + public static boolean isLoss(@NotNull String input) { + final String[] words = { + "§cGame Over!", + "§cPartie terminée!", + "§cDas Spiel ist vorbei!" + }; + return Arrays.asList(words).contains(input); + } + + public static boolean isWin(@NotNull String input) { + final String[] words = { + "§aYou Win!", + "§aVous avez gagné!", + "§aDu gewinnst!" + }; + return Arrays.asList(words).contains(input); + } + + public static boolean containsHard(@NotNull String input) { + final String[] words = { + "Hard Difficulty", + "Difficulté Hard", + "Hard Schwierigkeitsgrad", + "困难", + "困難" + }; + return Arrays.stream(words).anyMatch(input::contains); + } + + public static boolean containsRIP(@NotNull String input) { + final String[] words = { + "RIP Difficulty", + "Difficulté RIP", + "RIP Schwierigkeitsgrad", + "安息" + }; + return Arrays.stream(words).anyMatch(input::contains); + } + + public static boolean isHelicopterIncoming(@NotNull String input) { + final String[] words = { + "The Helicopter is on its way! Hold out for 120 more seconds!" + }; + return Arrays.stream(words).anyMatch(input::contains); + } + + public static @NotNull Pattern roundPattern(@NotNull String language) { + switch (language) { + case "EN": + return Pattern.compile("Round ([0-9]{1,3})"); + case "FR": + return Pattern.compile("Manche ([0-9]{1,3})"); + case "DE": + return Pattern.compile("Runde ([0-9]{1,3})"); + default: + throw new IllegalStateException("Unexpected value: " + language); + } + } + + public static String[] getLanguages() { + return LANGUAGES; + } +} \ No newline at end of file diff --git a/src/main/java/xyz/stachel/zombiesutils/util/Utils.java b/src/main/java/xyz/stachel/zombiesutils/util/Utils.java index 1e0df06..649e705 100644 --- a/src/main/java/xyz/stachel/zombiesutils/util/Utils.java +++ b/src/main/java/xyz/stachel/zombiesutils/util/Utils.java @@ -1,6 +1,5 @@ package xyz.stachel.zombiesutils.util; -import net.minecraft.client.MinecraftClient; import org.jetbrains.annotations.Nullable; import xyz.stachel.zombiesutils.game.GameMode; import xyz.stachel.zombiesutils.handlers.Location; @@ -10,8 +9,8 @@ import static xyz.stachel.zombiesutils.game.GameMode.Map.*; public class Utils { @Nullable - public static GameMode.Map getMap() { - return switch (Location.getMode()) { + public static GameMode.Map getMap(String mode) { + return switch (mode) { case "ZOMBIES_DEAD_END" -> DEAD_END; case "ZOMBIES_BAD_BLOOD" -> BAD_BLOOD; case "ZOMBIES_ALIEN_ARCADIUM" -> ALIEN_ARCADIUM; -- 2.39.5 From cc7c711bb74baa1967eeebcaf133e71144004387 Mon Sep 17 00:00:00 2001 From: Stachelbeere1248 Date: Wed, 28 May 2025 13:45:05 +0200 Subject: [PATCH 7/8] add yacl 3.6.6 to deps --- build.gradle | 11 +++++++++-- gradle.properties | 1 + src/main/resources/fabric.mod.json | 3 ++- 3 files changed, 12 insertions(+), 3 deletions(-) diff --git a/build.gradle b/build.gradle index 9af532f..afc746a 100644 --- a/build.gradle +++ b/build.gradle @@ -11,7 +11,11 @@ base { } repositories { - maven { url 'https://repo.hypixel.net/repository/Hypixel/' } + maven { url 'https://repo.hypixel.net/repository/Hypixel/' }, + maven { + name 'Xander Maven' + url 'https://maven.isxander.dev/releases' + } } loom { @@ -32,7 +36,10 @@ dependencies { // Fabric API. This is technically optional, but you probably want it anyway. modImplementation "net.fabricmc.fabric-api:fabric-api:${project.fabric_version}" + + // other deps modImplementation 'net.hypixel:mod-api:1.0.1' + modImplementation "dev.isxander:yet-another-config-lib:${project.yacl_version}" } processResources { @@ -81,4 +88,4 @@ publishing { // The repositories here will be used for publishing your artifact, not for // retrieving dependencies. } -} \ No newline at end of file +} diff --git a/gradle.properties b/gradle.properties index 29518a5..aaf68a2 100644 --- a/gradle.properties +++ b/gradle.properties @@ -16,3 +16,4 @@ archives_base_name=zombies-utils # Dependencies fabric_version=0.123.2+1.21.5 +yacl_version=3.6.6+1.21.5-fabric diff --git a/src/main/resources/fabric.mod.json b/src/main/resources/fabric.mod.json index 332d8b0..5235bee 100644 --- a/src/main/resources/fabric.mod.json +++ b/src/main/resources/fabric.mod.json @@ -33,7 +33,8 @@ "minecraft": "~1.21.5", "java": ">=21", "fabric-api": "*", - "hypixel-mod-api": ">=1.0.1" + "hypixel-mod-api": ">=1.0.1", + "yet_another_config_lib_v3": ">=3.6.6" }, "suggests": {} } -- 2.39.5 From 84a7a1cf66bf161021770a2303c24cc57295f56d Mon Sep 17 00:00:00 2001 From: Stachelbeere1248 Date: Thu, 29 May 2025 19:41:34 +0200 Subject: [PATCH 8/8] WIP: rewrite config system unfinished commit --- build.gradle | 2 +- .../zombiesutils/config/AbstractConfig.java | 45 -------------- .../stachel/zombiesutils/config/Config.java | 59 ------------------- .../stachel/zombiesutils/config/Configs.java | 5 -- .../zombiesutils/config/DoubleConfig.java | 39 ------------ .../zombiesutils/config/IntConfig.java | 39 ------------ .../config/PlayerVisibilityConfig.java | 30 ++++++++++ .../config/ZombiesUtilsConfig.java | 38 ++++++++++++ 8 files changed, 69 insertions(+), 188 deletions(-) delete mode 100644 src/main/java/xyz/stachel/zombiesutils/config/AbstractConfig.java delete mode 100644 src/main/java/xyz/stachel/zombiesutils/config/Config.java delete mode 100644 src/main/java/xyz/stachel/zombiesutils/config/Configs.java delete mode 100644 src/main/java/xyz/stachel/zombiesutils/config/DoubleConfig.java delete mode 100644 src/main/java/xyz/stachel/zombiesutils/config/IntConfig.java create mode 100644 src/main/java/xyz/stachel/zombiesutils/config/PlayerVisibilityConfig.java create mode 100644 src/main/java/xyz/stachel/zombiesutils/config/ZombiesUtilsConfig.java diff --git a/build.gradle b/build.gradle index afc746a..f8ec86a 100644 --- a/build.gradle +++ b/build.gradle @@ -11,7 +11,7 @@ base { } repositories { - maven { url 'https://repo.hypixel.net/repository/Hypixel/' }, + maven { url 'https://repo.hypixel.net/repository/Hypixel/' } maven { name 'Xander Maven' url 'https://maven.isxander.dev/releases' diff --git a/src/main/java/xyz/stachel/zombiesutils/config/AbstractConfig.java b/src/main/java/xyz/stachel/zombiesutils/config/AbstractConfig.java deleted file mode 100644 index 8d97cec..0000000 --- a/src/main/java/xyz/stachel/zombiesutils/config/AbstractConfig.java +++ /dev/null @@ -1,45 +0,0 @@ -package xyz.stachel.zombiesutils.config; - -import com.google.common.base.CaseFormat; -import net.minecraft.client.gui.tooltip.Tooltip; -import net.minecraft.client.gui.widget.ClickableWidget; -import net.minecraft.text.Text; - -public abstract class AbstractConfig { - private final Text message; - private final Tooltip tooltip; - private T value; - - public AbstractConfig(String key, T defaultValue) { - value = defaultValue; - message = Text.translatable(String.format("hardcover.config.%s", toSnakeCase(key))); - tooltip = Tooltip.of(Text.translatable(String.format("hardcover.config.tooltip.%s", toSnakeCase(key)))); - } - - private static String toSnakeCase(String text) { - return CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, text); - } - - public abstract ClickableWidget createWidget(Runnable callback); - - public ClickableWidget createWidget() { - return createWidget(() -> {}); - } - - public Text getMessage() { - return message; - } - - public Tooltip getTooltip() { - return tooltip; - } - - public T getValue() { - return value; - } - - public void setValue(T value) { - this.value = value; - Config.writeFile(); - } -} \ No newline at end of file diff --git a/src/main/java/xyz/stachel/zombiesutils/config/Config.java b/src/main/java/xyz/stachel/zombiesutils/config/Config.java deleted file mode 100644 index 2432c0b..0000000 --- a/src/main/java/xyz/stachel/zombiesutils/config/Config.java +++ /dev/null @@ -1,59 +0,0 @@ -package xyz.stachel.zombiesutils.config; - -import com.google.gson.Gson; -import com.google.gson.GsonBuilder; -import com.google.gson.JsonParser; -import net.fabricmc.loader.api.FabricLoader; -import xyz.stachel.zombiesutils.ZombiesUtils; - -import java.lang.reflect.Modifier; -import java.nio.file.Files; -import java.nio.file.Path; - -public final class Config { - private static final Gson GSON = new GsonBuilder() - .registerTypeAdapter(IntConfig.class, new IntConfig.Serializer()) - .registerTypeAdapter(DoubleConfig.class, new DoubleConfig.Serializer()) - .excludeFieldsWithModifiers(Modifier.TRANSIENT) - .setPrettyPrinting() - .create(); - private static final Path PATH = FabricLoader.getInstance().getConfigDir().resolve(String.format("%s.json", ZombiesUtils.MOD_ID)); - - public static void readFile() { - if (Files.exists(PATH)) { - try (var reader = Files.newBufferedReader(PATH)) { - var jsonElement = JsonParser.parseReader(reader); - - for (var field : Configs.class.getDeclaredFields()) { - var value = jsonElement.getAsJsonObject().get(field.getName()); - - if (value != null) { - var fieldType = field.getType(); - - if (IntConfig.class.isAssignableFrom(fieldType)) { - var intValue = value.getAsInt(); - ((IntConfig) field.get(null)).setValue(intValue); - } - - if (DoubleConfig.class.isAssignableFrom(fieldType)) { - var doubleValue = value.getAsDouble(); - ((DoubleConfig) field.get(null)).setValue(doubleValue); - } - } - } - } catch (Exception exception) { - ZombiesUtils.LOGGER.error("Failed to load configs at '{}'. Using default values.", PATH, exception); - } - } else { - writeFile(); - } - } - - public static void writeFile() { - try (var writer = Files.newBufferedWriter(PATH)) { - GSON.toJson(new Configs(), writer); - } catch (Exception exception) { - ZombiesUtils.LOGGER.error("Failed to save configs to '{}'.", PATH, exception); - } - } -} \ No newline at end of file diff --git a/src/main/java/xyz/stachel/zombiesutils/config/Configs.java b/src/main/java/xyz/stachel/zombiesutils/config/Configs.java deleted file mode 100644 index 65d3856..0000000 --- a/src/main/java/xyz/stachel/zombiesutils/config/Configs.java +++ /dev/null @@ -1,5 +0,0 @@ -package xyz.stachel.zombiesutils.config; - -public class Configs { - public static final DoubleConfig playerVisibleDistance = DoubleConfig.of("playerInvisibilityDistance", 0.8D); -} diff --git a/src/main/java/xyz/stachel/zombiesutils/config/DoubleConfig.java b/src/main/java/xyz/stachel/zombiesutils/config/DoubleConfig.java deleted file mode 100644 index 74e6421..0000000 --- a/src/main/java/xyz/stachel/zombiesutils/config/DoubleConfig.java +++ /dev/null @@ -1,39 +0,0 @@ -package xyz.stachel.zombiesutils.config; - -import com.google.gson.JsonElement; -import com.google.gson.JsonPrimitive; -import com.google.gson.JsonSerializationContext; -import com.google.gson.JsonSerializer; -import net.minecraft.client.gui.widget.ClickableWidget; -import net.minecraft.client.gui.widget.CyclingButtonWidget; -import net.minecraft.text.Text; - -import java.lang.reflect.Type; - -public class DoubleConfig extends AbstractConfig { - - public DoubleConfig(String key, Double defaultValue) { - super(key, defaultValue); - } - - public static DoubleConfig of(String key, double defaultValue) { - return new DoubleConfig(key, defaultValue); - } - - @Override - public ClickableWidget createWidget(Runnable callback) { - return CyclingButtonWidget.builder((value) -> Text.of(getValue().toString())).initially(getValue()) - .tooltip(value -> getTooltip()) - .build(getMessage(), (button, value) -> { - setValue(value); - callback.run(); - }); - } - - public static class Serializer implements JsonSerializer { - @Override - public JsonElement serialize(DoubleConfig value, Type type, JsonSerializationContext context) { - return new JsonPrimitive(value.getValue()); - } - } -} diff --git a/src/main/java/xyz/stachel/zombiesutils/config/IntConfig.java b/src/main/java/xyz/stachel/zombiesutils/config/IntConfig.java deleted file mode 100644 index 60534a0..0000000 --- a/src/main/java/xyz/stachel/zombiesutils/config/IntConfig.java +++ /dev/null @@ -1,39 +0,0 @@ -package xyz.stachel.zombiesutils.config; - -import com.google.gson.JsonElement; -import com.google.gson.JsonPrimitive; -import com.google.gson.JsonSerializationContext; -import com.google.gson.JsonSerializer; -import net.minecraft.client.gui.widget.ClickableWidget; -import net.minecraft.client.gui.widget.CyclingButtonWidget; -import net.minecraft.text.Text; - -import java.lang.reflect.Type; - -public class IntConfig extends AbstractConfig { - - public IntConfig(String key, Integer defaultValue) { - super(key, defaultValue); - } - - public static IntConfig of(String key, int defaultValue) { - return new IntConfig(key, defaultValue); - } - - @Override - public ClickableWidget createWidget(Runnable callback) { - return CyclingButtonWidget.builder((value) -> Text.of(getValue().toString())).initially(getValue()) - .tooltip(value -> getTooltip()) - .build(getMessage(), (button, value) -> { - setValue(value); - callback.run(); - }); - } - - public static class Serializer implements JsonSerializer { - @Override - public JsonElement serialize(IntConfig value, Type type, JsonSerializationContext context) { - return new JsonPrimitive(value.getValue()); - } - } -} diff --git a/src/main/java/xyz/stachel/zombiesutils/config/PlayerVisibilityConfig.java b/src/main/java/xyz/stachel/zombiesutils/config/PlayerVisibilityConfig.java new file mode 100644 index 0000000..7a1ddb8 --- /dev/null +++ b/src/main/java/xyz/stachel/zombiesutils/config/PlayerVisibilityConfig.java @@ -0,0 +1,30 @@ +package xyz.stachel.zombiesutils.config; + +import dev.isxander.yacl3.api.ConfigCategory; +import dev.isxander.yacl3.api.Option; +import dev.isxander.yacl3.api.OptionDescription; +import dev.isxander.yacl3.api.controller.FloatSliderControllerBuilder; +import dev.isxander.yacl3.config.v2.api.SerialEntry; +import net.minecraft.text.Text; + +public class PlayerVisibilityConfig { + + + @SerialEntry + private float range; + + ConfigCategory category(PlayerVisibilityConfig defaults) { + return ConfigCategory.createBuilder() + .name(Text.translatable("zombies-utils.config.player-visibility.name")) + .tooltip(Text.translatable("zombies-utils.config.player-visibility.tooltip")) + + .option(Option.createBuilder() + .name(Text.translatable("zombies-utils.config.player-visibility.range.name")) + .description(OptionDescription.of(Text.translatable("zombies-utils.config.player-visibility.range.tooltip"))) + .binding(defaults.range, () -> this.range, n -> {this.range = n;}) + .controller( o -> FloatSliderControllerBuilder.create(o).range(0f, 10f).step(0.01f)) + .build() + + ).build(); + } +} diff --git a/src/main/java/xyz/stachel/zombiesutils/config/ZombiesUtilsConfig.java b/src/main/java/xyz/stachel/zombiesutils/config/ZombiesUtilsConfig.java new file mode 100644 index 0000000..5de3382 --- /dev/null +++ b/src/main/java/xyz/stachel/zombiesutils/config/ZombiesUtilsConfig.java @@ -0,0 +1,38 @@ +package xyz.stachel.zombiesutils.config; + +import dev.isxander.yacl3.api.YetAnotherConfigLib; +import dev.isxander.yacl3.config.v2.api.ConfigClassHandler; +import dev.isxander.yacl3.config.v2.api.SerialEntry; +import dev.isxander.yacl3.config.v2.api.serializer.GsonConfigSerializerBuilder; +import net.fabricmc.loader.api.FabricLoader; +import net.minecraft.client.MinecraftClient; +import net.minecraft.client.gui.screen.Screen; +import xyz.stachel.zombiesutils.ZombiesUtils; + +public class ZombiesUtilsConfig { + private static final ConfigClassHandler configHandler = ConfigClassHandler + .createBuilder(ZombiesUtilsConfig.class) + .serializer(cfg -> GsonConfigSerializerBuilder.create(cfg).setPath(FabricLoader.getInstance().getConfigDir().resolve(ZombiesUtils.MOD_ID).resolve("config.json")).build()).build(); + + public ZombiesUtilsConfig() { + this.playerVis = new PlayerVisibilityConfig(); + } + + @SerialEntry + PlayerVisibilityConfig playerVis; + + public void display() { + Screen screen = YetAnotherConfigLib.create(configHandler, (defaults, current, b) -> b + .category(current.playerVis.category(defaults.playerVis))) + .generateScreen(null); + MinecraftClient.getInstance().setScreen(screen); + } + + public static void init() { + ZombiesUtilsConfig.configHandler.load(); + } + + public static ZombiesUtilsConfig getConfig() { + return ZombiesUtilsConfig.configHandler.instance(); + } +} -- 2.39.5