diff --git a/src/main/java/micdoodle8/mods/galacticraft/core/util/OxygenUtil.java b/src/main/java/micdoodle8/mods/galacticraft/core/util/OxygenUtil.java index 3df89801c9..f0d3217d55 100644 --- a/src/main/java/micdoodle8/mods/galacticraft/core/util/OxygenUtil.java +++ b/src/main/java/micdoodle8/mods/galacticraft/core/util/OxygenUtil.java @@ -433,26 +433,64 @@ public static boolean inOxygenBubble(World worldObj, double avgX, double avgY, d * @param player The player to check and apply the effect to. */ public static void applyWaterBreathingEffect(EntityPlayerMP player) { - // Check if the player is in water - if (player.isInWater()) { - // Check if the player has valid oxygen gear setup - if (hasValidOxygenSetup(player)) { - // Apply the water breathing effect - player.addPotionEffect(new PotionEffect(Potion.waterBreathing.id, 220, 0, true)); - - // Consume oxygen - GCPlayerStats stats = GCPlayerStats.get(player); - ItemStack tankInSlot1 = stats.extendedInventory.getStackInSlot(2); - ItemStack tankInSlot2 = stats.extendedInventory.getStackInSlot(3); - - if (tankInSlot1 != null && tankInSlot1.getItem() instanceof ItemOxygenTank - && tankInSlot1.getMaxDamage() - tankInSlot1.getItemDamage() > 0) { - tankInSlot1.damageItem(1, player); - } else if (tankInSlot2 != null && tankInSlot2.getItem() instanceof ItemOxygenTank - && tankInSlot2.getMaxDamage() - tankInSlot2.getItemDamage() > 0) { - tankInSlot2.damageItem(1, player); - } - } + if (!isHeadInWater(player)) { + return; + } + + if (hasSufficientWaterBreathing(player)) { + return; + } + + if (!hasValidOxygenSetup(player)) { + return; + } + + GCPlayerStats stats = GCPlayerStats.get(player); + ItemStack tank1 = stats.extendedInventory.getStackInSlot(2); + ItemStack tank2 = stats.extendedInventory.getStackInSlot(3); + + if (!hasOxygen(tank1) && !hasOxygen(tank2)) { + return; + } + + // Apply effect + player.addPotionEffect(new PotionEffect(Potion.waterBreathing.id, 220, 0, true)); + + // Consume oxygen (prefer first tank) + if (!consumeOxygen(tank1, player)) { + consumeOxygen(tank2, player); + } + } + + private static boolean isHeadInWater(EntityPlayerMP player) { + double eyeY = player.posY + player.getEyeHeight(); + int x = MathHelper.floor_double(player.posX); + int y = MathHelper.floor_double(eyeY); + int z = MathHelper.floor_double(player.posZ); + + return player.worldObj.getBlock(x, y, z).getMaterial() == Material.water; + } + + private static boolean hasSufficientWaterBreathing(EntityPlayerMP player) { + if (!player.isPotionActive(Potion.waterBreathing)) { + return false; + } + + PotionEffect effect = player.getActivePotionEffect(Potion.waterBreathing); + return effect != null && (effect.getDuration() > 220 || effect.getAmplifier() > 0); + } + + private static boolean hasOxygen(ItemStack tank) { + return tank != null && tank.getItem() instanceof ItemOxygenTank + && tank.getMaxDamage() - tank.getItemDamage() > 0; + } + + private static boolean consumeOxygen(ItemStack tank, EntityPlayerMP player) { + if (!hasOxygen(tank)) { + return false; } + + tank.damageItem(1, player); + return true; } }