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

106 lines
5.1 KiB
Java
Raw Normal View History

/*
2016-05-16 22:52:27 +02:00
* This file ("ItemLaserWrench.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
2016-05-16 22:52:27 +02:00
* 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-01-05 04:47:35 +01:00
package de.ellpeck.actuallyadditions.mod.items;
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;
2017-06-17 00:48:49 +02:00
import net.minecraft.client.util.ITooltipFlag;
2021-02-26 22:15:48 +01:00
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.item.ItemUseContext;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.ActionResultType;
2016-03-18 23:47:22 +01:00
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.text.ITextComponent;
import net.minecraft.util.text.StringTextComponent;
2016-03-18 23:47:22 +01:00
import net.minecraft.util.text.TextFormatting;
import net.minecraft.util.text.TranslationTextComponent;
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;
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 {
public ItemLaserWrench() {
super(ActuallyItems.defaultNonStacking());
}
@Override
public ActionResultType useOn(ItemUseContext context) {
BlockPos pos = context.getClickedPos();
World world = context.getLevel();
PlayerEntity player = context.getPlayer();
ItemStack stack = player.getItemInHand(context.getHand());
TileEntity tile = world.getBlockEntity(pos);
2019-05-02 09:10:29 +02:00
if (tile instanceof TileEntityLaserRelay) {
TileEntityLaserRelay relay = (TileEntityLaserRelay) tile;
if (!world.isClientSide) {
2019-05-02 09:10:29 +02:00
if (ItemPhantomConnector.getStoredPosition(stack) == null) {
ItemPhantomConnector.storeConnection(stack, pos.getX(), pos.getY(), pos.getZ(), world);
player.displayClientMessage(new TranslationTextComponent("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) {
TileEntity savedTile = world.getBlockEntity(savedPos);
2019-05-02 09:10:29 +02:00
if (savedTile instanceof TileEntityLaserRelay) {
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;
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");
2019-05-02 09:10:29 +02:00
((TileEntityLaserRelay) savedTile).sendUpdate();
2017-02-13 19:07:53 +01:00
relay.sendUpdate();
player.displayClientMessage(new TranslationTextComponent("tooltip." + ActuallyAdditions.MODID + ".laser.connected.desc"), true);
2017-02-13 19:07:53 +01:00
return ActionResultType.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
player.displayClientMessage(new TranslationTextComponent("tooltip." + ActuallyAdditions.MODID + ".laser.cantConnect.desc"), false);
2017-02-13 19:07:53 +01:00
ItemPhantomConnector.clearStorage(stack, "XCoordOfTileStored", "YCoordOfTileStored", "ZCoordOfTileStored", "WorldOfTileStored");
}
}
}
return ActionResultType.SUCCESS;
}
return ActionResultType.FAIL;
}
// // 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)
@Override
public void appendHoverText(ItemStack stack, World playerIn, List<ITextComponent> list, ITooltipFlag 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) {
list.add(new TranslationTextComponent("tooltip." + ActuallyAdditions.MODID + ".boundTo.desc").append(":"));
list.add(new StringTextComponent("X: " + coords.getX()));
list.add(new StringTextComponent("Y: " + coords.getY()));
list.add(new StringTextComponent("Z: " + coords.getZ()));
list.add(new TranslationTextComponent("tooltip." + ActuallyAdditions.MODID + ".clearStorage.desc").withStyle(TextFormatting.ITALIC));
}
}
}