package net.magmabits.echoing_depths.item; import net.minecraft.client.item.TooltipContext; import net.minecraft.entity.Entity; import net.minecraft.entity.EntityType; import net.minecraft.entity.LivingEntity; import net.minecraft.entity.mob.WardenEntity; import net.minecraft.entity.player.PlayerEntity; import net.minecraft.item.*; import net.minecraft.particle.ParticleTypes; import net.minecraft.sound.SoundCategory; import net.minecraft.sound.SoundEvents; import net.minecraft.text.Text; import net.minecraft.util.Hand; import net.minecraft.util.TypedActionResult; import net.minecraft.util.math.Box; import net.minecraft.world.World; import java.util.List; public class FinalProtectorItem extends Item { // Define cooldown time in ticks (20 ticks = 1 second) private static final int COOLDOWN_TICKS = 20 * 60; // 60 seconds cooldown // Define last time the ability was used private long lastUsedTime; private Entity player; private LivingEntity lastHit; public FinalProtectorItem(Settings settings) { super(settings); this.lastUsedTime = 0; this.player = null; this.lastHit = null; } public void appendTooltip(ItemStack itemStack, World world, List tooltip, TooltipContext tooltipContext) { tooltip.add(Text.translatable("item.echoing-depths.final_protector.tooltip")); } public TypedActionResult use(World world, PlayerEntity user, Hand hand) { if (!world.isClient) { this.player = user; if (lastHit == null) { world.playSound(null, user.getX(), user.getY(), user.getZ(), SoundEvents.BLOCK_BEACON_DEACTIVATE, SoundCategory.PLAYERS, 1.0f, 1.0f); return TypedActionResult.fail(user.getStackInHand(hand)); }; WardenEntity protector = EntityType.WARDEN.create(world); if(protector == null) { world.playSound(null, user.getX(), user.getY(), user.getZ(), SoundEvents.BLOCK_BEACON_DEACTIVATE, SoundCategory.PLAYERS, 1.0f, 1.0f); return TypedActionResult.fail(user.getStackInHand(hand)); } protector.refreshPositionAndAngles(user.getBlockPos(), user.getYaw(), user.getPitch()); protector.setAiDisabled(false); protector.setTarget(this.lastHit); protector.increaseAngerAt(this.lastHit, 1, true); world.spawnEntity(protector); user.getItemCooldownManager().set(this, COOLDOWN_TICKS); return TypedActionResult.success(user.getStackInHand(hand), true); } world.playSound(null, user.getX(), user.getY(), user.getZ(), SoundEvents.BLOCK_BEACON_DEACTIVATE, SoundCategory.PLAYERS, 1.0f, 1.0f); return TypedActionResult.fail(user.getStackInHand(hand)); } @Override public boolean postHit(ItemStack stack, LivingEntity target, LivingEntity attacker) { if(attacker == this.player && this.player != null) this.lastHit = target; return super.postHit(stack, target, attacker); } }