/* * This file is part of Industrial Wires. * Copyright (C) 2016-2017 malte0811 * * Industrial Wires is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * Industrial Wires is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with Industrial Wires. If not, see . */ package malte0811.industrialWires.items; import blusunrize.immersiveengineering.ImmersiveEngineering; import blusunrize.immersiveengineering.api.Lib; import blusunrize.immersiveengineering.api.TargetingInfo; import blusunrize.immersiveengineering.api.energy.wires.IImmersiveConnectable; import blusunrize.immersiveengineering.api.energy.wires.IWireCoil; import blusunrize.immersiveengineering.api.energy.wires.ImmersiveNetHandler; import blusunrize.immersiveengineering.api.energy.wires.ImmersiveNetHandler.Connection; import blusunrize.immersiveengineering.api.energy.wires.WireType; import blusunrize.immersiveengineering.common.IESaveData; import blusunrize.immersiveengineering.common.util.IEAchievements; import blusunrize.immersiveengineering.common.util.ItemNBTHelper; import blusunrize.immersiveengineering.common.util.Utils; import ic2.api.item.IC2Items; import malte0811.industrialWires.IWConfig; import malte0811.industrialWires.IndustrialWires; import malte0811.industrialWires.wires.IC2Wiretype; import net.minecraft.block.state.IBlockState; import net.minecraft.client.resources.I18n; import net.minecraft.creativetab.CreativeTabs; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.item.Item; import net.minecraft.item.ItemStack; import net.minecraft.nbt.NBTTagInt; import net.minecraft.tileentity.TileEntity; import net.minecraft.util.EnumActionResult; import net.minecraft.util.EnumFacing; import net.minecraft.util.EnumHand; import net.minecraft.util.NonNullList; import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.Vec3d; import net.minecraft.util.text.TextComponentTranslation; import net.minecraft.world.World; import javax.annotation.Nonnull; import java.util.HashSet; import java.util.List; import java.util.Set; public class ItemIC2Coil extends Item implements IWireCoil{ public final static String[] subNames = {"tin", "copper", "gold", "hv", "glass"}; public final static String lengthKey = "wireLength"; public ItemIC2Coil() { setUnlocalizedName(IndustrialWires.MODID + ".ic2_wire_coil"); setHasSubtypes(true); this.setCreativeTab(IndustrialWires.creativeTab); setMaxStackSize(64); ImmersiveEngineering.registerByFullName(this, IndustrialWires.MODID+":"+"ic2WireCoil"); } @Override public void getSubItems(@Nonnull Item itemIn, CreativeTabs tab, NonNullList subItems) { for (int i = 0;i list, boolean adv) { list.add(I18n.format(IndustrialWires.MODID+".desc.wireLength", getLength(stack))); list.add(I18n.format(IndustrialWires.MODID+".desc.recipe")); if(stack.getTagCompound()!=null && stack.getTagCompound().hasKey("linkingPos")) { int[] link = stack.getTagCompound().getIntArray("linkingPos"); if (link.length > 3) { list.add(I18n.format(Lib.DESC_INFO+"attachedToDim", link[1],link[2],link[3],link[0])); } } } //mostly copied from IE @Nonnull @Override public EnumActionResult onItemUseFirst(EntityPlayer player, World world, BlockPos pos, EnumFacing side, float hitX, float hitY, float hitZ, EnumHand hand) { ItemStack stack = player.getHeldItem(hand); if (!world.isRemote && !stack.isEmpty()) { if (stack.getCount() > 1) { player.sendMessage(new TextComponentTranslation(IndustrialWires.MODID + ".chat.stackSize")); return EnumActionResult.FAIL; } TileEntity tileEntity = world.getTileEntity(pos); if(tileEntity instanceof IImmersiveConnectable && ((IImmersiveConnectable)tileEntity).canConnect()) { TargetingInfo target = new TargetingInfo(side, hitX,hitY,hitZ); WireType wire = getWireType(stack); BlockPos masterPos = ((IImmersiveConnectable)tileEntity).getConnectionMaster(wire, target); tileEntity = world.getTileEntity(masterPos); if( !(tileEntity instanceof IImmersiveConnectable) || !((IImmersiveConnectable)tileEntity).canConnect()) { return EnumActionResult.PASS; } if( !((IImmersiveConnectable)tileEntity).canConnectCable(wire, target)) { player.sendMessage(new TextComponentTranslation(Lib.CHAT_WARN + "wrongCable")); return EnumActionResult.FAIL; } if(!ItemNBTHelper.hasKey(stack, "linkingPos")) { ItemNBTHelper.setIntArray(stack, "linkingPos", new int[]{world.provider.getDimension(),masterPos.getX(),masterPos.getY(),masterPos.getZ()}); target.writeToNBT(stack.getTagCompound()); } else { WireType type = getWireType(stack); int[] array = ItemNBTHelper.getIntArray(stack, "linkingPos"); BlockPos linkPos = new BlockPos(array[1],array[2],array[3]); TileEntity tileEntityLinkingPos = world.getTileEntity(linkPos); int distanceSq = (int) Math.ceil( linkPos.distanceSq(masterPos) ); if(array[0]!=world.provider.getDimension()) { player.sendMessage(new TextComponentTranslation(Lib.CHAT_WARN + "wrongDimension")); } else if(linkPos.equals(masterPos)) { player.sendMessage(new TextComponentTranslation(Lib.CHAT_WARN + "sameConnection")); } else if( distanceSq > (type.getMaxLength()*type.getMaxLength())) { player.sendMessage(new TextComponentTranslation(Lib.CHAT_WARN + "tooFar")); } else if(!(tileEntityLinkingPos instanceof IImmersiveConnectable)||!((IImmersiveConnectable) tileEntityLinkingPos).canConnectCable(type, TargetingInfo.readFromNBT(stack.getTagCompound()))) { player.sendMessage(new TextComponentTranslation(Lib.CHAT_WARN + "invalidPoint")); } else { IImmersiveConnectable nodeHere = (IImmersiveConnectable)tileEntity; IImmersiveConnectable nodeLink = (IImmersiveConnectable)tileEntityLinkingPos; boolean connectionExists = false; Set outputs = ImmersiveNetHandler.INSTANCE.getConnections(world, Utils.toCC(nodeHere)); if(outputs!=null) { for(Connection con : outputs) { if(con.end.equals(Utils.toCC(nodeLink))) { connectionExists = true; } } } if(connectionExists) { player.sendMessage(new TextComponentTranslation(Lib.CHAT_WARN + "connectionExists")); } else { Vec3d rtOff0 = nodeHere.getRaytraceOffset(nodeLink).addVector(masterPos.getX(), masterPos.getY(), masterPos.getZ()); Vec3d rtOff1 = nodeLink.getRaytraceOffset(nodeHere).addVector(linkPos.getX(), linkPos.getY(), linkPos.getZ()); Set ignore = new HashSet<>(); ignore.addAll(nodeHere.getIgnored(nodeLink)); ignore.addAll(nodeLink.getIgnored(nodeHere)); boolean canSee = Utils.rayTraceForFirst(rtOff0, rtOff1, world, ignore)==null; if(canSee) { int lengthOnStack = getLength(stack); int length = (int)Math.sqrt(distanceSq); if (length<=lengthOnStack) { TargetingInfo targetLink = TargetingInfo.readFromNBT(stack.getTagCompound()); ImmersiveNetHandler.INSTANCE.addConnection(world, Utils.toCC(nodeHere), Utils.toCC(nodeLink), length, type); nodeHere.connectCable(type, target, nodeLink); nodeLink.connectCable(type, targetLink, nodeHere); IESaveData.setDirty(world.provider.getDimension()); player.addStat(IEAchievements.connectWire); if(!player.capabilities.isCreativeMode) { if (length