Jump to content

[1.12.1] Making a mod that notify player when inventory is full


Gess1t

Recommended Posts

Right now, i'm stuck with an error :

 

[f]: The mod f appears to have an invalid event annotation EventHandler. This annotation can only apply to methods with recognized event arguments - it will not be called

 

here is the github for the mod https://github.com/Mrcubix/Full-Inventory-Checker

 

My Main class (Edited with @SubscribeEvent instead) :

 

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.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();
		}
	}
}

 

my invChK class

 

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;
				}
			}
		}
	}
}

 

The mod is supposed to output a message when the inventory is full (i'm also going to add a sound later on) but it doesn't, for some reasons, i suspect the error below to be the issue

The mod is supposed to be client side only.

I'm working with the recommended 1.12.1 version available in forge mod builder (14.22.1.2478)

i'm working in eclipse.

The game start, the mod is displayed in the list.

Edited by Gess1t
Link to comment
Share on other sites

Post your new code.

An event subscriber must take in exactly on parameter: the event.

 

Also does your invChk class compile?

Edited by DavidM

Some tips:

Spoiler

Modder Support:

Spoiler

1. Do not follow tutorials on YouTube, especially TechnoVision (previously called Loremaster) and HarryTalks, due to their promotion of bad practice and usage of outdated code.

2. Always post your code.

3. Never copy and paste code. You won't learn anything from doing that.

4. 

Quote

Programming via Eclipse's hotfixes will get you nowhere

5. Learn to use your IDE, especially the debugger.

6.

Quote

The "picture that's worth 1000 words" only works if there's an obvious problem or a freehand red circle around it.

Support & Bug Reports:

Spoiler

1. Read the EAQ before asking for help. Remember to provide the appropriate log(s).

2. Versions below 1.11 are no longer supported due to their age. Update to a modern version of Minecraft to receive support.

 

 

Link to comment
Share on other sites

Don’t create a new object each tick, as this is very expensive.

Create a function that takes in a player and checks the inventory of the given player instead.

 

I am pretty sure your invChk class is not valid Java though.

Edited by DavidM

Some tips:

Spoiler

Modder Support:

Spoiler

1. Do not follow tutorials on YouTube, especially TechnoVision (previously called Loremaster) and HarryTalks, due to their promotion of bad practice and usage of outdated code.

2. Always post your code.

3. Never copy and paste code. You won't learn anything from doing that.

4. 

Quote

Programming via Eclipse's hotfixes will get you nowhere

5. Learn to use your IDE, especially the debugger.

6.

Quote

The "picture that's worth 1000 words" only works if there's an obvious problem or a freehand red circle around it.

Support & Bug Reports:

Spoiler

1. Read the EAQ before asking for help. Remember to provide the appropriate log(s).

2. Versions below 1.11 are no longer supported due to their age. Update to a modern version of Minecraft to receive support.

 

 

Link to comment
Share on other sites

another forge forum advised me to either use TickEvent.PlayerTickEvent or TickEvent.ClientTickEvent, i used the second one cause i assumed i had less work to do with it cause i didn't had to check for a specific player inventory, which is complicated on a server, i assume, again.

 

well now i need to find how to make the first one work i guess?

Edited by Gess1t
Link to comment
Share on other sites

Umm not really.

I would say the second one is easier since you are making a client side mod that only checks the client player.

This should be irrelevant to your current problem though, as your method is not called from the event subscriber.

Do you know Java? If not, please learn Java before making a mod.

Edited by DavidM

Some tips:

Spoiler

Modder Support:

Spoiler

1. Do not follow tutorials on YouTube, especially TechnoVision (previously called Loremaster) and HarryTalks, due to their promotion of bad practice and usage of outdated code.

2. Always post your code.

3. Never copy and paste code. You won't learn anything from doing that.

4. 

Quote

Programming via Eclipse's hotfixes will get you nowhere

5. Learn to use your IDE, especially the debugger.

6.

Quote

The "picture that's worth 1000 words" only works if there's an obvious problem or a freehand red circle around it.

Support & Bug Reports:

Spoiler

1. Read the EAQ before asking for help. Remember to provide the appropriate log(s).

2. Versions below 1.11 are no longer supported due to their age. Update to a modern version of Minecraft to receive support.

 

 

Link to comment
Share on other sites

like i've said on another forum, there is java outside of forge, and java in forge, i know the basics i'm learning through other open-source mods now, right now, the difficulties are : why is the forge documentation so empty?

 

and yeah, i can pay 20% of my CPU just to run 4 lines of script every ticks that's why i'm doing it this way.

Edited by Gess1t
Link to comment
Share on other sites

6 minutes ago, Gess1t said:

there is java outside of forge, and java in forge

Not really. There is Java, and Forge (and Minecraft itself) is built upon it. Java is the same inside and outside Forge.

 

8 minutes ago, Gess1t said:

i can pay 20% of my CPU just to run 4 lines of script every ticks that's why i'm doing it this way.

You might have the processing power to do so, but it is very unnecessary. All you have to do is to mark that method static instead of initializing the object, which will accomplish the exact same thing, but both faster and less memory-consuming.

Besides, most users probably don't want their game to use processing power to run pointless operations.

 

Anyways, this is what you have to do:

  1. Create a event subscriber for ClientTickEvent (which you already have)
  2. Create a new function to iterate through the player's inventory (which you kind of have) or just do it inside the event subscriber
  3. Create a way of informing the user of the full inventory (which you already have)

Some tips:

Spoiler

Modder Support:

Spoiler

1. Do not follow tutorials on YouTube, especially TechnoVision (previously called Loremaster) and HarryTalks, due to their promotion of bad practice and usage of outdated code.

2. Always post your code.

3. Never copy and paste code. You won't learn anything from doing that.

4. 

Quote

Programming via Eclipse's hotfixes will get you nowhere

5. Learn to use your IDE, especially the debugger.

6.

Quote

The "picture that's worth 1000 words" only works if there's an obvious problem or a freehand red circle around it.

Support & Bug Reports:

Spoiler

1. Read the EAQ before asking for help. Remember to provide the appropriate log(s).

2. Versions below 1.11 are no longer supported due to their age. Update to a modern version of Minecraft to receive support.

 

 

Link to comment
Share on other sites

Forge is an API, and the documentation they give is far from complete.

 

i can afford to loose processing power just to make the mod work, then optimize it.

right now, i'm looking for a way to make the game actually send the message, cause it doesn't.

even with @SubscribeEvent

Link to comment
Share on other sites

This is because your invChk class has syntax errors. Looking at your code, I suppose you attempted to create a constructor and put the checking of the inventory in there; however, you created a method instead of a constructor, and you never called that method.

To learn about constructors in Java, read https://www.w3schools.com/java/java_constructors.asp.

 

Some tips:

Spoiler

Modder Support:

Spoiler

1. Do not follow tutorials on YouTube, especially TechnoVision (previously called Loremaster) and HarryTalks, due to their promotion of bad practice and usage of outdated code.

2. Always post your code.

3. Never copy and paste code. You won't learn anything from doing that.

4. 

Quote

Programming via Eclipse's hotfixes will get you nowhere

5. Learn to use your IDE, especially the debugger.

6.

Quote

The "picture that's worth 1000 words" only works if there's an obvious problem or a freehand red circle around it.

Support & Bug Reports:

Spoiler

1. Read the EAQ before asking for help. Remember to provide the appropriate log(s).

2. Versions below 1.11 are no longer supported due to their age. Update to a modern version of Minecraft to receive support.

 

 

Link to comment
Share on other sites

The annotation basically tells forge "ey there is an event here". Then forge checks which parameter that method has and sends the corresponding event. Just as a simple explanation. 

 

So you subscribe to the tick event and in there you create a new instance of invChk every single tick. And in there you dont even call the method. The annotation doesnt work in invChk since there are no method params. 

 

Solution: add the client tick event parameter to the method in invChk instead (remove the entire thing in your mod class) and then pass an instance of said class to

MinecraftForge.EVENT_BUS.register

Link to comment
Share on other sites

3 hours ago, CAS_ual_TY said:

The annotation basically tells forge "ey there is an event here". Then forge checks which parameter that method has and sends the corresponding event. Just as a simple explanation. 

 

So you subscribe to the tick event and in there you create a new instance of invChk every single tick. And in there you dont even call the method. The annotation doesnt work in invChk since there are no method params. 

 

Solution: add the client tick event parameter to the method in invChk instead (remove the entire thing in your mod class) and then pass an instance of said class to

MinecraftForge.EVENT_BUS.register

tried that, still not working, reverted the change

3 hours ago, DavidM said:

This is because your invChk class has syntax errors. Looking at your code, I suppose you attempted to create a constructor and put the checking of the inventory in there; however, you created a method instead of a constructor, and you never called that method.

To learn about constructors in Java, read https://www.w3schools.com/java/java_constructors.asp.

well it still doesn't work :

public Main() {
		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;
			}
		}
	}
	}
	@SubscribeEvent
	public static void chkInv(TickEvent.ClientTickEvent event) {
		if(Minecraft.getMinecraft().player != null) {
			Main main = new Main();
		}
	}

 

