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

126 lines
6.2 KiB
Java
Raw Normal View History

2020-04-04 02:32:34 +00:00
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;
2020-06-28 16:38:49 +00:00
import net.minecraft.datafixer.NbtOps;
2020-04-04 02:32:34 +00:00
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;
2020-04-04 02:32:34 +00:00
import net.minecraft.text.LiteralText;
2020-06-28 16:38:49 +00:00
import net.minecraft.text.MutableText;
2020-04-04 02:32:34 +00:00
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;
2020-06-28 16:38:49 +00:00
import net.minecraft.util.registry.RegistryKey;
2020-04-04 02:32:34 +00:00
import net.minecraft.world.World;
import java.util.List;
2020-06-28 16:38:49 +00:00
import java.util.UUID;
2020-04-04 02:32:34 +00:00
public class TargetedEnderPearlItem extends Item {
public TargetedEnderPearlItem() {
super(new Settings().rarity(Rarity.UNCOMMON).group(RelicCraft.ITEM_GROUP).maxCount(16));
2020-04-04 02:32:34 +00:00
}
@Override
2020-06-28 16:38:49 +00:00
public boolean hasGlint(ItemStack stack) {
2020-04-04 02:32:34 +00:00
return true;
}
@Override
public TypedActionResult<ItemStack> use(World world, PlayerEntity user, Hand hand) {
ItemStack stack = user.getStackInHand(hand);
user.getItemCooldownManager().set(this, 20);
2020-04-04 02:32:34 +00:00
if (!world.isClient()) {
2020-04-15 22:53:31 +00:00
user.incrementStat(Stats.USED.getOrCreateStat(this));
2020-04-04 02:32:34 +00:00
CompoundTag tag = stack.getTag();
if (tag == null) {
2020-04-06 17:20:15 +00:00
return new TypedActionResult<>(ActionResult.FAIL, stack);
2020-04-04 02:32:34 +00:00
}
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);
2020-06-28 16:38:49 +00:00
RegistryKey<World> dimension = World.CODEC.parse(NbtOps.INSTANCE, tag.get("TargetDimension")).result().orElse(World.OVERWORLD);
2020-04-04 02:32:34 +00:00
2020-06-28 16:38:49 +00:00
if (user.getEntityWorld().getRegistryKey() != dimension || dimension == null) {
user.sendMessage(new TranslatableText("chat." + RelicCraft.NAMESPACE + ".teleportation_beacon_in_different_dimension"), false);
2020-04-06 17:20:15 +00:00
return new TypedActionResult<>(ActionResult.FAIL, stack);
} else if (((TeleportationRestrictorBlock.TeleportingEntity) user).cannotTeleport()) {
2020-06-28 16:38:49 +00:00
user.sendMessage(new TranslatableText("chat." + RelicCraft.NAMESPACE + ".teleportation_beacon_restricted"), false);
2020-04-06 17:20:15 +00:00
return new TypedActionResult<>(ActionResult.FAIL, stack);
} else if (world.getBlockState(target).getBlock() != RelicCraft.TELEPORTATION_BEACON_BLOCK || !world.getBlockState(target).get(AbstractDragonEggHolderBlock.ACTIVE)) {
2020-06-28 16:38:49 +00:00
user.sendMessage(new TranslatableText("chat." + RelicCraft.NAMESPACE + ".missing_teleportation_beacon"), false);
2020-04-06 17:20:15 +00:00
return new TypedActionResult<>(ActionResult.FAIL, stack);
2020-04-04 02:32:34 +00:00
} else {
2020-04-06 17:20:15 +00:00
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);
2020-04-04 02:32:34 +00:00
2020-04-06 17:20:15 +00:00
RelicCraft.USE_TARGETED_ENDER_PEARL.trigger((ServerPlayerEntity) user);
2020-04-04 02:32:34 +00:00
2020-04-06 17:20:15 +00:00
user.damage(DamageSource.FALL, 16.0f);
2020-04-04 02:32:34 +00:00
2020-04-06 17:20:15 +00:00
user.fallDistance = 0f;
2020-04-04 20:45:56 +00:00
2020-04-06 17:20:15 +00:00
if (!user.isCreative()) {
stack.decrement(1);
2020-04-04 02:32:34 +00:00
}
2020-04-06 17:20:15 +00:00
} else {
2020-06-28 16:38:49 +00:00
user.sendSystemMessage(new TranslatableText("chat." + RelicCraft.NAMESPACE + ".teleportation_beacon_obstructed"), UUID.randomUUID());
2020-04-06 17:20:15 +00:00
return new TypedActionResult<>(ActionResult.FAIL, stack);
2020-04-04 02:32:34 +00:00
}
}
}
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) {
2020-04-06 17:20:15 +00:00
return;
2020-04-04 02:32:34 +00:00
}
2020-04-06 17:20:15 +00:00
2020-04-04 02:32:34 +00:00
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();
2020-06-28 16:38:49 +00:00
MutableText dimensionText;
2020-04-04 02:32:34 +00:00
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));
}
}