Jump to content
Search In
  • More options...
Find results that contain...
Find results in...

[1.16.5] How to get all mod classes?


Recommended Posts

As the title says i need all classes of my mod,
because i want to output them firstly on the console (for debugging),
and secondly for a custom registration of recipes.

I looked at the code from forge for the AutomaticEventSubscriber registration and understand it only partially.
I also have this code that gives me a ModFileScanData:

ModList.get().getModFileById(Nero.MOD_ID).getFile().getScanResult()

ModFileScanData has the getClasses method, which unfortunately gives me an unusable class,
because I don't get any classes or anything back from a ClassData

is there a way to get all classes of my mod?

Link to post
Share on other sites
1 hour ago, diesieben07 said:

This is a terrible idea. What do recipes have to do with getting all classes?

I have a custom recipe system, I use annotations to register the recipe methods faster and easier,
which currently only works in classes that extend my recipe helper.
I would like to extend this to all classes that have an annotation similar to the event registration of forge.


I also need the classes for a config system in which I add an annotation to a fields and these are automatically added to my config,
the implementaion is not perfect/finished but here is a small example:
 

	@ConfigValue(valueName = "example", comment = "This is an example")
	public static int example;

 

Link to post
Share on other sites
31 minutes ago, Luis_ST said:

I have a custom recipe system, I use annotations to register the recipe methods faster and easier,
which currently only works in classes that extend my recipe helper.
I would like to extend this to all classes that have an annotation similar to the event registration of forge.

You should use JSON files and data packs, similar to the normal Minecraft recipe system.

 

32 minutes ago, Luis_ST said:

I also need the classes for a config system in which I add an annotation to a fields and these are automatically added to my config,
the implementaion is not perfect/finished but here is a small example:

This is a valid use case, but it does not require you to "list all classes of the mod".
Use ModFileScanData#getAnnotations to find things annotated with your annotation, then process them however you like.

Link to post
Share on other sites
16 hours ago, diesieben07 said:

This is a valid use case, but it does not require you to "list all classes of the mod".
Use ModFileScanData#getAnnotations to find things annotated with your annotation, then process them however you like.

with which method do I get the object which has the annotation

Link to post
Share on other sites

AnnotationData#getAnnotationType: The class of the annotation.

AnnotationData#getTargetType: The type of element that was annotated (e.g. field, method, class)

AnnotationData#getClassType: The class of the element that was annotated (for fields and methods this is the containing class)

AnnotationData#getMemberName: The name of the element that was annotated (for fields and methods this is the name, for classes it is the class name).

Link to post
Share on other sites
7 minutes ago, diesieben07 said:

AnnotationData#getAnnotationType: The class of the annotation.

AnnotationData#getTargetType: The type of element that was annotated (e.g. field, method, class)

AnnotationData#getClassType: The class of the element that was annotated (for fields and methods this is the containing class)

AnnotationData#getMemberName: The name of the element that was annotated (for fields and methods this is the name, for classes it is the class name).

If I look at the AnnotationData and the methods you have listed, I cannot find a usable method
since I can't do anything with the target/class type, the name of the target doesn't help with fields and methods either.

what does the map contain, the AnnotationData#getAnnotationData returns

Link to post
Share on other sites
23 minutes ago, Luis_ST said:

If I look at the AnnotationData and the methods you have listed, I cannot find a usable method
since I can't do anything with the target/class type, the name of the target doesn't help with fields and methods either.

Why not? What is your issue?

24 minutes ago, Luis_ST said:

what does the map contain, the AnnotationData#getAnnotationData returns

It returns the data that was given to the annotation, i.e. its parameters. The keys are the names, the values are the respective annotation values represented as follows:

  • Any primitive boxed value (Integer, Double, etc.)
  • Any primitive array (byte[], int[], etc.)
  • String
  • org.objectweb.asm.Type for Class arguments
  • An instance of ModAnnotation.EnumHolder for enum arguments
  • An instance of ModAnnotation for nested annotation arguments
  • A List of the above for arrays
Link to post
Share on other sites
On 6/7/2021 at 9:20 PM, diesieben07 said:

