Afficher un texte à l'écran
-
Utilises la class GuiUtils, elle a des fonctions statiques crées par forge pour faire des rendus de Gui (texture, boutons, texte…)
GuiUtils.drawString (…) par exempleTu as aussi des fonctions pratiques dans Font renderer, tu le récupère comme ça :
Minecraft.getMinecraft().fontObj
@SubscribeEvent @SideOnly(Side.CLIENT) public void onGuiIngameRender(RenderGameOverlayEvent.Post event) { if (cd_est_insere) { String str; int x; int y; str = "Coucou"; x = 0; y = 0; font.drawStringWithShadow(str, x - font.getStringWidth(str) / 2, y, Integer.MAX_VALUE); } } VOIR AUSSI: Minecraft.getMinecraft().fontRenderer.drawStringWithShadow(…)
Bon là je t’ai donné tout le code, mais la prochaine fois essayes de chercher un peu plus, je t’avais donné tous les éléments
x: position x sur l’ecran
y: position y sur l’ecran
font.getStringWidth(String str); retournes la taille d’une chaine de caractere en pixel, le / 2 sert à la centrer
Integer.MAX_VALUE; couleur sous forme d’entier, pour écrire en blanc -
[font=monospaceJe look ça]
-
Fonctionne toujours pas
-
Envoie tout ton code.
-
import net.minecraft.client.Minecraft; import net.minecraft.client.gui.FontRenderer; import net.minecraft.client.gui.GuiIngame; import net.minecraft.client.gui.GuiIngameMenu; import net.minecraft.client.gui.GuiMainMenu; import net.minecraft.client.gui.GuiScreen; import net.minecraft.init.Blocks; import net.minecraftforge.client.GuiIngameForge; import net.minecraftforge.client.event.ClientChatReceivedEvent; import net.minecraftforge.client.event.RenderGameOverlayEvent; import net.minecraftforge.common.MinecraftForge; import cpw.mods.fml.common.FMLCommonHandler; import cpw.mods.fml.common.Mod; import cpw.mods.fml.common.Mod.EventHandler; import cpw.mods.fml.common.event.FMLInitializationEvent; import cpw.mods.fml.common.eventhandler.SubscribeEvent; import cpw.mods.fml.common.gameevent.TickEvent; import cpw.mods.fml.common.gameevent.TickEvent.Phase; import cpw.mods.fml.relauncher.Side; import cpw.mods.fml.relauncher.SideOnly; @Mod(modid = BaseMod.MODID, version = BaseMod.VERSION) public class BaseMod { public static final String MODID = "GuiInGame"; public static final String VERSION = "1.0"; private Minecraft mc; protected String msgPlaying = ""; protected int msgPlayingUpFor = 0; private FontRenderer font = null; protected boolean msgIsPlaying = false; private GuiIngameForge mcm; @EventHandler public void init(FMLInitializationEvent event) { // some example code System.out.println("DIRT BLOCK >> " + Blocks.dirt.getUnlocalizedName()); FMLCommonHandler.instance().bus().register(this); } @SideOnly(Side.CLIENT) public void onGuiIngameRender(RenderGameOverlayEvent.Post event) { if (this.msgIsPlaying) { String str; int x; int y; str = "§e ~"; x = 1; y = -4; font.drawStringWithShadow(str, x - font.getStringWidth(str) / 2, y, Integer.MAX_VALUE); } } public void renderMsg(String message) { this.msgPlaying = message; this.msgPlayingUpFor = 60; this.msgIsPlaying = true; } @EventHandler public void onClientChatMessage(ClientChatReceivedEvent event) { if(event.message.getFormattedText().startsWith("§e ~")) { this.mcm.renderMsg("§e ~"); } else this.mcm.getChatGUI().printChatMessage(event.message); }
Voilà
-
private FontRenderer font = null;
-> NPE
private GuiIngameForge mcm;
-> inutile + NPE à nouveau
private Minecraft mc;
-> encore NPE@SideOnly(Side.CLIENT)
public void onGuiIngameRender(RenderGameOverlayEvent.Post event)
il manque le subscribe event.
Il faut aussi ajouter l’enregistrement de l’event de forge dans la fonction init. -
[size=xx-smallIl faut aussi ajouter l’enregistrement de l’event de forge dans la fonction init.]
[size=xx-smallJe dois mêttre quoi dans la fonction init pour enregistré l’event ?]
-
package fr.frstyle13.ingame; import net.minecraft.client.Minecraft; import net.minecraft.client.gui.FontRenderer; import net.minecraft.client.gui.GuiIngame; import net.minecraft.client.gui.GuiIngameMenu; import net.minecraft.client.gui.GuiMainMenu; import net.minecraft.client.gui.GuiScreen; import net.minecraft.init.Blocks; import net.minecraftforge.client.GuiIngameForge; import net.minecraftforge.client.event.ClientChatReceivedEvent; import net.minecraftforge.client.event.RenderGameOverlayEvent; import net.minecraftforge.common.MinecraftForge; import cpw.mods.fml.common.FMLCommonHandler; import cpw.mods.fml.common.Mod; import cpw.mods.fml.common.Mod.EventHandler; import cpw.mods.fml.common.event.FMLInitializationEvent; import cpw.mods.fml.common.eventhandler.SubscribeEvent; import cpw.mods.fml.common.gameevent.TickEvent; import cpw.mods.fml.common.gameevent.TickEvent.Phase; import cpw.mods.fml.relauncher.Side; import cpw.mods.fml.relauncher.SideOnly; @Mod(modid = BaseMod.MODID, version = BaseMod.VERSION) public class BaseMod { public static final String MODID = "GuiInGame"; public static final String VERSION = "1.0"; private Minecraft mc; protected String msgPlaying = ""; protected int msgPlayingUpFor = 0; private FontRenderer font = null; protected boolean msgIsPlaying = false; private GuiIngameForge mcm; @EventHandler public void init(FMLInitializationEvent event) { // some example code System.out.println("DIRT BLOCK >> " + Blocks.dirt.getUnlocalizedName()); FMLCommonHandler.instance().bus().register(this); MinecraftForge.EVENT_BUS.register(new BaseMod()); } @SubscribeEvent @SideOnly(Side.CLIENT) public void onGuiIngameRender(RenderGameOverlayEvent.Post event) { if (this.msgIsPlaying) { String str; int x; int y; str = "§e ~"; x = 1; y = -4; Minecraft.getMinecraft().fontRenderer.drawStringWithShadow(str, x - Minecraft.getMinecraft().fontRenderer.getStringWidth(str) / 2, y, Integer.MAX_VALUE); } } public void renderMsg(String message) { this.msgPlaying = message; this.msgPlayingUpFor = 60; this.msgIsPlaying = true; } @EventHandler public void onClientChatMessage(ClientChatReceivedEvent event) { if(event.message.getFormattedText().startsWith("§e ~")) { this.mcm.renderMsg("§e ~"); } else this.mcm.getChatGUI().printChatMessage(event.message); } }
Quand je passe dans la faction claim, ça me l’affiche dans le chat et non à l’écran même avec tout ce que tu m’as dit
-
MinecraftForge.EVENT_BUS.register(this);
dans initPour l’événement, fait plutôt :
@EventHandler public void onClientChatMessage(ClientChatReceivedEvent event) { if(event.message.getFormattedText().startsWith("§e ~")) { this.mcm.renderMsg("§e ~"); event.setCancel(true); } }
-
Fonctionne toujours pas ça m’affiche toujours
~ Wilderness
dans le chat
-
Car ton message commence par ~ et non §e
Donc soit met juste ~, soit remplace getFormattedText par getUnformattedText -
ça change rien
-
ça ne change rien et quand je fais ça :
@EventHandler public void onClientChatMessage(ClientChatReceivedEvent event) { if(event.message.getFormattedText().startsWith("§e ~")) { this.mcm.renderMsg(event.message.getFormattedText()); } else System.out.println("Ce message s'affiche"); this.mcm.getChatGUI().printChatMessage(event.message); }
Quand je met le System.out, et que je passe dans le claim rien ne s’affiche dans la console Pourtant ça dois m’afficher : ‘Ce message s’affiche’ Vraiment bizzare
-
Renvoie toute ta classe.
-
package fr.frstyle13.ingame; import net.minecraft.client.Minecraft; import net.minecraft.client.gui.FontRenderer; import net.minecraft.client.gui.GuiIngame; import net.minecraft.client.gui.GuiIngameMenu; import net.minecraft.client.gui.GuiMainMenu; import net.minecraft.client.gui.GuiScreen; import net.minecraft.init.Blocks; import net.minecraftforge.client.GuiIngameForge; import net.minecraftforge.client.event.ClientChatReceivedEvent; import net.minecraftforge.client.event.RenderGameOverlayEvent; import net.minecraftforge.common.MinecraftForge; import cpw.mods.fml.common.FMLCommonHandler; import cpw.mods.fml.common.Mod; import cpw.mods.fml.common.Mod.EventHandler; import cpw.mods.fml.common.event.FMLInitializationEvent; import cpw.mods.fml.common.eventhandler.SubscribeEvent; import cpw.mods.fml.common.gameevent.TickEvent; import cpw.mods.fml.common.gameevent.TickEvent.Phase; import cpw.mods.fml.relauncher.Side; import cpw.mods.fml.relauncher.SideOnly; @Mod(modid = BaseMod.MODID, version = BaseMod.VERSION) public class BaseMod { public static final String MODID = "GuiInGame"; public static final String VERSION = "1.0"; protected String msgPlaying = ""; protected int msgPlayingUpFor = 0; protected boolean msgIsPlaying = false; private GuiIngameForge mcm; @EventHandler public void init(FMLInitializationEvent event) { // some example code System.out.println("DIRT BLOCK >> " + Blocks.dirt.getUnlocalizedName()); FMLCommonHandler.instance().bus().register(this); MinecraftForge.EVENT_BUS.register(this); } @SubscribeEvent @SideOnly(Side.CLIENT) public void onGuiIngameRender(RenderGameOverlayEvent.Post event) { if (this.msgIsPlaying) { String str; int x; int y; str = "§e ~"; x = 1; y = -4; Minecraft.getMinecraft().fontRenderer.drawStringWithShadow(str, x - Minecraft.getMinecraft().fontRenderer.getStringWidth(str) / 2, y, Integer.MAX_VALUE); } } public void renderMsg(String message) { this.msgPlaying = message; this.msgPlayingUpFor = 60; this.msgIsPlaying = true; } @EventHandler public void onClientChatMessage(ClientChatReceivedEvent event) { if(event.message.getFormattedText().startsWith("§e ~")) { this.mcm.renderMsg(event.message.getFormattedText()); } this.mcm.getChatGUI().printChatMessage(event.message); } }
et le private GuiIngameForge mcm; sert à quelque chose hein sinon il y aurais une erreur au this.mcm.renderMsg(event.message.getFormattedText());
**EDIT: Même quand je fais ça sa fonctionne toujours pas rien ne se passe **
@EventHandler public void onClientChatMessage(ClientChatReceivedEvent event) { if(event.message.getFormattedText().startsWith("§e ~")) { System.out.println("Mdr"); } else this.mcm.getChatGUI().printChatMessage(event.message); }
-
C’est bon j’ai trouver fallait mêttre : @SubscribeEvent , et aussi quand je rentre dans ma zone claim ça me Crash avec l’erreur :
–-- Minecraft Crash Report ---- // Who set us up the TNT? Time: 18/09/14 17:45 Description: Unexpected error java.lang.NullPointerException: Unexpected error at fr.frstyle13.ingame.BaseMod.onClientChatMessage(BaseMod.java:105) at cpw.mods.fml.common.eventhandler.ASMEventHandler_6_BaseMod_onClientChatMessage_ClientChatReceivedEvent.invoke(.dynamic) at cpw.mods.fml.common.eventhandler.ASMEventHandler.invoke(ASMEventHandler.java:51) at cpw.mods.fml.common.eventhandler.EventBus.post(EventBus.java:122) at net.minecraft.client.network.NetHandlerPlayClient.handleChat(NetHandlerPlayClient.java:788) at net.minecraft.network.play.server.S02PacketChat.processPacket(S02PacketChat.java:56) at net.minecraft.network.play.server.S02PacketChat.processPacket(S02PacketChat.java:83) at net.minecraft.network.NetworkManager.processReceivedPackets(NetworkManager.java:232) at net.minecraft.client.multiplayer.PlayerControllerMP.updateController(PlayerControllerMP.java:321) at net.minecraft.client.Minecraft.runTick(Minecraft.java:1649) at net.minecraft.client.Minecraft.runGameLoop(Minecraft.java:996) at net.minecraft.client.Minecraft.run(Minecraft.java:912) at net.minecraft.client.main.Main.main(Main.java:112) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) at net.minecraft.launchwrapper.Launch.launch(Launch.java:134) at net.minecraft.launchwrapper.Launch.main(Launch.java:28) A detailed walkthrough of the error, its code path and all known details is as follows: --------------------------------------------------------------------------------------- -- Head -- Stacktrace: at fr.frstyle13.ingame.BaseMod.onClientChatMessage(BaseMod.java:105) at cpw.mods.fml.common.eventhandler.ASMEventHandler_6_BaseMod_onClientChatMessage_ClientChatReceivedEvent.invoke(.dynamic) at cpw.mods.fml.common.eventhandler.ASMEventHandler.invoke(ASMEventHandler.java:51) at cpw.mods.fml.common.eventhandler.EventBus.post(EventBus.java:122) at net.minecraft.client.network.NetHandlerPlayClient.handleChat(NetHandlerPlayClient.java:788) at net.minecraft.network.play.server.S02PacketChat.processPacket(S02PacketChat.java:56) at net.minecraft.network.play.server.S02PacketChat.processPacket(S02PacketChat.java:83) at net.minecraft.network.NetworkManager.processReceivedPackets(NetworkManager.java:232) at net.minecraft.client.multiplayer.PlayerControllerMP.updateController(PlayerControllerMP.java:321)
Donc en gros l’erreur viens de :
this.mcm.renderMsg(event.message.getFormattedText());
-
Je te l’avais dit …
@‘robin4002’:private FontRenderer font = null;
-> NPE
private GuiIngameForge mcm;
-> inutile + NPE à nouveau
private Minecraft mc;
-> encore NPENPE = NullPointerException
-
Je fais comment alors ?
-
Apprend le java ?
package fr.frstyle13.ingame; import net.minecraft.client.Minecraft; import net.minecraft.client.gui.FontRenderer; import net.minecraft.client.gui.GuiIngame; import net.minecraft.client.gui.GuiIngameMenu; import net.minecraft.client.gui.GuiMainMenu; import net.minecraft.client.gui.GuiScreen; import net.minecraft.init.Blocks; import net.minecraftforge.client.GuiIngameForge; import net.minecraftforge.client.event.ClientChatReceivedEvent; import net.minecraftforge.client.event.RenderGameOverlayEvent; import net.minecraftforge.common.MinecraftForge; import cpw.mods.fml.common.FMLCommonHandler; import cpw.mods.fml.common.Mod; import cpw.mods.fml.common.Mod.EventHandler; import cpw.mods.fml.common.event.FMLInitializationEvent; import cpw.mods.fml.common.eventhandler.SubscribeEvent; import cpw.mods.fml.common.gameevent.TickEvent; import cpw.mods.fml.common.gameevent.TickEvent.Phase; import cpw.mods.fml.relauncher.Side; import cpw.mods.fml.relauncher.SideOnly; @Mod(modid = BaseMod.MODID, version = BaseMod.VERSION) public class BaseMod { public static final String MODID = "GuiInGame"; public static final String VERSION = "1.0"; protected String msgPlaying = ""; protected int msgPlayingUpFor = 0; @EventHandler public void init(FMLInitializationEvent event) { FMLCommonHandler.instance().bus().register(this); MinecraftForge.EVENT_BUS.register(this); } @SubscribeEvent @SideOnly(Side.CLIENT) public void onGuiIngameRender(RenderGameOverlayEvent.Post event) { if (this.msgPlayingUpFor > 0) { String str = "§e ~" + this.msgPlaying; int x = 1; int y = -4; Minecraft.getMinecraft().fontRenderer.drawStringWithShadow(str, x - Minecraft.getMinecraft().fontRenderer.getStringWidth(str) / 2, y, Integer.MAX_VALUE); } else { this.msgPlayingUpFor–; } } public void renderMsg(String message) { this.msgPlaying = message; this.msgPlayingUpFor = 300; } @EventHandler public void onClientChatMessage(ClientChatReceivedEvent event) { if(event.message.getFormattedText().startsWith("§e ~")) { this.renderMsg(event.message.getFormattedText()); } System.out.println("DEBUG : " + message.getFormattedText()); } }
-
Désolé mais j’ai un peu de mal avec Forge car il faut pas modifier la Class. donc… c’est pas évident
Et ton code ne fonctionne pas dans la console sa m’affiche ça , et c’est toujours écris dans le chat :
[20:36:23] [Client thread/INFO]: [CHAT] ~ WarZone - Not the safest place to be DEBUG : §e ~ §r§4WarZone - Not the safest place to be§r