Jump to content
View in the app

A better way to browse. Learn more.

Forge Forums

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Featured Replies

Posted

Hello,

I'm trying to spawn some particles and play some sound ones a block has been broken (particles and sounds must be observable for all players)
I have it setup that when a breakevent is emitted and some criteria has been met, I send out a packet to the client of all players:

	@SubscribeEvent
    public static void onBreakPowerCrystal(BlockEvent.BreakEvent event) {
        var world = event.getWorld();
        var block = event.getWorld().getBlockState(event.getPos()).getBlock();

        if (world.isClientSide())) {
          	// calling anything here seems to not work. looks like this event happens on the server side only
            return;
        }
  
  		if (!block.equals(ModBlocks.POWER_CRYSTAL_BLOCK.get()) {
            return;
        }

        event.setCanceled(true);
        Player player = event.getPlayer();

        KingdomManager manager = KingdomManager.get(player.level);
        getKingdom(player).ifPresentOrElse(
                kingdomOfPlayer -> {
                    manager.getKingdom(event.getPos()).ifPresentOrElse(
                            kingdomOfBlock -> {
                                if (!kingdomOfBlock.equals(kingdomOfPlayer)) {
                                    manager.decrementLives(kingdomOfBlock, 1);
                                  
                                    sendPacketToAllPlayers(player.getLevel(), new SPacketCrystalBreak(
                                      kingdomOfBlock.getName(), 
                                      event.getPos(), 																
                                      kingdomOfBlock.getLives()
                                    ));

                                    if (kingdomOfBlock.isDead()) {
                                        event.setCanceled(false);
                                    }

                                } else {
                                    // not important logic
                                }
                            },
                            () -> event.setCanceled(false)
                    );
                },
                () -> Messenger.sendClientError(player, "You are not in a kingdom"));
    }
            
    
	private static void sendPacketToAllPlayers(Level level, Object packet) {
        level.players().forEach(player -> {
            if (player instanceof ServerPlayer serverPlayer) {
                PacketHandler.sendToPlayer(packet, serverPlayer);
            }
        });
    }

Now this packet gets handled with the following code

   public boolean handle(Supplier<NetworkEvent.Context> supplier) {
        NetworkEvent.Context context = supplier.get();

        context.enqueueWork(() -> {
            DistExecutor.unsafeCallWhenOn(Dist.CLIENT, () -> () -> {
                Player player = Minecraft.getInstance().player;

                float randomSoundPitch = ((float) Math.random() / 10f) + 0.9f;

                // Adding this line crashes multiplayer servers on startup
                player.playSound(SoundEvents.ANVIL_PLACE, 1f, randomSoundPitch);
                return 0;
            });
        });

        return true;
    }

This will work fine on singleplayer. However, when trying to boot up a multiplayer server, it will refuse to start, giving the following error:

Caused by 0: java.lang.BootstrapMethodError: java.lang.RuntimeException: Attempted to load class net/minecraft/client/player/LocalPlayer for invalid dist DEDICATED_SERVER
		at org.daan.kingdomclash.common.network.PacketHandler.register(PacketHandler.java:37) ~[%2380!/:?] {re:classloading}
		at org.daan.kingdomclash.common.KingdomClash.setup(KingdomClash.java:62) ~[%2380!/:?] {re:classloading}
		at net.minecraftforge.eventbus.EventBus.doCastFilter(EventBus.java:247) ~[eventbus-5.0.7.jar%239!/:?] {}
		at net.minecraftforge.eventbus.EventBus.lambda$addListener$11(EventBus.java:239) ~[eventbus-5.0.7.jar%239!/:?] {}
		at net.minecraftforge.eventbus.EventBus.post(EventBus.java:302) ~[eventbus-5.0.7.jar%239!/:?] {}
		at net.minecraftforge.eventbus.EventBus.post(EventBus.java:283) ~[eventbus-5.0.7.jar%239!/:?] {}
		at net.minecraftforge.fml.javafmlmod.FMLModContainer.acceptEvent(FMLModContainer.java:106) ~[javafmllanguage-1.18.2-40.1.0.jar%2377!/:?] {}
		at net.minecraftforge.fml.ModContainer.lambda$buildTransitionHandler$4(ModContainer.java:107) ~[fmlcore-1.18.2-40.1.0.jar%2379!/:?] {}
		at java.util.concurrent.CompletableFuture$AsyncRun.run(CompletableFuture.java:1804) ~[?:?] {}
		at java.util.concurrent.CompletableFuture$AsyncRun.exec(CompletableFuture.java:1796) ~[?:?] {}
		at java.util.concurrent.ForkJoinTask.doExec(ForkJoinTask.java:373) ~[?:?] {}
		at java.util.concurrent.ForkJoinPool$WorkQueue.topLevelExec(ForkJoinPool.java:1182) ~[?:?] {}
		at java.util.concurrent.ForkJoinPool.scan(ForkJoinPool.java:1655) ~[?:?] {}
		at java.util.concurrent.ForkJoinPool.runWorker(ForkJoinPool.java:1622) ~[?:?] {}
		at java.util.concurrent.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:165) ~[?:?] {}

	Mod File: main
	Failure message: Kingdom Clash (kingdomclash) encountered an error during the common_setup event phase
		java.lang.BootstrapMethodError: java.lang.RuntimeException: Attempted to load class net/minecraft/client/player/LocalPlayer for invalid dist DEDICATED_SERVER
	Mod Version: 0.0NONE
	Mod Issue URL: NOT PROVIDED
	Exception message: java.lang.RuntimeException: Attempted to load class net/minecraft/client/player/LocalPlayer for invalid dist DEDICATED_SERVER