This is a valid use case, but it does not require you to "list all classes of the mod".
Use ModFileScanData#getAnnotations to find things annotated with your annotation, then process them however you like.

I have now created a custom config system which also works,
but I get an error that is actually not possible:
log part:

[11:43:37] [modloading-worker-1/WARN] [ne.lu.ne.Nero/]: Can't define Config Value coalXpMin
java.lang.IllegalArgumentException: Can not set net.minecraftforge.common.ForgeConfigSpec$ConfigValue field net.luis.nero.api.block.ModOreBlock.coalXpMin to net.minecraftforge.common.ForgeConfigSpec$ConfigValue

 

complete log:
log.log

this is the config class
this is the config value class

 

Link to post
Share on other sites
Posted (edited)
2 hours ago, diesieben07 said:

You have to use the correct ClassLoader when loading the classes, look at what AutomaticEventSubscriber does.

okay AutomaticEventSubscriber uses the ClassLoader of the mod main class,
but I didn't use a ClassLoader in my code, so where do I have to set/use the ClassLoader?

Edited by Luis_ST
Link to post
Share on other sites
44 minutes ago, diesieben07 said:

You have to do it the same way AutomaticEventSubscriber does.

huh? AutomaticEventSubscriber uses the ClassLoader to register the event to the EventBus,
but I don't have to register anything

Link to post
Share on other sites
1 hour ago, diesieben07 said:

No, it uses the class loader when looking up the class by name. You also look up the class by name, but you do not specify the class loader.

okay makes sense, but can i get the ClassLoader of my main mod class?
can i do something like that:
 

Nero.class.getClassLoader()

 

Link to post
Share on other sites
1 hour ago, diesieben07 said:

I can't reproduce this, the game starts up fine when I cloned your repo.

yes the game starts, the config is also created,
but the try-catch block still catches an exception for each ConfigValue,

I also get a NullPointerException when I use the values that I set via reflection in the code (xp drop of my custom ores)

Link to post
Share on other sites
2 hours ago, diesieben07 said:

The first parameter to Field#set is the instance holding the field. You're giving it the ConfigValue every time, but ConfigValue is not the class holding the field.

so i need the class of the ConfigValue there?
and ConfigValue.class doesn't work

Link to post
Share on other sites
1 minute ago, Luis_ST said:

so i need the class of the ConfigValue there?
and ConfigValue.class doesn't work

No. Here configField is a field in configClass. So if you call field.set you need to pass in an instance of configClass (so that the field knows on which instance to set the field...) or null for static fields. Read the documentation for Field#set if you do not understand this.

Link to post
Share on other sites
1 hour ago, diesieben07 said:

No. Here configField is a field in configClass. So if you call field.set you need to pass in an instance of configClass (so that the field knows on which instance to set the field...) or null for static fields. Read the documentation for Field#set if you do not understand this.

I understood how the method works my problem is that I don't have an instance of ConfigValue,
I can't create a temporary new one and if I need the instance from the class in which the ConfigValue is located that I want to add to the config,
the automatic addition is "for the bucket".

