Jump to content
  • Home
  • Files
  • Docs
Topics
  • All Content

  • This Topic
  • This Forum

  • Advanced Search
  • Existing user? Sign In  

    Sign In



    • Not recommended on shared computers


    • Forgot your password?

  • Sign Up
  • All Activity
  • Home
  • Mod Developer Central
  • Modder Support
  • [1.16.5] Properly using DistExecutor with arguments
Currently Supported: 1.16.X (Latest) and 1.15.X (LTS)
Sign in to follow this  
Followers 0
Choonster

[1.16.5] Properly using DistExecutor with arguments

By Choonster, January 23 in Modder Support

  • Reply to this topic
  • Start new topic

Recommended Posts

Choonster    1651

Choonster

Choonster    1651

  • Reality Controller
  • Choonster
  • Forge Modder
  • 1651
  • 5100 posts
Posted January 23 (edited)

I have a packet that's sent to the client to open a GUI, which I'm using DistExecutor to do.

 

The packet's handler method does the following:

DistExecutor.safeRunWhenOn(Dist.CLIENT, () -> ClientOnlyNetworkMethods.openClientScreen(message))

 

ClientOnlyNetworkMethods.openClientScreen currently looks like this:

public static DistExecutor.SafeRunnable openClientScreen(final OpenClientScreenMessage message) {
	return new DistExecutor.SafeRunnable() {
		@Override
		public void run() {
			ClientScreenManager.openScreen(message.getId(), message.getAdditionalData(), Minecraft.getInstance());
		}
	};
}

 

ClientScreenManager is a client-only class that handles opening the GUI.

 

As you can see from the code, I need to pass arguments from the packet to the client-only method; which rules out using a method reference as the SafeRunnable implementation.

 

When I replace the anonymous class implementation of SafeRunnable in ClientOnlyNetworkMethods.openClientScreen with a lambda, DistExecutor.validateSafeReferent throws an "Unsafe Referent usage found in safe referent method" exception. From what I can see, using any non-lambda implementation of SafeReferent simply bypasses the safety checks in validateSafeReferent but doesn't necessarily mean that the code is safe.

 

The current code with the anonymous class does seem to work on the dedicated server, but is this the correct way to use DistExecutor; or is there a better way to do it?

Edited January 23 by Choonster
  • Quote

Please don't PM me to ask for help. Asking your question in a public thread preserves it for people who are having the same problem in the future.

Share this post


Link to post
Share on other sites

ChampionAsh5357    165

ChampionAsh5357

ChampionAsh5357    165

  • World Shaper
  • ChampionAsh5357
  • Members
  • 165
  • 1038 posts
Posted January 23

From what I understand, this is not the correct way to use DistExecutor. For the case where you can't supply a runnable or supplier, DistExecutor#unsafe* should be used instead. This will supply a runnable of what you want to execute (e.g. () -> () -> //Do things). This does not verify nor guarantee that the code is completely safe to access; however, if the runnable executes another method that is isolated in a different class, it is 'safe' since classloading will not occur. So, the proper way to implement the code above is DistExecutor.unsafeRunWhenOn(Dist.CLIENT, () -> () -> ClientScreenManager#openScreen).

  • Thanks 1
  • Quote

Share this post


Link to post
Share on other sites

Choonster    1651

Choonster

Choonster    1651

  • Reality Controller
  • Choonster
  • Forge Modder
  • 1651
  • 5100 posts
Posted January 24
19 hours ago, ChampionAsh5357 said:

From what I understand, this is not the correct way to use DistExecutor. For the case where you can't supply a runnable or supplier, DistExecutor#unsafe* should be used instead. This will supply a runnable of what you want to execute (e.g. () -> () -> //Do things). This does not verify nor guarantee that the code is completely safe to access; however, if the runnable executes another method that is isolated in a different class, it is 'safe' since classloading will not occur. So, the proper way to implement the code above is DistExecutor.unsafeRunWhenOn(Dist.CLIENT, () -> () -> ClientScreenManager#openScreen).

 

