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.
Method of Searching for objects in the World
-
- English
- Textra
- Thread is marked as Resolved.
-
-
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).
JavaObject objects = getFieldOfType(chunkAddition.getClass(), "a", Class.forName("commons.util.c")).get(chunkAddition);o.worldPosition.x = (float) getMethodWithReturnType(rawObject.getClass(), "g", float.class).invoke(rawObject);o.worldPosition.y = (float) getMethodWithReturnType(rawObject.getClass(), "h", float.class).invoke(rawObject);As said, it's an ugly and extremely hacky solution, but it should do the trick 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:
To find all nearby objects, it certainly makes sense to check surrounding chunks as well
-
Wow, thanks for the response. This helps immensely.
Edit: Works a treat. Thanks again.
-
-
This looks great I'm glad to hear it worked!
Participate now!
Don’t have an account yet? Create a new account now and be part of our community!