CreateMod/src/main/java/com/simibubi/create/content/curiosities/armor/DivingHelmetItem.java
2023-03-07 14:46:08 -08:00

78 lines
2.3 KiB
Java

package com.simibubi.create.content.curiosities.armor;
import com.simibubi.create.AllItems;
import com.simibubi.create.foundation.advancement.AllAdvancements;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.effect.MobEffectInstance;
import net.minecraft.world.effect.MobEffects;
import net.minecraft.world.entity.EquipmentSlot;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.Level;
import net.minecraftforge.common.ForgeMod;
import net.minecraftforge.event.entity.living.LivingEvent.LivingTickEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod.EventBusSubscriber;
@EventBusSubscriber
public class DivingHelmetItem extends CopperArmorItem {
public DivingHelmetItem(Properties p_i48534_3_) {
super(EquipmentSlot.HEAD, p_i48534_3_);
}
@SubscribeEvent
public static void breatheUnderwater(LivingTickEvent event) {
LivingEntity entity = event.getEntity();
Level world = entity.level;
boolean second = world.getGameTime() % 20 == 0;
boolean drowning = entity.getAirSupply() == 0;
if (world.isClientSide)
entity.getPersistentData()
.remove("VisualBacktankAir");
if (!AllItems.DIVING_HELMET.get()
.isWornBy(entity))
return;
boolean lavaDiving = entity.isEyeInFluidType(ForgeMod.LAVA_TYPE.get());
if (!entity.canDrownInFluidType(entity.getEyeInFluidType()) && !lavaDiving)
return;
if (entity instanceof Player && ((Player) entity).isCreative())
return;
ItemStack backtank = BackTankUtil.get(entity);
if (backtank.isEmpty())
return;
if (!BackTankUtil.hasAirRemaining(backtank))
return;
if (lavaDiving) {
if (entity instanceof ServerPlayer sp)
AllAdvancements.DIVING_SUIT_LAVA.awardTo(sp);
return;
}
if (drowning)
entity.setAirSupply(10);
if (world.isClientSide)
entity.getPersistentData()
.putInt("VisualBacktankAir", (int) BackTankUtil.getAir(backtank));
if (!second)
return;
if (entity instanceof ServerPlayer sp)
AllAdvancements.DIVING_SUIT.awardTo(sp);
entity.setAirSupply(Math.min(entity.getMaxAirSupply(), entity.getAirSupply() + 10));
entity.addEffect(new MobEffectInstance(MobEffects.WATER_BREATHING, 30, 0, true, false, true));
BackTankUtil.consumeAir(entity, backtank, 1);
}
}