Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tick and pause indicator #12

Draft
wants to merge 3 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 80 additions & 11 deletions src/main/java/com/minecrafttas/lotas_light/LoTASLightClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import com.minecrafttas.lotas_light.duck.Tickratechanger;
import com.minecrafttas.lotas_light.event.EventClientGameLoop;
import com.minecrafttas.lotas_light.event.EventOnClientJoinServer;
import com.minecrafttas.lotas_light.event.HudRenderEffectsCallback;
import com.minecrafttas.lotas_light.event.HudRenderExperienceCallback;
import com.minecrafttas.lotas_light.keybind.KeybindManager;
import com.minecrafttas.lotas_light.keybind.KeybindManager.Keybind;
Expand All @@ -17,12 +18,14 @@
import com.minecrafttas.lotas_light.savestates.gui.SavestateGui;
import com.minecrafttas.lotas_light.savestates.gui.SavestateRenameGui;
import net.fabricmc.api.ClientModInitializer;
import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents;
import net.minecraft.ChatFormatting;
//# 1.21.1
//$$import net.minecraft.client.DeltaTracker;
//# end
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.client.multiplayer.ClientLevel;
import net.minecraft.client.resources.language.I18n;
import net.minecraft.client.server.IntegratedServer;
import net.minecraft.network.chat.Component;
Expand All @@ -48,6 +51,7 @@ public class LoTASLightClient implements ClientModInitializer {
private Path configpath;
public static Configuration config;
private boolean showHint = true;
private boolean showTickIndicator;

@Override
public void onInitializeClient() {
Expand All @@ -58,7 +62,18 @@ public void onInitializeClient() {
LoTASLight.startTickrate = Float.parseFloat(LoTASLightClient.config.get(ConfigOptions.DEFAULT_TICKRATE));
rateIndex = (short) findClosestRateIndex(LoTASLight.startTickrate);
registerKeybindings();

EventOnClientJoinServer.EVENT.register(player -> {
if (LoTASLight.savestateHandler.loadStateComplete != null) {
LoTASLight.savestateHandler.loadStateComplete.run();
LoTASLight.savestateHandler.loadStateComplete = null;
}
});

ClientTickEvents.START_CLIENT_TICK.register(client -> showTickIndicator = !showTickIndicator);

HudRenderExperienceCallback.EVENT.register(this::drawHud);
HudRenderEffectsCallback.EVENT.register(this::afterDrawEffects);
}

private void registerKeybindings() {
Expand All @@ -70,14 +85,6 @@ private void registerKeybindings() {
keybindManager.registerKeybind(new Keybind("key.lotaslight.loadstate", "keycategory.lotaslight.lotaslight", GLFW.GLFW_KEY_K, this::loadstate));

EventClientGameLoop.EVENT.register(keybindManager::onRunClientGameLoop);

EventOnClientJoinServer.EVENT.register(player -> {
if (LoTASLight.savestateHandler.loadStateComplete != null) {
LoTASLight.savestateHandler.loadStateComplete.run();
LoTASLight.savestateHandler.loadStateComplete = null;
}
});

}

private void increaseTickrate(Minecraft client) {
Expand Down Expand Up @@ -155,8 +162,9 @@ private void freezeTickrate(Minecraft client) {
Tickratechanger clientTickrateChanger = (Tickratechanger) clientTickrateManager;
Tickratechanger serverTickrateChanger = (Tickratechanger) serverTickrateManager;

serverTickrateChanger.toggleTickrate0();
clientTickrateChanger.toggleTickrate0();
boolean enable = clientTickrateManager.tickrate() != 0;
clientTickrateChanger.enableTickrate0(enable);
serverTickrateChanger.enableTickrate0(enable);
}

private void advanceTickrate(Minecraft client) {
Expand All @@ -173,8 +181,8 @@ private void advanceTickrate(Minecraft client) {
Tickratechanger clientTickrateChanger = (Tickratechanger) clientTickrateManager;
Tickratechanger serverTickrateChanger = (Tickratechanger) serverTickrateManager;

serverTickrateChanger.advanceTick();
clientTickrateChanger.advanceTick();
serverTickrateChanger.advanceTick();
}

private void drawHud(GuiGraphics context, float deltaTicks) { //@GraphicsDelta;
Expand All @@ -193,6 +201,67 @@ private void advanceTickrate(Minecraft client) {
//# end
}

private void afterDrawEffects(GuiGraphics context, float deltaTicks) { //@GraphicsDelta;
Minecraft mc = Minecraft.getInstance();
ClientLevel level = mc.level;

if (level == null) {
return;
}

TickRateManager tickrateChanger = level.tickRateManager();
IndicatorLocation location = IndicatorLocation.valueOf(config.get(ConfigOptions.TICKRATE_INDICATOR_LOCATION).toUpperCase());

if (config.getBoolean(ConfigOptions.TICKRATE_INDICATOR) && tickrateChanger.tickrate() <= 5F && showTickIndicator) {
float uvCoordinate = 0F;
renderIcon(location, uvCoordinate, context);
}
if (config.getBoolean(ConfigOptions.TICKRATE_PAUSE_INDICATOR) && tickrateChanger.tickrate() == 0) {
float uvCoordinate = 16F;
renderIcon(location, uvCoordinate, context);
}
}

private void renderIcon(IndicatorLocation location, float uvCoordinate, GuiGraphics context) {
ResourceLocation streamIcons = new ResourceLocation("lotaslight", "stream_indicator.png"); //@ResourceLocation;

int x = 0;
int y = 0;

switch (location) {
case TOP_LEFT:
x = 1;
y = 1;
break;
case BOTTOM_LEFT:
x = 1;
y = context.guiHeight() - 17;
break;
case BOTTOM_RIGHT:
x = context.guiWidth() - 17;
y = context.guiHeight() - 17;
break;
case TOP_RIGHT:
default:
x = context.guiWidth() - 17;
y = 1;
break;
}

//# 1.21.3
//$$ context.blit(RenderType::guiTexturedOverlay, streamIcons, x, y, uvCoordinate, uvCoordinate, 16, 16, 16, 64);
//# def
context.blit(streamIcons, x, y, uvCoordinate, uvCoordinate, 16, 16, 16, 64);
//# end
}

private static enum IndicatorLocation {
TOP_LEFT,
BOTTOM_LEFT,
TOP_RIGHT,
BOTTOM_RIGHT;
}

private void savestate(Minecraft mc) {
MinecraftServer server = mc.getSingleplayerServer();
if (server == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,24 @@ public static void register(CommandDispatcher<CommandSourceStack> commandDispatc
.then(Commands.literal("false").executes(LoTASLightCommand::hideMessages))
)
.then(Commands.literal("savestate_showControls")
.then(Commands.literal("true").executes(LoTASLightCommand::showControls))
.then(Commands.literal("false").executes(LoTASLightCommand::hideControls))
)
.executes(LoTASLightCommand::showControls))
.then(Commands.literal("trc_defaultTickrate")
.then(Commands.argument("tickrate", FloatArgumentType.floatArg(.1f, 60f)).executes(LoTASLightCommand::defaultTickrate)))
.then(Commands.literal("trc_tickIndicator")
.executes(LoTASLightCommand::tickIndicator))
.then(Commands.literal("trc_pauseIndicator")
.executes(LoTASLightCommand::tickPauseIndicator))
.then(Commands.literal("trc_indicatorLocation")
.then(Commands.literal("top_right")
.executes(context -> LoTASLightCommand.tickIndicatorLocation(context, "top_right")))
.then(Commands.literal("top_left")
.executes(context -> LoTASLightCommand.tickIndicatorLocation(context, "top_left")))
.then(Commands.literal("bottom_right")
.executes(context -> LoTASLightCommand.tickIndicatorLocation(context, "bottom_right")))
.then(Commands.literal("bottom_left")
.executes(context -> LoTASLightCommand.tickIndicatorLocation(context, "bottom_left")))
)

);
//@formatter:on
}
Expand All @@ -42,14 +55,8 @@ public static int hideMessages(CommandContext<CommandSourceStack> context) {
}

public static int showControls(CommandContext<CommandSourceStack> context) {
LoTASLightClient.config.set(ConfigOptions.SAVESTATE_SHOW_CONTROLS, true);
context.getSource().sendSuccess(() -> Component.translatable("msg.lotaslight.showctrls.true"), true);
return 1;
}

public static int hideControls(CommandContext<CommandSourceStack> context) {
LoTASLightClient.config.set(ConfigOptions.SAVESTATE_SHOW_CONTROLS, false);
context.getSource().sendSuccess(() -> Component.translatable("msg.lotaslight.showctrls.false"), true);
boolean enable = LoTASLightClient.config.toggle(ConfigOptions.SAVESTATE_SHOW_CONTROLS);
context.getSource().sendSuccess(() -> Component.translatable("msg.lotaslight.showctrls." + enable), true);
return 1;
}

Expand All @@ -58,4 +65,19 @@ public static int defaultTickrate(CommandContext<CommandSourceStack> context) {
LoTASLightClient.config.set(ConfigOptions.DEFAULT_TICKRATE, Float.toString(tickrate));
return 1;
}

public static int tickIndicator(CommandContext<CommandSourceStack> context) {
LoTASLightClient.config.toggle(ConfigOptions.TICKRATE_INDICATOR);
return 1;
}

public static int tickPauseIndicator(CommandContext<CommandSourceStack> context) {
LoTASLightClient.config.toggle(ConfigOptions.TICKRATE_PAUSE_INDICATOR);
return 1;
}

public static int tickIndicatorLocation(CommandContext<CommandSourceStack> context, String corner) {
LoTASLightClient.config.set(ConfigOptions.TICKRATE_INDICATOR_LOCATION, corner);
return 1;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ public enum ConfigOptions {

DEFAULT_TICKRATE("trcDefaultTickrate", "20.0"),
TICKRATE_SHOW_MESSAGES("trcShowMessages", "true"),
SAVESTATE_SHOW_CONTROLS("savestateShowControls", "true");
SAVESTATE_SHOW_CONTROLS("savestateShowControls", "true"),
TICKRATE_INDICATOR("trcTickIndicator", "true"),
TICKRATE_PAUSE_INDICATOR("trcPauseIndicator", "true"),
TICKRATE_INDICATOR_LOCATION("trcIndicatorLocation", "top_right");

private String key;
private String defaultValue;
Expand Down Expand Up @@ -107,4 +110,10 @@ public void delete(ConfigOptions configOption) {
properties.remove(configOption);
saveToXML();
}

public boolean toggle(ConfigOptions tickrateIndicator) {
boolean newVal = !getBoolean(tickrateIndicator);
set(tickrateIndicator, newVal);
return newVal;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.minecrafttas.lotas_light.event;

import net.fabricmc.fabric.api.event.Event;
import net.fabricmc.fabric.api.event.EventFactory;
//# 1.21.1
//$$import net.minecraft.client.DeltaTracker;
//# end
import net.minecraft.client.gui.GuiGraphics;

public interface HudRenderEffectsCallback {
public static Event<HudRenderEffectsCallback> EVENT = EventFactory.createArrayBacked(HudRenderEffectsCallback.class, (listeners) -> (matrixStack, delta) -> {
for (HudRenderEffectsCallback listener : listeners)
listener.onRenderPre(matrixStack, delta);
});

public void onRenderPre(GuiGraphics drawContext, float tickCounter); //@GraphicsDelta;
}
17 changes: 15 additions & 2 deletions src/main/java/com/minecrafttas/lotas_light/mixin/MixinGui.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

import com.minecrafttas.lotas_light.event.HudRenderEffectsCallback;
import com.minecrafttas.lotas_light.event.HudRenderExperienceCallback;

//# 1.21.1
//$$import com.minecrafttas.lotas_light.event.HudRenderExperienceCallback;
//$$
//$$import net.minecraft.client.DeltaTracker;
//# end
import net.minecraft.client.gui.Gui;
Expand All @@ -31,4 +30,18 @@ private void onRenderExperienceLevel(GuiGraphics guiGraphics, int deltaTracker,
HudRenderExperienceCallback.EVENT.invoker().onRenderPre(guiGraphics, deltaTracker);
}
//# end

@Inject(at = @At(value = "RETURN"), method = "renderEffects")

//# 1.21.1
//$$ public void onRenderEffects(GuiGraphics guiGraphics, DeltaTracker deltaTracker, CallbackInfo ci) {
//$$
//# 1.20.6
//$$ public void onRenderEffects(GuiGraphics guiGraphics, float deltaTracker, CallbackInfo ci) {
//# def
public void onRenderEffects(GuiGraphics guiGraphics, CallbackInfo ci) {
float deltaTracker = 0f;
//# end
HudRenderEffectsCallback.EVENT.invoker().onRenderPre(guiGraphics, deltaTracker);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,20 @@
import com.minecrafttas.lotas_light.duck.Tickratechanger;

import net.minecraft.client.Minecraft;
import net.minecraft.server.ServerTickRateManager;
import net.minecraft.util.TimeUtil;
import net.minecraft.world.TickRateManager;

/**
* Changes the vanilla tickrate manager to allow for lower tickrates,
* better freeze and stepping funcitonality.
* better freeze and stepping functionality.
*
* @author Scribble
*/
@Mixin(TickRateManager.class)
public abstract class MixinTickRateManager implements Tickratechanger {
@Unique
private static float tickrateSaved = 20;
@Unique
private boolean advanceTickrate;
@Unique
private boolean isDisconnecting;
Expand Down Expand Up @@ -124,7 +124,7 @@ public void disconnect() {

@Inject(method = "tick", at = @At("RETURN"))
public void inject_Tick(CallbackInfo ci) {
if (advanceTickrate) {
if (advanceTickrate && !(((TickRateManager) (Object) this) instanceof ServerTickRateManager)) {
this.advanceTickrate = false;
setTickRate(0);
}
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.