2016-07-14 02:11:41 +02:00
|
|
|
/*
|
|
|
|
* 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
|
2016-07-14 02:11:41 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
package de.ellpeck.actuallyadditions.mod.items;
|
|
|
|
|
2018-05-10 11:38:58 +02:00
|
|
|
import de.ellpeck.actuallyadditions.mod.ActuallyAdditions;
|
2016-07-14 02:11:41 +02:00
|
|
|
import de.ellpeck.actuallyadditions.mod.items.base.ItemBase;
|
|
|
|
import de.ellpeck.actuallyadditions.mod.tile.TileEntityPlayerInterface;
|
2016-11-16 20:31:16 +01:00
|
|
|
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;
|
2016-07-14 02:11:41 +02:00
|
|
|
|
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 {
|
2016-07-14 02:11:41 +02:00
|
|
|
|
2021-03-01 21:23:52 +01:00
|
|
|
public ItemPlayerProbe() {
|
2021-08-22 17:09:06 +02:00
|
|
|
super(ActuallyItems.defaultProps().stacksTo(1));
|
2016-07-14 02:11:41 +02:00
|
|
|
}
|
|
|
|
|
2021-05-04 18:47:45 +02:00
|
|
|
// TODO: [port] might be the wrong event
|
2016-07-14 02:11:41 +02:00
|
|
|
@Override
|
2024-03-02 21:23:08 +01:00
|
|
|
public void inventoryTick(ItemStack stack, Level world, Entity entity, int itemSlot, boolean isSelected) {
|
2021-08-22 17:09:06 +02:00
|
|
|
if (!world.isClientSide) {
|
2024-03-02 21:23:08 +01:00
|
|
|
CompoundTag compound = stack.getOrCreateTag();
|
2024-03-13 22:35:43 +01:00
|
|
|
if (compound.contains("UUID")) {
|
2021-08-22 17:09:06 +02:00
|
|
|
UUID id = compound.getUUID("UUID");
|
2024-03-02 21:23:08 +01:00
|
|
|
Player player = world.getPlayerByUUID(id);
|
2021-05-04 18:47:45 +02:00
|
|
|
if (player != null) {
|
2021-08-22 17:09:06 +02:00
|
|
|
if (player.isShiftKeyDown()) {
|
2024-03-13 22:35:43 +01:00
|
|
|
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);
|
2021-05-04 18:47:45 +02:00
|
|
|
//TheAchievements.GET_UNPROBED.get(player);
|
2016-07-14 02:11:41 +02:00
|
|
|
}
|
2021-05-04 18:47:45 +02:00
|
|
|
} else {
|
2024-03-13 22:35:43 +01:00
|
|
|
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);
|
2016-07-14 02:11:41 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2024-03-02 21:23:08 +01:00
|
|
|
public InteractionResult useOn(UseOnContext context) {
|
|
|
|
Player player = context.getPlayer();
|
2021-05-04 18:47:45 +02:00
|
|
|
if (player == null) {
|
2024-03-02 21:23:08 +01:00
|
|
|
return InteractionResult.FAIL;
|
2021-05-04 18:47:45 +02:00
|
|
|
}
|
|
|
|
|
2021-08-22 17:09:06 +02:00
|
|
|
ItemStack stack = player.getItemInHand(context.getHand());
|
2024-03-02 21:23:08 +01:00
|
|
|
BlockEntity tile = context.getLevel().getBlockEntity(context.getClickedPos());
|
2024-03-13 22:35:43 +01:00
|
|
|
if (tile instanceof TileEntityPlayerInterface face) {
|
2024-03-02 21:23:08 +01:00
|
|
|
CompoundTag compound = stack.getOrCreateTag();
|
2024-03-13 22:35:43 +01:00
|
|
|
if (compound.contains("UUID")) {
|
2021-08-22 17:09:06 +02:00
|
|
|
if (!context.getLevel().isClientSide) {
|
|
|
|
face.connectedPlayer = compound.getUUID("UUID");
|
2021-05-04 18:47:45 +02:00
|
|
|
face.playerName = compound.getString("Name");
|
2021-08-22 17:09:06 +02:00
|
|
|
face.setChanged();
|
2021-05-04 18:47:45 +02:00
|
|
|
face.sendUpdate();
|
2016-07-14 13:41:29 +02:00
|
|
|
|
2024-03-13 22:35:43 +01:00
|
|
|
ItemPhantomConnector.clearStorage(stack, "UUID", "Name");
|
2016-07-14 02:11:41 +02:00
|
|
|
}
|
2024-03-02 21:23:08 +01:00
|
|
|
return InteractionResult.SUCCESS;
|
2016-07-14 02:11:41 +02:00
|
|
|
}
|
|
|
|
}
|
2024-03-02 21:23:08 +01:00
|
|
|
return InteractionResult.FAIL;
|
2016-07-14 02:11:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@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) {
|
2021-08-22 17:09:06 +02:00
|
|
|
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) {
|
2016-07-14 02:11:41 +02:00
|
|
|
|
2024-03-03 01:20:53 +01:00
|
|
|
if (!playerHit.isShiftKeyDown()) {
|
2024-03-02 21:23:08 +01:00
|
|
|
CompoundTag compound = stack.getOrCreateTag();
|
2021-05-04 18:47:45 +02:00
|
|
|
compound.putString("Name", playerHit.getName().getString());
|
2021-08-22 17:09:06 +02:00
|
|
|
compound.putUUID("UUID", playerHit.getUUID());
|
2024-03-02 21:23:08 +01:00
|
|
|
return InteractionResult.SUCCESS;
|
2016-07-14 13:41:29 +02:00
|
|
|
}
|
2016-07-14 02:11:41 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-03-02 21:23:08 +01:00
|
|
|
return InteractionResult.FAIL;
|
2016-07-14 02:11:41 +02:00
|
|
|
}
|
|
|
|
|
2021-12-30 18:30:01 +01:00
|
|
|
@OnlyIn(Dist.CLIENT)
|
2016-07-14 02:11:41 +02:00
|
|
|
@Override
|
2024-03-02 21:23:08 +01:00
|
|
|
public void appendHoverText(ItemStack stack, @Nullable Level playerIn, List<Component> tooltip, TooltipFlag advanced) {
|
2021-05-04 18:47:45 +02:00
|
|
|
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));
|
2016-07-14 02:11:41 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|