Jump to content

[1.15.2] (Solved) How to setBlockState in ClientChatEvent?


foul_owl

Recommended Posts

private MinecraftServer server;

    @SubscribeEvent
    public void onServerStarting(FMLServerStartingEvent event) {
        // do something when the server starts
        LOGGER.info("HELLO from server starting");
        server = event.getServer();
    }

    @SubscribeEvent
    public void onChat(ClientChatEvent event){
        final String message = event.getMessage();
        if (message.equals("test")) {
            LOGGER.info("Running test code");
            server.getWorld(DimensionType.OVERWORLD).setBlockState(new BlockPos(50, 50, 100), Blocks.DIAMOND_BLOCK.getDefaultState());
        }
    }

 

If the player says a certain phrase, I want a single block to change.

 

Seems like a very simple task, but unfortunately this has no effect. I do see "Running test code" in the console, so I know it's getting to that point.

Edited by foul_owl
Link to comment
Share on other sites

Thank you! My code now looks like this:

private MinecraftServer server;

    @SubscribeEvent
    public void onServerStarting(FMLServerStartingEvent event) {
        // do something when the server starts
        LOGGER.info("HELLO from server starting");
        server = event.getServer();
    }

    @SubscribeEvent
    public void onChat(ServerChatEvent event){
        final String message = event.getMessage();
        if (message.equals("test")) {
            LOGGER.info("Running test code");
            server.getWorld(DimensionType.OVERWORLD).setBlockState(new BlockPos(50, 50, 100), Blocks.DIAMOND_BLOCK.getDefaultState());
        }
    }

 

Unfortunately same issue here. I can see the logger output "Running test code" but the block does not change.

 

Thank you so much for the help!

Edited by foul_owl
Link to comment
Share on other sites

Understood, thank you.

 

I found this post:

https://www.minecraftforge.net/forum/topic/82415-how-to-get-current-world-so-i-can-use-setblockstate-1152/

Which seems close to what I want to accomplish, so I tried:

 

    @SubscribeEvent
    public void onChat(ServerChatEvent event){
        final String message = event.getMessage();

        World world = DimensionManager.getWorld(event.getPlayer().getServer(), DimensionType.OVERWORLD, true, true);

        if (message.equals("test")) {
            LOGGER.info("Running test code");
            world.setBlockState(new BlockPos(50, 50, 100), Blocks.DIAMOND_BLOCK.getDefaultState());
        }
    }

 

Same issue, no effect. So then I figured maybe I am complicating things with the dimension manager, so I also tried:

 

    @SubscribeEvent
    public void onChat(ServerChatEvent event){
        final String message = event.getMessage();

        World world = event.getPlayer().getServer().getWorld(DimensionType.OVERWORLD);

        if (message.equals("test")) {
            LOGGER.info("Running test code");
            world.setBlockState(new BlockPos(50, 50, 100), Blocks.DIAMOND_BLOCK.getDefaultState());
        }
    }

 

Same issue. I see "Running test code" but the block does not change. I'm sorry, I really don't know what I'm doing wrong.

 

Thank you so much for all the help.

Link to comment
Share on other sites

Tried one more thing for the hell of it. Skipped the "world" variable entirely:

 

    @SubscribeEvent
    public void onChat(ServerChatEvent event){
        final String message = event.getMessage();
        if (message.equals("test")) {
            LOGGER.info("Running test code");
            event.getPlayer().getServer().getWorld(DimensionType.OVERWORLD).setBlockState(new BlockPos(50, 50, 100), Blocks.DIAMOND_BLOCK.getDefaultState());
        }
    }

 

Same thing, I get the logger output but no effect.

Link to comment
Share on other sites

I saw the "getServerWorld()" method from poking around and tried:

 

    @SubscribeEvent
    public void onChat(ServerChatEvent event){
        final String message = event.getMessage();

        if (message.equals("test")) {
            LOGGER.info("Running test code");
            event.getPlayer().getServerWorld().setBlockState(new BlockPos(50, 50, 100), Blocks.DIAMOND_BLOCK.getDefaultState());
        }
    }

 

Again, same issue. I get the logger output but no effect.

Link to comment
Share on other sites

1 hour ago, foul_owl said:

event.getPlayer().getServer()

uh... event.getPlayer().getEntityWorld()?

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.

Link to comment
Share on other sites

3 minutes ago, Draco18s said:

uh... event.getPlayer().getEntityWorld()?

Thank you! Unfortunately, no effect. Same output as before.

 

My code:

   @SubscribeEvent
    public void onChat(ServerChatEvent event){
        final String message = event.getMessage();

        if (message.equals("test")) {
            LOGGER.info("Running test code");
            event.getPlayer().getEntityWorld().setBlockState(new BlockPos(50, 50, 100), Blocks.DIAMOND_BLOCK.getDefaultState());
        }
    }

 

Link to comment
Share on other sites

I did getBlockState() and set a breakpoint to examine the contents.

 

It's definitely a diamond block, but it appears, visually, to the player as a block of air.

 

Is there some sort of refresh that has to happen after a setBlockState() is called?

Link to comment
Share on other sites

Here is my code:

    @SubscribeEvent
    public void onChat(ServerChatEvent event){
        final String message = event.getMessage();
        World world = event.getPlayer().getServer().getWorld(DimensionType.OVERWORLD);
        if (message.equals("get")) {
            LOGGER.info("Running get code");
            BlockState tmpstate;
            tmpstate = world.getBlockState(new BlockPos(50, 50, 100));
            LOGGER.info("End of get code");
        }
        else if (message.equals("set")) {
            LOGGER.info("Running test code");
            world.setBlockState(new BlockPos(50, 50, 100), Blocks.DIAMOND_BLOCK.getDefaultState());
        }
    }

Breakpoint is set at the end of the "get" block.

Link to comment
Share on other sites

Ok, I figured out what the issue is here. The world I have been testing with was in a bugged state.

 

It appears it is possible for the world to get into a bugged state where "getBlockState" shows the block as being one block type, where the player visually sees the block as a different block type.

 

I even tried filling in the area with other blocks, and my "get" code still showed the block state as being diamond.

 

Deleting the world and creating a new one resolved the issue. I didn't try this originally as a build takes ~2 minutes, and generating a world is an additional ~2 minutes. (Even 2 minutes is a long time to wait for a build for me, my C++ projects usually take seconds to build ?)

 

I should have tried a new world sooner, but I simply did not expect a world to get into a state where:

  • setBlockState has no effect.
  • getBlockState does not match the block shown on screen.
  • Replacing the block at the coordinates does not match the block type of "getBlockState".

Very strange bug.

 

(Yes, I confirmed my location each time both with "/teleport 50 50 100" and with F3.)

 

In any case, happy to have this issue resolved.

Edited by foul_owl
Link to comment
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.
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.



×
×
  • Create New...

Important Information

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