Création d'un mod client-side qui notifie son utilisateur quand son inventaire est full
-
Main :
package Gess.mod; import Gess.mod.proxy.iProxy; import Gess.mod.scripts.invChk; import net.minecraft.client.Minecraft; import net.minecraftforge.fml.common.Mod; import net.minecraftforge.fml.common.Mod.EventHandler; import net.minecraftforge.fml.common.SidedProxy; import net.minecraftforge.fml.common.event.FMLInitializationEvent; import net.minecraftforge.fml.common.event.FMLPostInitializationEvent; import net.minecraftforge.fml.common.event.FMLPreInitializationEvent; import net.minecraftforge.fml.common.eventhandler.SubscribeEvent; import net.minecraftforge.fml.common.gameevent.TickEvent; import util.Reference; @Mod(modid=Reference.MODID, name=Reference.MODNAME, version=Reference.VERSION) public class Main { public static Main instance; public static final String CLIENT = "gess.mod.proxy.ClientProxy"; public static final String SERVER = "gess.mod.proxy.CommonProxy"; @SidedProxy(clientSide = Reference.CLIENT, serverSide = Reference.COMMON) public static iProxy proxy; @EventHandler public void preInit(FMLPreInitializationEvent event){} @EventHandler public void init(FMLInitializationEvent event){} @EventHandler public void postInit(FMLPostInitializationEvent event){} @SubscribeEvent public void chkInv(TickEvent.ClientTickEvent event) { if(Minecraft.getMinecraft().player != null) { invChk invchk = new invChk(); } } }invChk:
package Gess.mod.scripts; import net.minecraft.client.Minecraft; import net.minecraft.item.ItemStack; import net.minecraft.util.text.TextComponentString; import net.minecraftforge.fml.common.eventhandler.SubscribeEvent; public class invChk { @SubscribeEvent public void invChecker() { checkFreeSlot : { for(int i=0; i > 36; i++) { if(Minecraft.getMinecraft().player.inventory.getStackInSlot(i).equals(ItemStack.EMPTY)) { Minecraft.getMinecraft().player.sendMessage(new TextComponentString("Your inventory is full")); break checkFreeSlot; } } } } }les forums officiel de forge m’ont conseillé d’utiliser TickEvent.PlayerTickEvent à la place mais ducoup faut que je trouve de mon côté comment le faire marcher.
donc si je peux faire marcher le mod avec l’event actuel, ça m’éviterais de perdre trop de temps.EDIT: j’ai éssayé avec un constructor à la place d’une class séparé, toujours la même chose
-
Ton event ClientTickEvent n’est jamais appelé car il n’est pas enregistré.
https://www.minecraftforgefrance.fr/topic/3948/les-événements#enregistrer-la-classe-contenant-les-événements -
tout est dans la même class maintenant :
package Gess.mod; import Gess.mod.proxy.iProxy; import net.minecraft.client.Minecraft; import net.minecraft.util.text.TextComponentString; import net.minecraftforge.fml.common.Mod; import net.minecraftforge.fml.common.Mod.EventHandler; import net.minecraftforge.fml.common.SidedProxy; import net.minecraftforge.fml.common.event.FMLInitializationEvent; import net.minecraftforge.fml.common.event.FMLPostInitializationEvent; import net.minecraftforge.fml.common.event.FMLPreInitializationEvent; import net.minecraftforge.fml.common.eventhandler.SubscribeEvent; import net.minecraftforge.fml.common.gameevent.TickEvent; import util.Reference; @Mod(modid=Reference.MODID, name=Reference.MODNAME, version=Reference.VERSION) public class Main { public static Main instance; public static final String CLIENT = "gess.mod.proxy.ClientProxy"; public static final String SERVER = "gess.mod.proxy.CommonProxy"; @SidedProxy(clientSide = Reference.CLIENT, serverSide = Reference.COMMON) public static iProxy proxy; @EventHandler public void preInit(FMLPreInitializationEvent event){} @EventHandler public void init(FMLInitializationEvent event){} @EventHandler public void postInit(FMLPostInitializationEvent event){} public Main() { for(int i=0; i < 37; i++) { if(!Minecraft.getMinecraft().player.inventory.getStackInSlot(i).isEmpty()) { Minecraft.getMinecraft().player.sendMessage(new TextComponentString("Your inventory is full")); return; } } return; } @SubscribeEvent public static void chkInv(TickEvent.ClientTickEvent event) { if(Minecraft.getMinecraft().player != null) { Minecraft.getMinecraft().player.sendMessage(new TextComponentString("you're ingame")); Main main = new Main(); } } }j’éssaie de voir comment fix le problème de ticks maintenant
FIXED les messages s’envoient je dois juste mettre du delay entre les message pour éviter un spam intensif :
package Gess.mod; import Gess.mod.proxy.iProxy; import net.minecraft.client.Minecraft; import net.minecraft.util.text.TextComponentString; import net.minecraftforge.fml.common.Mod; import net.minecraftforge.fml.common.Mod.EventBusSubscriber; import net.minecraftforge.fml.common.Mod.EventHandler; import net.minecraftforge.fml.common.SidedProxy; import net.minecraftforge.fml.common.event.FMLInitializationEvent; import net.minecraftforge.fml.common.event.FMLPostInitializationEvent; import net.minecraftforge.fml.common.event.FMLPreInitializationEvent; import net.minecraftforge.fml.common.eventhandler.SubscribeEvent; import net.minecraftforge.fml.common.gameevent.TickEvent; import net.minecraftforge.fml.relauncher.Side; import net.minecraftforge.fml.relauncher.SideOnly; import util.Reference; @EventBusSubscriber @Mod(modid=Reference.MODID, name=Reference.MODNAME, version=Reference.VERSION) public class Main { public static Main instance; public static final String CLIENT = "gess.mod.proxy.ClientProxy"; public static final String SERVER = "gess.mod.proxy.CommonProxy"; @SidedProxy(clientSide = Reference.CLIENT, serverSide = Reference.COMMON) public static iProxy proxy; @EventHandler public void preInit(FMLPreInitializationEvent event){} @EventHandler public void init(FMLInitializationEvent event){} @EventHandler public void postInit(FMLPostInitializationEvent event){} public Main() { } @SubscribeEvent public static void chkInv(TickEvent.ClientTickEvent event) throws InterruptedException { if(Minecraft.getMinecraft().player != null) { int s = 0; for(int i=0; i < 37; i++) { if(!Minecraft.getMinecraft().player.inventory.getStackInSlot(i).isEmpty()) { s++; if(s > 35) { Minecraft.getMinecraft().player.sendMessage(new TextComponentString("Your inventory is full")); return; } } } return; } } }le Seul truc que j’ai réussis à faire ces trois dernière heures c’est réussir à casser mon code, heuresement que j’ai la backup sur github^^ j’ai éssayer d’ajouter du délai entre chaque message et à chaque fois, plus aucun message apparaît.
j’ai notamment éssayé de call la méthod pour mon TickHandler(TickEvent.ClientTickEvent event) mais dès que j’éssaie de call la méthode via une instance, plus rien.
-
J’AI RIEN DIT! on peut utiliser getTotalWorldTime dans des method static apparement, ducoup ça marche! ya 10 secondes de délai entre chaques message, mais ducoup, quand la method est call avec full inv, il y a 2 messages qui apparaissent, je me demandais pourquoi.
package Gess.mod; import Gess.mod.proxy.iProxy; import net.minecraft.client.Minecraft; import net.minecraft.util.text.TextComponentString; import net.minecraftforge.fml.common.Mod; import net.minecraftforge.fml.common.Mod.EventBusSubscriber; import net.minecraftforge.fml.common.Mod.EventHandler; import net.minecraftforge.fml.common.SidedProxy; import net.minecraftforge.fml.common.event.FMLInitializationEvent; import net.minecraftforge.fml.common.event.FMLPostInitializationEvent; import net.minecraftforge.fml.common.event.FMLPreInitializationEvent; import net.minecraftforge.fml.common.eventhandler.SubscribeEvent; import net.minecraftforge.fml.common.gameevent.TickEvent; import net.minecraftforge.fml.relauncher.Side; import net.minecraftforge.fml.relauncher.SideOnly; import util.Reference; @EventBusSubscriber @Mod(modid=Reference.MODID, name=Reference.MODNAME, version=Reference.VERSION) public class Main { public static Main instance; public static final String CLIENT = "gess.mod.proxy.ClientProxy"; public static final String SERVER = "gess.mod.proxy.CommonProxy"; @SidedProxy(clientSide = Reference.CLIENT, serverSide = Reference.COMMON) public static iProxy proxy; @EventHandler public void preInit(FMLPreInitializationEvent event){} @EventHandler public void init(FMLInitializationEvent event){} @EventHandler public void postInit(FMLPostInitializationEvent event){} public Main() { } @SubscribeEvent public static void chkInv(TickEvent.ClientTickEvent event) throws InterruptedException { if(Minecraft.getMinecraft().player != null) { int s = 0; for(int i=0; i < 37; i++) { if(!Minecraft.getMinecraft().player.inventory.getStackInSlot(i).isEmpty()) { s++; if(s > 35) { if(Minecraft.getMinecraft().world.getTotalWorldTime() % 300 == 3L ) { Minecraft.getMinecraft().player.sendMessage(new TextComponentString("Your inventory is full")); } } } } return; } } //@SideOnly(Side.CLIENT) //public void TickhHandler(TickEvent.ClientTickEvent event) throws InterruptedException { //wait(200); } //}j’ai update mon repo avec le code et une liste de choses que je doit encore faire avant de build et potentiellement release le mod
-
car tu teste en solo et que en solo tu es à la fois client et serveur
-
@isador34 nope, c’est aussi le cas sur server et puis le solo utilise aussi server.jar pour fonctionner
-
TickEvent.ClientTickEventn’est qu’appelé sur le client donc il n’y a pas de problème de double appel client + serveur.Le problème vient du fait que l’event est appelé deux fois par phase, au début du tick et à la fin.
Ajouter unif (event.phase == TickEvent.Phase.END)devrait régler le problème. -
@robin4002 Effectivement, j’avais oublié cette notion de phase
-
@robin4002 a dit dans Création d'un mod client-side qui notifie son utilisateur quand son inventaire est full :
if (event.phase == TickEvent.Phase.END)
C’est fais, àa marche, mais ducoup je me demandais aussi pourquoi le jeu met autant de temps avant de redémarrer un autre check même si je l’ai pas déclanché une nouvelle fois depuis les 300 ticks expiré, c’est sensé être un clienttickEvent nan?
ce serais pas un problème d’ordes par hasard? si oui je crois savoir qu’il faut que je run mon délai pour attendre après avoir envoyé le message je supose?Ma class actuellement
@SubscribeEvent public static void chkInv(TickEvent.ClientTickEvent event) throws InterruptedException { if(Minecraft.getMinecraft().player != null) { int s = 0; for(int i=0; i < 37; i++) { if(!Minecraft.getMinecraft().player.inventory.getStackInSlot(i).isEmpty() && Minecraft.getMinecraft() .player.inventory.getStackInSlot(i).getCount() == Minecraft.getMinecraft().player.inventory.getStackInSlot(i).getMaxStackSize()) { s++; if(s > 35) { if(Minecraft.getMinecraft().world.getTotalWorldTime() % 240 == 3L && event.phase == TickEvent.Phase.END) { Minecraft.getMinecraft().player.sendMessage(new TextComponentString("Your inventory is full")); return; } } } } return; -
L’event est toujours appelé à chaque tick. 300 tick c’est 12 secondes, tu es sûr que le message ne s’affiche pas toutes les 12 secondes ? (au passage tes
returnne servent absolument à rien). -
@robin4002
je l’ai modifié, le message s’envois toutes les 12 secondes quand j’ai un inventaire plein, mais il faut attendre 12s quand je vais ingame pour que le premiers message apparaît, pareil quand je recupère un item au sol pour remplir l’inventaire, 12s après avoir recup l’item, le message est envoyé -
Vu comment ton code est fait c’est normal.
Il faudrait utiliser un boolean et mettre ceci sur false dès que le message est affiché et si elle est sur false lancer le compteur puis remettre sur true à la fin du compteur.
-
ya une fonction pour detecter un message client side?
si c’est pas le cas au pire j’ai juste à utiliser une variable right? -
Pourquoi tu veux une fonction pour ça ?
Tu as justement besoin d’une variable en effet. -
@robin4002 a dit dans Création d'un mod client-side qui notifie son utilisateur quand son inventaire est full :
Vu comment ton code est fait c’est normal.
Il faudrait utiliser un boolean et mettre ceci sur false dès que le message est affiché et si elle est sur false lancer le compteur puis remettre sur true à la fin du compteur.
donc je créer une bolean timer = true; au début de ma method chkInv, après mon message je met timer = !timer;:
mais je suis sensé lancer le check comment si je ne peux pas lancer le compteur en dehors de la boucle if?
je suis sensé run if(Minecraft.getMinecraft().world.getTotalWorldTime() % 240 == 3L ) comme première commande? comme genreEDIT: le message n’est plus déclenché donc j’ai ya un truc qui va pas, j’me relis 2s pour voir.
RE: je vois pas ce qui empêche le message de s’envoyer.
—> bon bas ma method n’a plus l’air de fonctionner dutout, pas d’errors, impossible de savoir d’où ça vient. même avec un manual undoEDIT2: les backups, c’est la vie. ducoup ma class de backup marche, juste perdu la boolean.
quand j’ai supprimé la bolean et que j’ai remis la fonction worldgetTotalWorldTime() manuellement à sa place, le problème étais toujours là, donc aucune idée de pourquoi j’ai eu le problème d’avant.@SubscribeEvent public static void chkInv(TickEvent.ClientTickEvent event) throws InterruptedException { boolean timer = false; if(Minecraft.getMinecraft().player != null ) { if(Minecraft.getMinecraft().world.getTotalWorldTime() % 200 == 3L && timer = false) int s = 0; for(int i=0; i < 37; i++) { if(!Minecraft.getMinecraft().player.inventory.getStackInSlot(i).isEmpty() && Minecraft.getMinecraft() .player.inventory.getStackInSlot(i).getCount() == Minecraft.getMinecraft().player.inventory.getStackInSlot(i).getMaxStackSize()) { s++; if(s > 35) { if(event.phase == TickEvent.Phase.END && timer == false) { Minecraft.getMinecraft().player.sendMessage(new TextComponentString("Your inventory is full")); timer = !timer; return; } } return; } } } { return; } } -
j’étais en train de créer mon fichier de config puis j’ai tenté de démarrer le jeu mais ça a crash et la ligne “if(cfg.haschanged()) {” en serait la cause
mon fichier configpublic static void readConfig() { Configuration cfg = CommonProxy.config; try { cfg.load(); initGeneralConfig(cfg); } finally { if (cfg.hasChanged()) { cfg.save(); } } }le crash report :
net.minecraftforge.fml.common.LoaderExceptionModCrash: Caught exception from Full Inventory Checker (fullinvchecker) Caused by: java.lang.NullPointerException at Gess.mod.config.Config.readConfig(Config.java:21) at Gess.mod.Main.preInit(Main.java:41) 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.minecraftforge.fml.common.FMLModContainer.handleModStateEvent(FMLModContainer.java:611) 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 com.google.common.eventbus.Subscriber.invokeSubscriberMethod(Subscriber.java:91) at com.google.common.eventbus.Subscriber$SynchronizedSubscriber.invokeSubscriberMethod(Subscriber.java:150) at com.google.common.eventbus.Subscriber$1.run(Subscriber.java:76) at com.google.common.util.concurrent.MoreExecutors$DirectExecutor.execute(MoreExecutors.java:399) at com.google.common.eventbus.Subscriber.dispatchEvent(Subscriber.java:71) at com.google.common.eventbus.Dispatcher$PerThreadQueuedDispatcher.dispatch(Dispatcher.java:116) at com.google.common.eventbus.EventBus.post(EventBus.java:217) at net.minecraftforge.fml.common.LoadController.sendEventToModContainer(LoadController.java:253) at net.minecraftforge.fml.common.LoadController.propogateStateMessage(LoadController.java:231) 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 com.google.common.eventbus.Subscriber.invokeSubscriberMethod(Subscriber.java:91) at com.google.common.eventbus.Subscriber$SynchronizedSubscriber.invokeSubscriberMethod(Subscriber.java:150) at com.google.common.eventbus.Subscriber$1.run(Subscriber.java:76) at com.google.common.util.concurrent.MoreExecutors$DirectExecutor.execute(MoreExecutors.java:399) at com.google.common.eventbus.Subscriber.dispatchEvent(Subscriber.java:71) at com.google.common.eventbus.Dispatcher$PerThreadQueuedDispatcher.dispatch(Dispatcher.java:116) at com.google.common.eventbus.EventBus.post(EventBus.java:217) at net.minecraftforge.fml.common.LoadController.distributeStateMessage(LoadController.java:148) at net.minecraftforge.fml.common.Loader.preinitializeMods(Loader.java:604) at net.minecraftforge.fml.client.FMLClientHandler.beginMinecraftLoading(FMLClientHandler.java:266) at net.minecraft.client.Minecraft.init(Minecraft.java:508) at net.minecraft.client.Minecraft.run(Minecraft.java:416) at net.minecraft.client.main.Main.main(Main.java:118) 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:135) at net.minecraft.launchwrapper.Launch.main(Launch.java:28) 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.minecraftforge.gradle.GradleStartCommon.launch(GradleStartCommon.java:97) at GradleStart.main(GradleStart.java:25)si jamais tu veux voir ma method preInit :
line 41 = Config.readConfig();@EventHandler public void preInit(FMLPreInitializationEvent event){ File directory = event.getModConfigurationDirectory(); config = new Configuration(new File(directory.getPath(), "FullInventoryChecker.cfg")); Config.readConfig(); }j’ai aussi éssayer de faire jouer un son du côté client avec Minecraft.getMinecraft().getSoundHandler().playSound(); mais ya une erreur dans mon code, quelque soit le code, j’ai éssayé de voir dans des exemples ou d’autres mods open source, mais ya rien qui marche
pour finir, je me demandais si dans
if(!Minecraft.getMinecraft().player.inventory.getStackInSlot(i).isEmpty() && Minecraft.getMinecraft() .player.inventory.getStackInSlot(i).getCount() == Minecraft.getMinecraft().player.inventory.getStackInSlot(i).getMaxStackSize())je pouvais ajouter l’ID d’un item comme exception car sur certain serveurs comme hypixel skyblock ou wynncraft, ya une limite custom de stacks par items .
je vais passer par un else et ajouter les ID manuellement, et si j’arrive à faire fonctionner ce fichier de config, alors j’ajouterais un filtre d’exception. je n’arrive pas à trouver comment comparer le stack trouvé avec getStackInSlot avec un item genre mineccraft:head (je supose je dois utiliser getItem() mais ya aucun moyen de savoir comment elle marche)
-
vu que j’avais plus de réponses sur ce forum, j’ai utilisé le forum officiel, puis j’ai fais une pause, car perdre son temps pour rien et pas spécialement mon truc, j’éssaye de faire marcher “e.entity” pour trouver un moyen de faire fonctionner le mod sur un serveur, le problème étant que “e.” ne marche pas, donc je me demandais à quoi ça correspondait.

je viens de faire un texte et genre event.getEntityPlayer() et tous les autres arguments pour minecraft ne fonctionnent plus et remplacé par ça :

event.[autre argument en rapport avec mc] ne marche plus sur tous mes projets.
-
C’est quoi le contexte complet dans lequel se trouve ta variable event ?
-
dans une methode test avec comme genre
@SubscribeEvent public void test(TickEvent.ClientTickEvent event) { if(event. }et ya rien qui s’affiche alors que c’était le cas avant
-
TickEvent.ClientTickEvent est bien reconnu ?