content: package com.aizistral.enigmaticlegacy.items;
  
  import java.util.List;
  
  import javax.annotation.Nullable;
  
  import com.aizistral.enigmaticlegacy.EnigmaticLegacy;
  import com.aizistral.enigmaticlegacy.crafting.BindToPlayerRecipe.IBound;
  import com.aizistral.enigmaticlegacy.helpers.ItemLoreHelper;
  import com.aizistral.enigmaticlegacy.helpers.ItemNBTHelper;
  import com.aizistral.enigmaticlegacy.items.generic.ItemBase;
  import com.aizistral.enigmaticlegacy.packets.clients.PacketPortalParticles;
  import com.aizistral.enigmaticlegacy.packets.clients.PacketRecallParticles;
  
  import net.minecraft.ChatFormatting;
  import net.minecraft.advancements.CriteriaTriggers;
  import net.minecraft.client.gui.screens.Screen;
  import net.minecraft.network.chat.Component;
  import net.minecraft.resources.ResourceLocation;
  import net.minecraft.server.level.ServerPlayer;
  import net.minecraft.sounds.SoundEvents;
  import net.minecraft.sounds.SoundSource;
  import net.minecraft.world.InteractionHand;
  import net.minecraft.world.InteractionResult;
  import net.minecraft.world.InteractionResultHolder;
  import net.minecraft.world.entity.LivingEntity;
  import net.minecraft.world.entity.player.Player;
  import net.minecraft.world.item.ItemStack;
  import net.minecraft.world.item.Items;
  import net.minecraft.world.item.Rarity;
  import net.minecraft.world.item.TooltipFlag;
  import net.minecraft.world.item.UseAnim;
  import net.minecraft.world.level.Level;
  import net.minecraft.world.phys.Vec3;
  import net.minecraftforge.api.distmarker.Dist;
  import net.minecraftforge.api.distmarker.OnlyIn;
  import net.minecraftforge.network.PacketDistributor;
  
  public class WormholePotion extends ItemBase implements IBound {
  
  	public WormholePotion() {
  		super(ItemBase.getDefaultProperties().stacksTo(1).rarity(Rarity.RARE));
  	}
  
  	@Override
  	@OnlyIn(Dist.CLIENT)
  	public void appendHoverText(ItemStack stack, @Nullable Level worldIn, List<Component> list, TooltipFlag flagIn) {
  		if (Screen.hasShiftDown()) {
  			ItemLoreHelper.addLocalizedString(list, "tooltip.enigmaticlegacy.wormholePotion1");
  			ItemLoreHelper.addLocalizedString(list, "tooltip.enigmaticlegacy.wormholePotion2");
  		} else {
  			ItemLoreHelper.addLocalizedString(list, "tooltip.enigmaticlegacy.holdShift");
  		}
  
  		if (ItemNBTHelper.verifyExistance(stack, "BoundPlayer")) {
  			ItemLoreHelper.addLocalizedString(list, "tooltip.enigmaticlegacy.void");
  			ItemLoreHelper.addLocalizedString(list, "tooltip.enigmaticlegacy.boundToPlayer", ChatFormatting.DARK_RED, ItemNBTHelper.getString(stack, "BoundPlayer", "Herobrine"));
  		}
  	}
  
  	@Override
  	public ItemStack finishUsingItem(ItemStack stack, Level worldIn, LivingEntity entityLiving) {
  		if (!(entityLiving instanceof Player))
  			return stack;
  
  		Player player = (Player) entityLiving;
  
  		if (player instanceof ServerPlayer) {
  			CriteriaTriggers.CONSUME_ITEM.trigger((ServerPlayer) player, stack);
  		}
  
  		Player receiver = this.getBoundPlayer(worldIn, stack);
  
  		if (!worldIn.isClientSide && receiver != null) {
  
  			Vec3 vec = receiver.position();
  
  			while (vec.distanceTo(receiver.position()) < 1.0D) {
  				vec = receiver.position().add((random.nextDouble() - 0.5D) * 4D, 0, (random.nextDouble() - 0.5D) * 4D);
  			}
  
  			worldIn.playSound(null, player.blockPosition(), SoundEvents.ENDERMAN_TELEPORT, SoundSource.PLAYERS, 1.0F, (float) (0.8F + (Math.random() * 0.2)));
  
  			EnigmaticLegacy.packetInstance.send(PacketDistributor.NEAR.with(() -> new PacketDistributor.TargetPoint(player.getX(), player.getY(), player.getZ(), 128, player.level().dimension())), new PacketPortalParticles(player.getX(), player.getY() + (player.getBbHeight() / 2), player.getZ(), 100, 1.25F, false));
  
  			player.teleportTo(vec.x, vec.y + 0.25, vec.z);
  			worldIn.playSound(null, player.blockPosition(), SoundEvents.ENDERMAN_TELEPORT, SoundSource.PLAYERS, 1.0F, (float) (0.8F + (Math.random() * 0.2)));
  
  			EnigmaticLegacy.packetInstance.send(PacketDistributor.NEAR.with(() -> new PacketDistributor.TargetPoint(player.getX(), player.getY(), player.getZ(), 128, player.level().dimension())), new PacketRecallParticles(player.getX(), player.getY() + (player.getBbHeight() / 2), player.getZ(), 48, false));
  		}
  
  		if (!player.getAbilities().instabuild) {
  
  			stack.shrink(1);
  
  			if (stack.isEmpty())
  				return new ItemStack(Items.GLASS_BOTTLE);
  
  			player.getInventory().add(new ItemStack(Items.GLASS_BOTTLE));
  
  		}
  
  		return stack;
  	}
  
  	@Override
  	public int getUseDuration(ItemStack stack) {
  		return 32;
  	}
  
  	@Override
  	public UseAnim getUseAnimation(ItemStack stack) {
  		return UseAnim.DRINK;
  	}
  
  	@Override
  	public InteractionResultHolder<ItemStack> use(Level world, Player player, InteractionHand hand) {
  		ItemStack stack = player.getItemInHand(hand);
  
  		Player receiver = this.getBoundPlayer(world, stack);
  
  		if (receiver != null/* && receiver != player */) {
  			player.startUsingItem(hand);
  			return new InteractionResultHolder<>(InteractionResult.SUCCESS, stack);
  		}
  
  		return new InteractionResultHolder<>(InteractionResult.FAIL, stack);
  	}
  
  	@Override
  	@OnlyIn(Dist.CLIENT)
  	public boolean isFoil(ItemStack stack) {
  		return true;
  	}
  
  }
  