Jump to content

[1.15.1]How to sync my custom capability from server to client ?


Recommended Posts

Posted

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!

Posted

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);
                        });
        }
    }

 

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

 

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

Posted (edited)
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 by egod
add some info
Posted
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!

Posted (edited)
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 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.

 

 

Posted
2 minutes ago, DavidM said:

That would still make the client class load commonly.

 

P.S. 看了好久才发现是“修仙”...

but server no more crash...?

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

 

Posted (edited)
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 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.

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



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • When I first heard about Bitcoin back in 2018, I was skeptical. The idea of a decentralized, digital currency seemed too good to be true. But I was intrigued as I learned more about the technology behind it and its potential. I started small, investing just a few hundred dollars, dipping my toes into the cryptocurrency waters. At first, it was exhilarating to watch the value of my investment grow exponentially. I felt like I was part of the future, an early adopter of this revolutionary new asset. But that euphoria was short-lived. One day, I logged into my digital wallet only to find it empty - my Bitcoin had vanished without a trace. It turned out that the online exchange I had trusted had been hacked, and my funds were stolen. I was devastated, both financially and emotionally. All the potential I had seen in Bitcoin was tainted by the harsh reality that with decentralization came a lack of regulation and oversight. My hard-earned money was gone, lost to the ether of the digital world. This experience taught me a painful lesson about the price of trust in the uncharted territory of cryptocurrency. While the technology holds incredible promise, the risks can be catastrophic if you don't approach it with extreme caution. My Bitcoin investment gamble had failed, and I was left to pick up the pieces, wiser but poorer for having placed my faith in the wrong hands. My sincere appreciation goes to MUYERN TRUST HACKER. You are my hero in recovering my lost funds. Send a direct m a i l ( muyerntrusted ( @ ) mail-me ( . )c o m ) or message on whats app : + 1 ( 4-4-0 ) ( 3 -3 -5 ) ( 0-2-0-5 )
    • You could try posting a log (if there is no log at all, it may be the launcher you are using, the FAQ may have info on how to enable the log) as described in the FAQ, however this will probably need to be reported to/remedied by the mod author.
    • So me and a couple of friends are playing with a shitpost mod pack and one of the mods in the pack is corail tombstone and for some reason there is a problem with it, where on death to fire the player will get kicked out of the server and the tombstone will not spawn basically deleting an entire inventory, it doesn't matter what type of fire it is, whether it's from vanilla fire/lava, or from modded fire like ice&fire/lycanites and it's common enough to where everyone on the server has experienced at least once or twice and it doesn't give any crash log. a solution to this would be much appreciated thank you!
    • It is 1.12.2 - I have no idea if there is a 1.12 pack
  • Topics

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.