Jump to content
View in the app

A better way to browse. Learn more.

Forge Forums

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (โ‹ฎ) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Featured Replies

Posted

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

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.

  • Author
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...

Important Information

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

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions โ†’ Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.