mod skeleton 2

This commit is contained in:
Stachelbeere1248 2025-05-19 08:53:34 +02:00
parent 0c258f8680
commit b2b8542604
Signed by: Stachelbeere1248
SSH key fingerprint: SHA256:IozEKdw2dB8TZxkpPdMxcWSoWTIMwoLaCcZJ1AJnY2o
13 changed files with 222 additions and 26 deletions

3
.gitignore vendored
View file

@ -39,3 +39,6 @@ hs_err_*.log
replay_*.log
*.hprof
*.jfr
# personal setup
.mcsources.jar

View file

@ -1,23 +1,19 @@
package xyz.stachel.zombiesutils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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 {
public static final String MOD_ID = "zombies-utils";
public static final Logger LOGGER = LoggerFactory.getLogger(MOD_ID);
@Override
public void onInitializeClient() {
HypixelModAPI.getInstance().subscribeToEventPacket(ClientboundLocationPacket.class);
//TODO: HypixelModAPI.getInstance().createHandler(ClientboundLocationPacket.class, todo);
LOGGER.info("initializing...");
HypixelModAPI.getInstance().createHandler(ClientboundLocationPacket.class, Location::onLocation);
ZombiesUtils.LOGGER.info("initializing...");
HudLayerRegistrationCallback.EVENT.register(new Renderer());
}

View file

@ -0,0 +1,25 @@
package xyz.stachel.zombiesutils.handlers;
import java.util.Optional;
import net.hypixel.modapi.packet.impl.clientbound.event.ClientboundLocationPacket;
public class Location {
private static String serverNumber;
private static String mode;
public static void onLocation(ClientboundLocationPacket p) {
Location.serverNumber = p.getServerName();
Optional<String> m = p.getMode();
if (m.isPresent()) Location.mode = m.get();
}
public static String getServerNumber() {
return serverNumber;
}
public static String getMode() {
return mode;
}
}

View file

@ -0,0 +1,30 @@
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 xyz.stachel.zombiesutils.game.GameManager;
@Mixin(ClientPlayNetworkHandler.class)
public class ClientPlayNetworkHandlerMixin {
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) {
GameManager.onRound(number);
}
}
}
}

View file

@ -1,15 +0,0 @@
package xyz.stachel.zombiesutils.mixin.client;
import net.minecraft.client.MinecraftClient;
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;
@Mixin(MinecraftClient.class)
public class ExampleClientMixin {
@Inject(at = @At("HEAD"), method = "run")
private void init(CallbackInfo info) {
// This code is injected into the start of MinecraftClient.run()V
}
}

View file

@ -3,9 +3,9 @@
"package": "xyz.stachel.zombiesutils.mixin.client",
"compatibilityLevel": "JAVA_21",
"client": [
"ExampleClientMixin"
"ClientPlayNetworkHandlerMixin"
],
"injectors": {
"defaultRequire": 1
}
}
}

View file

@ -0,0 +1,17 @@
package xyz.stachel.zombiesutils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import net.fabricmc.api.ModInitializer;
public class ZombiesUtils implements ModInitializer {
public static final String MOD_ID = "zombies-utils";
public static final Logger LOGGER = LoggerFactory.getLogger(MOD_ID);
@Override
public void onInitialize() {
}
}

View file

@ -0,0 +1,34 @@
package xyz.stachel.zombiesutils.game;
import java.io.File;
import org.jetbrains.annotations.NotNull;
class Game extends Timer {
private final boolean joinedRoundOne;
private int round;
private final File gameFile;
private String category;
public final GameMode mode;
public Game(@NotNull final GameFile gameFile, @NotNull final GameMode mode) {
super();
this.gameFile = gameFile;
this.round = 1;
this.joinedRoundOne = true;
this.mode = mode;
}
public Game(@NotNull final GameFile gameFile, @NotNull final GameMode mode, final int round) {
super();
this.gameFile = gameFile;
this.round = round;
this.joinedRoundOne = round == 1;
this.mode = mode;
}
public long split(final int round) {
if (round )
return super.split();
}
}

View file

@ -0,0 +1,42 @@
package xyz.stachel.zombiesutils.game;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
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;
GameFile(@NotNull final String serverNumber) {
super(new File("zombies", "runs"), formattedTime() + "_" + serverNumber + ".seg2");
}
private static String formattedTime() {
final LocalDateTime dateTime = LocalDateTime.now().truncatedTo(ChronoUnit.MINUTES);
return dateTime.format(DateTimeFormatter.ISO_LOCAL_DATE_TIME).replace(':', '-').replaceFirst("T", "_");
}
void init() throws IOException {
if (!this.exists()) {
this.getParentFile().mkdirs();
this.createNewFile();
}
if (this.isFile()) {
this.setWritable(true);
}
this.writer = new FileWriter(this);
}
void addSplit(final long ticks) throws IOException {
this.writer.append(String.format("%d", ticks));
}
void clean() throws IOException {
this.writer.close();
}
}

View file

@ -0,0 +1,27 @@
package xyz.stachel.zombiesutils.game;
import java.util.HashMap;
import xyz.stachel.zombiesutils.game.GameMode.Map;
import xyz.stachel.zombiesutils.handlers.Location;
public class GameManager {
private final HashMap<String, Game> games = new HashMap<>();
private void addGame(final String serverNumber, final GameMode mode, final int round) {
this.games.put(serverNumber, new Game(new GameFile(serverNumber), mode));
}
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 (!games.containsKey(sn)) addGame(sn, new GameMode(Map.DEAD_END), round);
else games.get(sn).split(round);
}
public Game getGame() {
}
}

View file

@ -0,0 +1,34 @@
package xyz.stachel.zombiesutils.game;
import org.jetbrains.annotations.NotNull;
class GameMode {
private final Map map;
private Difficulty difficulty;
GameMode(final Map map) {
this.map = map;
this.difficulty = Difficulty.NORMAL;
}
GameMode(@NotNull final Map map, @NotNull final Difficulty difficulty) {
this.map = map;
}
void changeDifficulty(@NotNull final Difficulty difficulty) {
this.difficulty = map != Map.ALIEN_ARCADIUM ? difficulty : Difficulty.NORMAL;
}
enum Map {
DEAD_END, BAD_BLOOD, PRISON, ALIEN_ARCADIUM;
}
enum Difficulty {
NORMAL, HARD, RIP;
}
@Override
public String toString() {
return map.toString() + "_" + difficulty.toString();
}
}

View file

@ -1,7 +1,7 @@
package xyz.stachel.zombiesutils.game;
import net.minecraft.client.MinecraftClient;
import xyz.stachel.zombiesutils.ZombiesUtilsClient;
import xyz.stachel.zombiesutils.ZombiesUtils;
class Timer {
// world-absolute tick the game started on
@ -30,7 +30,7 @@ class Timer {
private long worldTick() {
MinecraftClient mc = MinecraftClient.getInstance();
if (mc == null || mc.world == null) {
ZombiesUtilsClient.LOGGER.warn("Timer is running, but the player is not in game.");
ZombiesUtils.LOGGER.warn("Timer is running, but the player is not in game.");
return 0;
}
return mc.world.getTime();

View file

@ -15,6 +15,9 @@
"icon": "assets/zombies-utils/icon.png",
"environment": "client",
"entrypoints": {
"main": [
"xyz.stachel.zombiesutils.ZombiesUtils"
],
"client": [
"xyz.stachel.zombiesutils.ZombiesUtilsClient"
]