Search in sources :

Example 1 with ICapabilityLastRelay

use of ValkyrienWarfareControl.Capability.ICapabilityLastRelay in project Valkyrien-Warfare-Revamped by ValkyrienWarfare.

the class ControlEventsCommon method onAttachCapabilityEventItem.

@SubscribeEvent
public void onAttachCapabilityEventItem(AttachCapabilitiesEvent event) {
    if (event instanceof AttachCapabilitiesEvent.Item) {
        AttachCapabilitiesEvent.Item itemEvent = (AttachCapabilitiesEvent.Item) event;
        ItemStack stack = itemEvent.getItemStack();
        if (itemEvent.getItem() instanceof ItemRelayWire) {
            //				System.out.println("Obama?");
            event.addCapability(new ResourceLocation(ValkyrienWarfareControlMod.MODID, "LastRelay"), new ICapabilitySerializable<NBTTagIntArray>() {

                ICapabilityLastRelay inst = ValkyrienWarfareControlMod.lastRelayCapability.getDefaultInstance();

                @Override
                public boolean hasCapability(Capability<?> capability, EnumFacing facing) {
                    return capability == ValkyrienWarfareControlMod.lastRelayCapability;
                }

                @Override
                public <T> T getCapability(Capability<T> capability, EnumFacing facing) {
                    return capability == ValkyrienWarfareControlMod.lastRelayCapability ? ValkyrienWarfareControlMod.lastRelayCapability.<T>cast(inst) : null;
                }

                @Override
                public NBTTagIntArray serializeNBT() {
                    return (NBTTagIntArray) ValkyrienWarfareControlMod.lastRelayCapability.getStorage().writeNBT(ValkyrienWarfareControlMod.lastRelayCapability, inst, null);
                }

                @Override
                public void deserializeNBT(NBTTagIntArray nbt) {
                    ValkyrienWarfareControlMod.lastRelayCapability.getStorage().readNBT(ValkyrienWarfareControlMod.lastRelayCapability, inst, null, nbt);
                }
            });
        }
    }
}
Also used : EnumFacing(net.minecraft.util.EnumFacing) NBTTagIntArray(net.minecraft.nbt.NBTTagIntArray) AttachCapabilitiesEvent(net.minecraftforge.event.AttachCapabilitiesEvent) ItemRelayWire(ValkyrienWarfareControl.Item.ItemRelayWire) ResourceLocation(net.minecraft.util.ResourceLocation) ICapabilityLastRelay(ValkyrienWarfareControl.Capability.ICapabilityLastRelay) ItemStack(net.minecraft.item.ItemStack) SubscribeEvent(net.minecraftforge.fml.common.eventhandler.SubscribeEvent)

Example 2 with ICapabilityLastRelay

use of ValkyrienWarfareControl.Capability.ICapabilityLastRelay in project Valkyrien-Warfare-Revamped by ValkyrienWarfare.

the class ItemRelayWire method onItemUse.

@Override
public EnumActionResult onItemUse(ItemStack stack, EntityPlayer playerIn, World worldIn, BlockPos pos, EnumHand hand, EnumFacing facing, float hitX, float hitY, float hitZ) {
    IBlockState clickedState = worldIn.getBlockState(pos);
    Block block = clickedState.getBlock();
    TileEntity currentTile = worldIn.getTileEntity(pos);
    if (currentTile instanceof INodeProvider && !worldIn.isRemote) {
        ICapabilityLastRelay inst = stack.getCapability(ValkyrienWarfareControlMod.lastRelayCapability, null);
        if (inst != null) {
            if (!inst.hasLastRelay()) {
                inst.setLastRelay(pos);
            //Draw a wire in the player's hand after this
            } else {
                BlockPos lastPos = inst.getLastRelay();
                double distance = lastPos.distanceSq(pos);
                TileEntity lastPosTile = worldIn.getTileEntity(lastPos);
                if (!lastPos.equals(pos) && lastPosTile != null && currentTile != null) {
                    if (distance < range * range) {
                        Node lastPosNode = ((INodeProvider) lastPosTile).getNode();
                        Node currentPosNode = ((INodeProvider) currentTile).getNode();
                        //Connect the two bastards
                        //							inst.setLastRelay(pos);
                        inst.setLastRelay(null);
                        if (lastPosNode != null && currentPosNode != null) {
                            currentPosNode.linkNode(lastPosNode);
                        }
                        //							System.out.println("Success");
                        stack.damageItem(1, playerIn);
                    } else {
                        playerIn.addChatComponentMessage(new TextComponentString("Nodes are too far away, try better wire"));
                        inst.setLastRelay(null);
                    }
                } else {
                    inst.setLastRelay(pos);
                }
            }
        }
    }
    return EnumActionResult.FAIL;
}
Also used : TileEntity(net.minecraft.tileentity.TileEntity) IBlockState(net.minecraft.block.state.IBlockState) Node(ValkyrienWarfareControl.GraphTheory.Node) Block(net.minecraft.block.Block) INodeProvider(ValkyrienWarfareControl.GraphTheory.INodeProvider) ICapabilityLastRelay(ValkyrienWarfareControl.Capability.ICapabilityLastRelay) BlockPos(net.minecraft.util.math.BlockPos) TextComponentString(net.minecraft.util.text.TextComponentString)

Aggregations

ICapabilityLastRelay (ValkyrienWarfareControl.Capability.ICapabilityLastRelay)2 INodeProvider (ValkyrienWarfareControl.GraphTheory.INodeProvider)1 Node (ValkyrienWarfareControl.GraphTheory.Node)1 ItemRelayWire (ValkyrienWarfareControl.Item.ItemRelayWire)1 Block (net.minecraft.block.Block)1 IBlockState (net.minecraft.block.state.IBlockState)1 ItemStack (net.minecraft.item.ItemStack)1 NBTTagIntArray (net.minecraft.nbt.NBTTagIntArray)1 TileEntity (net.minecraft.tileentity.TileEntity)1 EnumFacing (net.minecraft.util.EnumFacing)1 ResourceLocation (net.minecraft.util.ResourceLocation)1 BlockPos (net.minecraft.util.math.BlockPos)1 TextComponentString (net.minecraft.util.text.TextComponentString)1 AttachCapabilitiesEvent (net.minecraftforge.event.AttachCapabilitiesEvent)1 SubscribeEvent (net.minecraftforge.fml.common.eventhandler.SubscribeEvent)1