Posted January 6, 20205 yr I have create a new capability for PlayerEntity and I want to know how can I sync the data from server to client ? my Interface IXiuXianState.java package egod.mc.lingkiworld.capabilities; public interface IXiuXianState { public Realm getRealm(); public void setRealm(Realm newRealm); public float getXiuWei(); public void setXiuWei(float newValue); public void addXiuWei(float delta); public float getMagic(); public void setMagic(float value); public void healMagic(); public void costMagic(float value); public float getMaxMagic(); public void setMaxMagic(float value); public void copyForRespawn(IXiuXianState deadPlayer); } implementation of the interface XiuXianStateClass.java package egod.mc.lingkiworld.capabilities; import net.minecraft.entity.player.PlayerEntity; public class XiuXianStateClass implements IXiuXianState { private Realm realm; private float xiuWei; private float magic; private float maxMagic; public XiuXianStateClass(){ realm = Realm.FANREN; xiuWei = 0f; magic = 0f; maxMagic = 0f; } @Override public Realm getRealm() { return realm; } @Override public void setRealm(Realm newRealm) { this.realm = newRealm; } @Override public float getXiuWei() { return xiuWei; } @Override public void setXiuWei(float newValue) { this.xiuWei = newValue; } @Override public void addXiuWei(float delta) { this.xiuWei += delta; this.magic += delta; this.maxMagic += delta; } @Override public float getMagic() { return magic; } @Override public void setMagic(float value) { this.magic = value; } @Override public void healMagic() { magic += maxMagic/20f; } @Override public void costMagic(float value) { magic -= value; } @Override public float getMaxMagic() { return maxMagic; } @Override public void setMaxMagic(float value) { this.maxMagic = value; } public static IXiuXianState getFromPlayer(PlayerEntity player){ return player .getCapability(XiuXianStateProvider.XiuXianStateCap,null) .orElseThrow(()->new IllegalArgumentException("LazyOptional must be not empty!")); } @Override public void copyForRespawn(IXiuXianState deadPlayer){ this.setRealm(deadPlayer.getRealm()); this.setXiuWei(deadPlayer.getXiuWei()); this.setMagic(deadPlayer.getMaxMagic()); this.setMaxMagic(deadPlayer.getMaxMagic()); } } Storage for that XiuXianStateProvider.java package egod.mc.lingkiworld.capabilities; import net.minecraft.nbt.CompoundNBT; import net.minecraft.nbt.INBT; import net.minecraft.util.Direction; import net.minecraftforge.common.capabilities.Capability; import javax.annotation.Nullable; public class XiuXianStateStorage implements Capability.IStorage<IXiuXianState> { @Nullable @Override public INBT writeNBT(Capability<IXiuXianState> capability, IXiuXianState instance, Direction side) { CompoundNBT tag =new CompoundNBT(); tag.putString("realm", instance.getRealm().name); tag.putFloat("xiuWei", instance.getXiuWei()); tag.putFloat("magic", instance.getMagic()); tag.putFloat("maxMagic", instance.getMaxMagic()); return tag; } @Override public void readNBT(Capability<IXiuXianState> capability, IXiuXianState instance, Direction side, INBT nbt) { CompoundNBT tag = (CompoundNBT)nbt; instance.setRealm(Realm.fromName(tag.getString("realm"))); instance.setXiuWei(tag.getFloat("xiuWei")); instance.setMagic(tag.getFloat("magic")); instance.setMaxMagic(tag.getFloat("maxMagic")); } } Provider for that XiuXianStateProvider.java package egod.mc.lingkiworld.capabilities; import net.minecraft.nbt.CompoundNBT; import net.minecraft.util.Direction; import net.minecraftforge.common.capabilities.Capability; import net.minecraftforge.common.capabilities.CapabilityInject; import net.minecraftforge.common.capabilities.ICapabilitySerializable; import net.minecraftforge.common.util.LazyOptional; import javax.annotation.Nonnull; import javax.annotation.Nullable; public class XiuXianStateProvider implements ICapabilitySerializable<CompoundNBT> { @CapabilityInject(IXiuXianState.class) public static final Capability<IXiuXianState> XiuXianStateCap=null; private LazyOptional<IXiuXianState> instance = LazyOptional.of(XiuXianStateClass::new); @Nonnull @Override public <T> LazyOptional<T> getCapability(@Nonnull Capability<T> cap, @Nullable Direction side) { if(cap != XiuXianStateCap){ return LazyOptional.empty(); } return this.instance.cast(); } @Override public CompoundNBT serializeNBT() { return (CompoundNBT)XiuXianStateCap.getStorage() .writeNBT(XiuXianStateCap, instance .orElseThrow(()->new IllegalArgumentException("LazyOptional must not be empty!")), null); } @Override public void deserializeNBT(CompoundNBT nbt) { XiuXianStateCap.getStorage() .readNBT(XiuXianStateCap, instance .orElseThrow(()->new IllegalArgumentException("LazyOptional must not be empty!")), null, nbt); } } And here is my package XiuXianStateSyncMessage.java , but it does not work. package egod.mc.lingkiworld.network; import egod.mc.lingkiworld.capabilities.CapabilityLoader; import egod.mc.lingkiworld.capabilities.IXiuXianState; import net.minecraft.client.Minecraft; import net.minecraft.entity.player.PlayerEntity; import net.minecraft.nbt.CompoundNBT; import net.minecraft.network.PacketBuffer; import net.minecraftforge.common.capabilities.Capability; import net.minecraftforge.fml.network.NetworkEvent; import java.util.function.Supplier; public class XiuXianStateSyncMessage { private CompoundNBT data; XiuXianStateSyncMessage(PacketBuffer buf) { this.data = buf.readCompoundTag(); } public XiuXianStateSyncMessage(CompoundNBT nbt) { this.data = nbt; } void encode(PacketBuffer buf) { buf.writeCompoundTag(data); } void handle(Supplier<NetworkEvent.Context> context) { NetworkEvent.Context ctx = context.get(); ctx.enqueueWork(() -> { if (ctx.getDirection().getReceptionSide().isClient() && ctx.getDirection().getOriginationSide().isServer()) { PlayerEntity player = Minecraft.getInstance().player; player.getCapability(CapabilityLoader.XiuXianState, null) .ifPresent(state -> { Capability.IStorage<IXiuXianState> storage = CapabilityLoader.XiuXianState.getStorage(); storage.readNBT(CapabilityLoader.XiuXianState, state, null, data); }); } }); ctx.setPacketHandled(true); } } This is really long, but thanks if you can help me!
January 6, 20205 yr Author and here is how I try to sync @SubscribeEvent public void onPlayerTracking(PlayerEvent.StartTracking event){ if(!(event.getTarget() instanceof PlayerEntity)) return; PlayerEntity player = (PlayerEntity) event.getTarget(); ServerPlayerEntity target = (ServerPlayerEntity) event.getPlayer(); if (!player.world.isRemote()) { player.getCapability(CapabilityLoader.XiuXianState, null) .ifPresent(state -> { CompoundNBT nbt = new CompoundNBT(); Capability<IXiuXianState> cap = XiuXianStateProvider.XiuXianStateCap; Capability.IStorage<IXiuXianState> storage = cap.getStorage(); nbt.put(cap.getName(), storage.writeNBT(cap, state, null)); XiuXianStateSyncMessage message = new XiuXianStateSyncMessage(nbt); NetworkLoader.channel.send(PacketDistributor.PLAYER.with(() ->target ), message); }); } }
January 6, 20205 yr Author 2 hours ago, diesieben07 said: If you want the data to be available to the client who's player it is attached to: Send the data to the player in PlayerLoggedInEvent, PlayerRespawnEvent and PlayerChangedDimensionEvent. Send the data to the player whenever it changes. do you mean that when I want to sync data, I can post a Event above rather than a Packet?
January 6, 20205 yr 9 minutes ago, egod said: do you mean that when I want to sync data, I can post a Event above rather than a Packet? No, you will still need to use packets when the data is updated; the events are only for players logging in, respawning, or changing dimensions, because during those events, I believe the player does not have the data, and will need it resynced/copied to them in those events.
January 6, 20205 yr Author 11 minutes ago, Ugdhar said: No, you will still need to use packets when the data is updated; the events are only for players logging in, respawning, or changing dimensions, because during those events, I believe the player does not have the data, and will need it resynced/copied to them in those events. tanks for your reply, actually this works well when "Single Player" mode, but when I try to start a server and a client, the server would crash. here is my packet handle void handle(Supplier<NetworkEvent.Context> context) { NetworkEvent.Context ctx = context.get(); ctx.enqueueWork(() -> { if (ctx.getDirection().getReceptionSide().isClient() && ctx.getDirection().getOriginationSide().isServer()) { PlayerEntity player = Minecraft.getInstance().player; player.getCapability(XiuXianStateProvider.XiuXianStateCap, null) .ifPresent(state -> { Capability.IStorage<IXiuXianState> storage = XiuXianStateProvider.XiuXianStateCap.getStorage(); storage.readNBT(XiuXianStateProvider.XiuXianStateCap, state, null, data); }); } }); ctx.setPacketHandled(true); } and here is how I register it ublic class NetworkLoader { private static int id=1; private final static ResourceLocation res = new ResourceLocation(Main.MODID); public static final SimpleChannel channel = NetworkRegistry.ChannelBuilder.named(res) .clientAcceptedVersions(s-> Objects.equals(s,"1")) .serverAcceptedVersions(s -> Objects.equals(s,"1")) .networkProtocolVersion(()->"1") .simpleChannel(); public static void registerMessages(){ channel.messageBuilder(XiuXianStateSyncMessage.class,id++) .decoder(XiuXianStateSyncMessage::new) .encoder(XiuXianStateSyncMessage::encode) .consumer(XiuXianStateSyncMessage::handle) .add(); } } and the server crash-report net.minecraftforge.fml.LoadingFailedException: Loading errors encountered: [ LingKi World (lingkiworld) encountered an error during the common_setup event phase §7Attempted to load class net/minecraft/client/entity/player/ClientPlayerEntity for invalid dist DEDICATED_SERVER ] at net.minecraftforge.fml.ModLoader.dispatchAndHandleError(ModLoader.java:201) ~[?:?] {re:classloading} at net.minecraftforge.fml.ModLoader.loadMods(ModLoader.java:154) ~[?:?] {re:classloading} at net.minecraftforge.fml.server.ServerModLoader.begin(ServerModLoader.java:46) ~[?:?] {re:classloading} at net.minecraft.server.dedicated.DedicatedServer.init(DedicatedServer.java:124) ~[?:?] {re:classloading,pl:accesstransformer:B} at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:634) [?:?] {re:classloading,pl:accesstransformer:B,pl:runtimedistcleaner:A} at java.lang.Thread.run(Thread.java:748) [?:1.8.0_221] {} Edited January 6, 20205 yr by egod add some info
January 6, 20205 yr Author 6 minutes ago, diesieben07 said: You need to use DistExecutor to run side-specific code (Minecraft is a client-only class). Unfortunately it is not a completely replacement for @SidedProxy, because code that triggers classloading in the JVM verifier still explodes. In your case that is the following statement: PlayerEntity player = Minecraft.getInstance().player; Because Minecraft#player is of a different type (ClientPlayerEntity) than PlayerEntity this triggers classloading (the JVM needs to load both classes to verify they are compatible). To make the code work you need to adjust your variable player to be the same type. It works! Thank you! I modify PlayerEntity player = Minecraft.getInstance().player; to ClientPlayerEntity player = Minecraft.getInstance().player; and everything done!
January 6, 20205 yr 1 hour ago, egod said: It works! Thank you! I modify PlayerEntity player = Minecraft.getInstance().player; to ClientPlayerEntity player = Minecraft.getInstance().player; and everything done! That would still make the client class load commonly. I was mistaken. Edited January 6, 20205 yr by DavidM Some tips: Spoiler Modder Support: Spoiler 1. Do not follow tutorials on YouTube, especially TechnoVision (previously called Loremaster) and HarryTalks, due to their promotion of bad practice and usage of outdated code. 2. Always post your code. 3. Never copy and paste code. You won't learn anything from doing that. 4. Quote Programming via Eclipse's hotfixes will get you nowhere 5. Learn to use your IDE, especially the debugger. 6. Quote The "picture that's worth 1000 words" only works if there's an obvious problem or a freehand red circle around it. Support & Bug Reports: Spoiler 1. Read the EAQ before asking for help. Remember to provide the appropriate log(s). 2. Versions below 1.11 are no longer supported due to their age. Update to a modern version of Minecraft to receive support.
January 6, 20205 yr Author 2 minutes ago, DavidM said: That would still make the client class load commonly. P.S. 看了好久才发现是“修仙”... but server no more crash...?
January 6, 20205 yr 16 minutes ago, DavidM said: 看了好久才发现是“修仙” Since these forums are supposed to be in English, here's the translation according to google translate: Quote After looking at it for a long time, I found that it is "Xiuxian"
January 6, 20205 yr 13 minutes ago, Ugdhar said: Since these forums are supposed to be in English, here's the translation according to google translate: My apologies. That was a random off-topic reference. Edited January 6, 20205 yr by DavidM Some tips: Spoiler Modder Support: Spoiler 1. Do not follow tutorials on YouTube, especially TechnoVision (previously called Loremaster) and HarryTalks, due to their promotion of bad practice and usage of outdated code. 2. Always post your code. 3. Never copy and paste code. You won't learn anything from doing that. 4. Quote Programming via Eclipse's hotfixes will get you nowhere 5. Learn to use your IDE, especially the debugger. 6. Quote The "picture that's worth 1000 words" only works if there's an obvious problem or a freehand red circle around it. Support & Bug Reports: Spoiler 1. Read the EAQ before asking for help. Remember to provide the appropriate log(s). 2. Versions below 1.11 are no longer supported due to their age. Update to a modern version of Minecraft to receive support.
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.