This repository has been archived on 2023-11-26. You can view files and clone it, but cannot push or open issues or pull requests.
RelicCraft/src/main/java/com/thebrokenrail/reliccraft/item/TargetedEnderPearlItem.java

117 lines
5.6 KiB
Java

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.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.text.LiteralText;
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.world.World;
import net.minecraft.world.dimension.DimensionType;
import java.util.List;
public class TargetedEnderPearlItem extends Item {
public TargetedEnderPearlItem() {
super(new Settings().rarity(Rarity.UNCOMMON).maxCount(16));
}
@Override
public boolean hasEnchantmentGlint(ItemStack stack) {
return true;
}
@Override
public TypedActionResult<ItemStack> use(World world, PlayerEntity user, Hand hand) {
ItemStack stack = user.getStackInHand(hand);
if (!world.isClient()) {
RelicCraft.playRelicSound(user);
CompoundTag tag = stack.getTag();
if (tag == null) {
tag = new CompoundTag();
}
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);
DimensionType dimension = DimensionType.byId(new Identifier(tag.getString("TargetDimension")));
if (user.dimension != dimension || dimension == null) {
user.sendMessage(new TranslatableText("chat." + RelicCraft.NAMESPACE + ".teleportation_beacon_in_different_dimension"));
} else {
if (((TeleportationRestrictorBlock.TeleportingEntity) user).cannotTeleport()) {
user.sendMessage(new TranslatableText("chat." + RelicCraft.NAMESPACE + ".teleportation_beacon_restricted"));
} 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"));
} 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;
} else {
user.sendMessage(new TranslatableText("chat." + RelicCraft.NAMESPACE + ".teleportation_beacon_obstructed"));
}
}
}
}
if (!user.isCreative()) {
stack.decrement(1);
}
return new TypedActionResult<>(ActionResult.SUCCESS, stack);
}
@Override
@Environment(EnvType.CLIENT)
public void appendTooltip(ItemStack stack, World world, List<Text> tooltip, TooltipContext context) {
super.appendTooltip(stack, world, tooltip, context);
CompoundTag tag = stack.getTag();
if (tag == null) {
tag = new CompoundTag();
}
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();
Text 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));
}
}