Thanks, I think that makes sense.

 

I've tried to follow this advice and clean up all my DistExecutor code in this commit, does this look correct?

  • Quote

Please don't PM me to ask for help. Asking your question in a public thread preserves it for people who are having the same problem in the future.

Share this post


Link to post
Share on other sites

ChampionAsh5357    165

ChampionAsh5357

ChampionAsh5357    165

  • World Shaper
  • ChampionAsh5357
  • Members
  • 165
  • 1038 posts
Posted January 24
18 minutes ago, Choonster said:

I've tried to follow this advice and clean up all my DistExecutor code in this commit, does this look correct?

My only comment would be on OpenClientScreenMessage as the Minecraft instance can just be obtained inside the method itself. This is still 'safe' I believe, but we should avoid calling anything that might only be available on the client that is not isolated in a different class. This is my opinion as it still won't be loaded unless on the physical client.

  • Thanks 1
  • Quote

Share this post


Link to post
Share on other sites

Choonster    1651

Choonster

Choonster    1651

  • Reality Controller
  • Choonster
  • Forge Modder
  • 1651
  • 5100 posts
Posted January 26
On 1/25/2021 at 1:47 AM, ChampionAsh5357 said:

My only comment would be on OpenClientScreenMessage as the Minecraft instance can just be obtained inside the method itself. This is still 'safe' I believe, but we should avoid calling anything that might only be available on the client that is not isolated in a different class. This is my opinion as it still won't be loaded unless on the physical client.

 

Thanks, that makes sense.

  • Quote

Please don't PM me to ask for help. Asking your question in a public thread preserves it for people who are having the same problem in the future.

Share this post


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.   Paste as plain text instead

  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.

    • Insert image from URL
×
  • Desktop
  • Tablet
  • Phone
Sign in to follow this  
Followers 0
Go To Topic Listing



  • Recently Browsing

    No registered users viewing this page.

  • Posts

    • diesieben07
      My game keeps crashing and I haven't even added any mods on yet

      By diesieben07 · Posted just now

      Are you using the Curse Launcher?
    • milkman69
      My game keeps crashing and I haven't even added any mods on yet

      By milkman69 · Posted 4 minutes ago

    • diesieben07
      My game keeps crashing and I haven't even added any mods on yet

      By diesieben07 · Posted 5 minutes ago

      Show a screenshot of your logs folder.
    • milkman69
      My game keeps crashing and I haven't even added any mods on yet

      By milkman69 · Posted 5 minutes ago

      I dont see any logs for February 27
    • milkman69
      My game keeps crashing and I haven't even added any mods on yet

      By milkman69 · Posted 7 minutes ago

      Ill try and see if there is a log for it but i dont know
  • Topics

    • milkman69
      19
      My game keeps crashing and I haven't even added any mods on yet

      By milkman69
      Started Wednesday at 11:53 PM

    • Coloringgamer
      1
      Error

      By Coloringgamer
      Started 58 minutes ago

    • NullDev
      3
      Detect Bottle use on fluid

      By NullDev
      Started 13 hours ago

    • NorthWestWind
      0
      [1.16.5] Make item render like bow in thirdperson

      By NorthWestWind
      Started 1 hour ago

    • ehbean
      3
      1.16.x Custom Furnace/Brewing Stand

      By ehbean
      Started 14 hours ago

  • Who's Online (See full list)

    • IronLeDXY
    • Fred the Wizard
    • Th1Minecraft
    • Abrynos
    • pitbox46
    • milkman69
    • diesieben07
    • NullDev
    • MrBelieve
    • loordgek
    • Altstatt123
    • polete9
    • Wende
    • maddogni
  • All Activity
  • Home
  • Mod Developer Central
  • Modder Support
  • [1.16.5] Properly using DistExecutor with arguments
  • Theme

Copyright © 2019 ForgeDevelopment LLC · Ads by Longitude Ads LLC Powered by Invision Community