ActuallyAdditions/src/main/java/de/ellpeck/actuallyadditions/mod/items/ItemPlayerProbe.java

119 lines
4.9 KiB
Java
Raw Normal View History

/*
* This file ("ItemPlayerProbe.java") is part of the Actually Additions mod for Minecraft.
* It is created and owned by Ellpeck and distributed
* under the Actually Additions License to be found at
* http://ellpeck.de/actaddlicense
* View the source code at https://github.com/Ellpeck/ActuallyAdditions
*
2017-01-01 16:23:26 +01:00
* © 2015-2017 Ellpeck
*/
package de.ellpeck.actuallyadditions.mod.items;
2018-05-10 11:38:58 +02:00
import de.ellpeck.actuallyadditions.mod.ActuallyAdditions;
import de.ellpeck.actuallyadditions.mod.items.base.ItemBase;
import de.ellpeck.actuallyadditions.mod.tile.TileEntityPlayerInterface;
import de.ellpeck.actuallyadditions.mod.util.StackUtil;
2024-03-02 21:23:08 +01:00
import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.chat.Component;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.TooltipFlag;
import net.minecraft.world.item.context.UseOnContext;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.entity.BlockEntity;
2024-03-04 20:21:48 +01:00
import net.neoforged.api.distmarker.Dist;
import net.neoforged.api.distmarker.OnlyIn;
2021-02-26 22:15:48 +01:00
import javax.annotation.Nullable;
import java.util.List;
import java.util.UUID;
2019-05-02 09:10:29 +02:00
public class ItemPlayerProbe extends ItemBase {
public ItemPlayerProbe() {
super(ActuallyItems.defaultProps().stacksTo(1));
}
// TODO: [port] might be the wrong event
@Override
2024-03-02 21:23:08 +01:00
public void inventoryTick(ItemStack stack, Level world, Entity entity, int itemSlot, boolean isSelected) {
if (!world.isClientSide) {
2024-03-02 21:23:08 +01:00
CompoundTag compound = stack.getOrCreateTag();
if (compound.contains("UUID")) {
UUID id = compound.getUUID("UUID");
2024-03-02 21:23:08 +01:00
Player player = world.getPlayerByUUID(id);
if (player != null) {
if (player.isShiftKeyDown()) {
ItemPhantomConnector.clearStorage(stack, "UUID", "Name");
2024-03-03 01:20:53 +01:00
((Player) entity).displayClientMessage(Component.translatable("tooltip." + ActuallyAdditions.MODID + ".playerProbe.disconnect.1"), false);
player.displayClientMessage(Component.translatable("tooltip." + ActuallyAdditions.MODID + ".playerProbe.notice"), false);
//TheAchievements.GET_UNPROBED.get(player);
}
} else {
ItemPhantomConnector.clearStorage(stack, "UUID", "Name");
2024-03-03 01:20:53 +01:00
((Player) entity).displayClientMessage(Component.translatable("tooltip." + ActuallyAdditions.MODID + ".playerProbe.disconnect.2"), false);
}
}
}
}
@Override
2024-03-02 21:23:08 +01:00
public InteractionResult useOn(UseOnContext context) {
Player player = context.getPlayer();
if (player == null) {
2024-03-02 21:23:08 +01:00
return InteractionResult.FAIL;
}
ItemStack stack = player.getItemInHand(context.getHand());
2024-03-02 21:23:08 +01:00
BlockEntity tile = context.getLevel().getBlockEntity(context.getClickedPos());
if (tile instanceof TileEntityPlayerInterface face) {
2024-03-02 21:23:08 +01:00
CompoundTag compound = stack.getOrCreateTag();
if (compound.contains("UUID")) {
if (!context.getLevel().isClientSide) {
face.connectedPlayer = compound.getUUID("UUID");
face.playerName = compound.getString("Name");
face.setChanged();
face.sendUpdate();
ItemPhantomConnector.clearStorage(stack, "UUID", "Name");
}
2024-03-02 21:23:08 +01:00
return InteractionResult.SUCCESS;
}
}
2024-03-02 21:23:08 +01:00
return InteractionResult.FAIL;
}
@Override
2024-03-02 21:23:08 +01:00
public InteractionResult interactLivingEntity(ItemStack aStack, Player player, LivingEntity entity, InteractionHand hand) {
2024-03-03 01:20:53 +01:00
if (!player.level().isClientSide) {
ItemStack stack = player.getMainHandItem();
2019-05-02 09:10:29 +02:00
if (StackUtil.isValid(stack) && stack.getItem() == this) {
2024-03-03 01:20:53 +01:00
if (entity instanceof Player playerHit) {
2024-03-03 01:20:53 +01:00
if (!playerHit.isShiftKeyDown()) {
2024-03-02 21:23:08 +01:00
CompoundTag compound = stack.getOrCreateTag();
compound.putString("Name", playerHit.getName().getString());
compound.putUUID("UUID", playerHit.getUUID());
2024-03-02 21:23:08 +01:00
return InteractionResult.SUCCESS;
}
}
}
}
2024-03-02 21:23:08 +01:00
return InteractionResult.FAIL;
}
2021-12-30 18:30:01 +01:00
@OnlyIn(Dist.CLIENT)
@Override
2024-03-02 21:23:08 +01:00
public void appendHoverText(ItemStack stack, @Nullable Level playerIn, List<Component> tooltip, TooltipFlag advanced) {
if (stack.getOrCreateTag().contains("Name")) {
String name = stack.getOrCreateTag().getString("Name");
2024-03-03 01:20:53 +01:00
tooltip.add(Component.translatable("tooltip." + ActuallyAdditions.MODID + ".playerProbe.probing").append(": " + name));
}
}
}