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

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 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.


  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • i was using java 18 before it launched 1 time then it did not do it again
    • what did u do to solve this problem?? i have the same error and crashes when putting coal in furnace
    • i tried java 17 getting this    C:\Users\user\Desktop\Minecraft server april>REM Forge requires a configured set of both JVM and program arguments. C:\Users\user\Desktop\Minecraft server april>REM Add custom JVM arguments to the user_jvm_args.txt C:\Users\user\Desktop\Minecraft server april>REM Add custom program arguments {such as nogui} to this file in the next line before the  or C:\Users\user\Desktop\Minecraft server april>REM  pass them to this script directly C:\Users\user\Desktop\Minecraft server april>java @user_jvm_args.txt @libraries/net/minecraftforge/forge/1.18.2-40.2.0/win_args.txt 2023-03-31 20:34:50,316 main WARN Advanced terminal features are not available in this environment [20:34:50] [main/INFO] [cp.mo.mo.Launcher/MODLAUNCHER]: ModLauncher running: args [--launchTarget, forgeserver, --fml.forgeVersion, 40.2.0, --fml.mcVersion, 1.18.2, --fml.forgeGroup, net.minecraftforge, --fml.mcpVersion, 20220404.173914] [20:34:50] [main/INFO] [cp.mo.mo.Launcher/MODLAUNCHER]: ModLauncher 9.1.3+9.1.3+main.9b69c82a starting: java version 20 by Oracle Corporation [20:34:50] [main/INFO] [mixin/]: SpongePowered MIXIN Subsystem Version=0.8.5 Source=union:/C:/Users/user/Desktop/Minecraft%20server%20april/libraries/org/spongepowered/mixin/0.8.5/mixin-0.8.5.jar%2314!/ Service=ModLauncher Env=SERVER [20:34:51] [main/WARN] [ne.mi.fm.lo.mo.ModFileParser/LOADING]: Mod file C:\Users\user\Desktop\Minecraft server april\libraries\net\minecraftforge\fmlcore\1.18.2-40.2.0\fmlcore-1.18.2-40.2.0.jar is missing mods.toml file [20:34:51] [main/WARN] [ne.mi.fm.lo.mo.ModFileParser/LOADING]: Mod file C:\Users\user\Desktop\Minecraft server april\libraries\net\minecraftforge\javafmllanguage\1.18.2-40.2.0\javafmllanguage-1.18.2-40.2.0.jar is missing mods.toml file [20:34:51] [main/WARN] [ne.mi.fm.lo.mo.ModFileParser/LOADING]: Mod file C:\Users\user\Desktop\Minecraft server april\libraries\net\minecraftforge\lowcodelanguage\1.18.2-40.2.0\lowcodelanguage-1.18.2-40.2.0.jar is missing mods.toml file [20:34:51] [main/WARN] [ne.mi.fm.lo.mo.ModFileParser/LOADING]: Mod file C:\Users\user\Desktop\Minecraft server april\libraries\net\minecraftforge\mclanguage\1.18.2-40.2.0\mclanguage-1.18.2-40.2.0.jar is missing mods.toml file [20:34:51] [main/INFO] [ne.mi.fm.lo.mo.JarInJarDependencyLocator/]: No dependencies to load found. Skipping! [20:34:53] [main/INFO] [mixin/]: Compatibility level set to JAVA_17 [20:34:53] [main/INFO] [cp.mo.mo.LaunchServiceHandler/MODLAUNCHER]: Launching target 'forgeserver' with arguments [] [20:34:53] [main/WARN] [mixin/]: Error loading class: java/lang/invoke/MethodHandles$Lookup (java.lang.IllegalArgumentException: Unsupported class file major version 64) [20:34:53] [main/WARN] [mixin/]: Error loading class: java/lang/invoke/MethodHandles$Lookup (java.lang.IllegalArgumentException: Unsupported class file major version 64) [20:34:53] [main/WARN] [mixin/]: Error loading class: java/lang/invoke/MethodHandles$Lookup (java.lang.IllegalArgumentException: Unsupported class file major version 64) [20:34:53] [main/WARN] [mixin/]: Error loading class: java/lang/invoke/MethodHandles$Lookup (java.lang.IllegalArgumentException: Unsupported class file major version 64) [20:34:53] [main/WARN] [mixin/]: Error loading class: java/lang/invoke/MethodHandles$Lookup (java.lang.IllegalArgumentException: Unsupported class file major version 64) [20:34:53] [main/WARN] [mixin/]: Error loading class: java/lang/invoke/MethodHandles$Lookup (java.lang.IllegalArgumentException: Unsupported class file major version 64) [20:34:53] [main/WARN] [mixin/]: Error loading class: java/util/Map$Entry (java.lang.IllegalArgumentException: Unsupported class file major version 64) [20:34:53] [main/WARN] [mixin/]: Error loading class: java/util/concurrent/CompletableFuture (java.lang.IllegalArgumentException: Unsupported class file major version 64) Exception in thread "main" org.spongepowered.asm.mixin.transformer.throwables.MixinTransformerError: An unexpected critical error was encountered         at MC-BOOTSTRAP/org.spongepowered.mixin/org.spongepowered.asm.mixin.transformer.MixinProcessor.applyMixins(MixinProcessor.java:392)         at MC-BOOTSTRAP/org.spongepowered.mixin/org.spongepowered.asm.mixin.transformer.MixinTransformer.transformClass(MixinTransformer.java:250)         at MC-BOOTSTRAP/org.spongepowered.mixin/org.spongepowered.asm.service.modlauncher.MixinTransformationHandler.processClass(MixinTransformationHandler.java:131)         at MC-BOOTSTRAP/org.spongepowered.mixin/org.spongepowered.asm.launch.MixinLaunchPluginLegacy.processClass(MixinLaunchPluginLegacy.java:131)         at MC-BOOTSTRAP/cpw.mods.modlauncher@9.1.3/cpw.mods.modlauncher.serviceapi.ILaunchPluginService.processClassWithFlags(ILaunchPluginService.java:156)         at MC-BOOTSTRAP/cpw.mods.modlauncher@9.1.3/cpw.mods.modlauncher.LaunchPluginHandler.offerClassNodeToPlugins(LaunchPluginHandler.java:88)         at MC-BOOTSTRAP/cpw.mods.modlauncher@9.1.3/cpw.mods.modlauncher.ClassTransformer.transform(ClassTransformer.java:120)         at MC-BOOTSTRAP/cpw.mods.modlauncher@9.1.3/cpw.mods.modlauncher.TransformingClassLoader.maybeTransformClassBytes(TransformingClassLoader.java:50)         at cpw.mods.securejarhandler@1.0.3/cpw.mods.cl.ModuleClassLoader.readerToClass(ModuleClassLoader.java:110)         at cpw.mods.securejarhandler@1.0.3/cpw.mods.cl.ModuleClassLoader.lambda$findClass$16(ModuleClassLoader.java:216)         at cpw.mods.securejarhandler@1.0.3/cpw.mods.cl.ModuleClassLoader.loadFromModule(ModuleClassLoader.java:226)         at cpw.mods.securejarhandler@1.0.3/cpw.mods.cl.ModuleClassLoader.findClass(ModuleClassLoader.java:216)         at cpw.mods.securejarhandler@1.0.3/cpw.mods.cl.ModuleClassLoader.loadClass(ModuleClassLoader.java:132)         at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)         at java.base/java.lang.Class.getDeclaredMethods0(Native Method)         at java.base/java.lang.Class.privateGetDeclaredMethods(Class.java:3502)         at java.base/java.lang.Class.getMethodsRecursive(Class.java:3643)         at java.base/java.lang.Class.getMethod0(Class.java:3629)         at java.base/java.lang.Class.getMethod(Class.java:2319)         at MC-BOOTSTRAP/fmlloader@1.18.2-40.2.0/net.minecraftforge.fml.loading.targets.CommonServerLaunchHandler.lambda$launchService$0(CommonServerLaunchHandler.java:32)         at MC-BOOTSTRAP/cpw.mods.modlauncher@9.1.3/cpw.mods.modlauncher.LaunchServiceHandlerDecorator.launch(LaunchServiceHandlerDecorator.java:37)         at MC-BOOTSTRAP/cpw.mods.modlauncher@9.1.3/cpw.mods.modlauncher.LaunchServiceHandler.launch(LaunchServiceHandler.java:53)         at MC-BOOTSTRAP/cpw.mods.modlauncher@9.1.3/cpw.mods.modlauncher.LaunchServiceHandler.launch(LaunchServiceHandler.java:71)         at MC-BOOTSTRAP/cpw.mods.modlauncher@9.1.3/cpw.mods.modlauncher.Launcher.run(Launcher.java:106)         at MC-BOOTSTRAP/cpw.mods.modlauncher@9.1.3/cpw.mods.modlauncher.Launcher.main(Launcher.java:77)         at MC-BOOTSTRAP/cpw.mods.modlauncher@9.1.3/cpw.mods.modlauncher.BootstrapLaunchConsumer.accept(BootstrapLaunchConsumer.java:26)         at MC-BOOTSTRAP/cpw.mods.modlauncher@9.1.3/cpw.mods.modlauncher.BootstrapLaunchConsumer.accept(BootstrapLaunchConsumer.java:23)         at cpw.mods.bootstraplauncher@1.0.0/cpw.mods.bootstraplauncher.BootstrapLauncher.main(BootstrapLauncher.java:149) Caused by: org.spongepowered.asm.mixin.throwables.ClassMetadataNotFoundException: java.util.concurrent.CompletableFuture         at MC-BOOTSTRAP/org.spongepowered.mixin/org.spongepowered.asm.mixin.transformer.MixinPreProcessorStandard.transformMethod(MixinPreProcessorStandard.java:754)         at MC-BOOTSTRAP/org.spongepowered.mixin/org.spongepowered.asm.mixin.transformer.MixinPreProcessorStandard.transform(MixinPreProcessorStandard.java:739)         at MC-BOOTSTRAP/org.spongepowered.mixin/org.spongepowered.asm.mixin.transformer.MixinPreProcessorStandard.attach(MixinPreProcessorStandard.java:310)         at MC-BOOTSTRAP/org.spongepowered.mixin/org.spongepowered.asm.mixin.transformer.MixinPreProcessorStandard.createContextFor(MixinPreProcessorStandard.java:280)         at MC-BOOTSTRAP/org.spongepowered.mixin/org.spongepowered.asm.mixin.transformer.MixinInfo.createContextFor(MixinInfo.java:1288)         at MC-BOOTSTRAP/org.spongepowered.mixin/org.spongepowered.asm.mixin.transformer.MixinApplicatorStandard.apply(MixinApplicatorStandard.java:292)         at MC-BOOTSTRAP/org.spongepowered.mixin/org.spongepowered.asm.mixin.transformer.TargetClassContext.apply(TargetClassContext.java:383)         at MC-BOOTSTRAP/org.spongepowered.mixin/org.spongepowered.asm.mixin.transformer.TargetClassContext.applyMixins(TargetClassContext.java:365)         at MC-BOOTSTRAP/org.spongepowered.mixin/org.spongepowered.asm.mixin.transformer.MixinProcessor.applyMixins(MixinProcessor.java:363)         ... 27 more C:\Users\user\Desktop\Minecraft server april>pause Press any key to continue . . .  
    • Use java 17, mixin does not support java 20
  • Topics

×
×
  • Create New...

Important Information

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