BloodMagic/src/main/java/WayofTime/bloodmagic/item/routing/ItemNodeRouter.java

199 lines
10 KiB
Java
Raw Normal View History

package WayofTime.bloodmagic.item.routing;
import WayofTime.bloodmagic.BloodMagic;
import WayofTime.bloodmagic.apibutnotreally.Constants;
import WayofTime.bloodmagic.apibutnotreally.iface.INodeRenderer;
import WayofTime.bloodmagic.apibutnotreally.util.helper.NBTHelper;
2016-03-16 01:10:33 -07:00
import WayofTime.bloodmagic.client.IVariantProvider;
import WayofTime.bloodmagic.routing.IMasterRoutingNode;
import WayofTime.bloodmagic.routing.IRoutingNode;
import WayofTime.bloodmagic.util.helper.TextHelper;
2017-08-15 20:21:54 -07:00
import net.minecraft.client.util.ITooltipFlag;
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;
import net.minecraft.util.EnumActionResult;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.EnumHand;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.text.TextComponentTranslation;
import net.minecraft.world.World;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
2016-03-16 01:10:33 -07:00
import org.apache.commons.lang3.tuple.ImmutablePair;
import org.apache.commons.lang3.tuple.Pair;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
2017-08-15 21:30:48 -07:00
public class ItemNodeRouter extends Item implements INodeRenderer, IVariantProvider {
public ItemNodeRouter() {
setUnlocalizedName(BloodMagic.MODID + ".nodeRouter");
setMaxStackSize(1);
setCreativeTab(BloodMagic.TAB_BM);
}
@Override
@SideOnly(Side.CLIENT)
2017-08-15 21:30:48 -07:00
public void addInformation(ItemStack stack, World world, List<String> tooltip, ITooltipFlag flag) {
if (!stack.hasTagCompound())
return;
NBTTagCompound tag = stack.getTagCompound();
BlockPos coords = getBlockPos(stack);
2017-08-15 21:30:48 -07:00
if (coords != null && tag != null) {
2017-01-02 01:18:29 -08:00
tooltip.add(TextHelper.localizeEffect("tooltip.bloodmagic.telepositionFocus.coords", coords.getX(), coords.getY(), coords.getZ()));
}
}
2016-03-16 01:10:33 -07:00
@Override
2017-08-15 21:30:48 -07:00
public EnumActionResult onItemUseFirst(EntityPlayer player, World world, BlockPos pos, EnumFacing side, float hitX, float hitY, float hitZ, EnumHand hand) {
2017-01-01 22:26:42 -08:00
ItemStack stack = player.getHeldItem(hand);
2017-08-15 21:30:48 -07:00
if (world.isRemote) {
return EnumActionResult.PASS;
}
TileEntity tileHit = world.getTileEntity(pos);
2017-08-15 21:30:48 -07:00
if (!(tileHit instanceof IRoutingNode)) {
// TODO: Remove contained position?
BlockPos containedPos = getBlockPos(stack);
2017-08-15 21:30:48 -07:00
if (!containedPos.equals(BlockPos.ORIGIN)) {
this.setBlockPos(stack, BlockPos.ORIGIN);
player.sendStatusMessage(new TextComponentTranslation("chat.bloodmagic.routing.remove"), true);
return EnumActionResult.FAIL;
}
return EnumActionResult.FAIL;
}
IRoutingNode node = (IRoutingNode) tileHit;
BlockPos containedPos = getBlockPos(stack);
2017-08-15 21:30:48 -07:00
if (containedPos.equals(BlockPos.ORIGIN)) {
this.setBlockPos(stack, pos);
player.sendStatusMessage(new TextComponentTranslation("chat.bloodmagic.routing.set"), true);
return EnumActionResult.SUCCESS;
2017-08-15 21:30:48 -07:00
} else {
TileEntity pastTile = world.getTileEntity(containedPos);
2017-08-15 21:30:48 -07:00
if (pastTile instanceof IRoutingNode) {
IRoutingNode pastNode = (IRoutingNode) pastTile;
2017-08-15 21:30:48 -07:00
if (pastNode instanceof IMasterRoutingNode) {
IMasterRoutingNode master = (IMasterRoutingNode) pastNode;
2017-08-15 21:30:48 -07:00
if (!node.isMaster(master)) {
if (node.getMasterPos().equals(BlockPos.ORIGIN)) //If the node is not the master and it is receptive
{
node.connectMasterToRemainingNode(world, new LinkedList<BlockPos>(), master);
master.addConnection(pos, containedPos);
master.addNodeToList(node);
node.addConnection(containedPos);
player.sendStatusMessage(new TextComponentTranslation("chat.bloodmagic.routing.link.master"), true);
this.setBlockPos(stack, BlockPos.ORIGIN);
return EnumActionResult.SUCCESS;
}
2017-08-15 21:30:48 -07:00
} else {
master.addConnection(pos, containedPos);
node.addConnection(containedPos);
player.sendStatusMessage(new TextComponentTranslation("chat.bloodmagic.routing.link.master"), true);
this.setBlockPos(stack, BlockPos.ORIGIN);
return EnumActionResult.SUCCESS;
}
2017-08-15 21:30:48 -07:00
} else if (node instanceof IMasterRoutingNode) {
IMasterRoutingNode master = (IMasterRoutingNode) node;
2017-08-15 21:30:48 -07:00
if (!pastNode.isMaster(master)) {
if (pastNode.getMasterPos().equals(BlockPos.ORIGIN)) //TODO: This is where the issue is
{
pastNode.connectMasterToRemainingNode(world, new LinkedList<BlockPos>(), master);
master.addConnection(pos, containedPos);
pastNode.addConnection(pos);
master.addNodeToList(pastNode);
player.sendStatusMessage(new TextComponentTranslation("chat.bloodmagic.routing.link.master"), true);
this.setBlockPos(stack, BlockPos.ORIGIN);
return EnumActionResult.SUCCESS;
}
2017-08-15 21:30:48 -07:00
} else {
master.addConnection(pos, containedPos);
pastNode.addConnection(pos);
player.sendStatusMessage(new TextComponentTranslation("chat.bloodmagic.routing.link.master"), true);
this.setBlockPos(stack, BlockPos.ORIGIN);
return EnumActionResult.SUCCESS;
}
2017-08-15 21:30:48 -07:00
} else {
//Both nodes are not master nodes, so normal linking
2017-08-15 21:30:48 -07:00
if (pastNode.getMasterPos().equals(node.getMasterPos())) {
if (!pastNode.getMasterPos().equals(BlockPos.ORIGIN)) {
TileEntity testTile = world.getTileEntity(pastNode.getMasterPos());
2017-08-15 21:30:48 -07:00
if (testTile instanceof IMasterRoutingNode) {
IMasterRoutingNode master = (IMasterRoutingNode) testTile;
master.addConnection(pos, containedPos);
}
}
pastNode.addConnection(pos);
node.addConnection(containedPos);
player.sendStatusMessage(new TextComponentTranslation("chat.bloodmagic.routing.link.master"), true);
this.setBlockPos(stack, BlockPos.ORIGIN);
return EnumActionResult.SUCCESS;
} else if (pastNode.getMasterPos().equals(BlockPos.ORIGIN)) //pastNode is not connected to a master, but node is
{
TileEntity tile = world.getTileEntity(node.getMasterPos());
2017-08-15 21:30:48 -07:00
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);
player.sendStatusMessage(new TextComponentTranslation("chat.bloodmagic.routing.link"), true);
this.setBlockPos(stack, BlockPos.ORIGIN);
return EnumActionResult.SUCCESS;
} else if (node.getMasterPos().equals(BlockPos.ORIGIN)) //node is not connected to a master, but pastNode is
{
TileEntity tile = world.getTileEntity(pastNode.getMasterPos());
2017-08-15 21:30:48 -07:00
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);
player.sendStatusMessage(new TextComponentTranslation("chat.bloodmagic.routing.link"), true);
this.setBlockPos(stack, BlockPos.ORIGIN);
return EnumActionResult.SUCCESS;
2017-08-15 21:30:48 -07:00
} else {
this.setBlockPos(stack, BlockPos.ORIGIN);
return EnumActionResult.SUCCESS;
}
}
}
}
return EnumActionResult.FAIL;
}
2016-03-16 01:10:33 -07:00
@Override
2017-08-15 21:30:48 -07:00
public List<Pair<Integer, String>> getVariants() {
2016-03-16 01:10:33 -07:00
List<Pair<Integer, String>> ret = new ArrayList<Pair<Integer, String>>();
ret.add(new ImmutablePair<Integer, String>(0, "type=normal"));
return ret;
}
2017-08-15 21:30:48 -07: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));
}
2017-08-15 21:30:48 -07:00
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;
}
}