From 05c78126859231a68e199dc34613689bd0978e2f Mon Sep 17 00:00:00 2001 From: Foghrye4 Date: Mon, 11 Apr 2016 19:44:54 +0300 Subject: Initial commit --- ihl/items_blocks/FlexibleCableItem.java | 573 ++++++++++++++++++++++++++++++++ 1 file changed, 573 insertions(+) create mode 100644 ihl/items_blocks/FlexibleCableItem.java (limited to 'ihl/items_blocks/FlexibleCableItem.java') diff --git a/ihl/items_blocks/FlexibleCableItem.java b/ihl/items_blocks/FlexibleCableItem.java new file mode 100644 index 0000000..11023c4 --- /dev/null +++ b/ihl/items_blocks/FlexibleCableItem.java @@ -0,0 +1,573 @@ +package ihl.items_blocks; + +import java.util.ArrayList; +import java.util.HashSet; +import java.util.Iterator; +import java.util.List; +import java.util.Map; +import java.util.Set; + +import cpw.mods.fml.common.eventhandler.SubscribeEvent; +import cpw.mods.fml.common.gameevent.PlayerEvent.PlayerChangedDimensionEvent; +import cpw.mods.fml.common.registry.GameRegistry; +import cpw.mods.fml.relauncher.Side; +import cpw.mods.fml.relauncher.SideOnly; +import net.minecraft.block.Block; +import net.minecraft.client.renderer.texture.IIconRegister; +import net.minecraft.creativetab.CreativeTabs; +import net.minecraft.entity.Entity; +import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.init.Blocks; +import net.minecraft.item.Item; +import net.minecraft.item.ItemStack; +import net.minecraft.nbt.NBTBase; +import net.minecraft.nbt.NBTTagCompound; +import net.minecraft.server.MinecraftServer; +import net.minecraft.tileentity.TileEntity; +import net.minecraft.util.AxisAlignedBB; +import net.minecraft.util.IIcon; +import net.minecraft.util.MathHelper; +import net.minecraft.util.MovingObjectPosition; +import net.minecraft.util.Vec3; +import net.minecraft.world.World; +import net.minecraftforge.common.util.ForgeDirection; +import ic2.api.energy.tile.IEnergyTile; +import ihl.IHLCreativeTab; +import ihl.IHLMod; +import ihl.IHLModInfo; +import ihl.flexible_cable.AnchorTileEntity; +import ihl.flexible_cable.NodeEntity; +import ihl.flexible_cable.PowerCableNodeEntity; +import ihl.interfaces.ICableHolder; +import ihl.interfaces.IDataCableHolder; +import ihl.interfaces.IEnergyNetNode; +import ihl.interfaces.IMultiPowerCableHolder; +import ihl.interfaces.IWire; +import ihl.utils.IHLUtils; + +public class FlexibleCableItem extends Item implements IWire { + + @SideOnly(Side.CLIENT) + IIcon thickCopper, + thinIron, + thickIron, + insulatedCopperRawruber, + insulatedThickCopperRawruber, + insulatedIronRawruber, + insulatedThickIronRawruber, + insulatedCopperRuber, + insulatedThickCopperRuber, + insulatedIronRuber, + insulatedThickIronRuber; + public static FlexibleCableItem instance; + public final Set yellowColoredWires = new HashSet(3); + public boolean isDataCable=false; + + public FlexibleCableItem() + { + super(); + yellowColoredWires.add("Gold"); + yellowColoredWires.add("Copper"); + yellowColoredWires.add("Bronze"); + this.setCreativeTab(IHLCreativeTab.tab); + this.setMaxStackSize(1); + this.setFull3D(); + instance=this; + } + + @Override + @SideOnly(Side.CLIENT) + public void getSubItems(Item item, CreativeTabs tabs, List itemList) + { + itemList.add(IHLUtils.getUninsulatedWire("Copper", 160, 15)); + itemList.add(IHLUtils.getUninsulatedWire("Copper", 160, 240)); + itemList.add(IHLUtils.getUninsulatedWire("Steel", 160, 15)); + itemList.add(IHLUtils.getUninsulatedWire("Steel", 160, 240)); + itemList.add(IHLUtils.getInsulatedWire("Copper", 160, 15,"RawRubber",100)); + itemList.add(IHLUtils.getInsulatedWire("Copper", 160, 240,"RawRubber",100)); + itemList.add(IHLUtils.getInsulatedWire("Steel", 160, 15,"RawRubber",100)); + itemList.add(IHLUtils.getInsulatedWire("Steel", 160, 240,"RawRubber",100)); + itemList.add(IHLUtils.getInsulatedWire("Copper", 160, 15,"Rubber",100)); + itemList.add(IHLUtils.getInsulatedWire("Copper", 160, 240,"Rubber",100)); + itemList.add(IHLUtils.getInsulatedWire("Steel", 160, 15,"Rubber",100)); + itemList.add(IHLUtils.getInsulatedWire("Steel", 160, 240,"Rubber",100)); + } + + public static void init() + { + GameRegistry.registerItem(new FlexibleCableItem(),"copperWire"); + } + + public boolean onItemUse(ItemStack stack, EntityPlayer player, World world, int x, int y, int z, int par7, float par8, float par9, float par10) + { + TileEntity t = world.getTileEntity(x, y, z); + short facing = IHLUtils.getFacingFromPlayerView(player, true); + if(!world.isRemote && t instanceof IEnergyTile && !(t instanceof IMultiPowerCableHolder) && !(t instanceof ICableHolder)) + { + ForgeDirection direction = ForgeDirection.getOrientation(facing); + x+=direction.offsetX; + y+=direction.offsetY; + z+=direction.offsetZ; + t=world.getTileEntity(x, y, z); + if(IHLUtils.isBlockCanBeReplaced(world, x,y,z)) + { + world.setBlock(x, y, z, IHLMod.cableAnchorBlock); + } + else if(!(t instanceof AnchorTileEntity)) + { + return false; + } + t=world.getTileEntity(x, y, z); + } + if(world.isRemote)world.playSoundEffect(x + 0.5F, y + 0.5F, z + 0.5F, "step.stone", 1.0F, 0.8F); + if(!world.isRemote && t!=null && + ((t instanceof IEnergyNetNode && !isDataCable) || + (t instanceof IMultiPowerCableHolder && !isDataCable) || + (t instanceof IDataCableHolder && isDataCable)) && + stack.stackTagCompound.getInteger("fullLength")>=1) + { + double ppx,ppy,ppz; + ICableHolder te; + if(t instanceof IMultiPowerCableHolder) + { + facing = ((IMultiPowerCableHolder)t).getSide(player); + if(facing==-1) + { + return false; + } + else + { + te = ((IMultiPowerCableHolder)t).getEnergyNetNode(facing); + } + } + else + { + te = (ICableHolder)t; + } + ppx=te.getPortPos(player)[0]; + ppy=te.getPortPos(player)[1]; + ppz=te.getPortPos(player)[2]; + if(stack.stackTagCompound.getBoolean("firstConnection")) + { + int x1 = stack.stackTagCompound.getInteger("connectorX"); + int y1 = stack.stackTagCompound.getInteger("connectorY"); + int z1 = stack.stackTagCompound.getInteger("connectorZ"); + int l1=stack.stackTagCompound.getInteger("fullLength"); + int l2=stack.stackTagCompound.getInteger("length"); + if(x==x1 && y==y1 && z==z1) + { + this.cleanUp(stack.stackTagCompound.getInteger("chainUID")); + stack.stackTagCompound.setBoolean("firstConnection", false); + stack.stackTagCompound.setInteger("length", l1); + return true; + } + int t2DimensionId = stack.stackTagCompound.getInteger("connectorDimensionId"); + short facing2 = stack.stackTagCompound.getShort("connectorFacing"); + TileEntity t2 = MinecraftServer.getServer().worldServerForDimension(t2DimensionId).getTileEntity(x1, y1, z1); + if(t2==null) + { + stack.stackTagCompound.setBoolean("firstConnection", false); + return false; + } + else + { + if(t2 instanceof IMultiPowerCableHolder) + { + te = ((IMultiPowerCableHolder)t).getEnergyNetNode(facing); + } + else + { + te = (ICableHolder)t; + } + te.setCableCheck(true); + } + this.connect(t,facing, t2, facing2, stack); + stack.stackTagCompound.setInteger("fullLength", l2); + if(l2<=0) + { + stack.stackSize--; + } + stack.stackTagCompound.setBoolean("firstConnection", false); + stack.stackTagCompound.setInteger("chainArrangeNumber",stack.stackTagCompound.getInteger("chainArrangeNumber")+1); + x=x1; + y=y1; + z=z1; + } + else + { + te.setCableCheck(false); + stack.stackTagCompound.setBoolean("firstConnection", true); + stack.stackTagCompound.setInteger("connectorX", x); + stack.stackTagCompound.setInteger("connectorY", y); + stack.stackTagCompound.setInteger("connectorZ", z); + stack.stackTagCompound.setShort("connectorFacing", facing); + stack.stackTagCompound.setInteger("connectorDimensionId", world.provider.dimensionId); + stack.stackTagCompound.setInteger("prevDimensionId", world.provider.dimensionId); + stack.stackTagCompound.setInteger("chainArrangeNumber",0); + stack.stackTagCompound.setInteger("chainUID",world.rand.nextInt()); + stack.stackTagCompound.setDouble("prevNodePosX",ppx); + stack.stackTagCompound.setDouble("prevNodePosY",ppy); + stack.stackTagCompound.setDouble("prevNodePosZ",ppz); + } + NodeEntity node = newNode(world, player.posX, player.posY, player.posZ, stack, stack.stackTagCompound.getInteger("chainArrangeNumber"),x,y,z); + node.virtualNodePosX=ppx; + node.virtualNodePosY=ppy; + node.virtualNodePosZ=ppz; + return true; + } + else + { + return false; + } + } + + protected void connect(TileEntity t1,short facing, TileEntity t2, short facing2, ItemStack stack) + { + IEnergyNetNode te; + IEnergyNetNode te1; + if(t1 instanceof IEnergyNetNode) + { + te = (IEnergyNetNode)t1; + } + else + { + te = ((IMultiPowerCableHolder)t1).getEnergyNetNode(facing); + } + if(t2 instanceof IEnergyNetNode) + { + te1 = (IEnergyNetNode)t2; + } + else + { + te1 = ((IMultiPowerCableHolder)t2).getEnergyNetNode(facing2); + } + int newGridID=IHLMod.enet.mergeGrids(te.getGridID(),te1.getGridID()); + int l1=stack.stackTagCompound.getInteger("fullLength"); + int l2=stack.stackTagCompound.getInteger("length"); + NBTTagCompound cable = (NBTTagCompound) stack.stackTagCompound.copy(); + cable.setInteger("length", l1-l2); + cable.setBoolean("firstConnection", false); + cable.setInteger("connectorX1", t1.xCoord); + cable.setInteger("connectorY1", t1.yCoord); + cable.setInteger("connectorZ1", t1.zCoord); + cable.setShort("connectorFacing1", facing); + cable.setInteger("connectorDimensionId1", t1.getWorldObj().provider.dimensionId); + if(te.addCable(cable) && te1.addCable(cable)) + { + te.setGrid(newGridID); + te1.setGrid(newGridID); + } + } + + private int getTransverseSection(ItemStack stack) + { + if(stack.stackTagCompound==null) + { + stack.stackTagCompound=new NBTTagCompound(); + } + return stack.stackTagCompound.getInteger("transverseSection"); + } + + public String getMaterial(ItemStack stack) + { + if(stack.stackTagCompound==null) + { + stack.stackTagCompound=new NBTTagCompound(); + } + return stack.stackTagCompound.getString("material"); + } + + public int getVoltageLimit(ItemStack stack) + { + if(stack.stackTagCompound==null) + { + stack.stackTagCompound=new NBTTagCompound(); + } + if(stack.stackTagCompound.hasKey("maxVoltage")) + { + return stack.stackTagCompound.getInteger("maxVoltage"); + } + return 0; + } + + private long getResistance(ItemStack stack) + { + if(stack.stackTagCompound==null) + { + stack.stackTagCompound=new NBTTagCompound(); + } + return IHLUtils.getResistance(stack.stackTagCompound); + } + + private void cleanUp(int uid) + { + Set cs = IHLMod.proxy.nodeEntityRegistry.get(uid); + if(cs!=null) + { + for(NodeEntity ne:cs) + { + if(ne!=null) + { + ne.setDead(); + } + } + } + } + + @Override + public void onUpdate(ItemStack stack, World world, Entity entity, int slotIndex, boolean isCurrentItem) + { + if(!world.isRemote && stack.stackTagCompound!=null) + { + if(stack.stackTagCompound.getBoolean("firstConnection")) + { + int leight = stack.stackTagCompound.getInteger("length"); + if(leight>0) + { + double pnpx = stack.stackTagCompound.getDouble("prevNodePosX"); + double pnpy = stack.stackTagCompound.getDouble("prevNodePosY"); + double pnpz = stack.stackTagCompound.getDouble("prevNodePosZ"); + double ppx=entity.posX; + double ppy=entity.posY; + double ppz=entity.posZ; + + if(pnpx!=0D && pnpy!=0D && pnpz!=0D) + { + double sqd = (ppx - pnpx)*(ppx - pnpx) + (ppy - pnpy)*(ppy - pnpy) + (ppz - pnpz)*(ppz - pnpz); + if(sqd>2D) + { + int can = stack.stackTagCompound.getInteger("chainArrangeNumber")+1; + int cx = stack.stackTagCompound.getInteger("connectorX"); + int cy = stack.stackTagCompound.getInteger("connectorY"); + int cz = stack.stackTagCompound.getInteger("connectorZ"); + NodeEntity node = newNode(world, ppx, ppy, ppz, stack, can,cx,cy,cz); + node.shouldFollowPlayer=true; + node.virtualNodePosX=ppx+(ppx - pnpx)*2; + node.virtualNodePosY=ppy+(ppy - pnpy)*2; + node.virtualNodePosZ=ppz+(ppz - pnpz)*2; + stack.stackTagCompound.setInteger("chainArrangeNumber",can); + stack.stackTagCompound.setDouble("prevNodePosX",ppx); + stack.stackTagCompound.setDouble("prevNodePosY",ppy); + stack.stackTagCompound.setDouble("prevNodePosZ",ppz); + stack.stackTagCompound.setInteger("length",--leight); + stack.stackTagCompound.setInteger("prevDimensionId", world.provider.dimensionId); + } + } + } + } + } + } + + + public void onPlayerTeleport(PlayerChangedDimensionEvent event) + { + EntityPlayer player = event.player; + for(ItemStack stack:player.inventory.mainInventory) + { + if(stack!=null && stack.getItem() instanceof FlexibleCableItem) + { + if(stack.stackTagCompound.getBoolean("firstConnection")) + { + int can = -1; + Set ns = IHLMod.proxy.nodeEntityRegistry.get(stack.stackTagCompound.getInteger("chainUID")); + if(ns!=null) + { + NodeEntity lastNode = null; + for(NodeEntity node:ns) + { + node.shouldFollowPlayer=false; + if(node.chainArrangeNumber>can) + { + lastNode=node; + can=node.chainArrangeNumber; + } + } + if(lastNode!=null) + { + lastNode.setVirtualNodePosToNearestPortal(); + } + } + double[] cc = IHLUtils.tracePlayerView(player); + can = stack.stackTagCompound.getInteger("chainArrangeNumber")+1; + int cx = stack.stackTagCompound.getInteger("connectorX"); + int cy = stack.stackTagCompound.getInteger("connectorY"); + int cz = stack.stackTagCompound.getInteger("connectorZ"); + int leight = stack.stackTagCompound.getInteger("length"); + NodeEntity node = newNode(MinecraftServer.getServer().worldServerForDimension(event.toDim), cc[0], player.posY, cc[2], stack, can,cx,cy,cz); + node.setVirtualNodePosToNearestPortal(); + stack.stackTagCompound.setInteger("chainArrangeNumber",can); + stack.stackTagCompound.setDouble("prevNodePosX",cc[0]); + stack.stackTagCompound.setDouble("prevNodePosY",cc[1]); + stack.stackTagCompound.setDouble("prevNodePosZ",cc[2]); + stack.stackTagCompound.setInteger("length",--leight); + stack.stackTagCompound.setInteger("prevDimensionId", event.toDim); + } + } + } + } + + + protected NodeEntity newNode(World world, double ppx, double ppy, double ppz, ItemStack stack, int can, int x, int y, int z) + { + PowerCableNodeEntity node = new PowerCableNodeEntity(world); + node.setPosition(ppx, ppy, ppz); + node.setChainUniqueID(stack.stackTagCompound.getInteger("chainUID")); + short facing = stack.stackTagCompound.getShort("connectorFacing"); + int dimensionId = stack.stackTagCompound.getInteger("connectorDimensionId"); + node.setAnchor(x, y, z,facing,dimensionId); + node.chainArrangeNumber=can; + node.colorIndex = this.getColor(stack); + node.addCable((NBTTagCompound) stack.stackTagCompound.copy()); + if(stack.stackTagCompound.hasKey("insulationMaterial")) + { + node.type=1; + } + else + { + node.type=0; + } + world.spawnEntityInWorld(node); + return node; + } + + private int getColor(ItemStack stack) + { + if(!stack.stackTagCompound.hasKey("insulationMaterial")) + { + if(yellowColoredWires.contains(this.getMaterial(stack))) + { + return 0xFF9900; + } + else + { + return 0xAAAABB; + } + } + return 0xFFFFFF; + } + + @Override + public void addInformation(ItemStack itemStack, EntityPlayer player, List info, boolean flag) + { + if(itemStack.stackTagCompound!=null) + { + info.add("Length: " + itemStack.stackTagCompound.getInteger("length") +" m"); + info.add("Wire material: " + this.getMaterial(itemStack)); + info.add("Resistivity: " + this.getResistance(itemStack)/1000F + " V^2/(EU*m)"); + info.add("Transverse section: " + this.getTransverseSection(itemStack)/10F + " sq. mm."); + if(itemStack.stackTagCompound.hasKey("insulationMaterial")) + { + info.add("Insulation material: "+this.getInsulationMaterial(itemStack)); + info.add("Insulation thickness: " + this.getInsulationThickness(itemStack)/10f+" mm"); + info.add("Insulation breakdown voltage: "+ this.getVoltageLimit(itemStack)/1000 + " kV"); + } + + } + } + + @Override + public String getTag() + { + return "length"; + } + + @Override + public String getTagSecondary() + { + return "fullLength"; + } + + @Override + @SideOnly(Side.CLIENT) + public void registerIcons(IIconRegister par1IconRegister) + { + this.itemIcon = par1IconRegister.registerIcon(IHLModInfo.MODID + ":copperWire"); + this.thickCopper = par1IconRegister.registerIcon(IHLModInfo.MODID + ":copperWire16x"); + this.thinIron = par1IconRegister.registerIcon(IHLModInfo.MODID + ":steelWire"); + this.thickIron = par1IconRegister.registerIcon(IHLModInfo.MODID + ":steelWire16x"); + this.insulatedIronRawruber = par1IconRegister.registerIcon(IHLModInfo.MODID + ":flexibleCableSteel2"); + } + + @Override + @SideOnly(Side.CLIENT) + public IIcon getIcon(ItemStack stack, int i) + { + if(stack.stackTagCompound!=null) + { + if(this.getInsulationMaterial(stack).equals("null")) + { + if(this.getTransverseSection(stack)>=240) + { + if(yellowColoredWires.contains(this.getMaterial(stack))) + { + return this.thickCopper; + } + else + { + return this.thickIron; + } + } + else + { + if(yellowColoredWires.contains(this.getMaterial(stack))) + { + return this.itemIcon; + } + else + { + return this.thinIron; + } + } + } + else + { + return this.insulatedIronRawruber; + } + } + return this.itemIcon; + } + + @Override + public String getUnlocalizedName(ItemStack stack) + { + if(stack.stackTagCompound!=null && !this.getInsulationMaterial(stack).equals("null")) + { + return "cable"; + } + return "wire"; + } + + @Override + public boolean isSameWire(ItemStack stack1,ItemStack stack2) + { + if(stack1.getItem()==stack2.getItem() && stack1.getItemDamage()==stack2.getItemDamage()) + { + if(stack1.stackTagCompound!=null && stack2.stackTagCompound!=null) + { + return this.getMaterial(stack1).equals(this.getMaterial(stack2)) && + this.getTransverseSection(stack1)==this.getTransverseSection(stack2) && + this.getInsulationMaterial(stack1).equals(this.getInsulationMaterial(stack2)) && + this.getInsulationThickness(stack1)==this.getInsulationThickness(stack2); + } + } + return false; + } + + public String getInsulationMaterial(ItemStack stack) + { + if(stack.stackTagCompound.hasKey("insulationMaterial")) + { + return stack.stackTagCompound.getString("insulationMaterial"); + } + return "null"; + } + + public int getInsulationThickness(ItemStack stack) + { + if(stack.stackTagCompound.hasKey("insulationThickness")) + { + return stack.stackTagCompound.getInteger("insulationThickness"); + } + return 0; + } +} -- cgit v1.2.3