spawn time feature v2

This commit is contained in:
Stachelbeere1248 2023-11-23 08:05:53 +01:00
parent dcceb09186
commit 289aba550c
9 changed files with 167 additions and 104 deletions

View file

@ -3,4 +3,4 @@ org.gradle.jvmargs=-Xmx2g
baseGroup = com.github.stachelbeere1248.zombiesutils
mcVersion = 1.8.9
modid = zombiesutils
version = 1.1.4
version = 1.2.0

View file

@ -12,6 +12,7 @@ public class ZombiesUtilsConfig {
private static boolean slaShortener;
private static String chatMacro;
private static String defaultCategory;
private static short waveOffset;
public static void load() {
ZombiesUtils.getInstance().getLogger().debug("Loading config...");
config.load();
@ -41,11 +42,24 @@ public class ZombiesUtilsConfig {
"general",
"name of the category to be selected unless specified using /runCategory"
);
waveOffset = (short) config.getInt(
"spawn-time offset ticks",
Configuration.CATEGORY_GENERAL,
0,
-200,
200,
"max: 200 ticks"
);
ZombiesUtils.getInstance().getLogger().debug("Saving Config...");
config.save();
ZombiesUtils.getInstance().getLogger().debug("Config saved.");
}
public static short getWaveOffset() {
return waveOffset;
}
@SubscribeEvent
public void onConfigChange(ConfigChangedEvent.@NotNull OnConfigChangedEvent event) {
if (event.modID.equals("zombiesutils") && event.configID == null) {

View file

@ -3,7 +3,6 @@ package com.github.stachelbeere1248.zombiesutils.game;
import org.jetbrains.annotations.NotNull;
public class GameMode {
public static GameMode currentGameMode = null;
private final Map map;
private Difficulty difficulty;
@ -30,17 +29,6 @@ public class GameMode {
throw new RuntimeException("Achievement Get: Alien Arcadium Hard/RIP" + Map.ALIEN_ARCADIUM);
}
}
public static GameMode getCurrentGameMode() {
return currentGameMode;
}
/**
* Call to invalidate {@link #currentGameMode} to trigger the garbage collector
*/
public static void drop() {
currentGameMode = null;
}
public boolean is(Map map, Difficulty difficulty) {
return this.getDifficulty() == difficulty && this.getMap() == map;
}

View file

@ -1,6 +1,7 @@
package com.github.stachelbeere1248.zombiesutils.game.waves;
import com.github.stachelbeere1248.zombiesutils.game.Map;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
@SuppressWarnings("DuplicatedCode")
@ -16,22 +17,18 @@ public class Waves {
@Contract(pure = true)
public static byte[] get(@NotNull Map map, byte round) {
byte[] waves;
switch (map) {
case DEAD_END:
waves = deadEndWaveTimes[round - 1];
break;
return deadEndWaveTimes[round - 1];
case BAD_BLOOD:
waves = badBloodWaveTimes[round - 1];
break;
return badBloodWaveTimes[round - 1];
case ALIEN_ARCADIUM:
waves = alienArcadiumWaveTimes[round - 1];
break;
return alienArcadiumWaveTimes[round - 1];
default:
throw new IllegalStateException("Unexpected value: " + map);
}
return waves;
}
public static short getSum(@NotNull Map map, byte round) {
short sum;

View file

@ -3,6 +3,7 @@ package com.github.stachelbeere1248.zombiesutils.handlers;
import com.github.stachelbeere1248.zombiesutils.ZombiesUtils;
import com.github.stachelbeere1248.zombiesutils.game.Difficulty;
import com.github.stachelbeere1248.zombiesutils.game.GameMode;
import com.github.stachelbeere1248.zombiesutils.timer.Timer;
import net.minecraftforge.client.event.ClientChatReceivedEvent;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import org.jetbrains.annotations.NotNull;
@ -17,11 +18,11 @@ public class ChatHandler {
@SubscribeEvent
public void difficultyChange(@NotNull ClientChatReceivedEvent event) {
if (!Timer.getInstance().isPresent()) return;
String message = STRIP_COLOR_PATTERN.matcher(event.message.getUnformattedText()).replaceAll("").trim();
GameMode gameMode = GameMode.getCurrentGameMode();
GameMode gameMode = Timer.getInstance().get().getGameMode();
if (message.contains(":")) return;
if (gameMode == null) return;
if (message.contains("Hard Difficulty") || message.contains("困难") || message.contains("困難")) {
gameMode.changeDifficulty(Difficulty.HARD);

View file

@ -1,7 +1,5 @@
package com.github.stachelbeere1248.zombiesutils.mixin;
import com.github.stachelbeere1248.zombiesutils.ZombiesUtils;
import com.github.stachelbeere1248.zombiesutils.game.GameMode;
import com.github.stachelbeere1248.zombiesutils.timer.Timer;
import com.github.stachelbeere1248.zombiesutils.utils.Scoreboard;
import net.minecraft.client.network.NetHandlerPlayClient;
@ -44,7 +42,7 @@ public class MixinNetHandlerPlayClient {
} else { Timer timer = Timer.getInstance().get();
final Scoreboard.MapContainer map = Scoreboard.getMap().orElse(new Scoreboard.MapContainer(
GameMode.getCurrentGameMode().getMap(),
timer.getGameMode().getMap(),
false
));
@ -65,28 +63,23 @@ public class MixinNetHandlerPlayClient {
@Unique
private void zombies_utils$handleTitle(@NotNull S45PacketTitle packet) {
if (packet.getType() != S45PacketTitle.Type.TITLE) return;
Timer.getInstance().ifPresent(timer -> {
if (Scoreboard.isZombies()) return;
final String message = packet.getMessage().getUnformattedText().trim();
if (message.equals("§aYou Win!")) {
switch (GameMode.getCurrentGameMode().getMap()) {
switch (timer.getGameMode().getMap()) {
case DEAD_END: case BAD_BLOOD:
Timer.getInstance().ifPresent(timer -> {
timer.split((byte) 30);
Timer.dropInstances();
});
break;
case ALIEN_ARCADIUM:
Timer.getInstance().ifPresent(timer -> {
timer.split((byte) 105);
Timer.dropInstances();
});
break;
}
} else if (message.equals("§cGame Over!")) {
Timer.dropInstances();
} else {
ZombiesUtils.getInstance().getLogger().debug(message);
}
} else if (message.equals("§cGame Over!")) Timer.dropInstances();
});
}
}

View file

@ -2,6 +2,7 @@ package com.github.stachelbeere1248.zombiesutils.render;
import com.github.stachelbeere1248.zombiesutils.config.ZombiesUtilsConfig;
import com.github.stachelbeere1248.zombiesutils.game.sla.SLA;
import com.github.stachelbeere1248.zombiesutils.game.waves.Waves;
import com.github.stachelbeere1248.zombiesutils.game.windows.Room;
import com.github.stachelbeere1248.zombiesutils.timer.Timer;
import com.github.stachelbeere1248.zombiesutils.utils.Scoreboard;
@ -16,7 +17,6 @@ import java.util.Objects;
public class RenderGameOverlayHandler {
private final FontRenderer fontRenderer;
public RenderGameOverlayHandler() {
this.fontRenderer = Objects.requireNonNull(Minecraft.getMinecraft().fontRendererObj, "FontRenderer must not be null!");
}
@ -24,27 +24,41 @@ public class RenderGameOverlayHandler {
@SubscribeEvent
public void onRenderGameOverlay(RenderGameOverlayEvent.@NotNull Post event) {
if (event.type != RenderGameOverlayEvent.ElementType.TEXT) return;
Timer.getInstance().ifPresent(timer -> renderTime(timer.roundTime()));
Timer.getInstance().ifPresent(timer -> {
renderTime(timer.roundTime());
renderSpawnTime(
Waves.get(
timer.getGameMode().getMap(),
timer.getRound()
),
timer.roundTime()
);
});
SLA.getInstance().ifPresent(sla -> {
sla.refreshActives();
renderSla(sla.getRooms());
});
}
private void renderTime(long timerTicks) {
if (Scoreboard.isZombies()) return;
long minutesPart = (timerTicks*50) / 60000;
long secondsPart = ((timerTicks*50) % 60000) / 1000;
long tenthSecondsPart = ((timerTicks*50) % 1000) / 100;
String time = String.format("%d:%02d.%d", minutesPart, secondsPart, tenthSecondsPart);
int width = fontRenderer.getStringWidth(time);
ScaledResolution scaledResolution = new ScaledResolution(Minecraft.getMinecraft());
int screenWidth = scaledResolution.getScaledWidth();
int screenHeight = scaledResolution.getScaledHeight();
final int color = 0xFFFFFF;
fontRenderer.drawStringWithShadow(time, screenWidth - width, screenHeight - fontRenderer.FONT_HEIGHT, color);
final String time = getTimeString(timerTicks);
final int width = fontRenderer.getStringWidth(time);
final ScaledResolution scaledResolution = new ScaledResolution(Minecraft.getMinecraft());
final int screenWidth = scaledResolution.getScaledWidth();
final int screenHeight = scaledResolution.getScaledHeight();
fontRenderer.drawStringWithShadow(
time,
screenWidth - width,
screenHeight - fontRenderer.FONT_HEIGHT,
0xFFFFFF
);
}
private void renderSla(Room @NotNull [] rooms) {
int y = 0;
for (Room room: rooms) {
@ -58,4 +72,45 @@ public class RenderGameOverlayHandler {
y++;
}
}
private void renderSpawnTime(byte @NotNull [] waveTimes, short roundTicks) {
final int length = waveTimes.length + 1;
int heightIndex = 0;
int color = 0xFFFF55;
for (byte waveTime: waveTimes) {
final short waveTicks = (short) ((waveTime * 20)-ZombiesUtilsConfig.getWaveOffset());
if (roundTicks>waveTicks) {
heightIndex++;
continue;
}
final String time = getWaveString(waveTicks, heightIndex + 1);
final int width = fontRenderer.getStringWidth(time);
final ScaledResolution scaledResolution = new ScaledResolution(Minecraft.getMinecraft());
final int screenWidth = scaledResolution.getScaledWidth();
final int screenHeight = scaledResolution.getScaledHeight();
fontRenderer.drawStringWithShadow(
time,
screenWidth - width,
screenHeight - fontRenderer.FONT_HEIGHT * (length-heightIndex),
color
);
color = 0xAAAAAA;
heightIndex++;
}
}
private static String getTimeString(long timerTicks) {
final long minutesPart = (timerTicks *50) / 60000;
final long secondsPart = ((timerTicks *50) % 60000) / 1000;
final long tenthSecondsPart = ((timerTicks *50) % 1000) / 100;
return String.format("%d:%02d.%d", minutesPart, secondsPart, tenthSecondsPart);
}
private static String getWaveString(long waveTicks, int wave) {
final long minutesPart = (waveTicks *50) / 60000;
final long secondsPart = ((waveTicks *50) % 60000) / 1000;
return String.format("W%d %d:%02d", wave, minutesPart, secondsPart);
}
}

View file

@ -1,6 +1,5 @@
package com.github.stachelbeere1248.zombiesutils.timer;
import com.github.stachelbeere1248.zombiesutils.game.GameMode;
import com.github.stachelbeere1248.zombiesutils.timer.recorder.Category;
import com.github.stachelbeere1248.zombiesutils.timer.recorder.TimesFile;
import net.minecraft.client.Minecraft;
@ -9,68 +8,66 @@ import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
public class RecordManager {
private static final String bar = "§l§a▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬";
public static void compareSegment(byte round, short roundTime, @NotNull Category category) throws IndexOutOfBoundsException {
sendBar();
TimesFile timesFile = category.getByGameMode(GameMode.getCurrentGameMode());
String segmentMessage = bar +
"\n§e Category: §d" + category.getName()
;
@SuppressWarnings("OptionalGetWithoutIsPresent")
final TimesFile timesFile = category.getByGameMode(Timer.getInstance().get().getGameMode());
short bestSegment = timesFile.getBestSegment(round);
if (bestSegment == (short) 0) {
timesFile.setBestSegment(round, roundTime);
Minecraft.getMinecraft().thePlayer.addChatMessage(new ChatComponentText(
"§l§e Category: " + category.getName() + " - ***" + "§l§6 NEW BEST SEGMENT! " + "§l§e***"
));
segmentMessage += "\n§e§l***§6§l NEW BEST SEGMENT! §e§l***";
final String timeString = formattedTime(roundTime);
final String message = "§cRound " + round + "§e took §a" + timeString + "§e!";
Minecraft.getMinecraft().thePlayer.addChatMessage(new ChatComponentText(message));
segmentMessage += "\n§cRound " + round + "§e took §a" + timeString + "§e!";
} else {
if (roundTime<bestSegment) {
Minecraft.getMinecraft().thePlayer.addChatMessage(new ChatComponentText(
"§l§e Category: " + category.getName() + " - ***" + "§l§6 NEW BEST SEGMENT! " + "§l§e***"
));
segmentMessage += "\n§e§l***§6§l NEW BEST SEGMENT! §e§l***";
timesFile.setBestSegment(round, roundTime);
}
final String timeString = formattedTime(roundTime);
final String message = "§cRound " + round + "§e took §a" + timeString + " §9" + formattedDelta(roundTime,bestSegment);
Minecraft.getMinecraft().thePlayer.addChatMessage(new ChatComponentText(message));
segmentMessage += "\n§cRound " + round + "§e took §a" + timeString + " §9" + formattedDelta(roundTime,bestSegment);
}
sendBar();
segmentMessage += "\n" + bar;
Minecraft.getMinecraft().thePlayer.addChatMessage(new ChatComponentText(segmentMessage));
}
public static void compareBest(byte round, int gameTime, @NotNull Category category) throws IndexOutOfBoundsException {
sendBar();
TimesFile timesFile = category.getByGameMode(GameMode.getCurrentGameMode());
String bestMessage = bar +
"\n§e Category: §d" + category.getName()
;
@SuppressWarnings("OptionalGetWithoutIsPresent")
final TimesFile timesFile = category.getByGameMode(Timer.getInstance().get().getGameMode());
int personalBest = timesFile.getPersonalBest(round);
if (personalBest == 0) {
timesFile.setPersonalBest(round, gameTime);
Minecraft.getMinecraft().thePlayer.addChatMessage(new ChatComponentText(
"§l§e Category: " + category.getName() + " - ***" + "§l§6 NEW PERSONAL BEST! " + "§l§e***"
));
bestMessage += "\n§e§l***§6§l NEW PERSONAL BEST! §e§l***";
final String timeString = formattedTime(gameTime);
final String message = "§cRound " + round + "§e finished at §a" + timeString + "§e!";
Minecraft.getMinecraft().thePlayer.addChatMessage(new ChatComponentText(message));
bestMessage += "\n§cRound " + round + "§e finished at §a" + timeString + "§e!";
} else {
if (gameTime<personalBest) {
Minecraft.getMinecraft().thePlayer.addChatMessage(new ChatComponentText(
"§l§e Category: " + category.getName() + " - ***" + "§l§6 NEW PERSONAL BEST! " + "§l§e***"
));
bestMessage += "\n§e§l***§6§l NEW PERSONAL BEST! §e§l***";
timesFile.setPersonalBest(round, gameTime);
}
final String timeString = formattedTime(gameTime);
final String message = "§cRound " + round + "§e finished at §a" + timeString + " §9" + formattedDelta(gameTime, personalBest);
Minecraft.getMinecraft().thePlayer.addChatMessage(new ChatComponentText(message));
bestMessage += "\n§cRound " + round + "§e finished at §a" + timeString + " §9" + formattedDelta(gameTime, personalBest);
}
sendBar();
bestMessage += "\n" + bar;
Minecraft.getMinecraft().thePlayer.addChatMessage(new ChatComponentText(bestMessage));
}
private static String formattedTime(int gameTime) {
return String.format("%d:%02d.%d",
(gameTime *50) / 60000,
((gameTime *50) % 60000) / 1000,
((gameTime *50) % 1000) / 100
gameTime *= 50;
return String.format("%d:%02d.%d%d",
gameTime / 60000,
(gameTime % 60000) / 1000,
(gameTime % 1000) / 100,
(gameTime % 100) / 10
);
}
@Contract(pure = true)
@ -80,9 +77,4 @@ public class RecordManager {
return String.valueOf(delta);
} else return ("+" + delta);
}
private static void sendBar() {
Minecraft.getMinecraft().thePlayer.addChatMessage(new ChatComponentText(
"§l§a▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬"
));
}
}

View file

@ -15,6 +15,7 @@ import java.util.Optional;
public class Timer {
private static Timer instance;
private final GameMode gameMode;
private final long savedTotalWorldTime;
private int passedRoundsTickSum = 0;
private final String serverNumber;
@ -28,15 +29,34 @@ public class Timer {
* @param map The map the timer should be started for.
*/
public Timer (@NotNull String serverNumber, @NotNull Map map) {
instance = this;
savedTotalWorldTime = getCurrentTotalWorldTime();
if (!serverNumber.trim().isEmpty()) this.serverNumber = serverNumber.trim();
else throw new RuntimeException("invalid servernumber");
this.category = new Category();
GameMode.currentGameMode = new GameMode(map);
this.gameMode = new GameMode(map);
if (ZombiesUtilsConfig.isSlaToggled()) SLA.instance = new SLA(map);
instance = this;
}
/**
* Constructs a timer and saves it to {@link #instance}.
* @param serverNumber The game's server the timer should be bound to.
* @param map The map the timer should be started for.
* @param round If available, round to begin splitting.
*/
//TODO: brooooooooo
public Timer (@NotNull String serverNumber, @NotNull Map map, byte round) {
savedTotalWorldTime = getCurrentTotalWorldTime();
if (!serverNumber.trim().isEmpty()) this.serverNumber = serverNumber.trim();
else throw new RuntimeException("invalid servernumber");
this.category = new Category();
this.gameMode = new GameMode(map);
this.round = round;
if (ZombiesUtilsConfig.isSlaToggled()) SLA.instance = new SLA(map);
instance = this;
}
@ -100,9 +120,12 @@ public class Timer {
*/
public static void dropInstances() {
instance = null;
GameMode.drop();
}
public int getRound() {
return round+1;
public byte getRound() {
return (byte) (round+1);
}
public GameMode getGameMode() {
return gameMode;
}
}