Stacktrace:
	at net.minecraftforge.fml.loading.RuntimeDistCleaner.processClassWithFlags(RuntimeDistCleaner.java:57) ~[fmlloader-1.18.2-40.1.0.jar:1.0] {}
	at cpw.mods.modlauncher.LaunchPluginHandler.offerClassNodeToPlugins(LaunchPluginHandler.java:88) ~[modlauncher-9.1.3.jar:?] {}
	at cpw.mods.modlauncher.ClassTransformer.transform(ClassTransformer.java:120) ~[modlauncher-9.1.3.jar:?] {}
	at cpw.mods.modlauncher.TransformingClassLoader.maybeTransformClassBytes(TransformingClassLoader.java:50) ~[modlauncher-9.1.3.jar:?] {}
	at cpw.mods.cl.ModuleClassLoader.readerToClass(ModuleClassLoader.java:110) ~[securejarhandler-1.0.3.jar:?] {}
	at cpw.mods.cl.ModuleClassLoader.lambda$findClass$16(ModuleClassLoader.java:216) ~[securejarhandler-1.0.3.jar:?] {}
	at cpw.mods.cl.ModuleClassLoader.loadFromModule(ModuleClassLoader.java:226) ~[securejarhandler-1.0.3.jar:?] {}
	at cpw.mods.cl.ModuleClassLoader.findClass(ModuleClassLoader.java:216) ~[securejarhandler-1.0.3.jar:?] {}
	at cpw.mods.cl.ModuleClassLoader.loadClass(ModuleClassLoader.java:132) ~[securejarhandler-1.0.3.jar:?] {}
	at java.lang.ClassLoader.loadClass(ClassLoader.java:520) ~[?:?] {}
	at org.daan.kingdomclash.common.network.PacketHandler.register(PacketHandler.java:37) ~[%2380!/:?] {re:classloading}
	at org.daan.kingdomclash.common.KingdomClash.setup(KingdomClash.java:62) ~[%2380!/:?] {re:classloading}
	at net.minecraftforge.eventbus.EventBus.doCastFilter(EventBus.java:247) ~[eventbus-5.0.7.jar%239!/:?] {}
	at net.minecraftforge.eventbus.EventBus.lambda$addListener$11(EventBus.java:239) ~[eventbus-5.0.7.jar%239!/:?] {}
	at net.minecraftforge.eventbus.EventBus.post(EventBus.java:302) ~[eventbus-5.0.7.jar%239!/:?] {}
	at net.minecraftforge.eventbus.EventBus.post(EventBus.java:283) ~[eventbus-5.0.7.jar%239!/:?] {}
	at net.minecraftforge.fml.javafmlmod.FMLModContainer.acceptEvent(FMLModContainer.java:106) ~[javafmllanguage-1.18.2-40.1.0.jar%2377!/:?] {}
	at net.minecraftforge.fml.ModContainer.lambda$buildTransitionHandler$4(ModContainer.java:107) ~[fmlcore-1.18.2-40.1.0.jar%2379!/:?] {}
	at java.util.concurrent.CompletableFuture$AsyncRun.run(CompletableFuture.java:1804) ~[?:?] {}
	at java.util.concurrent.CompletableFuture$AsyncRun.exec(CompletableFuture.java:1796) ~[?:?] {}
	at java.util.concurrent.ForkJoinTask.doExec(ForkJoinTask.java:373) ~[?:?] {}
	at java.util.concurrent.ForkJoinPool$WorkQueue.topLevelExec(ForkJoinPool.java:1182) ~[?:?] {}
	at java.util.concurrent.ForkJoinPool.scan(ForkJoinPool.java:1655) ~[?:?] {}
	at java.util.concurrent.ForkJoinPool.runWorker(ForkJoinPool.java:1622) ~[?:?] {}
	at java.util.concurrent.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:165) ~[?:?] {}

Swapping player to equal context.getSender() will make the server boot again, but won't make it so that the player hears the sound, because from my understanding, it will then not be called on the client side.
How do i access the player from the client side in a packet without upsetting the multiplayer server?

The short answer to your question is you shouldn't have client only classes in code that gets loaded during common setup.

See the example of ClientPacketHandlerClass here: https://forge.gemwire.uk/wiki/SimpleChannel

 

Edited by warjort

Boilerplate:

If you don't post your logs/debug.log we can't help you. For curseforge you need to enable the forge debug.log in its minecraft settings. You should also post your crash report if you have one.

If there is no error in the log file and you don't have a crash report then post the launcher_log.txt from the minecraft folder. Again for curseforge this will be in your curseforge/minecraft/Install

Large files should be posted to a file sharing site like https://gist.github.com  You should also read the support forum sticky post.

That stops the code getting executed, it doesn't stop the classloader from loading the code where forge will check the classes referenced are for the correct side.

By using a separate class for the client code you will avoid that class getting loaded until it is executed (which is never on the server).

.

Boilerplate:

If you don't post your logs/debug.log we can't help you. For curseforge you need to enable the forge debug.log in its minecraft settings. You should also post your crash report if you have one.

If there is no error in the log file and you don't have a crash report then post the launcher_log.txt from the minecraft folder. Again for curseforge this will be in your curseforge/minecraft/Install

Large files should be posted to a file sharing site like https://gist.github.com  You should also read the support forum sticky post.

  • Author

Thank you for the insight!

I've moved code that uses any class with @OnlyIn(Dist.CLIENT) into a seperate "proxy" class and just call that class instead, as you've suggested.

 

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

Important Information

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

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.