Jump to content
Search In
  • More options...
Find results that contain...
Find results in...

[1.16.4]Best practice to get Gametype in EventHandler


Recommended Posts

Hello,

coming from 1.7.2, and 1.12.2 I figured the "old" way of getting game type does no longer work. It took a while and a bit of searching to find an old 2019 thread, were @diesieben07 is giving instructions, that did not work out of the box, but finally did with a bit of fiddling. This is the code block in my event handler:

	    @SubscribeEvent
    public static void onBlockBreak(BlockEvent.BreakEvent event) {
	        //Entity playerRef = event.getPlayer();
        @SuppressWarnings("resource")
        GameType gameType = Minecraft.getInstance().playerController.getCurrentGameType();
        System.out.println(gameType);
    }  

It does work, but I'm concerned about the warning suppression Eclipse forced on me to fix the logic. If anyone has some input share if there is a different way, or can confirm that my way is valid as well, I'd appreciate it.

Link to post
Share on other sites
  • The warning is eclipse being stupid.
  • You are reaching across logical sides. You have to use ServerPlayerEntity#gameMode and then PlayerInteractionManager#getGameModeForPlayer to get the game mode.
Link to post
Share on other sites
1 hour ago, diesieben07 said:
  • The warning is eclipse being stupid.
  • You are reaching across logical sides. You have to use ServerPlayerEntity#gameMode and then PlayerInteractionManager#getGameModeForPlayer to get the game mode.

Sorry, I'm out of practice. It's hard for me to comprehend what you're saying. With "reaching across logical sides" you mean I'm utilizing server and client in one sentence?

Also, why gameMode? What's the difference to gameType?

I was actually referring to this thread, were you gave that example...

For the main client player: Minecraft#playerController.getCurrentGameType()

...to get the client side gameType. I want the client side.

Link to post
Share on other sites
4 minutes ago, Cratthorax said:

Sorry, I'm out of practice. It's hard for me to comprehend what you're saying. With "reaching across logical sides" you mean I'm utilizing server and client in one sentence?

Minecraft#getInstance -> is client side
and the BlockEvent$BreakEvent is only executed on the server

 

6 minutes ago, Cratthorax said:

Also, why gameMode? What's the difference to gameType?

GameType = GameMode

the player's game mode is saved in the PlayerInteractionManager.
if you need your gamemode of the player do something like this:
Note: this is only on server side, so you need to check if the Player you get, an instance of ServerPlayerEntity is

player.gameMode.getGameModeForPlayer();

 

Link to post
Share on other sites
10 minutes ago, Cratthorax said:

With "reaching across logical sides" you mean I'm utilizing server and client in one sentence?

Reaching across logical sides means: You are on the server thread (in single player) and "reaching over" into client territory. This "works" in SP because the server and client are just two threads running in the same JVM. But this will hard-crash on an actual server (because it has no idea what the Minecraft class even is) and it will cause hard to trace bugs in single player because the code is not designed for multi threaded access.

11 minutes ago, Cratthorax said:

Also, why gameMode? What's the difference to gameType?

Forge recently switched to Mojang names for methods and fields (class names still MCP, this will change with 1.17). Mojang calls it GameMode.

12 minutes ago, Cratthorax said:

I was actually referring to this thread, were you gave that example...

 


For the main client player: Minecraft#playerController.getCurrentGameType()

 

...to get the client side gameType. I want the client side.

BlockEvent.BreakEvent is a server side event, so you cannot do anything client side there.

Link to post
Share on other sites
Posted (edited)

Better?

	    @SubscribeEvent
    public static void onBlockBreak(BlockEvent.BreakEvent event) {
        
        PlayerEntity playerRef = event.getPlayer();        
        ServerPlayerEntity playerMain = (ServerPlayerEntity) playerRef;
        GameType gameMode = playerMain.interactionManager.getGameType();
	        //@SuppressWarnings("resource")
        //GameType gameType = Minecraft.getInstance().playerController.getCurrentGameType();
        ItemStack heldItems = event.getPlayer().getHeldItemMainhand();
        Item toolRef = heldItems.getItem();        
        if(!gameMode.isCreative()) {

            
        }
    } 
Edited by Cratthorax
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.   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.



×
×
  • Create New...

Important Information

By using this site, you agree to our Privacy Policy.