the other possibility would be to set the fields with the ConfigValues to static, but i dont want to do that.

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



  • Recently Browsing

    No registered users viewing this page.

  • Posts

    • Hello There! The finale is FINALLY here of this really wholesome video series! In this one we switch some things up and we surprise my one friend with a MINECRAFT ACCOUNT! Then we get back to the usual stuff where we surprise the ones who have Java Minecraft with their statues and everything! Sadly this little series was cut short because of the server shutting down but it was fun nonetheless! If you are looking for a wholesome/good vibe video series this is the one for you! I hope you all enjoy this video and if you did don't forget to like and sub for more!  
    • My forge 1.12.2 shows up in launcher and the tap shows up when I launches at the bottom of my screen. The game itself doesn't pop up on my screen. When launching forge again, it will just say that a game is already there. Can someone please help me solve this problem? I have already tried searching for multiple instances of minecraft using task manager, finding nothing. 
    • After looking through a ton of functions, I found the issue was that my entity was extending MonsterEntity, which extends MobEntity, which creates a GroundPathNavigator, which has a rules for canUpdatePath that include "this.mob.isOnGround()". My entity is a flying entity. Whoops. Solution was to simply override the createNavigation function, where I simply "return new FlyingPathNavigator(this, world);". Everything works fine now.
    • do you already have a custom recipe implemented? if not, you'll a class that'l hold the recipe and check for matches, the recipe inventory and the recipe serializer   then it's up to you to write the logic in your container. you can use of the world's recipe manager to see if a recipe inventory matches any recipe
    • I'm trying to write an AI Goal for an entity to move towards its target on each tick (with quite a few restrictions on when, which is why I need a new goal). It seemed to be working when I also added a WaterAvoidingRandomWalkingGoal, but I don't want the entity to move around randomly anymore, so I removed that. My code overrides the canUse function to determine whether or not it should start moving towards the target, and that ends with creating a path to the target, which always returns NULL, so nothing happens. Here's the relevant functions: public FloatingEyesGoal(EntityFloatingEyes e, double speed) { this.entity = e; this.random = e.getRandom(); this.speed = speed; this.setFlags(EnumSet.of(Flag.LOOK, Flag.MOVE, Flag.TARGET)); this.entity.getNavigation().setCanFloat(true); } @Override public boolean canUse() { LivingEntity target = this.entity.getTarget(); if (target == null || !target.isAlive() || !isFacingTargetBack()) return false; setTargetPos(target); this.path = this.entity.getNavigation().createPath(target, 0); // Logging this.path == null prints "true", and this.entity.getNavigation() == null prints "false" return this.path != null || isInRange(target); } @Override public boolean canContinueToUse() { LivingEntity target = this.entity.getTarget(); if (target == null || !target.isAlive() || !isFacingTargetBack()) return false; return !this.entity.getNavigation().isDone(); } @Override public void start() { this.entity.getNavigation().moveTo(this.path, this.speed); this.entity.setAggressive(true); this.ticksToMove = 0; } @Override public void stop() { LivingEntity target = this.entity.getTarget(); if (!EntityPredicates.NO_CREATIVE_OR_SPECTATOR.test(target)) { this.entity.setTarget(null); } this.entity.setAggressive(false); this.entity.getNavigation().stop(); this.targetPosX = 0.0D; this.targetPosY = 0.0D; this.targetPosZ = 0.0D; } @Override public void tick() { // Look at target LivingEntity target = this.entity.getTarget(); this.entity.getLookControl().setLookAt(target, 32.0F, 32.0F); setTargetPos(target); final double distance = this.entity.distanceToSqr(this.targetPosX, this.targetPosY, this.targetPosZ); this.ticksToMove--; LilSpooks.LOGGER.info(this.ticksToMove); if (this.ticksToMove <= 0 && this.entity.getSensing().canSee(target) && target.distanceToSqr(this.targetPosX, this.targetPosY, this.targetPosZ) >= 1.0D) { this.ticksToMove = 5 + this.random.nextInt(8); // Add extra time if eyes can't move to hopefully wait enough to try again (1 second). this.path = this.entity.getNavigation().createPath(new BlockPos(this.targetPosX, this.targetPosY, this.targetPosZ), 0); // Logging this.path == null returns "true" here as well if (!this.entity.getNavigation().moveTo(this.path, this.speed)) { this.ticksToMove += 20; } } this.attackTick = Math.max(this.attackTick - 1, 0); this.attemptAttack(target, distance); } protected void setTargetPos(LivingEntity target) { this.targetPosX = target.getX(); this.targetPosY = target.getBoundingBox().minY; this.targetPosZ = target.getZ(); } I have tried multiple ways of invoking createPath, I tried setting the moveControl of the entity with the goal to a new FlyingMovementController, and I've looked at how the createPath function works (though I can't figure out how to do a live debugging session, so I couldn't set a breakpoint at that code which would tell me exactly what causes it to be null). I tried my best to work through this for a few hours but I can't seem to figure it out. Any help is appreciated.
  • Topics

  • Who's Online (See full list)

×
×
  • Create New...

Important Information

By using this site, you agree to our Privacy Policy.