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;
|
2017-06-17 00:48:49 +02:00
|
|
|
import net.minecraft.client.util.ITooltipFlag;
|
2016-07-14 02:11:41 +02:00
|
|
|
import net.minecraft.entity.Entity;
|
2021-05-04 18:47:45 +02:00
|
|
|
import net.minecraft.entity.LivingEntity;
|
2021-02-26 22:15:48 +01:00
|
|
|
import net.minecraft.entity.player.PlayerEntity;
|
2016-07-14 02:11:41 +02:00
|
|
|
import net.minecraft.item.ItemStack;
|
2021-05-04 18:47:45 +02:00
|
|
|
import net.minecraft.item.ItemUseContext;
|
2021-02-26 22:15:48 +01:00
|
|
|
import net.minecraft.nbt.CompoundNBT;
|
2016-07-14 02:11:41 +02:00
|
|
|
import net.minecraft.tileentity.TileEntity;
|
2021-05-04 18:47:45 +02:00
|
|
|
import net.minecraft.util.ActionResultType;
|
2021-02-26 22:15:48 +01:00
|
|
|
import net.minecraft.util.Hand;
|
2021-05-04 18:47:45 +02:00
|
|
|
import net.minecraft.util.text.ITextComponent;
|
|
|
|
import net.minecraft.util.text.TranslationTextComponent;
|
2016-07-14 02:11:41 +02:00
|
|
|
import net.minecraft.world.World;
|
2021-12-30 18:30:01 +01:00
|
|
|
import net.minecraftforge.api.distmarker.Dist;
|
|
|
|
import net.minecraftforge.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
|
2021-05-04 18:47:45 +02:00
|
|
|
public void inventoryTick(ItemStack stack, World world, Entity entity, int itemSlot, boolean isSelected) {
|
2021-08-22 17:09:06 +02:00
|
|
|
if (!world.isClientSide) {
|
2021-05-04 18:47:45 +02:00
|
|
|
CompoundNBT compound = stack.getOrCreateTag();
|
|
|
|
if (compound.contains("UUIDMost")) {
|
2021-08-22 17:09:06 +02:00
|
|
|
UUID id = compound.getUUID("UUID");
|
|
|
|
PlayerEntity 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()) {
|
2016-12-30 20:09:05 +01:00
|
|
|
ItemPhantomConnector.clearStorage(stack, "UUIDLeast", "UUIDMost", "Name");
|
2021-08-22 17:09:06 +02:00
|
|
|
((PlayerEntity) entity).displayClientMessage(new TranslationTextComponent("tooltip." + ActuallyAdditions.MODID + ".playerProbe.disconnect.1"), false);
|
|
|
|
player.displayClientMessage(new TranslationTextComponent("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 {
|
|
|
|
ItemPhantomConnector.clearStorage(stack, "UUIDLeast", "UUIDMost", "Name");
|
2021-08-22 17:09:06 +02:00
|
|
|
((PlayerEntity) entity).displayClientMessage(new TranslationTextComponent("tooltip." + ActuallyAdditions.MODID + ".playerProbe.disconnect.2"), false);
|
2016-07-14 02:11:41 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2021-08-22 17:09:06 +02:00
|
|
|
public ActionResultType useOn(ItemUseContext context) {
|
2021-05-04 18:47:45 +02:00
|
|
|
PlayerEntity player = context.getPlayer();
|
|
|
|
if (player == null) {
|
|
|
|
return ActionResultType.FAIL;
|
|
|
|
}
|
|
|
|
|
2021-08-22 17:09:06 +02:00
|
|
|
ItemStack stack = player.getItemInHand(context.getHand());
|
|
|
|
TileEntity tile = context.getLevel().getBlockEntity(context.getClickedPos());
|
2019-05-02 09:10:29 +02:00
|
|
|
if (tile instanceof TileEntityPlayerInterface) {
|
2021-05-04 18:47:45 +02:00
|
|
|
CompoundNBT compound = stack.getOrCreateTag();
|
|
|
|
if (compound.contains("UUIDMost")) {
|
2021-08-22 17:09:06 +02:00
|
|
|
if (!context.getLevel().isClientSide) {
|
2021-05-04 18:47:45 +02:00
|
|
|
TileEntityPlayerInterface face = (TileEntityPlayerInterface) tile;
|
2021-08-22 17:09:06 +02:00
|
|
|
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
|
|
|
|
2021-05-04 18:47:45 +02:00
|
|
|
ItemPhantomConnector.clearStorage(stack, "UUIDLeast", "UUIDMost", "Name");
|
2016-07-14 02:11:41 +02:00
|
|
|
}
|
2021-05-04 18:47:45 +02:00
|
|
|
return ActionResultType.SUCCESS;
|
2016-07-14 02:11:41 +02:00
|
|
|
}
|
|
|
|
}
|
2021-05-04 18:47:45 +02:00
|
|
|
return ActionResultType.FAIL;
|
2016-07-14 02:11:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2021-08-22 17:09:06 +02:00
|
|
|
public ActionResultType interactLivingEntity(ItemStack aStack, PlayerEntity player, LivingEntity entity, Hand hand) {
|
|
|
|
if (!player.level.isClientSide) {
|
|
|
|
ItemStack stack = player.getMainHandItem();
|
2019-05-02 09:10:29 +02:00
|
|
|
if (StackUtil.isValid(stack) && stack.getItem() == this) {
|
2021-02-26 22:15:48 +01:00
|
|
|
if (entity instanceof PlayerEntity) {
|
|
|
|
PlayerEntity playerHit = (PlayerEntity) entity;
|
2016-07-14 02:11:41 +02:00
|
|
|
|
2021-08-22 17:09:06 +02:00
|
|
|
if (!playerHit.isShiftKeyDown()) {
|
2021-05-04 18:47:45 +02:00
|
|
|
CompoundNBT compound = stack.getOrCreateTag();
|
|
|
|
compound.putString("Name", playerHit.getName().getString());
|
2021-08-22 17:09:06 +02:00
|
|
|
compound.putUUID("UUID", playerHit.getUUID());
|
2021-05-04 18:47:45 +02:00
|
|
|
return ActionResultType.SUCCESS;
|
2016-07-14 13:41:29 +02:00
|
|
|
}
|
2016-07-14 02:11:41 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-05-04 18:47:45 +02:00
|
|
|
return ActionResultType.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
|
2021-08-22 17:09:06 +02:00
|
|
|
public void appendHoverText(ItemStack stack, @Nullable World playerIn, List<ITextComponent> tooltip, ITooltipFlag advanced) {
|
2021-05-04 18:47:45 +02:00
|
|
|
if (stack.getOrCreateTag().contains("Name")) {
|
|
|
|
String name = stack.getOrCreateTag().getString("Name");
|
2021-08-22 17:09:06 +02:00
|
|
|
tooltip.add(new TranslationTextComponent("tooltip." + ActuallyAdditions.MODID + ".playerProbe.probing").append(": " + name));
|
2016-07-14 02:11:41 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|