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

I'm trying to figure out if there's a way to convert an IBlockState into some form that can be passed through a Packet to the server. I'd like to be able to update a certain position on a map with a specific block (in a specific orientation) whenever this is called. The position was easy enough, but I can't seem to find any way to get the block with all its orientation data converted into something that can be passed...

 

public class SetBlock implements IMessage, IMessageHandler<SetBlock, IMessage>
{
    private int x;
    private int y;
    private int z;
    private IBlockState blockState;
  
    public SetBlock() {}
   
    public SetBlock(int x, int y, int z, IBlockState blockState)
    {
        this.x = x;
        this.y = y;
        this.z = z;
        this.blockState = blockState;
    }
   
    @Override   
    public void toBytes(ByteBuf buf)
    {
    	buf.writeInt(x);
    	buf.writeInt(y);
    	buf.writeInt(z);
    	//blockState stuff
    }
    
    @Override
    public void fromBytes(ByteBuf buf)
    {
    	this.x = buf.readInt();
    	this.y = buf.readInt();
    	this.z = buf.readInt();
    	//blockState stuff
    }
    
    @Override
    public IMessage onMessage(final SetBlock message, final MessageContext ctx) 
    {
    	World world = ctx.getServerHandler().playerEntity.worldObj;
    	world.setBlockState(new BlockPos(message.x,message.y,message.z),Blocks.dirt.getDefaultState());
    	return null;
    }
    
}

 

The Dirt Block created was just a way for me to tell if the position was being passed correctly, and it is.

Any help would be fantastic...this is that last thing I need to figure out.

 

Thanks

 

P.S. Yes, I know this probably isn't an ideal way to do this, but this mod is just for my personal use so there's no worrying about others potentially abusing the placement system.

Minecraft does this using

Block.BLOCK_STATE_IDS

, which is the same map returned by

GameData.getBlockStateIDMap

.

 

I would recommend against implementing

IMessage

and

IMessageHandler

in the same class, since it's easy to get confused and use values from

this

instead of the

message

argument in

IMessageHandler#onMessage

. Either move the handler to a separate class or a static inner class.

 

It's not safe to interact with normal Minecraft classes directly in your

IMessageHandler

, since it's called from a separate thread to the main client/server thread. You need to schedule a task on the main thread using

IThreadListener#addScheduledTask

, this page explains how to do this.

Please don't PM me to ask for help. Asking your question in a public thread preserves it for people who are having the same problem in the future.

  • Author

Thanks!

 

I was able to get it to work finally. Now just to clean it up like you suggested and I should be good to go.

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.