Jump to content

[1.16.4] HandleCommand on PlayerWakeUpEvent crashing with java.lang.StackOverflowError


Recommended Posts

Posted

Hi folks, 

Very new to game-dev work so my apologies if I'm just a n00b but I've been combing the forums for hours and my head is sore from banging it against the wall:

 

I'm trying to add an event listener on the "PlayerWakeUpEvent" that will send a message in chat and throw the player across the world using the "/teleport" command.

It worked absolutely perfectly when I used the "PlayerSleepInBedEvent" but for the wake-up event I seem to get stuck in a loop whereby my chat message and the error stack is verbose'd indefinitely until the game crashes (I was going to post the full error logs but there are 569773 lines so I just took what looked important)

 

Logs:

https://pastebin.com/5gFYL6Y4

 

package com.cheesecake.helloworld.events;

import com.cheesecake.helloworld.HelloWorld;
import com.cheesecake.helloworld.util.RegistryHandler;
import net.minecraft.client.Minecraft;
import net.minecraft.command.CommandSource;
import net.minecraft.entity.Entity;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.player.ServerPlayerEntity;
import net.minecraft.server.MinecraftServer;
import net.minecraft.util.text.StringTextComponent;
import net.minecraft.util.text.TextFormatting;
import net.minecraft.world.World;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.event.entity.player.PlayerEvent;
import net.minecraftforge.event.entity.player.PlayerSleepInBedEvent;
import net.minecraftforge.event.entity.player.PlayerWakeUpEvent;
import net.minecraftforge.eventbus.api.Event;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;

@Mod.EventBusSubscriber(modid = HelloWorld.MOD_ID, bus=Mod.EventBusSubscriber.Bus.FORGE, value = Dist.CLIENT)
public class ModClientEvents {

    @SubscribeEvent
    public static void onPlayerWakeUp(PlayerWakeUpEvent event){
        PlayerEntity player = event.getPlayer();
        World world = player.getEntityWorld();
        MinecraftServer server = world.getServer();
        CommandSource source = player.getCommandSource();

        //Static X Y Z Values until we can find biomes
        int px = 4226;
        int py = 67; // Above sea level but below clouds
        int pz = -79;

        if(!event.getPlayer().getEntityWorld().isRemote()) {
            player.sendMessage(new StringTextComponent("Have a nice trip"), player.getUniqueID());
            try {
                server.getCommandManager().handleCommand(source, "/teleport " + player.getUniqueID() + " " + px + " " + py + " " + pz);
            } catch (Exception e) {
                HelloWorld.LOGGER.info("Could not teleport the player");
            }
        }
    }
}

 

For reference here is the PlayerSleepInBedEvent I was using earlier (I disabled it when running the above in case of conflicts)
 

@SubscribeEvent
public static void MagicalDreamBed(PlayerSleepInBedEvent event){
   LivingEntity player = event.getPlayer();
   World world = player.getEntityWorld();
   MinecraftServer server = world.getServer();
   CommandSource commandsource = server.getCommandSource();

   // Static X Y Z Values until we can find biomes
   int px = 4226;
   int py = 67; // Above sea level but below clouds
   int pz = -79;

   if(!event.getPlayer().getEntityWorld().isRemote()){
       server.getCommandManager().handleCommand(commandsource, "/tp " + player.getUniqueID() + " " + px + " " + py + " " + pz);
       player.sendMessage(new StringTextComponent("Have a nice trip"), player.getUniqueID());
   }
}

 

I'm pretty sure its the server.getCommandManager().handleCommand part thats the problem but I have no idea as to why

Thanks in advance folks

"It's not broken, it just doesn't work" - Will Jones 2019

Posted

Rather than trying to fake executing a command, just do the things that the command does.

i.e. instead of telling the server "the player typed /teleport, run the teleport command" just...teleport the player by changing their position.

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Posted
19 minutes ago, Draco18s said:

Rather than trying to fake executing a command, just do the things that the command does.

i.e. instead of telling the server "the player typed /teleport, run the teleport command" just...teleport the player by changing their position.

Thanks for the response - I've tried implementing player.setPositionAndUpdate (having changed my coord variables to doubles) as per the below but its not actually doing anything o.O

        if(!event.getPlayer().getEntityWorld().isRemote()) {
            player.sendMessage(new StringTextComponent("Have a nice trip"), player.getUniqueID());
            try {
                player.setPositionAndUpdate(px, py, pz);
                HelloWorld.LOGGER.info("The player has been teleported");
            } catch (Exception e) {
                HelloWorld.LOGGER.info("Could not teleport the player");
            }
        }

 

I've tried it with player as a LivingEntity and a PlayerEntity but neither works

"It's not broken, it just doesn't work" - Will Jones 2019

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

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

Announcements



×
×
  • Create New...

Important Information

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