Method of Searching for objects in the World

A new update is now available, introducing seasons and more!
Latest hotfix: 0.8.0.1 (2024-12-20)
  • Hi All. I'm wondering if there is a way to search for objects that are currently existing in the world; e.g. torches, lights, and the like? I'm basically trying to create some rules for the spawning of mobs on the surface and I want to check a position vector for nearby lights to determine if my mob can spawn there. A pseudo light level system if you will. I've looked through the API Javadocs but can't find any obvious method of doing this. I was hoping someone here might know of a way I could achieve this.


  • Basically it is possible, but a bit cumbersome because there is no direct method to retrieve objects :/ But you could use the Internals class to get access to the internal chunk data, then access the internal object data via reflection - the only issue with this approach is that the game is obfuscated, so this becomes a rather hacky solution (all methods and fields are named a, b, c etc)... there is also a chance this breaks with the next update (although we have no ETA for the next Java update)...


    This code wouldn't work in the new version anymore of course, but it's our intention to add methods to the new API which enables you to get objects from a chunk easily ^^


    One issue of the the obfuscation is that it results in one class having multiple variables or methods with the same name - this is illegal in Java, but legal in byte code. Unfortuantely this makes reflection more complicated - I've added two helper methods to get the correct fields and methods.


    The Java version stores the objects in a so called "ChunkAddition" - these special chunks only have a X and Z coordinate, and they contain all objects irrespective of their Y (vertical) position (so when calling this method, you may also get objects from underground dungeons, for example).


    Here is a basic example where I also created a new "ObjectElement" class to hold the individual object data we get via reflection (definition and world position for now).



    As said, it's an ugly and extremely hacky solution, but it should do the trick :saint: There is also room for improvements: You could cache the Method and Field objects so the game doesn't have to search them for every object.


    Anyway, if you now want to get all objects from the chunk where the player is, you could get the player chunk position and then call our new method:


    Java
    Vector3i chunkPos = player.getChunkPosition();
    ArrayList<ObjectElement> objects = getAllObjects(chunkPos.x, chunkPos.z);
    if(objects != null){
    for(ObjectElement o : objects){
    System.out.println("Object " + o.definition.getName() + " @ " + o.worldPosition);
    }
    }


    To find all nearby objects, it certainly makes sense to check surrounding chunks as well ;)

  • So, this is me, using your code to create a distance based light level system in order to spawn mobs on the surface at night.


    It's working nicely. :D


    Calm before the storm.



    Nice and safe on the porch.



    A bit less safe outside in the dwindling light.



    Outside in the dark being chased by the undead.

Participate now!

Don’t have an account yet? Create a new account now and be part of our community!