Jump to content

Recommended Posts

Posted (edited)

I have a capability which gets attached to the player, and yea it stays the same even when you go to another world

Here are all the events todo with refreshing the capability

Spoiler
@SubscribeEvent
    public static void playerLoggedIn(PlayerEvent.PlayerLoggedInEvent event)
    {
        ServerPlayerEntity player = (ServerPlayerEntity) event.getPlayer();
        PacketHandler.INSTANCE.send(PacketDistributor.PLAYER.with(() -> player), new MessageStatus(
                player.getCapability(StatusProvider.STATUS_CAP, Status.capSide).orElseThrow(ArithmeticException::new)));
    }

    @SubscribeEvent
    public static void playerDeath(LivingDeathEvent event)
    {
        if(!event.getEntity().getCommandSenderWorld().isClientSide)
        {
            if (event.getEntity() instanceof PlayerEntity)
            {
                ServerPlayerEntity player = (ServerPlayerEntity) event.getEntity();
                PacketHandler.INSTANCE.send(PacketDistributor.PLAYER.with(() -> player), new MessageStatus(
                        player.getCapability(StatusProvider.STATUS_CAP, Status.capSide).orElseThrow(ArithmeticException::new)));
            }
        }
    }

Here are the packets

Spoiler
public class MessageStatus
{
    public IStatus status;

    public static void handle(MessageStatus msg, Supplier<NetworkEvent.Context> ctx)
    {
        ctx.get().enqueueWork(() ->
                DistExecutor.unsafeRunWhenOn(Dist.CLIENT, () -> () -> ClientPacketHandler.handleStatus(msg, ctx)));
        ctx.get().setPacketHandled(true);
    }

    public static MessageStatus decode(PacketBuffer buf)
    {
        IStatus status = new Status();

        status.setFamilia(buf.readUtf());
        status.setArray(buf.readVarIntArray());

        return new MessageStatus(status);
    }

    public static void encode(MessageStatus message, PacketBuffer buf)
    {
        buf.writeUtf(message.status.getFamilia());
        buf.writeVarIntArray(message.status.getArray());
    }

    public MessageStatus(IStatus status)
    {
        this.status = status;
    }

    public MessageStatus()
    {
        status = new Status();
    }
}

 

Spoiler
public class MessageClientStatus
{
    public IStatus status;

    public static void handle(MessageClientStatus msg, Supplier<NetworkEvent.Context> ctx)
    {
        ctx.get().enqueueWork(() ->
        {
            ServerPlayerEntity sender = ctx.get().getSender();

            if(sender == null) return;

            IStatus status = sender.getCapability(StatusProvider.STATUS_CAP, Status.capSide)
                    .orElseThrow(ArithmeticException::new);

            if(status.getLevel() != msg.status.getLevel()) sender.awardStat(Stats.LEVEL);

            status.setArray(msg.status.getArray());
        });
        ctx.get().setPacketHandled(true);
    }

    public static MessageClientStatus decode(PacketBuffer buf)
    {
        IStatus status = new Status();

        status.setFamilia(buf.readUtf());
        status.setArray(buf.readVarIntArray());

        return new MessageClientStatus(status);
    }

    public static void encode(MessageClientStatus message, PacketBuffer buf)
    {
        buf.writeUtf(message.status.getFamilia());
        buf.writeVarIntArray(message.status.getArray());
    }

    public MessageClientStatus(IStatus status)
    {
        this.status = status;
    }

    public MessageClientStatus()
    {
        status = new Status();
    }
}

 

And here's the rest of the source code https://github.com/ImNotJahan/danmachi-mod/tree/master/v1.16.5/src/main/java/imnotjahan/mod/danmachi

Edited by ImNotJahan
Removed code that was placed twice
Posted

You need to listen for the PlayerEvent.Clone event, which is fired upon changing dimensions, because entities don't actually move between dimensions. They're destroyed and recreated on the other side.

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Posted
2 hours ago, diesieben07 said:

What a strange choice of exception to throw here.

Some of the code was made at a time I didn't know how to make exceptions so back then I just used ArithmeticExceptions for capabilities. I do got an exception for that now tho so I should probably change it so thanks for pointing that out.

 

I did fix it and the solution was a large mix of things so I'm not exactly sure what fixed it, but I know a few of the things I needed to do were this:

  1. private static final LazyOptional<IStatus> lazyStatus = LazyOptional.of(Status::new);
      This line in my capability provider was static which was what was causing it to transfer over worlds, making it not static however made it stop saving.
  2. To fix that I had to change the parameters in both my readNBT and writeNBT calls from
    STATUS_CAP, this.instance, Status.capSide, nbt
    to 
    STATUS_CAP, this.lazyStatus.orElseThrow(MissingStatus::new), Status.capSide, nbt

    because instance was a variable I for some reason created, which was never used other than in these functions, that was the default value of the capability

  3. After that I just had to fix up networking a bunch to get everything completely working, and the only problem there is I had no way to discern the packets being sent at the time, but yea fixing that got them all working correctly

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.