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

How can I sync WorldSavedData between server and client(s)?

I thought it was automatic, but the Forge doc says:

Quote

Keep in mind the separation between client and server, as they get separate instances of global data, so if data is needed on both sides, manual synchronization will be required.

So I'm figuring out how to sync it. Is there any way to do this easily, or should I send my NBTTagCompound to client using packets?

52 minutes ago, XiNiHa said:

So I'm figuring out how to sync it. Is there any way to do this easily, or should I send my NBTTagCompound to client using packets?

Depends on what you are saving, it might be more efficient to sync the data as primitives. IE ByteBuf#writeInt or ByteBuf#writeShort. You will need to have a packet either way.

VANILLA MINECRAFT CLASSES ARE THE BEST RESOURCES WHEN MODDING

I will be posting 1.15.2 modding tutorials on this channel. If you want to be notified of it do the normal YouTube stuff like subscribing, ect.

Forge and vanilla BlockState generator.

  • Author

Thanks. And then, if I should use packet, can I reuse my own IMessage implementation that I've used to send data to the server? Here's the code:

public class PacketMemo implements IMessage {
    private BlockPos pos;
    private String text;
    private int color;

    public PacketMemo(){}

    public PacketMemo(BlockPos pos, String text, int color) {
        this.pos = pos;
        this.text = text;
        this.color = color;
    }

    @Override
    public void fromBytes(ByteBuf buf) {
        pos = BlockPos.fromLong(buf.readLong());
        int textLength = buf.readInt();
        char[] textArr = new char[textLength];
        for (int i = 0; i < textLength; i++) {
            textArr[i] = buf.readChar();
        }
        text = new String(textArr);
        color = buf.readInt();
    }

    @Override
    public void toBytes(ByteBuf buf) {
        buf.writeLong(pos.toLong());
        buf.writeInt(text.length());
        for (int i = 0; i < text.length(); i++) {
            buf.writeChar(text.charAt(i));
        }
        buf.writeInt(color);
    }

    public static class Handler implements IMessageHandler<PacketMemo, IMessage> {
        @Override
        public IMessage onMessage(PacketMemo message, MessageContext ctx) {
            FMLCommonHandler.instance().getWorldThread(ctx.netHandler).addScheduledTask(() -> handle(message, ctx));
            return null;
        }

        public void handle(PacketMemo message, MessageContext ctx) {
            WSDMemo wsdMemo = WSDMemo.get(ctx.getServerHandler().player.getServerWorld());
            wsdMemo.addOrEditMemo(message.pos, message.text, message.color);
        }
    }
}

ย 

Edited by XiNiHa

  • Author

Oh, sorry. I just realized that mc.getIntegratedServer() only works on singleplayer mode. ;(

4 minutes ago, XiNiHa said:

Oh, sorry. I just realized that mc.getIntegratedServer() only works on singleplayer mode. ;(

When you send data to the client you must use Minecraft.getMInecraft().addScheduledTask

VANILLA MINECRAFT CLASSES ARE THE BEST RESOURCES WHEN MODDING

I will be posting 1.15.2 modding tutorials on this channel. If you want to be notified of it do the normal YouTube stuff like subscribing, ect.

Forge and vanilla BlockState generator.

  • Author
3 minutes ago, Animefan8888 said:

you must use Minecraft.getMInecraft().addScheduledTask

Where should I call it? On Handler? And, what method should I pass?

1 hour ago, XiNiHa said:

FMLCommonHandler.instance().getWorldThread(ctx.netHandler).addScheduledTask(() -> handle(message, ctx));

Should be Minecraft.getMinecraft().addScheduledTask

ย 

VANILLA MINECRAFT CLASSES ARE THE BEST RESOURCES WHEN MODDING

I will be posting 1.15.2 modding tutorials on this channel. If you want to be notified of it do the normal YouTube stuff like subscribing, ect.

Forge and vanilla BlockState generator.

  • Author

@Animefan8888ย Can you help me one more time? I tried to sync between (logical) server and client using PlayerLoggedInEvent and ServerConnectionFromClientEvent and both are failed with NullPointerException while loading basic classes(like BlockPos, etc.)

I have literally no ideaย to fix this error.

Is there any solutions? (or reference mods for more information...)

  • Author
public class PlayerEventHandler {
    @SubscribeEvent
    public void onPlayerLoggedIn(PlayerEvent.PlayerLoggedInEvent event) {
        PacketHandler.INSTANCE.sendTo(new PacketMemoClient(true, WSDMemo.get(event.player.world).getMemos()), (EntityPlayerMP) event.player);
    }
}
public MemoItem[] getMemos() {
        Set<String> keySet = memos.getKeySet();
        MemoItem[] items = new MemoItem[keySet.size()];
        for (String key: keySet) {
            BlockPos pos = BlockPos.fromLong(Long.getLong(key)); // Crashed Line
            NBTTagCompound memo = memos.getCompoundTag(key);
            String text = memo.getString("text");
            int color = memo.getInteger("color");
            MemoItem item = new MemoItem(pos, text, color, false);
        }
        return items;
    }

getMemos() is the crashed function.

And another question, the error was also appeared on client thread. In my knowledge, PlayerLoggedInEvent is only fired on server. Am Iย right?

  • Author

Afterย reading Actually Additions' world data code, I've decided to completely rewrite my WorldSavedData and packet code. Thanks for help!

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.