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

138 lines
5.2 KiB
Java
Raw Normal View History

2015-08-29 14:33:25 +02:00
/*
2016-05-16 22:52:27 +02:00
* This file ("ItemPhantomConnector.java") is part of the Actually Additions mod for Minecraft.
2015-08-29 14:33:25 +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-08-29 14:33:25 +02:00
* View the source code at https://github.com/Ellpeck/ActuallyAdditions
*
2017-01-01 16:23:26 +01:00
* © 2015-2017 Ellpeck
2015-08-29 14:33:25 +02:00
*/
2016-01-05 04:47:35 +01:00
package de.ellpeck.actuallyadditions.mod.items;
2015-05-20 22:39:43 +02:00
2016-01-05 04:47:35 +01:00
import de.ellpeck.actuallyadditions.api.tile.IPhantomTile;
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.TileEntityBase;
import de.ellpeck.actuallyadditions.mod.util.StringUtil;
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;
2015-05-20 22:39:43 +02:00
import net.minecraft.item.EnumRarity;
import net.minecraft.item.ItemStack;
2021-02-26 22:15:48 +01:00
import net.minecraft.nbt.CompoundNBT;
2015-05-20 22:39:43 +02:00
import net.minecraft.tileentity.TileEntity;
2016-03-18 23:47:22 +01:00
import net.minecraft.util.EnumActionResult;
import net.minecraft.util.Direction;
2021-02-26 22:15:48 +01:00
import net.minecraft.util.Hand;
2016-03-18 23:47:22 +01:00
import net.minecraft.util.math.BlockPos;
2016-05-27 18:54:17 +02:00
import net.minecraft.util.text.TextComponentTranslation;
2016-03-18 23:47:22 +01:00
import net.minecraft.util.text.TextFormatting;
2015-05-20 22:39:43 +02:00
import net.minecraft.world.World;
import net.minecraftforge.common.DimensionManager;
2015-05-20 22:39:43 +02:00
2021-02-26 22:15:48 +01:00
import javax.annotation.Nullable;
import java.util.List;
2019-05-02 09:10:29 +02:00
public class ItemPhantomConnector extends ItemBase {
2015-05-20 22:39:43 +02:00
2019-05-02 09:10:29 +02:00
public ItemPhantomConnector(String name) {
super(name);
2015-05-20 22:39:43 +02:00
this.setMaxStackSize(1);
}
2019-05-02 09:10:29 +02:00
public static World getStoredWorld(ItemStack stack) {
2021-02-26 22:15:48 +01:00
CompoundNBT tag = stack.getTagCompound();
if (tag != null) {
return DimensionManager.getWorld(tag.getInteger("WorldOfTileStored"));
}
2016-02-01 20:39:11 +01:00
return null;
}
2019-05-02 09:10:29 +02:00
public static BlockPos getStoredPosition(ItemStack stack) {
2021-02-26 22:15:48 +01:00
CompoundNBT tag = stack.getTagCompound();
2019-05-02 09:10:29 +02:00
if (tag != null) {
2016-02-01 20:39:11 +01:00
int x = tag.getInteger("XCoordOfTileStored");
int y = tag.getInteger("YCoordOfTileStored");
int z = tag.getInteger("ZCoordOfTileStored");
2021-02-26 22:15:48 +01:00
if (!(x == 0 && y == 0 && z == 0)) {
return new BlockPos(x, y, z);
}
2016-02-01 20:39:11 +01:00
}
return null;
}
2019-05-02 09:10:29 +02:00
public static void clearStorage(ItemStack stack, String... keys) {
if (stack.hasTagCompound()) {
2021-02-26 22:15:48 +01:00
CompoundNBT compound = stack.getTagCompound();
2019-05-02 09:10:29 +02:00
for (String key : keys) {
compound.removeTag(key);
}
}
2016-02-01 20:39:11 +01:00
}
2019-05-02 09:10:29 +02:00
public static void storeConnection(ItemStack stack, int x, int y, int z, World world) {
2021-02-26 22:15:48 +01:00
CompoundNBT tag = stack.getTagCompound();
2019-05-02 09:10:29 +02:00
if (tag == null) {
2021-02-26 22:15:48 +01:00
tag = new CompoundNBT();
2016-02-01 20:39:11 +01:00
}
tag.setInteger("XCoordOfTileStored", x);
tag.setInteger("YCoordOfTileStored", y);
tag.setInteger("ZCoordOfTileStored", z);
2016-03-18 23:47:22 +01:00
tag.setInteger("WorldOfTileStored", world.provider.getDimension());
2016-02-01 20:39:11 +01:00
stack.setTagCompound(tag);
}
2015-05-20 22:39:43 +02:00
@Override
public EnumActionResult onItemUse(PlayerEntity player, World world, BlockPos pos, Hand hand, Direction par7, float par8, float par9, float par10) {
2016-11-19 21:11:17 +01:00
ItemStack stack = player.getHeldItem(hand);
2019-05-02 09:10:29 +02:00
if (!world.isRemote) {
2015-05-25 17:00:54 +02:00
//Passing Data to Phantoms
TileEntity tile = world.getTileEntity(pos);
2019-05-02 09:10:29 +02:00
if (tile != null) {
2015-08-22 19:47:20 +02:00
//Passing to Phantom
2019-05-02 09:10:29 +02:00
if (tile instanceof IPhantomTile) {
BlockPos stored = getStoredPosition(stack);
2019-05-02 09:10:29 +02:00
if (stored != null && getStoredWorld(stack) == world) {
((IPhantomTile) tile).setBoundPosition(stored);
if (tile instanceof TileEntityBase) {
((TileEntityBase) tile).sendUpdate();
}
clearStorage(stack, "XCoordOfTileStored", "YCoordOfTileStored", "ZCoordOfTileStored", "WorldOfTileStored");
2019-05-02 09:10:29 +02:00
player.sendStatusMessage(new TextComponentTranslation("tooltip." + ActuallyAdditions.MODID + ".phantom.connected.desc"), true);
2016-03-18 23:47:22 +01:00
return EnumActionResult.SUCCESS;
2015-05-25 17:00:54 +02:00
}
2016-03-18 23:47:22 +01:00
return EnumActionResult.FAIL;
2015-05-20 22:39:43 +02:00
}
}
2015-05-25 17:00:54 +02:00
//Storing Connections
storeConnection(stack, pos.getX(), pos.getY(), pos.getZ(), world);
2019-05-02 09:10:29 +02:00
player.sendStatusMessage(new TextComponentTranslation("tooltip." + ActuallyAdditions.MODID + ".phantom.stored.desc"), true);
2015-05-25 17:00:54 +02:00
}
2016-03-18 23:47:22 +01:00
return EnumActionResult.SUCCESS;
2015-05-25 17:00:54 +02:00
}
2015-05-20 22:39:43 +02:00
2015-10-03 10:19:40 +02:00
@Override
2019-05-02 09:10:29 +02:00
public boolean getShareTag() {
2015-10-03 10:19:40 +02:00
return true;
2015-05-20 22:39:43 +02:00
}
2015-10-23 16:54:33 +02:00
@Override
2019-05-02 09:10:29 +02:00
public void addInformation(ItemStack stack, @Nullable World playerIn, List<String> list, ITooltipFlag advanced) {
2016-01-08 13:31:58 +01:00
BlockPos coords = getStoredPosition(stack);
2019-05-02 09:10:29 +02:00
if (coords != null) {
list.add(StringUtil.localize("tooltip." + ActuallyAdditions.MODID + ".boundTo.desc") + ":");
list.add("X: " + coords.getX());
list.add("Y: " + coords.getY());
list.add("Z: " + coords.getZ());
list.add(TextFormatting.ITALIC + StringUtil.localize("tooltip." + ActuallyAdditions.MODID + ".clearStorage.desc"));
2015-10-23 16:54:33 +02:00
}
}
2015-05-20 22:39:43 +02:00
@Override
2019-05-02 09:10:29 +02:00
public EnumRarity getRarity(ItemStack stack) {
return EnumRarity.EPIC;
2015-05-20 22:39:43 +02:00
}
}