Skip to content

Commit

Permalink
- 自定义标题功能。
Browse files Browse the repository at this point in the history
  • Loading branch information
KasumiNova committed Mar 16, 2024
1 parent 19b1907 commit ba8b633
Show file tree
Hide file tree
Showing 5 changed files with 141 additions and 38 deletions.
23 changes: 11 additions & 12 deletions src/main/java/github/kasuminova/stellarcore/client/ClientProxy.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import github.kasuminova.stellarcore.client.handler.ClientEventHandler;
import github.kasuminova.stellarcore.client.hitokoto.HitokotoAPI;
import github.kasuminova.stellarcore.client.util.TitleUtils;
import github.kasuminova.stellarcore.common.CommonProxy;
import net.minecraftforge.client.event.ModelRegistryEvent;
import net.minecraftforge.common.MinecraftForge;
Expand All @@ -12,44 +13,42 @@

import java.util.concurrent.CompletableFuture;

@Mod.EventBusSubscriber(Side.CLIENT)
public class ClientProxy extends CommonProxy {

static {
CompletableFuture.runAsync(HitokotoAPI::getRandomHitokoto);
}

public ClientProxy() {
MinecraftForge.EVENT_BUS.register(this);
}

@Override
public void construction() {
super.construction();

TitleUtils.setRandomTitle("*Construction*");
}

@Override
public void preInit() {
super.preInit();
MinecraftForge.EVENT_BUS.register(ClientEventHandler.INSTANCE);

TitleUtils.setRandomTitle("*PreInit*");
}

@Override
public void init() {
super.init();

TitleUtils.setRandomTitle("*Init*");
}

@Override
public void postInit() {
super.postInit();

TitleUtils.setRandomTitle("*PostInit*");
}

@Override
public void loadComplete() {
super.loadComplete();
}

@SubscribeEvent
public void onModelRegister(ModelRegistryEvent event) {
TitleUtils.setRandomTitle();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import github.kasuminova.stellarcore.StellarCore;
import github.kasuminova.stellarcore.client.profiler.PacketProfiler;
import github.kasuminova.stellarcore.client.profiler.TEUpdatePacketProfiler;
import github.kasuminova.stellarcore.client.util.TitleUtils;
import net.minecraft.client.Minecraft;
import net.minecraft.util.text.TextFormatting;
import net.minecraftforge.client.event.RenderGameOverlayEvent;
Expand All @@ -15,14 +16,8 @@
public class ClientEventHandler {
public static final ClientEventHandler INSTANCE = new ClientEventHandler();

public static int debugPacketProfilerMessageLimit = 5;
public static int debugTEPacketProfilerMessageLimit = 5;

private long clientTick = 0;

private final List<String> debugMessageCache = new ArrayList<>();
private boolean debugMessageUpdateRequired = true;

private ClientEventHandler() {
}

Expand All @@ -34,26 +29,7 @@ public void onClientTick(TickEvent.ClientTickEvent event) {
clientTick++;

if (clientTick % 5 == 0) {
debugMessageUpdateRequired = true;
}
}

@SubscribeEvent
public void onDebugText(RenderGameOverlayEvent.Text event) {
if (!Minecraft.getMinecraft().gameSettings.showDebugInfo) {
return;
TitleUtils.checkTitleState();
}

if (debugMessageUpdateRequired) {
debugMessageUpdateRequired = false;
debugMessageCache.clear();
debugMessageCache.add("");
debugMessageCache.add(TextFormatting.BLUE + "[JustEnoughPatches] Ver: " + StellarCore.VERSION);
debugMessageCache.addAll(PacketProfiler.getProfilerMessages(debugPacketProfilerMessageLimit));
debugMessageCache.addAll(TEUpdatePacketProfiler.getProfilerMessages(debugTEPacketProfilerMessageLimit));
}

List<String> right = event.getRight();
right.addAll(debugMessageCache);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
package github.kasuminova.stellarcore.client.util;

import github.kasuminova.stellarcore.client.hitokoto.HitokotoAPI;
import github.kasuminova.stellarcore.common.config.StellarCoreConfig;
import org.lwjgl.opengl.Display;

import java.util.concurrent.CompletableFuture;

public class TitleUtils {
public static final String VANILLA_TITLE = "Minecraft 1.12.2";

public static String currentTitle = null;
public static String lastCurrentTitle = null;

/**
* 设置一言随机标题,必须在客户端主线程使用。
* 如果一言缓存为空,则尝试重新获取一言。
*
* @param state 当前状态
*/
public static void setRandomTitle(final String state) {
if (!StellarCoreConfig.FEATURES.enbleTitle) {
return;
}
lastCurrentTitle = currentTitle;

String hitokotoCache = HitokotoAPI.getHitokotoCache();
if (hitokotoCache != null) {
currentTitle = buildTitle(state, hitokotoCache);
Display.setTitle(currentTitle);
} else {
CompletableFuture.runAsync(HitokotoAPI::getRandomHitokoto);
currentTitle = buildTitle(state, null);
Display.setTitle(currentTitle);
}
}

/**
* 设置一言随机标题,必须在客户端主线程使用。
* 如果一言缓存为空,则尝试重新获取一言。
*/
public static void setRandomTitle() {
if (!StellarCoreConfig.FEATURES.enbleTitle) {
return;
}
lastCurrentTitle = currentTitle;

String hitokotoCache = HitokotoAPI.getHitokotoCache();

if (hitokotoCache != null) {
currentTitle = buildTitle(null, hitokotoCache);
Display.setTitle(currentTitle);
} else {
currentTitle = buildTitle(null, null);
Display.setTitle(currentTitle);
}
}

/**
* 设置一言随机标题,可以在其他线程使用。
*
* @param state 当前状态
*/
public static void setRandomTitleSync(String state) {
if (!StellarCoreConfig.FEATURES.enbleTitle) {
return;
}
lastCurrentTitle = currentTitle;
currentTitle = buildTitle(state, HitokotoAPI.getHitokotoCache());
}

/**
* 设置一言随机标题,可以在其他线程使用。
*/
public static void setRandomTitleSync() {
if (!StellarCoreConfig.FEATURES.enbleTitle) {
return;
}
lastCurrentTitle = currentTitle;
currentTitle = buildTitle(null, HitokotoAPI.getHitokotoCache());
}

public static String buildTitle(final String state, final String hitokoto) {
String title = StellarCoreConfig.FEATURES.title;
boolean useHitokoto = StellarCoreConfig.FEATURES.hitokoto;
if (state == null) {
if (!useHitokoto || hitokoto == null) {
currentTitle = String.format("%s", title);
}
return String.format("%s | %s", title, hitokoto);
}
if (!useHitokoto || hitokoto == null) {
return String.format("%s | %s", title, state);
}

return String.format("%s | %s | %s", title, state, hitokoto);
}

public static void checkTitleState() {
if (!StellarCoreConfig.FEATURES.enbleTitle) {
return;
}
if (currentTitle == null) {
return;
}

String title = Display.getTitle();
if (!title.equals(currentTitle)) {
Display.setTitle(currentTitle);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,14 @@ public static class Mekanism {
}

public static class Features {
@Config.Name("Enable Custom Game Title")
public final boolean enbleTitle = false;

@Config.Name("Title Use Hitokoto API")
public final boolean hitokoto = true;

@Config.Name("Custom Game Title")
public final String title = "Minecraft 1.12.2";

@Config.Name("Font Scale")
public final FontScale fontScale = new FontScale();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package github.kasuminova.stellarcore.mixin;

import github.kasuminova.stellarcore.client.hitokoto.HitokotoAPI;
import github.kasuminova.stellarcore.common.config.StellarCoreConfig;
import net.minecraftforge.fml.common.Loader;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import zone.rong.mixinbooter.ILateMixinLoader;

import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.util.function.BooleanSupplier;

@SuppressWarnings("unused")
Expand Down Expand Up @@ -47,6 +49,12 @@ public class StellarCoreLateMixinLoader implements ILateMixinLoader {
addModdedMixinCFG("mixins.stellar_core_thermaldynamics.json", "thermaldynamics");
}

static {
if (StellarCoreConfig.FEATURES.hitokoto) {
CompletableFuture.runAsync(HitokotoAPI::getRandomHitokoto);
}
}

@Override
public List<String> getMixinConfigs() {
return new ArrayList<>(MIXIN_CONFIGS.keySet());
Expand Down

0 comments on commit ba8b633

Please sign in to comment.