Jump to content

[1.16.4]Best practice to get Gametype in EventHandler


Cratthorax

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 comment
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 comment
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 comment
Share on other sites

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 comment
Share on other sites

Guest
This topic is now closed to further replies.

Announcements



×
×
  • Create New...

Important Information

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