Jump to content

[Solved] [1.15.1] How to synchronize health modifier after coming from The End?


FireController1847

Recommended Posts

I have a mod that applies a health modifier and uses a capability to keep track of it. My problem is, when I'm coming back from the end, the player's health kind of "resets" back to 20. For example, let's say I've entered the end with 30 hearts, applied by my capability. I defeat the ender dragon, and then I come back. Suddenly, I still appear to have all 30 hearts, but I only have 10 of them with health left (so it's like I took imaginary damage). Leaving and re-joining the world does not work, and I've tried both to sync the client to the server and the server to the client, but none of my efforts have worked.

Currently, I've set up my onChangedDimension event like so:

@SubscribeEvent
public static void onPlayerChangedDimension(PlayerChangedDimensionEvent event) {
    // Ensure server-side only
    if (event.getPlayer().getEntityWorld().isRemote) {
        return;
    }
    debug("PlayerChangedDimension{Event}");

    // Fetch Capability
    PlayerEntity player = event.getPlayer();
    IMoreHealth cap = MoreHealth.getFromPlayer(player);

    // Re-add health modifier
    LevelHearts.applyHealthModifier(player, cap.getTrueModifier());

    // Synchronize
    cap.synchronise(player);
}

cap.synchronize will send a packet from the client to the server.

This works great for coming from and going to the nether, as well as going to the end. But oh no, coming back FROM the end has to be different, and indeed it instead calls the PlayerRespawn event. Currently, my respawn event looks like this:
 

@SubscribeEvent
public static void onPlayerRespawn(PlayerRespawnEvent event) {
    // Ensure server-side only
    if (event.getPlayer().getEntityWorld().isRemote) {
        return;
    }
    debug("PlayerRespawn{Event}");

    // Fetch Capability
    PlayerEntity player = event.getPlayer();
    IMoreHealth cap = MoreHealth.getFromPlayer(player);

    // Handle "The End"
    if (event.isEndConquered()) {
        debug("PlayerRespawn{Event}: Coming from end, syncing!");
        // ??? What do I put here? ???
        return;
    }

    // Handle Hardcore Mode
    if (Config.enableHardcore.get()) {
        debug("PlayerRespawn{Event}: Hardcore mode enabled, removing health.");
        cap.setModifier(MoreHealth.getDefaultModifier());
        cap.setRampPosition((short) 0);
        cap.setHeartContainers((byte) 0);
        MoreHealth.updateClient((ServerPlayerEntity) player, cap);
    }

    // Handle force lose xp on death
    if (Config.loseXpOnDeath.get()) {
        player.addExperienceLevel(-player.experienceLevel - 1);
    }

    // Re-add health modifier and fill health
    LevelHearts.applyHealthModifier(player, cap.getTrueModifier());
    player.setHealth(player.getMaxHealth());
}


When the player enters the end, and inside the end, let's say they have 40 hearts. When they come back, the player appears to still have those forty hearts, but the health has reset back to 20. I have absolutely no idea why, and it means that I cannot do what I do on a dimension change to just update the client / update the server (for other situations). So... what do I do to have the player keep their health when coming back from the end?

I am on my journey of making a remake of matmos, as explained here.

Link to comment
Share on other sites

  • FireController1847 changed the title to [1.15.1] How to synchronize health modifier after coming from The End?
  • FireController1847 changed the title to [Solved] [1.15.1] How to synchronize health modifier after coming from The End?

By doing a little bit more digging, now that I'm not really tired today haha, I found that the PlayerRespawn event (when the player dies) actually also calls the PlayerClone event, helping me discover there was an option in PlayerEvent.Clone called "isWasDeath."

 

By using this event, I was able to determine if the player died, and re-apply my health modifier appropriately, which allowed me to set the playerNew's health to the same as the playerOld's health, thereby keeping their health across dimension change. Here's my fixed code:

@SubscribeEvent
public static void onPlayerClone(PlayerEvent.Clone event) {
    debug("PlayerClone{Event}");

    // Fetch & Copy Capability
    PlayerEntity playerOld = event.getOriginal();
    PlayerEntity playerNew = event.getPlayer();
    IMoreHealth capOld = MoreHealth.getFromPlayer(playerOld);
    IMoreHealth capNew = MoreHealth.getFromPlayer(playerNew);
    capNew.copy(capOld);
    
    // Copy Health on Dimension Change
    if (!event.isWasDeath()) {
    	debug("PlayerClone{Event}: Cloning from dimension change, re-applying health modifier.");
    	LevelHearts.applyHealthModifier(playerNew, capNew.getTrueModifier());
    	playerNew.setHealth(playerOld.getHealth());
    }
}

@SubscribeEvent
public static void onPlayerRespawn(PlayerRespawnEvent event) {
    // Ensure server-side only
    if (event.getPlayer().getEntityWorld().isRemote) {
        return;
    }
    debug("PlayerRespawn{Event}");

    // Fetch Capability
    PlayerEntity player = event.getPlayer();
    IMoreHealth cap = MoreHealth.getFromPlayer(player);

    // Handle "The End"
    if (event.isEndConquered()) {
        debug("PlayerRespawn{Event}: Coming from end, syncing!");
        MoreHealth.updateClient((ServerPlayerEntity) player, cap);
        return;
    }

    // ... other stuff
}

 

  • Like 2

I am on my journey of making a remake of matmos, as explained here.

Link to comment
Share on other sites

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.