2015-10-20 00:22:36 +02:00
|
|
|
/*
|
2016-05-16 22:52:27 +02:00
|
|
|
* This file ("ItemLaserWrench.java") is part of the Actually Additions mod for Minecraft.
|
2015-10-20 00:22:36 +02:00
|
|
|
* It is created and owned by Ellpeck and distributed
|
|
|
|
* under the Actually Additions License to be found at
|
2016-05-16 22:52:27 +02:00
|
|
|
* http://ellpeck.de/actaddlicense
|
2015-10-20 00:22:36 +02:00
|
|
|
* View the source code at https://github.com/Ellpeck/ActuallyAdditions
|
|
|
|
*
|
2017-01-01 16:23:26 +01:00
|
|
|
* © 2015-2017 Ellpeck
|
2015-10-20 00:22:36 +02:00
|
|
|
*/
|
|
|
|
|
2016-01-05 04:47:35 +01:00
|
|
|
package de.ellpeck.actuallyadditions.mod.items;
|
2015-10-20 00:22:36 +02:00
|
|
|
|
2016-07-30 17:07:32 +02:00
|
|
|
import de.ellpeck.actuallyadditions.api.ActuallyAdditionsAPI;
|
2018-05-10 11:38:58 +02:00
|
|
|
import de.ellpeck.actuallyadditions.mod.ActuallyAdditions;
|
2016-01-05 04:47:35 +01:00
|
|
|
import de.ellpeck.actuallyadditions.mod.items.base.ItemBase;
|
|
|
|
import de.ellpeck.actuallyadditions.mod.tile.TileEntityLaserRelay;
|
2024-03-02 21:23:08 +01:00
|
|
|
import net.minecraft.ChatFormatting;
|
|
|
|
import net.minecraft.core.BlockPos;
|
|
|
|
import net.minecraft.network.chat.Component;
|
|
|
|
import net.minecraft.network.chat.TextComponent;
|
|
|
|
import net.minecraft.network.chat.TranslatableComponent;
|
|
|
|
import net.minecraft.world.InteractionResult;
|
|
|
|
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;
|
2021-12-30 18:30:01 +01:00
|
|
|
import net.minecraftforge.api.distmarker.Dist;
|
|
|
|
import net.minecraftforge.api.distmarker.OnlyIn;
|
2015-10-20 00:22:36 +02:00
|
|
|
|
2021-02-26 22:15:48 +01:00
|
|
|
import java.util.List;
|
|
|
|
|
2019-05-02 09:10:29 +02:00
|
|
|
public class ItemLaserWrench extends ItemBase {
|
2015-10-21 18:31:35 +02:00
|
|
|
|
2021-03-01 21:23:52 +01:00
|
|
|
public ItemLaserWrench() {
|
2021-05-04 19:50:50 +02:00
|
|
|
super(ActuallyItems.defaultNonStacking());
|
2015-10-20 00:22:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2024-03-02 21:23:08 +01:00
|
|
|
public InteractionResult useOn(UseOnContext context) {
|
2021-08-22 17:09:06 +02:00
|
|
|
BlockPos pos = context.getClickedPos();
|
2024-03-02 21:23:08 +01:00
|
|
|
Level world = context.getLevel();
|
|
|
|
Player player = context.getPlayer();
|
2021-05-04 19:50:50 +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 = world.getBlockEntity(pos);
|
2019-05-02 09:10:29 +02:00
|
|
|
if (tile instanceof TileEntityLaserRelay) {
|
|
|
|
TileEntityLaserRelay relay = (TileEntityLaserRelay) tile;
|
2021-08-22 17:09:06 +02:00
|
|
|
if (!world.isClientSide) {
|
2019-05-02 09:10:29 +02:00
|
|
|
if (ItemPhantomConnector.getStoredPosition(stack) == null) {
|
2016-01-07 21:41:28 +01:00
|
|
|
ItemPhantomConnector.storeConnection(stack, pos.getX(), pos.getY(), pos.getZ(), world);
|
2024-03-02 21:23:08 +01:00
|
|
|
player.displayClientMessage(new TranslatableComponent("tooltip." + ActuallyAdditions.MODID + ".laser.stored.desc"), true);
|
2019-05-02 09:10:29 +02:00
|
|
|
} else {
|
2016-01-08 13:31:58 +01:00
|
|
|
BlockPos savedPos = ItemPhantomConnector.getStoredPosition(stack);
|
2019-05-02 09:10:29 +02:00
|
|
|
if (savedPos != null) {
|
2024-03-02 21:23:08 +01:00
|
|
|
BlockEntity savedTile = world.getBlockEntity(savedPos);
|
2019-05-02 09:10:29 +02:00
|
|
|
if (savedTile instanceof TileEntityLaserRelay) {
|
2021-08-22 17:09:06 +02:00
|
|
|
int distanceSq = (int) savedPos.distSqr(pos);
|
2019-05-02 09:10:29 +02:00
|
|
|
TileEntityLaserRelay savedRelay = (TileEntityLaserRelay) savedTile;
|
2015-10-21 00:22:50 +02:00
|
|
|
|
2017-02-13 19:07:53 +01:00
|
|
|
int lowestRange = Math.min(relay.getMaxRange(), savedRelay.getMaxRange());
|
2019-05-02 09:10:29 +02:00
|
|
|
int range = lowestRange * lowestRange;
|
2021-08-22 17:09:06 +02:00
|
|
|
if (ItemPhantomConnector.getStoredWorld(stack) == world.dimension() && savedRelay.type == relay.type && distanceSq <= range && ActuallyAdditionsAPI.connectionHandler.addConnection(savedPos, pos, relay.type, world, false, true)) {
|
2017-02-13 19:07:53 +01:00
|
|
|
ItemPhantomConnector.clearStorage(stack, "XCoordOfTileStored", "YCoordOfTileStored", "ZCoordOfTileStored", "WorldOfTileStored");
|
2015-10-21 19:14:57 +02:00
|
|
|
|
2019-05-02 09:10:29 +02:00
|
|
|
((TileEntityLaserRelay) savedTile).sendUpdate();
|
2017-02-13 19:07:53 +01:00
|
|
|
relay.sendUpdate();
|
|
|
|
|
2024-03-02 21:23:08 +01:00
|
|
|
player.displayClientMessage(new TranslatableComponent("tooltip." + ActuallyAdditions.MODID + ".laser.connected.desc"), true);
|
2017-02-13 19:07:53 +01:00
|
|
|
|
2024-03-02 21:23:08 +01:00
|
|
|
return InteractionResult.SUCCESS;
|
2017-02-13 19:07:53 +01:00
|
|
|
}
|
2016-05-16 22:52:27 +02:00
|
|
|
}
|
2017-02-13 19:07:53 +01:00
|
|
|
|
2024-03-02 21:23:08 +01:00
|
|
|
player.displayClientMessage(new TranslatableComponent("tooltip." + ActuallyAdditions.MODID + ".laser.cantConnect.desc"), false);
|
2017-02-13 19:07:53 +01:00
|
|
|
ItemPhantomConnector.clearStorage(stack, "XCoordOfTileStored", "YCoordOfTileStored", "ZCoordOfTileStored", "WorldOfTileStored");
|
2015-10-20 00:22:36 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-03-02 21:23:08 +01:00
|
|
|
return InteractionResult.SUCCESS;
|
2015-10-20 00:22:36 +02:00
|
|
|
}
|
2024-03-02 21:23:08 +01:00
|
|
|
return InteractionResult.FAIL;
|
2015-10-20 00:22:36 +02:00
|
|
|
}
|
|
|
|
|
2021-05-04 19:50:50 +02:00
|
|
|
// // TODO: [port] ensure this is correct
|
|
|
|
// @Nullable
|
|
|
|
// @Override
|
|
|
|
// public CompoundNBT getShareTag(ItemStack stack) {
|
|
|
|
// return new CompoundNBT();
|
|
|
|
// }
|
2015-10-23 16:54:33 +02:00
|
|
|
|
2021-12-30 18:30:01 +01:00
|
|
|
@OnlyIn(Dist.CLIENT)
|
2015-10-21 18:31:35 +02:00
|
|
|
@Override
|
2024-03-02 21:23:08 +01:00
|
|
|
public void appendHoverText(ItemStack stack, Level playerIn, List<Component> list, TooltipFlag advanced) {
|
2016-01-08 13:31:58 +01:00
|
|
|
BlockPos coords = ItemPhantomConnector.getStoredPosition(stack);
|
2019-05-02 09:10:29 +02:00
|
|
|
if (coords != null) {
|
2024-03-02 21:23:08 +01:00
|
|
|
list.add(new TranslatableComponent("tooltip." + ActuallyAdditions.MODID + ".boundTo.desc").append(":"));
|
|
|
|
list.add(new TextComponent("X: " + coords.getX()));
|
|
|
|
list.add(new TextComponent("Y: " + coords.getY()));
|
|
|
|
list.add(new TextComponent("Z: " + coords.getZ()));
|
|
|
|
list.add(new TranslatableComponent("tooltip." + ActuallyAdditions.MODID + ".clearStorage.desc").withStyle(ChatFormatting.ITALIC));
|
2015-10-21 18:31:35 +02:00
|
|
|
}
|
|
|
|
}
|
2015-10-20 00:22:36 +02:00
|
|
|
}
|