package com.thebrokenrail.reliccraft.item; import com.thebrokenrail.reliccraft.RelicCraft; import com.thebrokenrail.reliccraft.block.AbstractDragonEggHolderBlock; import com.thebrokenrail.reliccraft.block.TeleportationRestrictorBlock; import net.fabricmc.api.EnvType; import net.fabricmc.api.Environment; import net.minecraft.client.item.TooltipContext; import net.minecraft.datafixer.NbtOps; import net.minecraft.entity.damage.DamageSource; import net.minecraft.entity.player.PlayerEntity; import net.minecraft.item.Item; import net.minecraft.item.ItemStack; import net.minecraft.nbt.CompoundTag; import net.minecraft.server.network.ServerPlayerEntity; import net.minecraft.sound.SoundCategory; import net.minecraft.sound.SoundEvents; import net.minecraft.stat.Stats; import net.minecraft.text.LiteralText; import net.minecraft.text.MutableText; import net.minecraft.text.Text; import net.minecraft.text.TranslatableText; import net.minecraft.util.ActionResult; import net.minecraft.util.Formatting; import net.minecraft.util.Hand; import net.minecraft.util.Identifier; import net.minecraft.util.Language; import net.minecraft.util.Rarity; import net.minecraft.util.TypedActionResult; import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.Vec3d; import net.minecraft.util.registry.RegistryKey; import net.minecraft.world.World; import java.util.List; import java.util.UUID; public class TargetedEnderPearlItem extends Item { public TargetedEnderPearlItem() { super(new Settings().rarity(Rarity.UNCOMMON).group(RelicCraft.ITEM_GROUP).maxCount(16)); } @Override public boolean hasGlint(ItemStack stack) { return true; } @Override public TypedActionResult use(World world, PlayerEntity user, Hand hand) { ItemStack stack = user.getStackInHand(hand); user.getItemCooldownManager().set(this, 20); if (!world.isClient()) { user.incrementStat(Stats.USED.getOrCreateStat(this)); CompoundTag tag = stack.getTag(); if (tag == null) { return new TypedActionResult<>(ActionResult.FAIL, stack); } BlockPos target = new BlockPos(tag.getInt("TargetX"), tag.getInt("TargetY"), tag.getInt("TargetZ")); Vec3d teleportTarget = new Vec3d(target.getX() + 0.5d, target.getY() + 1.0d, target.getZ() + 0.5d); RegistryKey dimension = World.CODEC.parse(NbtOps.INSTANCE, tag.get("TargetDimension")).result().orElse(World.OVERWORLD); if (user.getEntityWorld().getRegistryKey() != dimension || dimension == null) { user.sendMessage(new TranslatableText("chat." + RelicCraft.NAMESPACE + ".teleportation_beacon_in_different_dimension"), false); return new TypedActionResult<>(ActionResult.FAIL, stack); } else if (((TeleportationRestrictorBlock.TeleportingEntity) user).cannotTeleport()) { user.sendMessage(new TranslatableText("chat." + RelicCraft.NAMESPACE + ".teleportation_beacon_restricted"), false); return new TypedActionResult<>(ActionResult.FAIL, stack); } else if (world.getBlockState(target).getBlock() != RelicCraft.TELEPORTATION_BEACON_BLOCK || !world.getBlockState(target).get(AbstractDragonEggHolderBlock.ACTIVE)) { user.sendMessage(new TranslatableText("chat." + RelicCraft.NAMESPACE + ".missing_teleportation_beacon"), false); return new TypedActionResult<>(ActionResult.FAIL, stack); } else { Vec3d oldPos = user.getPos(); if (user.teleport(teleportTarget.getX(), teleportTarget.getY(), teleportTarget.getZ(), true)) { world.playSound(null, oldPos.getX(), oldPos.getY(), oldPos.getZ(), SoundEvents.ITEM_CHORUS_FRUIT_TELEPORT, SoundCategory.PLAYERS, 1.0f, 1.0f); world.playSound(null, teleportTarget.getX(), teleportTarget.getY(), teleportTarget.getZ(), SoundEvents.ITEM_CHORUS_FRUIT_TELEPORT, SoundCategory.PLAYERS, 1.0f, 1.0f); RelicCraft.USE_TARGETED_ENDER_PEARL.trigger((ServerPlayerEntity) user); user.damage(DamageSource.FALL, 16.0f); user.fallDistance = 0f; if (!user.isCreative()) { stack.decrement(1); } } else { user.sendSystemMessage(new TranslatableText("chat." + RelicCraft.NAMESPACE + ".teleportation_beacon_obstructed"), UUID.randomUUID()); return new TypedActionResult<>(ActionResult.FAIL, stack); } } } return new TypedActionResult<>(ActionResult.SUCCESS, stack); } @Override @Environment(EnvType.CLIENT) public void appendTooltip(ItemStack stack, World world, List tooltip, TooltipContext context) { super.appendTooltip(stack, world, tooltip, context); CompoundTag tag = stack.getTag(); if (tag == null) { return; } tooltip.add(new TranslatableText("item." + RelicCraft.NAMESPACE + ".tooltip.targeted_ender_pearl.x", new LiteralText(String.valueOf(tag.getInt("TargetX"))).formatted(Formatting.GRAY)).formatted(Formatting.WHITE)); tooltip.add(new TranslatableText("item." + RelicCraft.NAMESPACE + ".tooltip.targeted_ender_pearl.y", new LiteralText(String.valueOf(tag.getInt("TargetY"))).formatted(Formatting.GRAY)).formatted(Formatting.WHITE)); tooltip.add(new TranslatableText("item." + RelicCraft.NAMESPACE + ".tooltip.targeted_ender_pearl.z", new LiteralText(String.valueOf(tag.getInt("TargetZ"))).formatted(Formatting.GRAY)).formatted(Formatting.WHITE)); Identifier id = new Identifier(tag.getString("TargetDimension")); String key = "text." + RelicCraft.NAMESPACE + ".dimension." + id.getNamespace() + '.' + id.getPath(); MutableText dimensionText; if (Language.getInstance().hasTranslation(key)) { dimensionText = new TranslatableText(key); } else { dimensionText = new LiteralText(id.toString()); } dimensionText.formatted(Formatting.GRAY); tooltip.add(new TranslatableText("item." + RelicCraft.NAMESPACE + ".tooltip.targeted_ender_pearl.dimension", dimensionText).formatted(Formatting.WHITE)); } }