0

I've been attempting to develop a Minecraft mod, but have hit a road block in which I have had no luck in surpassing. In my attempts to find a fix for this issue, I have sifted through multiple other questions and websites, but to no avail.

The closest I have gotten would be where I created the variable outside of the method and then set the value inside of the method. But, the issue with this is that it would always return with null.

Also, if it was not clear, I am talking about the "taskEntriesMethod" variable.

Here's my code:

 public void getExecutingTaskEntries(Profiler profiler)
    {
        try
        {
            Class<?> AITasks = Class.forName("net.minecraft.entity.ai.EntityAITasks");

            Field taskEntries = AITasks.getDeclaredField("executingTaskEntries");
                  taskEntries.setAccessible(true);

            Object AITasksObj = taskEntries.get(new EntityAITasks(profiler));

            Set<EntityAITasks.EntityAITaskEntry> taskEntriesMethod = (Set<EntityAITaskEntry>) AITasksObj;



        }
        catch (ClassNotFoundException | NoSuchFieldException | IllegalAccessException exception) { exception.printStackTrace(); }

    }


    private final Set<EntityAITasks.EntityAITaskEntry> executingTaskEntries = taskEntriesMethod; //  <-- (this is what errors out)
Strobilus
  • 9
  • 2
  • 1
    you wouldn't, since it is a local variable, and thus doesn't exist outside of that method. you can declare it a field, instead of a local variable – Stultuske Jun 04 '20 at 07:34
  • Why are you reflecting an object you just made yourself? You'd want to be retrieving that from an actual instance on the server – Rogue Jun 04 '20 at 16:04

2 Answers2

0

Do you need executingTaskEntries to be final?

You could simply:

public void initExecutingTaskEntries(Profiler profiler) // changed name here, get suggested its going to return something
    {
        try
        {
            Class<?> AITasks = Class.forName("net.minecraft.entity.ai.EntityAITasks");

        Field taskEntries = AITasks.getDeclaredField("executingTaskEntries");
              taskEntries.setAccessible(true);

        Object AITasksObj = taskEntries.get(new EntityAITasks(profiler));

        executingTaskEntries = (Set<EntityAITaskEntry>) AITasksObj;



    }
    catch (ClassNotFoundException | NoSuchFieldException | IllegalAccessException exception) { exception.printStackTrace(); }

}


private Set<EntityAITasks.EntityAITaskEntry> executingTaskEntries;

if you need executingTaskEntries to be final then I would initialize it as empty Set and then just add entires to it within your method.

potatojesz
  • 81
  • 6
0

You've created a get method but have declared it as a void. You probably want to change your code to something like this:

public Set<EntityAITasks.EntityAITaskEntry> getExecutingTaskEntries(Profiler profiler) {
    //your code
    return (Set<EntityAITaskEntry>) AITasksObj;
}

And then use it like so

Set<EntityAITasks.EntityAITaskEntry> executingTaskEntries = getExecutingTaskEntries(profiler)
Lucan
  • 2,907
  • 2
  • 16
  • 30