Posted September 14, 20187 yr 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?
September 14, 20187 yr 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.
September 14, 20187 yr 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 September 14, 20187 yr by XiNiHa
September 14, 20187 yr Author Oh, sorry. I just realized that mc.getIntegratedServer() only works on singleplayer mode. ;(
September 14, 20187 yr 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.
September 14, 20187 yr 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?
September 14, 20187 yr 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.
September 19, 20187 yr 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...)
September 19, 20187 yr 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?
September 19, 20187 yr 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.