Edited by Gess1t
Link to comment
Share on other sites

learn java

checkFreeSlot : { // this is not valid java
		for(int i=0; i > 36; i++) { // this does not work
		if(Minecraft.getMinecraft().player.inventory.getStackInSlot(i).equals(ItemStack.EMPTY)) {
			Minecraft.getMinecraft().player.sendMessage(new TextComponentString("Your inventory is full"));
			break checkFreeSlot; // this is not valid java
			}
		}

you cant use equals on itemstack use itemstack.isEmpty

put the inv check code inside the event method

 

Edited by loordgek
Link to comment
Share on other sites

@loordgek that part isn't my code tbh look what i said above : "i know the basics i'm learning through other open-source mods now, right now, the difficulties are : why is the forge documentation so empty?"

if i knew how forge worked then i wouldn't have done a mistake, this isn't a problem about learning java, this is a problem about learning forge with a coffee spoon quantity of documentation.

 

one peeps on another forum pointed out that itemstack.isEmpty thing, maybe should have replaced what was wrong first before posting

Link to comment
Share on other sites

The main mistakes here are lack of java knowledge.

- In your topic you create a new instance of something, but nothing is being done with it, nothing gets called because of instantiation.

- In your last example your syntax does not work at all (as pointed out already).

 

These are things that have nothing to do with forge documentation.

Surely there are things you have to know beforehand, but these are all listed here (itemstacks are point 12):

 

 

Again, if you show me how you implemented my previous suggestion(s) I can help you with it.

Link to comment
Share on other sites

17 minutes ago, loordgek said:

show your (new) code then

??????? you posted it

 

you dont need documentation for what you are doing really

you know copy-paste right?

19 minutes ago, CAS_ual_TY said:

The main mistakes here are lack of java knowledge.

- In your topic you create a new instance of something, but nothing is being done with it, nothing gets called because of instantiation.

- In your last example your syntax does not work at all (as pointed out already).

 

These are things that have nothing to do with forge documentation.

Surely there are things you have to know beforehand, but these are all listed here (itemstacks are point 12):

 

 

Again, if you show me how you implemented my previous suggestion(s) I can help you with it.

"Itemstack cannot be resolved"

brain wash time! going to try to find where the problem is. 

Looking for the exact thread where i've found about the same code (cause i  edited it)

Edited by Gess1t
Link to comment
Share on other sites

22 minutes ago, loordgek said:

oops it is Itemstack

 

good job you copy pasted code that does not work

EDITED yeah i know, just probably gonna start fresh at this points. with no knowledge or proper documentation (totally not overkill and impossible tbh)

having code but not knowing how to apply it because an API lack of documentation or any kind of other tutorials that talk about it is like having the answer before knowing what the question is about

Edited by Gess1t
Link to comment
Share on other sites

Guest
This topic is now closed to further replies.

Announcements




×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.