2016-01-12 22:05:56 +00:00
|
|
|
package WayofTime.bloodmagic.item.routing;
|
|
|
|
|
2016-03-17 20:00:44 +00:00
|
|
|
import WayofTime.bloodmagic.BloodMagic;
|
|
|
|
import WayofTime.bloodmagic.api.Constants;
|
2016-03-01 07:20:57 +00:00
|
|
|
import WayofTime.bloodmagic.api.iface.INodeRenderer;
|
2016-03-17 20:00:44 +00:00
|
|
|
import WayofTime.bloodmagic.api.util.helper.NBTHelper;
|
2016-03-16 08:10:33 +00:00
|
|
|
import WayofTime.bloodmagic.client.IVariantProvider;
|
2016-03-17 20:00:44 +00:00
|
|
|
import WayofTime.bloodmagic.routing.IMasterRoutingNode;
|
|
|
|
import WayofTime.bloodmagic.routing.IRoutingNode;
|
|
|
|
import WayofTime.bloodmagic.util.ChatUtil;
|
|
|
|
import WayofTime.bloodmagic.util.helper.TextHelper;
|
2016-01-12 22:05:56 +00:00
|
|
|
import net.minecraft.entity.player.EntityPlayer;
|
|
|
|
import net.minecraft.item.Item;
|
|
|
|
import net.minecraft.item.ItemStack;
|
|
|
|
import net.minecraft.nbt.NBTTagCompound;
|
|
|
|
import net.minecraft.tileentity.TileEntity;
|
2016-03-18 19:45:37 +00:00
|
|
|
import net.minecraft.util.EnumActionResult;
|
2016-01-12 22:05:56 +00:00
|
|
|
import net.minecraft.util.EnumFacing;
|
2016-03-18 19:45:37 +00:00
|
|
|
import net.minecraft.util.EnumHand;
|
|
|
|
import net.minecraft.util.math.BlockPos;
|
2016-01-12 22:05:56 +00:00
|
|
|
import net.minecraft.world.World;
|
|
|
|
import net.minecraftforge.fml.relauncher.Side;
|
|
|
|
import net.minecraftforge.fml.relauncher.SideOnly;
|
2016-03-16 08:10:33 +00:00
|
|
|
import org.apache.commons.lang3.tuple.ImmutablePair;
|
|
|
|
import org.apache.commons.lang3.tuple.Pair;
|
2016-01-12 22:05:56 +00:00
|
|
|
|
2016-03-17 20:00:44 +00:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.LinkedList;
|
|
|
|
import java.util.List;
|
|
|
|
|
2016-03-16 08:10:33 +00:00
|
|
|
public class ItemNodeRouter extends Item implements INodeRenderer, IVariantProvider
|
2016-01-12 22:05:56 +00:00
|
|
|
{
|
|
|
|
public ItemNodeRouter()
|
|
|
|
{
|
|
|
|
setUnlocalizedName(Constants.Mod.MODID + ".nodeRouter");
|
|
|
|
setMaxStackSize(1);
|
|
|
|
setCreativeTab(BloodMagic.tabBloodMagic);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@SideOnly(Side.CLIENT)
|
|
|
|
public void addInformation(ItemStack stack, EntityPlayer player, List<String> tooltip, boolean advanced)
|
|
|
|
{
|
2016-09-07 01:55:32 +00:00
|
|
|
if (!stack.hasTagCompound())
|
|
|
|
return;
|
2016-01-12 22:05:56 +00:00
|
|
|
NBTTagCompound tag = stack.getTagCompound();
|
|
|
|
BlockPos coords = getBlockPos(stack);
|
|
|
|
|
|
|
|
if (coords != null && tag != null)
|
|
|
|
{
|
|
|
|
tooltip.add(TextHelper.localizeEffect("tooltip.BloodMagic.telepositionFocus.coords", coords.getX(), coords.getY(), coords.getZ()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-16 08:10:33 +00:00
|
|
|
@Override
|
2017-01-02 06:26:42 +00:00
|
|
|
public EnumActionResult onItemUseFirst(EntityPlayer player, World world, BlockPos pos, EnumFacing side, float hitX, float hitY, float hitZ, EnumHand hand)
|
2016-01-12 22:05:56 +00:00
|
|
|
{
|
2017-01-02 06:26:42 +00:00
|
|
|
ItemStack stack = player.getHeldItem(hand);
|
2016-04-12 19:25:16 +00:00
|
|
|
if (world.isRemote)
|
|
|
|
{
|
|
|
|
return EnumActionResult.PASS;
|
|
|
|
}
|
|
|
|
|
2016-01-12 22:05:56 +00:00
|
|
|
TileEntity tileHit = world.getTileEntity(pos);
|
|
|
|
|
|
|
|
if (!(tileHit instanceof IRoutingNode))
|
|
|
|
{
|
|
|
|
// TODO: Remove contained position?
|
|
|
|
BlockPos containedPos = getBlockPos(stack);
|
|
|
|
if (!containedPos.equals(BlockPos.ORIGIN))
|
|
|
|
{
|
|
|
|
this.setBlockPos(stack, BlockPos.ORIGIN);
|
2016-03-26 19:32:51 +00:00
|
|
|
ChatUtil.sendChat(player, TextHelper.localize("chat.BloodMagic.routing.remove"));
|
2016-03-18 19:45:37 +00:00
|
|
|
return EnumActionResult.FAIL;
|
2016-01-12 22:05:56 +00:00
|
|
|
}
|
2016-03-18 19:45:37 +00:00
|
|
|
return EnumActionResult.FAIL;
|
2016-01-12 22:05:56 +00:00
|
|
|
}
|
|
|
|
IRoutingNode node = (IRoutingNode) tileHit;
|
|
|
|
BlockPos containedPos = getBlockPos(stack);
|
|
|
|
if (containedPos.equals(BlockPos.ORIGIN))
|
|
|
|
{
|
|
|
|
this.setBlockPos(stack, pos);
|
2016-03-26 19:32:51 +00:00
|
|
|
ChatUtil.sendChat(player, TextHelper.localize("chat.BloodMagic.routing.set"));
|
2016-03-18 19:45:37 +00:00
|
|
|
return EnumActionResult.SUCCESS;
|
2016-01-12 22:05:56 +00:00
|
|
|
} else
|
|
|
|
{
|
|
|
|
TileEntity pastTile = world.getTileEntity(containedPos);
|
|
|
|
if (pastTile instanceof IRoutingNode)
|
|
|
|
{
|
|
|
|
IRoutingNode pastNode = (IRoutingNode) pastTile;
|
|
|
|
if (pastNode instanceof IMasterRoutingNode)
|
|
|
|
{
|
|
|
|
IMasterRoutingNode master = (IMasterRoutingNode) pastNode;
|
|
|
|
|
|
|
|
if (!node.isMaster(master))
|
|
|
|
{
|
2016-04-12 19:25:16 +00:00
|
|
|
if (node.getMasterPos().equals(BlockPos.ORIGIN)) //If the node is not the master and it is receptive
|
2016-01-12 22:05:56 +00:00
|
|
|
{
|
|
|
|
node.connectMasterToRemainingNode(world, new LinkedList<BlockPos>(), master);
|
|
|
|
master.addConnection(pos, containedPos);
|
|
|
|
master.addNodeToList(node);
|
|
|
|
node.addConnection(containedPos);
|
2016-03-26 19:32:51 +00:00
|
|
|
ChatUtil.sendChat(player, TextHelper.localize("chat.BloodMagic.routing.link.master"));
|
2016-01-12 22:05:56 +00:00
|
|
|
this.setBlockPos(stack, BlockPos.ORIGIN);
|
2016-03-18 19:45:37 +00:00
|
|
|
return EnumActionResult.SUCCESS;
|
2016-01-12 22:05:56 +00:00
|
|
|
}
|
|
|
|
} else
|
|
|
|
{
|
|
|
|
master.addConnection(pos, containedPos);
|
|
|
|
node.addConnection(containedPos);
|
2016-03-26 19:32:51 +00:00
|
|
|
ChatUtil.sendChat(player, TextHelper.localize("chat.BloodMagic.routing.link.master"));
|
2016-01-12 22:05:56 +00:00
|
|
|
this.setBlockPos(stack, BlockPos.ORIGIN);
|
2016-03-18 19:45:37 +00:00
|
|
|
return EnumActionResult.SUCCESS;
|
2016-01-12 22:05:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} else if (node instanceof IMasterRoutingNode)
|
|
|
|
{
|
|
|
|
IMasterRoutingNode master = (IMasterRoutingNode) node;
|
|
|
|
|
|
|
|
if (!pastNode.isMaster(master))
|
|
|
|
{
|
2016-04-12 19:25:16 +00:00
|
|
|
if (pastNode.getMasterPos().equals(BlockPos.ORIGIN)) //TODO: This is where the issue is
|
2016-01-12 22:05:56 +00:00
|
|
|
{
|
|
|
|
pastNode.connectMasterToRemainingNode(world, new LinkedList<BlockPos>(), master);
|
|
|
|
master.addConnection(pos, containedPos);
|
|
|
|
pastNode.addConnection(pos);
|
|
|
|
master.addNodeToList(pastNode);
|
2016-03-26 19:32:51 +00:00
|
|
|
ChatUtil.sendChat(player, TextHelper.localize("chat.BloodMagic.routing.link.master"));
|
2016-01-12 22:05:56 +00:00
|
|
|
this.setBlockPos(stack, BlockPos.ORIGIN);
|
2016-03-18 19:45:37 +00:00
|
|
|
return EnumActionResult.SUCCESS;
|
2016-01-12 22:05:56 +00:00
|
|
|
}
|
|
|
|
} else
|
|
|
|
{
|
|
|
|
master.addConnection(pos, containedPos);
|
|
|
|
pastNode.addConnection(pos);
|
2016-03-26 19:32:51 +00:00
|
|
|
ChatUtil.sendChat(player, TextHelper.localize("chat.BloodMagic.routing.link.master"));
|
2016-01-12 22:05:56 +00:00
|
|
|
this.setBlockPos(stack, BlockPos.ORIGIN);
|
2016-03-18 19:45:37 +00:00
|
|
|
return EnumActionResult.SUCCESS;
|
2016-01-12 22:05:56 +00:00
|
|
|
}
|
|
|
|
} else
|
|
|
|
{
|
|
|
|
//Both nodes are not master nodes, so normal linking
|
|
|
|
if (pastNode.getMasterPos().equals(node.getMasterPos()))
|
|
|
|
{
|
|
|
|
if (!pastNode.getMasterPos().equals(BlockPos.ORIGIN))
|
|
|
|
{
|
|
|
|
TileEntity testTile = world.getTileEntity(pastNode.getMasterPos());
|
|
|
|
if (testTile instanceof IMasterRoutingNode)
|
|
|
|
{
|
|
|
|
IMasterRoutingNode master = (IMasterRoutingNode) testTile;
|
|
|
|
master.addConnection(pos, containedPos);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pastNode.addConnection(pos);
|
|
|
|
node.addConnection(containedPos);
|
2016-03-26 19:32:51 +00:00
|
|
|
ChatUtil.sendChat(player, TextHelper.localize("chat.BloodMagic.routing.link"));
|
2016-01-12 22:05:56 +00:00
|
|
|
this.setBlockPos(stack, BlockPos.ORIGIN);
|
2016-03-18 19:45:37 +00:00
|
|
|
return EnumActionResult.SUCCESS;
|
2016-01-12 22:05:56 +00:00
|
|
|
} else if (pastNode.getMasterPos().equals(BlockPos.ORIGIN)) //pastNode is not connected to a master, but node is
|
|
|
|
{
|
|
|
|
TileEntity tile = world.getTileEntity(node.getMasterPos());
|
|
|
|
if (tile instanceof IMasterRoutingNode)
|
|
|
|
{
|
|
|
|
IMasterRoutingNode master = (IMasterRoutingNode) tile;
|
|
|
|
master.addConnection(pos, containedPos);
|
|
|
|
master.addNodeToList(pastNode);
|
|
|
|
pastNode.connectMasterToRemainingNode(world, new LinkedList<BlockPos>(), master);
|
|
|
|
}
|
|
|
|
pastNode.addConnection(pos);
|
|
|
|
node.addConnection(containedPos);
|
2016-03-26 19:32:51 +00:00
|
|
|
ChatUtil.sendChat(player, TextHelper.localize("chat.BloodMagic.routing.link"));
|
2016-01-12 22:05:56 +00:00
|
|
|
this.setBlockPos(stack, BlockPos.ORIGIN);
|
2016-03-18 19:45:37 +00:00
|
|
|
return EnumActionResult.SUCCESS;
|
2016-01-12 22:05:56 +00:00
|
|
|
} else if (node.getMasterPos().equals(BlockPos.ORIGIN)) //node is not connected to a master, but pastNode is
|
|
|
|
{
|
|
|
|
TileEntity tile = world.getTileEntity(pastNode.getMasterPos());
|
|
|
|
if (tile instanceof IMasterRoutingNode)
|
|
|
|
{
|
|
|
|
IMasterRoutingNode master = (IMasterRoutingNode) tile;
|
|
|
|
master.addConnection(pos, containedPos);
|
|
|
|
master.addNodeToList(node);
|
|
|
|
node.connectMasterToRemainingNode(world, new LinkedList<BlockPos>(), master);
|
|
|
|
}
|
|
|
|
pastNode.addConnection(pos);
|
|
|
|
node.addConnection(containedPos);
|
2016-03-26 19:32:51 +00:00
|
|
|
ChatUtil.sendChat(player, TextHelper.localize("chat.BloodMagic.routing.link"));
|
2016-01-12 22:05:56 +00:00
|
|
|
this.setBlockPos(stack, BlockPos.ORIGIN);
|
2016-03-18 19:45:37 +00:00
|
|
|
return EnumActionResult.SUCCESS;
|
2016-01-12 22:05:56 +00:00
|
|
|
} else
|
|
|
|
{
|
|
|
|
this.setBlockPos(stack, BlockPos.ORIGIN);
|
2016-03-18 19:45:37 +00:00
|
|
|
return EnumActionResult.SUCCESS;
|
2016-01-12 22:05:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-18 19:45:37 +00:00
|
|
|
return EnumActionResult.FAIL;
|
2016-01-12 22:05:56 +00:00
|
|
|
}
|
|
|
|
|
2016-03-16 08:10:33 +00:00
|
|
|
@Override
|
2016-03-16 22:41:06 +00:00
|
|
|
public List<Pair<Integer, String>> getVariants()
|
|
|
|
{
|
2016-03-16 08:10:33 +00:00
|
|
|
List<Pair<Integer, String>> ret = new ArrayList<Pair<Integer, String>>();
|
|
|
|
ret.add(new ImmutablePair<Integer, String>(0, "type=normal"));
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2016-01-12 22:05:56 +00:00
|
|
|
public BlockPos getBlockPos(ItemStack stack)
|
|
|
|
{
|
|
|
|
stack = NBTHelper.checkNBT(stack);
|
|
|
|
return new BlockPos(stack.getTagCompound().getInteger(Constants.NBT.X_COORD), stack.getTagCompound().getInteger(Constants.NBT.Y_COORD), stack.getTagCompound().getInteger(Constants.NBT.Z_COORD));
|
|
|
|
}
|
|
|
|
|
|
|
|
public ItemStack setBlockPos(ItemStack stack, BlockPos pos)
|
|
|
|
{
|
|
|
|
NBTHelper.checkNBT(stack);
|
|
|
|
NBTTagCompound itemTag = stack.getTagCompound();
|
|
|
|
itemTag.setInteger(Constants.NBT.X_COORD, pos.getX());
|
|
|
|
itemTag.setInteger(Constants.NBT.Y_COORD, pos.getY());
|
|
|
|
itemTag.setInteger(Constants.NBT.Z_COORD, pos.getZ());
|
|
|
|
return stack;
|
|
|
|
}
|
|
|
|
}
|