mirror of
https://github.com/GTNewHorizons/GTNHLib.git
synced 2026-11-26 14:53:14 +00:00
add title/subtitle api, overlay, and /title command (#330)
This commit is contained in:
11 files changed
+462
-1
No files matched your search
@@ -17,6 +17,7 @@ import com.gtnewhorizon.gtnhlib.chat.customcomponents.ChatComponentFluid;
|
||||
import com.gtnewhorizon.gtnhlib.chat.customcomponents.ChatComponentFluidName;
|
||||
import com.gtnewhorizon.gtnhlib.chat.customcomponents.ChatComponentItemName;
|
||||
import com.gtnewhorizon.gtnhlib.chat.customcomponents.ChatComponentNumber;
|
||||
import com.gtnewhorizon.gtnhlib.commands.TitleCommand;
|
||||
import com.gtnewhorizon.gtnhlib.config.ConfigException;
|
||||
import com.gtnewhorizon.gtnhlib.config.ConfigurationManager;
|
||||
import com.gtnewhorizon.gtnhlib.eventbus.AutoEventBus;
|
||||
@@ -106,6 +107,7 @@ public class CommonProxy {
|
||||
|
||||
public void serverStarting(FMLServerStartingEvent event) {
|
||||
event.registerServerCommand(new BlockStateCommand());
|
||||
event.registerServerCommand(new TitleCommand());
|
||||
}
|
||||
|
||||
public void serverStarted(FMLServerStartedEvent event) {}
|
||||
|
||||
@@ -0,0 +1,74 @@
|
||||
package com.gtnewhorizon.gtnhlib.client.title;
|
||||
|
||||
import net.minecraft.util.IChatComponent;
|
||||
|
||||
import cpw.mods.fml.relauncher.Side;
|
||||
import cpw.mods.fml.relauncher.SideOnly;
|
||||
import lombok.Getter;
|
||||
|
||||
/**
|
||||
* Client-side title/subtitle API. Any mod can call {@link #setTitle}/{@link #setSubtitle} to display overlay text. The
|
||||
* renderer is shipped in this lib and runs whenever a title is active.
|
||||
*/
|
||||
@SideOnly(Side.CLIENT)
|
||||
public class TitleAPI {
|
||||
|
||||
@Getter
|
||||
private static volatile IChatComponent title;
|
||||
@Getter
|
||||
private static volatile IChatComponent subtitle;
|
||||
@Getter
|
||||
private static volatile int titleTime;
|
||||
@Getter
|
||||
private static volatile int fadeInTime = 10;
|
||||
@Getter
|
||||
private static volatile int stayTime = 70;
|
||||
@Getter
|
||||
private static volatile int fadeOutTime = 20;
|
||||
|
||||
/** Display a title for {@code fadeIn + stay + fadeOut} ticks. */
|
||||
public static void setTitle(IChatComponent component) {
|
||||
title = component;
|
||||
titleTime = fadeInTime + stayTime + fadeOutTime;
|
||||
}
|
||||
|
||||
/** Set the subtitle. Only visible while a title is active. */
|
||||
public static void setSubtitle(IChatComponent component) {
|
||||
subtitle = component;
|
||||
}
|
||||
|
||||
/** Set fade timing. Negative values leave that field unchanged. If a title is displaying, the timer restarts. */
|
||||
public static void setTimes(int fadeIn, int stay, int fadeOut) {
|
||||
if (fadeIn >= 0) fadeInTime = fadeIn;
|
||||
if (stay >= 0) stayTime = stay;
|
||||
if (fadeOut >= 0) fadeOutTime = fadeOut;
|
||||
if (titleTime > 0) {
|
||||
titleTime = fadeInTime + stayTime + fadeOutTime;
|
||||
}
|
||||
}
|
||||
|
||||
/** Clear title and subtitle and stop the timer. */
|
||||
public static void clear() {
|
||||
title = null;
|
||||
subtitle = null;
|
||||
titleTime = 0;
|
||||
}
|
||||
|
||||
/** Reset fade times to defaults (10/70/20). Does not clear the current title. */
|
||||
public static void reset() {
|
||||
fadeInTime = 10;
|
||||
stayTime = 70;
|
||||
fadeOutTime = 20;
|
||||
}
|
||||
|
||||
/** Decrement the timer once per client tick; clears title and subtitle when it reaches zero. */
|
||||
public static void tick() {
|
||||
if (titleTime > 0) {
|
||||
titleTime--;
|
||||
if (titleTime <= 0) {
|
||||
title = null;
|
||||
subtitle = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,100 @@
|
||||
package com.gtnewhorizon.gtnhlib.commands;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import net.minecraft.command.CommandBase;
|
||||
import net.minecraft.command.ICommandSender;
|
||||
import net.minecraft.command.WrongUsageException;
|
||||
import net.minecraft.entity.player.EntityPlayerMP;
|
||||
import net.minecraft.server.MinecraftServer;
|
||||
import net.minecraft.util.ChatComponentText;
|
||||
import net.minecraft.util.IChatComponent;
|
||||
|
||||
import com.gtnewhorizon.gtnhlib.network.TitlePacketHandler;
|
||||
|
||||
public final class TitleCommand extends CommandBase {
|
||||
|
||||
@Override
|
||||
public String getCommandName() {
|
||||
return "title";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getCommandUsage(ICommandSender sender) {
|
||||
return "/title <player> <clear|reset|title|subtitle|times> [args...]";
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getRequiredPermissionLevel() {
|
||||
return 2;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void processCommand(ICommandSender sender, String[] args) {
|
||||
if (args.length < 2) {
|
||||
throw new WrongUsageException(getCommandUsage(sender));
|
||||
}
|
||||
|
||||
EntityPlayerMP player = getPlayer(sender, args[0]);
|
||||
String action = args[1].toLowerCase();
|
||||
|
||||
switch (action) {
|
||||
case "clear":
|
||||
TitlePacketHandler.sendClear(player);
|
||||
func_152373_a(sender, this, "commands.title.cleared.single", player.getCommandSenderName());
|
||||
break;
|
||||
case "reset":
|
||||
TitlePacketHandler.sendReset(player);
|
||||
func_152373_a(sender, this, "commands.title.reset.single", player.getCommandSenderName());
|
||||
break;
|
||||
case "title":
|
||||
if (args.length < 3) throw new WrongUsageException("/title <player> title <text...>");
|
||||
TitlePacketHandler.sendTitle(player, textFromArgs(args, 2));
|
||||
func_152373_a(sender, this, "commands.title.show.title.single", player.getCommandSenderName());
|
||||
break;
|
||||
case "subtitle":
|
||||
if (args.length < 3) throw new WrongUsageException("/title <player> subtitle <text...>");
|
||||
TitlePacketHandler.sendSubtitle(player, textFromArgs(args, 2));
|
||||
func_152373_a(sender, this, "commands.title.show.subtitle.single", player.getCommandSenderName());
|
||||
break;
|
||||
case "times":
|
||||
if (args.length < 5) throw new WrongUsageException("/title <player> times <fadeIn> <stay> <fadeOut>");
|
||||
TitlePacketHandler.sendTimes(
|
||||
player,
|
||||
parseInt(sender, args[2]),
|
||||
parseInt(sender, args[3]),
|
||||
parseInt(sender, args[4]));
|
||||
func_152373_a(sender, this, "commands.title.times.single", player.getCommandSenderName());
|
||||
break;
|
||||
default:
|
||||
throw new WrongUsageException(getCommandUsage(sender));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public List addTabCompletionOptions(ICommandSender sender, String[] args) {
|
||||
if (args.length == 1) {
|
||||
return getListOfStringsMatchingLastWord(args, MinecraftServer.getServer().getAllUsernames());
|
||||
}
|
||||
if (args.length == 2) {
|
||||
return getListOfStringsMatchingLastWord(args, "clear", "reset", "title", "subtitle", "times");
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isUsernameIndex(String[] args, int index) {
|
||||
return index == 0;
|
||||
}
|
||||
|
||||
private static IChatComponent textFromArgs(String[] args, int start) {
|
||||
String text = String.join(" ", Arrays.copyOfRange(args, start, args.length));
|
||||
// Try parsing as JSON component first, fall back to plain text
|
||||
try {
|
||||
return IChatComponent.Serializer.func_150699_a(text);
|
||||
} catch (Exception e) {
|
||||
return new ChatComponentText(text);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -56,7 +56,8 @@ public enum Mixins implements IMixins {
|
||||
"Allow Thaumcraft Infusion Recipes to transform items instead of consuming them in an EnhancedInfusionRecipe")
|
||||
.setPhase(Phase.LATE).addCommonMixins("MixinEnhancedInfusionRecipe")
|
||||
.addRequiredMod(TargetMods.THAUMCRAFT)),
|
||||
CONFIG_ORDER(Side.CLIENT, "fml.MixinGuiConfig")
|
||||
CONFIG_ORDER(Side.CLIENT, "fml.MixinGuiConfig"),
|
||||
TITLE_OVERLAY(Side.CLIENT, "MixinGuiIngameForge_TitleRender", "MixinGuiIngame_TitleTick")
|
||||
//
|
||||
;
|
||||
|
||||
|
||||
+93
@@ -0,0 +1,93 @@
|
||||
package com.gtnewhorizon.gtnhlib.mixins.early;
|
||||
|
||||
import net.minecraft.client.Minecraft;
|
||||
import net.minecraft.client.gui.FontRenderer;
|
||||
import net.minecraft.client.gui.ScaledResolution;
|
||||
import net.minecraft.util.IChatComponent;
|
||||
import net.minecraft.util.MathHelper;
|
||||
import net.minecraftforge.client.GuiIngameForge;
|
||||
|
||||
import org.lwjgl.opengl.GL11;
|
||||
import org.spongepowered.asm.mixin.Mixin;
|
||||
import org.spongepowered.asm.mixin.Shadow;
|
||||
import org.spongepowered.asm.mixin.injection.At;
|
||||
import org.spongepowered.asm.mixin.injection.Inject;
|
||||
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
|
||||
|
||||
import com.gtnewhorizon.gtnhlib.client.title.TitleAPI;
|
||||
|
||||
@Mixin(GuiIngameForge.class)
|
||||
public class MixinGuiIngameForge_TitleRender {
|
||||
|
||||
@Shadow
|
||||
private ScaledResolution res;
|
||||
|
||||
@Shadow
|
||||
private FontRenderer fontrenderer;
|
||||
|
||||
@Inject(
|
||||
method = "renderGameOverlay",
|
||||
at = @At(
|
||||
value = "INVOKE",
|
||||
target = "Lnet/minecraftforge/client/GuiIngameForge;renderRecordOverlay(IIF)V",
|
||||
shift = At.Shift.AFTER,
|
||||
remap = false))
|
||||
private void gtnhlib$renderTitle(float partialTicks, boolean hasScreen, int mouseX, int mouseY, CallbackInfo ci) {
|
||||
IChatComponent title = TitleAPI.getTitle();
|
||||
int titleTime = TitleAPI.getTitleTime();
|
||||
if (title == null || titleTime <= 0) return;
|
||||
|
||||
Minecraft mc = Minecraft.getMinecraft();
|
||||
mc.mcProfiler.startSection("titleAndSubtitle");
|
||||
|
||||
int width = res.getScaledWidth();
|
||||
int height = res.getScaledHeight();
|
||||
|
||||
float f = (float) titleTime - partialTicks;
|
||||
int fadeIn = TitleAPI.getFadeInTime();
|
||||
int stay = TitleAPI.getStayTime();
|
||||
int fadeOut = TitleAPI.getFadeOutTime();
|
||||
int alpha = 255;
|
||||
|
||||
if (titleTime > fadeOut + stay) {
|
||||
float totalTime = (float) (fadeIn + stay + fadeOut);
|
||||
alpha = (int) ((totalTime - f) * 255.0F / (float) fadeIn);
|
||||
} else if (titleTime <= fadeOut) {
|
||||
alpha = (int) (f * 255.0F / (float) fadeOut);
|
||||
}
|
||||
|
||||
alpha = MathHelper.clamp_int(alpha, 0, 255);
|
||||
if (alpha <= 8) {
|
||||
mc.mcProfiler.endSection();
|
||||
return;
|
||||
}
|
||||
|
||||
int color = 0xFFFFFF | (alpha << 24);
|
||||
|
||||
GL11.glPushMatrix();
|
||||
GL11.glTranslatef((float) (width / 2), (float) (height / 2), 0.0F);
|
||||
GL11.glEnable(GL11.GL_BLEND);
|
||||
|
||||
GL11.glPushMatrix();
|
||||
GL11.glScalef(4.0F, 4.0F, 4.0F);
|
||||
String titleText = title.getFormattedText();
|
||||
int titleWidth = fontrenderer.getStringWidth(titleText);
|
||||
fontrenderer.drawStringWithShadow(titleText, -titleWidth / 2, -10, color);
|
||||
GL11.glPopMatrix();
|
||||
|
||||
IChatComponent subtitle = TitleAPI.getSubtitle();
|
||||
if (subtitle != null) {
|
||||
GL11.glPushMatrix();
|
||||
GL11.glScalef(2.0F, 2.0F, 2.0F);
|
||||
String subText = subtitle.getFormattedText();
|
||||
int subWidth = fontrenderer.getStringWidth(subText);
|
||||
fontrenderer.drawStringWithShadow(subText, -subWidth / 2, 5, color);
|
||||
GL11.glPopMatrix();
|
||||
}
|
||||
|
||||
GL11.glDisable(GL11.GL_BLEND);
|
||||
GL11.glPopMatrix();
|
||||
|
||||
mc.mcProfiler.endSection();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package com.gtnewhorizon.gtnhlib.mixins.early;
|
||||
|
||||
import net.minecraft.client.gui.GuiIngame;
|
||||
|
||||
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 com.gtnewhorizon.gtnhlib.client.title.TitleAPI;
|
||||
|
||||
@Mixin(GuiIngame.class)
|
||||
public class MixinGuiIngame_TitleTick {
|
||||
|
||||
@Inject(method = "updateTick", at = @At("TAIL"))
|
||||
private void gtnhlib$tickTitle(CallbackInfo ci) {
|
||||
TitleAPI.tick();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,94 @@
|
||||
package com.gtnewhorizon.gtnhlib.network;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
import net.minecraft.util.IChatComponent;
|
||||
|
||||
import cpw.mods.fml.common.network.simpleimpl.IMessage;
|
||||
import io.netty.buffer.ByteBuf;
|
||||
|
||||
public class MessageTitle implements IMessage {
|
||||
|
||||
/** 0=TITLE, 1=SUBTITLE, 2=TIMES, 3=CLEAR, 4=RESET */
|
||||
public int action;
|
||||
|
||||
/** For TITLE/SUBTITLE: the IChatComponent JSON */
|
||||
public String componentJson;
|
||||
|
||||
/** For TIMES: fadeIn, stay, fadeOut ticks */
|
||||
public int fadeIn, stay, fadeOut;
|
||||
|
||||
public MessageTitle() {}
|
||||
|
||||
public static MessageTitle title(IChatComponent component) {
|
||||
MessageTitle msg = new MessageTitle();
|
||||
msg.action = 0;
|
||||
msg.componentJson = IChatComponent.Serializer.func_150696_a(component);
|
||||
return msg;
|
||||
}
|
||||
|
||||
public static MessageTitle subtitle(IChatComponent component) {
|
||||
MessageTitle msg = new MessageTitle();
|
||||
msg.action = 1;
|
||||
msg.componentJson = IChatComponent.Serializer.func_150696_a(component);
|
||||
return msg;
|
||||
}
|
||||
|
||||
public static MessageTitle times(int fadeIn, int stay, int fadeOut) {
|
||||
MessageTitle msg = new MessageTitle();
|
||||
msg.action = 2;
|
||||
msg.fadeIn = fadeIn;
|
||||
msg.stay = stay;
|
||||
msg.fadeOut = fadeOut;
|
||||
return msg;
|
||||
}
|
||||
|
||||
public static MessageTitle clear() {
|
||||
MessageTitle msg = new MessageTitle();
|
||||
msg.action = 3;
|
||||
return msg;
|
||||
}
|
||||
|
||||
public static MessageTitle reset() {
|
||||
MessageTitle msg = new MessageTitle();
|
||||
msg.action = 4;
|
||||
return msg;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void fromBytes(ByteBuf buf) {
|
||||
action = buf.readByte();
|
||||
switch (action) {
|
||||
case 0:
|
||||
case 1:
|
||||
int len = buf.readShort();
|
||||
byte[] bytes = new byte[len];
|
||||
buf.readBytes(bytes);
|
||||
componentJson = new String(bytes, StandardCharsets.UTF_8);
|
||||
break;
|
||||
case 2:
|
||||
fadeIn = buf.readInt();
|
||||
stay = buf.readInt();
|
||||
fadeOut = buf.readInt();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void toBytes(ByteBuf buf) {
|
||||
buf.writeByte(action);
|
||||
switch (action) {
|
||||
case 0:
|
||||
case 1:
|
||||
byte[] bytes = componentJson.getBytes(StandardCharsets.UTF_8);
|
||||
buf.writeShort(bytes.length);
|
||||
buf.writeBytes(bytes);
|
||||
break;
|
||||
case 2:
|
||||
buf.writeInt(fadeIn);
|
||||
buf.writeInt(stay);
|
||||
buf.writeInt(fadeOut);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
package com.gtnewhorizon.gtnhlib.network;
|
||||
|
||||
import net.minecraft.util.ChatComponentText;
|
||||
import net.minecraft.util.IChatComponent;
|
||||
|
||||
import com.gtnewhorizon.gtnhlib.client.title.TitleAPI;
|
||||
|
||||
import cpw.mods.fml.common.network.simpleimpl.IMessage;
|
||||
import cpw.mods.fml.common.network.simpleimpl.IMessageHandler;
|
||||
import cpw.mods.fml.common.network.simpleimpl.MessageContext;
|
||||
|
||||
public class MessageTitleHandler implements IMessageHandler<MessageTitle, IMessage> {
|
||||
|
||||
@Override
|
||||
public IMessage onMessage(MessageTitle msg, MessageContext ctx) {
|
||||
switch (msg.action) {
|
||||
case 0: // TITLE
|
||||
TitleAPI.setTitle(deserialize(msg.componentJson));
|
||||
break;
|
||||
case 1: // SUBTITLE
|
||||
TitleAPI.setSubtitle(deserialize(msg.componentJson));
|
||||
break;
|
||||
case 2: // TIMES
|
||||
TitleAPI.setTimes(msg.fadeIn, msg.stay, msg.fadeOut);
|
||||
break;
|
||||
case 3: // CLEAR
|
||||
TitleAPI.clear();
|
||||
break;
|
||||
case 4: // RESET
|
||||
TitleAPI.clear();
|
||||
TitleAPI.reset();
|
||||
break;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private static IChatComponent deserialize(String json) {
|
||||
if (json == null || json.isEmpty()) return new ChatComponentText("");
|
||||
try {
|
||||
return IChatComponent.Serializer.func_150699_a(json);
|
||||
} catch (Exception e) {
|
||||
return new ChatComponentText(json);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -21,5 +21,6 @@ public class NetworkHandler {
|
||||
instance.registerMessage(PacketSyncConfig.Handler.class, PacketSyncConfig.class, 1, Side.CLIENT);
|
||||
instance.registerMessage(PacketKeyDown.HandlerKeyDown.class, PacketKeyDown.class, 2, Side.SERVER);
|
||||
instance.registerMessage(PacketViewDistance.Handler.class, PacketViewDistance.class, 3, Side.CLIENT);
|
||||
instance.registerMessage(MessageTitleHandler.class, MessageTitle.class, 4, Side.CLIENT);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package com.gtnewhorizon.gtnhlib.network;
|
||||
|
||||
import net.minecraft.entity.player.EntityPlayerMP;
|
||||
import net.minecraft.util.IChatComponent;
|
||||
|
||||
public class TitlePacketHandler {
|
||||
|
||||
public static void sendTitle(EntityPlayerMP player, IChatComponent component) {
|
||||
NetworkHandler.instance.sendTo(MessageTitle.title(component), player);
|
||||
}
|
||||
|
||||
public static void sendSubtitle(EntityPlayerMP player, IChatComponent component) {
|
||||
NetworkHandler.instance.sendTo(MessageTitle.subtitle(component), player);
|
||||
}
|
||||
|
||||
public static void sendTimes(EntityPlayerMP player, int fadeIn, int stay, int fadeOut) {
|
||||
NetworkHandler.instance.sendTo(MessageTitle.times(fadeIn, stay, fadeOut), player);
|
||||
}
|
||||
|
||||
public static void sendClear(EntityPlayerMP player) {
|
||||
NetworkHandler.instance.sendTo(MessageTitle.clear(), player);
|
||||
}
|
||||
|
||||
public static void sendReset(EntityPlayerMP player) {
|
||||
NetworkHandler.instance.sendTo(MessageTitle.reset(), player);
|
||||
}
|
||||
}
|
||||
@@ -122,3 +122,8 @@ gtnhlib.chat.rpupdater.info.no_updater=No updater metadata found for this pack.
|
||||
gtnhlib.chat.rpupdater.info.updater_name=Updater name: §7%s
|
||||
gtnhlib.chat.rpupdater.info.version=Version: §7%s (GTNH %s)
|
||||
gtnhlib.chat.rpupdater.info.source=Source: §7%s/%s
|
||||
commands.title.cleared.single=Cleared title for %s
|
||||
commands.title.reset.single=Reset title options for %s
|
||||
commands.title.show.title.single=Showed title to %s
|
||||
commands.title.show.subtitle.single=Showed subtitle to %s
|
||||
commands.title.times.single=Changed title display times for %s
|
||||
Reference in new issue
Block a user