From 50cb3117f47c785ec4d80a9e0e81e9473a62ecf2 Mon Sep 17 00:00:00 2001 From: Unknown Date: Sat, 24 Aug 2019 16:20:58 +0200 Subject: [PATCH] Added a breathing grace period after exiting breathable area or dying --- .../java/cr0s/warpdrive/BreathingManager.java | 27 +++++++++++++------ .../cr0s/warpdrive/event/LivingHandler.java | 10 +++++++ 2 files changed, 29 insertions(+), 8 deletions(-) diff --git a/src/main/java/cr0s/warpdrive/BreathingManager.java b/src/main/java/cr0s/warpdrive/BreathingManager.java index de561a6f..2b6e7f21 100644 --- a/src/main/java/cr0s/warpdrive/BreathingManager.java +++ b/src/main/java/cr0s/warpdrive/BreathingManager.java @@ -112,9 +112,8 @@ public class BreathingManager { } final boolean notInVacuum = vAirBlock != null; - Integer air; + Integer air = entity_airBlock.get(uuidEntity); if (notInVacuum) {// no atmosphere with air blocks - air = entity_airBlock.get(uuidEntity); if (air == null) { entity_airBlock.put(uuidEntity, AIR_BLOCK_TICKS); } else if (air <= 1) {// time elapsed => consume air block @@ -124,15 +123,23 @@ public class BreathingManager { } } else {// no atmosphere without air blocks - // finish air from blocks - air = entity_airBlock.get(uuidEntity); - if (air != null && air > 0) { - entity_airBlock.put(uuidEntity, air - 1); - return; - } else if (air == null) { + // add grace period on first breath + if (air == null) { entity_airBlock.put(uuidEntity, AIR_FIRST_BREATH_TICKS); return; } + // players have a grace period when exiting breathable area + // others just finish air from blocks + if (air > 0) { + if (entityLivingBase instanceof EntityPlayerMP) { + entity_airBlock.put(uuidEntity, 0); + player_airTank.put(uuidEntity, AIR_FIRST_BREATH_TICKS); + return; + } else { + entity_airBlock.put(uuidEntity, air - 1); + return; + } + } // damage entity if in vacuum without protection final boolean hasValidSetup = hasValidSetup(entityLivingBase); @@ -460,4 +467,8 @@ public class BreathingManager { // first air breath is free return airCanister.getAirTicksPerConsumption(itemStackAirCanister); } + + public static void onEntityLivingDeath(@Nonnull final EntityLivingBase entityLivingBase) { + entity_airBlock.remove(entityLivingBase.getUniqueID()); + } } diff --git a/src/main/java/cr0s/warpdrive/event/LivingHandler.java b/src/main/java/cr0s/warpdrive/event/LivingHandler.java index b0a3aae9..68262b4f 100644 --- a/src/main/java/cr0s/warpdrive/event/LivingHandler.java +++ b/src/main/java/cr0s/warpdrive/event/LivingHandler.java @@ -34,8 +34,10 @@ import net.minecraft.world.WorldServer; import net.minecraftforge.common.DimensionManager; import net.minecraftforge.event.entity.living.EnderTeleportEvent; +import net.minecraftforge.event.entity.living.LivingDeathEvent; import net.minecraftforge.event.entity.living.LivingEvent.LivingUpdateEvent; import net.minecraftforge.event.entity.living.LivingFallEvent; +import net.minecraftforge.fml.common.eventhandler.EventPriority; import net.minecraftforge.fml.common.eventhandler.SubscribeEvent; public class LivingHandler { @@ -348,4 +350,12 @@ public class LivingHandler { } } } + + @SubscribeEvent(priority = EventPriority.LOWEST) + void onLivingDeath(@Nonnull final LivingDeathEvent event) { + final EntityLivingBase entityLivingBase = event.getEntityLiving(); + if (entityLivingBase != null) { + BreathingManager.onEntityLivingDeath(entityLivingBase); + } + } }