diff --git a/src/main/java/com/gtnewhorizon/gtnhlib/CommonProxy.java b/src/main/java/com/gtnewhorizon/gtnhlib/CommonProxy.java index 78db175f..d189bb5b 100644 --- a/src/main/java/com/gtnewhorizon/gtnhlib/CommonProxy.java +++ b/src/main/java/com/gtnewhorizon/gtnhlib/CommonProxy.java @@ -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) {} diff --git a/src/main/java/com/gtnewhorizon/gtnhlib/client/title/TitleAPI.java b/src/main/java/com/gtnewhorizon/gtnhlib/client/title/TitleAPI.java new file mode 100644 index 00000000..1b5f55a7 --- /dev/null +++ b/src/main/java/com/gtnewhorizon/gtnhlib/client/title/TitleAPI.java @@ -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; + } + } + } +} diff --git a/src/main/java/com/gtnewhorizon/gtnhlib/commands/TitleCommand.java b/src/main/java/com/gtnewhorizon/gtnhlib/commands/TitleCommand.java new file mode 100644 index 00000000..ea9ccc58 --- /dev/null +++ b/src/main/java/com/gtnewhorizon/gtnhlib/commands/TitleCommand.java @@ -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 [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 title "); + 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 subtitle "); + 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 times "); + 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); + } + } +} diff --git a/src/main/java/com/gtnewhorizon/gtnhlib/mixins/Mixins.java b/src/main/java/com/gtnewhorizon/gtnhlib/mixins/Mixins.java index 8d79cec3..c6aa56ba 100644 --- a/src/main/java/com/gtnewhorizon/gtnhlib/mixins/Mixins.java +++ b/src/main/java/com/gtnewhorizon/gtnhlib/mixins/Mixins.java @@ -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") // ; diff --git a/src/main/java/com/gtnewhorizon/gtnhlib/mixins/early/MixinGuiIngameForge_TitleRender.java b/src/main/java/com/gtnewhorizon/gtnhlib/mixins/early/MixinGuiIngameForge_TitleRender.java new file mode 100644 index 00000000..4d44aca7 --- /dev/null +++ b/src/main/java/com/gtnewhorizon/gtnhlib/mixins/early/MixinGuiIngameForge_TitleRender.java @@ -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(); + } +} diff --git a/src/main/java/com/gtnewhorizon/gtnhlib/mixins/early/MixinGuiIngame_TitleTick.java b/src/main/java/com/gtnewhorizon/gtnhlib/mixins/early/MixinGuiIngame_TitleTick.java new file mode 100644 index 00000000..2460eaa0 --- /dev/null +++ b/src/main/java/com/gtnewhorizon/gtnhlib/mixins/early/MixinGuiIngame_TitleTick.java @@ -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(); + } +} diff --git a/src/main/java/com/gtnewhorizon/gtnhlib/network/MessageTitle.java b/src/main/java/com/gtnewhorizon/gtnhlib/network/MessageTitle.java new file mode 100644 index 00000000..14603477 --- /dev/null +++ b/src/main/java/com/gtnewhorizon/gtnhlib/network/MessageTitle.java @@ -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; + } + } +} diff --git a/src/main/java/com/gtnewhorizon/gtnhlib/network/MessageTitleHandler.java b/src/main/java/com/gtnewhorizon/gtnhlib/network/MessageTitleHandler.java new file mode 100644 index 00000000..3a76e6d4 --- /dev/null +++ b/src/main/java/com/gtnewhorizon/gtnhlib/network/MessageTitleHandler.java @@ -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 { + + @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); + } + } +} diff --git a/src/main/java/com/gtnewhorizon/gtnhlib/network/NetworkHandler.java b/src/main/java/com/gtnewhorizon/gtnhlib/network/NetworkHandler.java index 64475542..65f78ce6 100644 --- a/src/main/java/com/gtnewhorizon/gtnhlib/network/NetworkHandler.java +++ b/src/main/java/com/gtnewhorizon/gtnhlib/network/NetworkHandler.java @@ -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); } } diff --git a/src/main/java/com/gtnewhorizon/gtnhlib/network/TitlePacketHandler.java b/src/main/java/com/gtnewhorizon/gtnhlib/network/TitlePacketHandler.java new file mode 100644 index 00000000..05534b6d --- /dev/null +++ b/src/main/java/com/gtnewhorizon/gtnhlib/network/TitlePacketHandler.java @@ -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); + } +} diff --git a/src/main/resources/assets/gtnhlib/lang/en_US.lang b/src/main/resources/assets/gtnhlib/lang/en_US.lang index 83cd2e27..3d79a6ad 100644 --- a/src/main/resources/assets/gtnhlib/lang/en_US.lang +++ b/src/main/resources/assets/gtnhlib/lang/en_US.lang @@ -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