Jump to content

Recommended Posts

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?

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

  • Like 1

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.

Posted (edited)

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

Posted
3 minutes ago, Animefan8888 said:

you must use Minecraft.getMInecraft().addScheduledTask

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

Posted
1 hour ago, XiNiHa said:

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

Should be Minecraft.getMinecraft().addScheduledTask

 

  • Like 1

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.

Posted

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

Posted
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?

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.