Jump to content

[SOLVED] [1.12] How to get a List of TileEntities


MrDj200

Recommended Posts

Hey there!

I'm new to modding and would like to get a List of all registered TileEntities.

I tried:

Minecraft.getMinecraft().world.loadedTileEntityList;

 But it only gives the TE's already in the World and I want a list with All registered TE's. 

Is that possible?

If yes, how?

 

I'd love to see an example :)

 

Greetings

-Dj

Edited by MrDj200
Link to comment
Share on other sites

TileEntity has a REGISTRY field that is like all other registries. You will need to use reflection to access it, however.

  • Like 1

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Link to comment
Share on other sites

Thanks for the reply!

I tried using that. And it works in my IDE, however not on "real" servers. I think it has something to do with Obfuscation(?).

Here is the Code I used:

try
        {
            Field field = TileEntity.class.getDeclaredField("REGISTRY");
            field.setAccessible(true);
            RegistryNamespaced<ResourceLocation, Class<? extends TileEntity>> reg = (RegistryNamespaced) field.get(null);
            for(ResourceLocation location : reg.getKeys())
            {
                Class<? extends TileEntity> entity = reg.getObject(location);
                LogHelper.info("Registered TE: " + entity.getName());
            }
        }
        catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException ex)
        {
            LogHelper.error("An unexpected error occured ", ex);
        }

Is there a way to use it on "real" servers/clients?

 

-Dj

Link to comment
Share on other sites

Spoiler

java.lang.NoSuchFieldException: REGISTRY
	at java.lang.Class.getDeclaredField(Class.java:2070) ~[?:1.8.0_144]
	at me.dj.spatialserver.SpatialServer.init(SpatialServer.java:41) [SpatialServer.class:?]
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_144]
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:1.8.0_144]
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_144]
	at java.lang.reflect.Method.invoke(Method.java:498) ~[?:1.8.0_144]
	at net.minecraftforge.fml.common.FMLModContainer.handleModStateEvent(FMLModContainer.java:614) [FMLModContainer.class:?]
	at sun.reflect.GeneratedMethodAccessor10.invoke(Unknown Source) ~[?:?]
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_144]
	at java.lang.reflect.Method.invoke(Method.java:498) ~[?:1.8.0_144]
	at com.google.common.eventbus.Subscriber.invokeSubscriberMethod(Subscriber.java:91) [minecraft_server.1.12.jar:?]
	at com.google.common.eventbus.Subscriber$SynchronizedSubscriber.invokeSubscriberMethod(Subscriber.java:150) [minecraft_server.1.12.jar:?]
	at com.google.common.eventbus.Subscriber$1.run(Subscriber.java:76) [minecraft_server.1.12.jar:?]
	at com.google.common.util.concurrent.MoreExecutors$DirectExecutor.execute(MoreExecutors.java:399) [minecraft_server.1.12.jar:?]
	at com.google.common.eventbus.Subscriber.dispatchEvent(Subscriber.java:71) [minecraft_server.1.12.jar:?]
	at com.google.common.eventbus.Dispatcher$PerThreadQueuedDispatcher.dispatch(Dispatcher.java:116) [minecraft_server.1.12.jar:?]
	at com.google.common.eventbus.EventBus.post(EventBus.java:217) [minecraft_server.1.12.jar:?]
	at net.minecraftforge.fml.common.LoadController.sendEventToModContainer(LoadController.java:252) [LoadController.class:?]
	at net.minecraftforge.fml.common.LoadController.propogateStateMessage(LoadController.java:230) [LoadController.class:?]
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_144]
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:1.8.0_144]
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_144]
	at java.lang.reflect.Method.invoke(Method.java:498) ~[?:1.8.0_144]
	at com.google.common.eventbus.Subscriber.invokeSubscriberMethod(Subscriber.java:91) [minecraft_server.1.12.jar:?]
	at com.google.common.eventbus.Subscriber$SynchronizedSubscriber.invokeSubscriberMethod(Subscriber.java:150) [minecraft_server.1.12.jar:?]
	at com.google.common.eventbus.Subscriber$1.run(Subscriber.java:76) [minecraft_server.1.12.jar:?]
	at com.google.common.util.concurrent.MoreExecutors$DirectExecutor.execute(MoreExecutors.java:399) [minecraft_server.1.12.jar:?]
	at com.google.common.eventbus.Subscriber.dispatchEvent(Subscriber.java:71) [minecraft_server.1.12.jar:?]
	at com.google.common.eventbus.Dispatcher$PerThreadQueuedDispatcher.dispatch(Dispatcher.java:116) [minecraft_server.1.12.jar:?]
	at com.google.common.eventbus.EventBus.post(EventBus.java:217) [minecraft_server.1.12.jar:?]
	at net.minecraftforge.fml.common.LoadController.distributeStateMessage(LoadController.java:147) [LoadController.class:?]
	at net.minecraftforge.fml.common.Loader.initializeMods(Loader.java:781) [Loader.class:?]
	at net.minecraftforge.fml.server.FMLServerHandler.finishServerLoading(FMLServerHandler.java:107) [FMLServerHandler.class:?]
	at net.minecraftforge.fml.common.FMLCommonHandler.onServerStarted(FMLCommonHandler.java:336) [FMLCommonHandler.class:?]
	at net.minecraft.server.dedicated.DedicatedServer.func_71197_b(DedicatedServer.java:216) [nx.class:?]
	at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:483) [MinecraftServer.class:?]
	at java.lang.Thread.run(Thread.java:748) [?:1.8.0_144]

 

Thats the info the LogHelper gives me on a real Server. Forgot to add it.

 

-Dj

Link to comment
Share on other sites

You need the SRG name as well. You can get it from MCPbot.

  • Like 1

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Link to comment
Share on other sites

Hmm, It gave me something, but it seems like it was wrong.

I now use this:

Class cls = Class.forName("avh.f");
field = cls.getDeclaredField("field_190562_f");

Instead of this:

field = TileEntity.class.getDeclaredField("REGISTRY");

 

 

But neither works on servers. 

Do I have to type in something else in MCPbot?

 

My input in MCP was this:

Spoiler

eb31982565.png

 

 

-Dj

Edited by MrDj200
Link to comment
Share on other sites

Use ReflectionHelper.

(Note: The class name is not "avh.f" it's "avh" with a field name of "f", but you don't need Notch names anyway)

  • Like 1

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Link to comment
Share on other sites

https://github.com/Draco18s/ReasonableRealism/blob/1.12.1/src/main/java/com/draco18s/hardlib/util/AdvancementUtils.java#L20

Here's an example. It does not point to the same object you're interested in, it just shows how to use ReflectionHelper.

  • Like 1

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Link to comment
Share on other sites

Thanks.

So like this?

Field listField = ReflectionHelper.findField(TileEntity.class, "REGISTRY", "field_190562_f");

Or Like this:

Field listField = ReflectionHelper.findField(avh.class, "REGISTRY", "field_190562_f");

 

Edited by MrDj200
Link to comment
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.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
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.

Announcements



×
×
  • Create New...

Important Information

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