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

152 lines
5.5 KiB
Java
Raw Normal View History

2015-08-29 14:33:25 +02:00
/*
* This file ("ItemPhantomConnector.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-01-03 16:05:51 +01:00
* http://ellpeck.de/actaddlicense/
2015-08-29 14:33:25 +02:00
* View the source code at https://github.com/Ellpeck/ActuallyAdditions
*
2016-01-03 16:05:51 +01:00
* © 2016 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;
import de.ellpeck.actuallyadditions.mod.items.base.ItemBase;
import de.ellpeck.actuallyadditions.mod.tile.TileEntityBase;
import de.ellpeck.actuallyadditions.mod.util.ModUtil;
import de.ellpeck.actuallyadditions.mod.util.StringUtil;
2015-05-20 22:39:43 +02:00
import net.minecraft.entity.Entity;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.EnumRarity;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.BlockPos;
2015-05-20 22:39:43 +02:00
import net.minecraft.util.ChatComponentText;
import net.minecraft.util.EnumChatFormatting;
import net.minecraft.util.EnumFacing;
2015-05-20 22:39:43 +02:00
import net.minecraft.world.World;
import net.minecraftforge.common.DimensionManager;
2016-01-07 18:20:59 +01:00
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
2015-05-20 22:39:43 +02:00
import java.util.List;
public class ItemPhantomConnector extends ItemBase{
2015-05-20 22:39:43 +02:00
public ItemPhantomConnector(String name){
super(name);
2015-05-20 22:39:43 +02:00
this.setMaxStackSize(1);
}
@Override
public boolean onItemUse(ItemStack stack, EntityPlayer player, World world, BlockPos pos, EnumFacing par7, float par8, float par9, float par10){
2015-05-20 22:39:43 +02:00
if(!world.isRemote){
2015-05-25 17:00:54 +02:00
//Passing Data to Phantoms
TileEntity tile = world.getTileEntity(pos);
2015-05-25 17:00:54 +02:00
if(tile != null){
2015-08-22 19:47:20 +02:00
//Passing to Phantom
if(tile instanceof IPhantomTile){
if(this.checkHasConnection(stack, player, tile) && getStoredWorld(stack) == world){
((IPhantomTile)tile).setBoundPosition(getStoredPosition(stack));
if(tile instanceof TileEntityBase){
((TileEntityBase)tile).sendUpdate();
}
clearStorage(stack);
player.addChatComponentMessage(new ChatComponentText(StringUtil.localize("tooltip."+ModUtil.MOD_ID_LOWER+".phantom.connected.desc")));
2015-05-25 17:00:54 +02:00
return true;
}
return false;
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);
player.addChatComponentMessage(new ChatComponentText(StringUtil.localize("tooltip."+ModUtil.MOD_ID_LOWER+".phantom.stored.desc")));
2015-05-25 17:00:54 +02:00
}
return true;
}
2015-05-20 22:39:43 +02:00
public boolean checkHasConnection(ItemStack stack, EntityPlayer player, TileEntity tile){
if(getStoredPosition(stack) != null){
2015-05-25 17:00:54 +02:00
return true;
}
else{
2015-08-22 19:47:20 +02:00
if(tile instanceof IPhantomTile){
((IPhantomTile)tile).setBoundPosition(null);
}
player.addChatComponentMessage(new ChatComponentText(StringUtil.localize("tooltip."+ModUtil.MOD_ID_LOWER+".phantom.unbound.desc")));
2015-05-25 17:00:54 +02:00
return false;
2015-05-20 22:39:43 +02:00
}
}
2016-02-01 17:49:55 +01:00
public static World getStoredWorld(ItemStack stack){
NBTTagCompound tag = stack.getTagCompound();
if(tag != null){
return DimensionManager.getWorld(tag.getInteger("WorldOfTileStored"));
}
return null;
}
2016-01-08 13:31:58 +01:00
public static BlockPos getStoredPosition(ItemStack stack){
2015-05-20 22:39:43 +02:00
NBTTagCompound tag = stack.getTagCompound();
if(tag != null){
int x = tag.getInteger("XCoordOfTileStored");
int y = tag.getInteger("YCoordOfTileStored");
int z = tag.getInteger("ZCoordOfTileStored");
2015-10-27 20:58:42 +01:00
if(!(x == 0 && y == 0 && z == 0)){
2016-01-08 13:31:58 +01:00
return new BlockPos(x, y, z);
}
2015-05-20 22:39:43 +02:00
}
return null;
}
public static void clearStorage(ItemStack stack){
2015-10-03 10:19:40 +02:00
stack.setTagCompound(new NBTTagCompound());
}
public static void storeConnection(ItemStack stack, int x, int y, int z, World world){
2015-05-20 22:39:43 +02:00
NBTTagCompound tag = stack.getTagCompound();
2015-10-03 10:16:18 +02:00
if(tag == null){
tag = new NBTTagCompound();
}
2015-05-20 22:39:43 +02:00
2015-05-25 17:00:54 +02:00
tag.setInteger("XCoordOfTileStored", x);
tag.setInteger("YCoordOfTileStored", y);
tag.setInteger("ZCoordOfTileStored", z);
tag.setInteger("WorldOfTileStored", world.provider.getDimensionId());
2015-05-20 22:39:43 +02:00
stack.setTagCompound(tag);
}
2015-10-03 10:19:40 +02:00
@Override
public boolean getShareTag(){
return true;
2015-05-20 22:39:43 +02:00
}
@Override
2015-10-03 10:19:40 +02:00
public void onUpdate(ItemStack stack, World world, Entity entity, int par4, boolean par5){
if(getStoredPosition(stack) == null){
clearStorage(stack);
2015-10-03 10:19:40 +02:00
}
2015-05-20 22:39:43 +02:00
}
2015-10-23 16:54:33 +02:00
@Override
@SuppressWarnings("unchecked")
@SideOnly(Side.CLIENT)
public void addInformation(ItemStack stack, EntityPlayer player, List list, boolean isHeld){
2016-01-08 13:31:58 +01:00
BlockPos coords = getStoredPosition(stack);
2015-10-23 16:54:33 +02:00
if(coords != null){
2015-10-27 20:58:42 +01:00
list.add(StringUtil.localize("tooltip."+ModUtil.MOD_ID_LOWER+".boundTo.desc")+":");
list.add("X: "+coords.getX());
list.add("Y: "+coords.getY());
list.add("Z: "+coords.getZ());
list.add(EnumChatFormatting.ITALIC+StringUtil.localize("tooltip."+ModUtil.MOD_ID_LOWER+".clearStorage.desc"));
2015-10-23 16:54:33 +02:00
}
}
2015-05-20 22:39:43 +02:00
@Override
2015-10-03 10:19:40 +02:00
public EnumRarity getRarity(ItemStack stack){
return EnumRarity.EPIC;
2015-05-20 22:39:43 +02:00
}
}