From 05c78126859231a68e199dc34613689bd0978e2f Mon Sep 17 00:00:00 2001 From: Foghrye4 Date: Mon, 11 Apr 2016 19:44:54 +0300 Subject: Initial commit --- ihl/tunneling_shield/DriverTileEntity.java | 1138 ++++++++++++++++++++++++++++ 1 file changed, 1138 insertions(+) create mode 100644 ihl/tunneling_shield/DriverTileEntity.java (limited to 'ihl/tunneling_shield/DriverTileEntity.java') diff --git a/ihl/tunneling_shield/DriverTileEntity.java b/ihl/tunneling_shield/DriverTileEntity.java new file mode 100644 index 0000000..2c3f2d8 --- /dev/null +++ b/ihl/tunneling_shield/DriverTileEntity.java @@ -0,0 +1,1138 @@ +package ihl.tunneling_shield; + +import java.util.ArrayList; +import java.util.HashSet; +import java.util.Iterator; +import java.util.List; +import java.util.Set; + +import cpw.mods.fml.relauncher.Side; +import cpw.mods.fml.relauncher.SideOnly; +import ic2.api.energy.event.EnergyTileLoadEvent; +import ic2.api.energy.event.EnergyTileUnloadEvent; +import ic2.api.energy.tile.IEnergySink; +import ic2.api.item.IC2Items; +import ic2.api.network.INetworkClientTileEntityEventListener; +import ic2.api.recipe.RecipeOutput; +import ic2.api.recipe.Recipes; +import ic2.core.ContainerBase; +import ic2.core.IC2; +import ic2.core.IHasGui; +import ic2.core.Ic2Items; +import ic2.core.audio.AudioSource; +import ic2.core.audio.PositionSpec; +import ic2.core.block.TileEntityInventory; +import ic2.core.block.invslot.InvSlotConsumableId; +import ic2.core.block.invslot.InvSlot; +import ic2.core.block.invslot.InvSlotUpgrade; +import ic2.core.block.invslot.InvSlot.Access; +import ic2.core.network.NetworkManager; +import ic2.core.upgrade.IUpgradableBlock; +import ic2.core.upgrade.UpgradableProperty; +import ic2.core.util.StackUtil; +import ihl.IHLMod; +import ihl.utils.IHLInvSlotDischarge; +import ihl.utils.IHLUtils; +import net.minecraft.block.Block; +import net.minecraft.client.gui.GuiScreen; +import net.minecraft.entity.Entity; +import net.minecraft.entity.EntityLiving; +import net.minecraft.entity.EntityLivingBase; +import net.minecraft.entity.item.EntityItem; +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.NBTTagCompound; +import net.minecraft.tileentity.TileEntity; +import net.minecraft.util.AxisAlignedBB; +import net.minecraft.util.DamageSource; +import net.minecraftforge.common.MinecraftForge; +import net.minecraftforge.common.util.ForgeDirection; + +public class DriverTileEntity extends TileEntityInventory implements IEnergySink, IHasGui, INetworkClientTileEntityEventListener, IUpgradableBlock, IMultiBlock { + + private AudioSource audioSource; + private AudioSource startAS; + private AudioSource loopAS; + private AudioSource stopAS; + private int startUpCounter=0; + public final InvSlotConsumableId miningPipesSlot; + public final InvSlotUpgrade upgradeSlot; + public final IHLInvSlotDischarge dischargeSlot; + public int tier; + private int defaultTier; + public int maxStorage; + private int defaultMaxStorage; + public boolean hasShield=false; + private boolean prevHasShield=false; + public int shaftDestroyedAtA=0; + private int prevShaftDestroyedAtA=0; + public int shaftDestroyedAtB=0; + private int prevShaftDestroyedAtB=0; + public int modelShift=0; + public int modelShiftSpeed; + private int defaultModelShiftSpeed; + public int blockShift=1; + private int lastBlockShift=1; + private double energy = 0; + private double energyConsume; + private double defaultEnergyConsume; + private boolean addedToEnergyNet=false; + public int xShaftMin, xShaftMax, yShaft, zShaftMin, zShaftMax, xShieldMin, xShieldMax, yShieldMin, yShieldMax, zShieldMin, zShieldMax; + private int updateChecksum=0; + private boolean renderHelperExist=false; + public DriverEntity renderHelper; + public boolean reverseModeOn=false; + public boolean autoReverseModeOn=true; + private float lastModelShiftAmount=1F; + private List blockBlackList = new ArrayList(); + private List blockWhiteList = new ArrayList(); + public boolean advancedShield=false; + + public DriverTileEntity() + { + super(); + this.miningPipesSlot = new InvSlotConsumableId(this, "miningPipesSlot", 1, 12, new Item[] {Ic2Items.miningPipe.getItem()}); + this.upgradeSlot = new InvSlotUpgrade(this, "upgrade", 2, 4); + this.dischargeSlot = new IHLInvSlotDischarge(this, 1, Access.IO, this.tier, InvSlot.InvSide.BOTTOM); + this.defaultTier=IHLMod.config.ts02DefaultTier; + this.defaultMaxStorage=IHLMod.config.ts02DefaultMaxEnergyStorage; + this.defaultModelShiftSpeed=IHLMod.config.ts02DefaultSpeed; + this.defaultEnergyConsume=IHLMod.config.ts02DefaultOperationEUCost; + this.tier=this.defaultTier; + this.maxStorage=this.defaultMaxStorage; + this.modelShiftSpeed=this.defaultModelShiftSpeed; + this.energyConsume=this.defaultEnergyConsume; + for(int i6=0;i6 getNetworkedFields() + { + List fields = super.getNetworkedFields(); + fields.add("hasShield"); + fields.add("modelShift"); + fields.add("shaftDestroyedAtA"); + fields.add("shaftDestroyedAtB"); + fields.add("tier"); + fields.add("modelShiftSpeed"); + fields.add("maxStorage"); + fields.add("reverseModeOn"); + fields.add("autoReverseModeOn"); + fields.add("advancedShield"); + return fields; + } + + public String getShieldSoundFile() + { + return "Machines/MinerOp.ogg"; + } + + public String getStartSoundFile() + { + return "Machines/IHL Tunneling Shield/TS02Start.ogg"; + } + + public String getLoopSoundFile() + { + return "Machines/IHL Tunneling Shield/TS02Loop.ogg"; + } + + public String getStopSoundFile() + { + return "Machines/IHL Tunneling Shield/TS02Stop.ogg"; + } + + @Override + public void onLoaded() + { + super.onLoaded(); + if (IC2.platform.isSimulating()&&!this.addedToEnergyNet) + { + MinecraftForge.EVENT_BUS.post(new EnergyTileLoadEvent(this)); + this.addedToEnergyNet = true; + } + if (IC2.platform.isRendering() && (this.startAS==null||this.loopAS==null||this.stopAS==null)) + { + this.startAS = IC2.audioManager.createSource(this, PositionSpec.Center, this.getStartSoundFile(),false,false, 0.5F); + this.loopAS = IC2.audioManager.createSource(this, PositionSpec.Center, this.getLoopSoundFile(),true,false, 0.5F); + this.stopAS = IC2.audioManager.createSource(this, PositionSpec.Center, this.getStopSoundFile(),false,false, 0.5F); + } + } + + @Override + public void onUnloaded() + { + super.onUnloaded(); + + if (IC2.platform.isRendering() && this.audioSource != null) + { + this.audioSource.stop(); + this.audioSource = null; + this.startAS.stop(); + this.loopAS.stop(); + this.stopAS.stop(); + this.startAS = null; + this.loopAS = null; + this.stopAS = null; + IC2.audioManager.removeSources(this); + } + + if (IC2.platform.isSimulating()&&this.addedToEnergyNet) + { + MinecraftForge.EVENT_BUS.post(new EnergyTileUnloadEvent(this)); + this.addedToEnergyNet = false; + } + } + + + @Override + public void updateEntityClient() + { + super.updateEntityClient(); + updateEntityServer(); + if(!this.renderHelperExist) + { + List deList = this.worldObj.getEntitiesWithinAABB(DriverEntity.class, this.getOperationZone()); + if(deList.size()>0) + { + Iterator dei = deList.iterator(); + while(dei.hasNext()) + { + DriverEntity rh=dei.next(); + if(!rh.isDead) + { + this.renderHelper=rh; + rh.parent=this; + this.renderHelperExist=true; + break; + } + } + if(!this.renderHelperExist) + { + this.renderHelper=new DriverEntity(this.worldObj, this, this.xCoord+this.blockShift*this.mX(), this.yCoord, this.zCoord+this.blockShift*this.mZ()); + this.renderHelperExist=true; + } + } + else + { + this.renderHelper=new DriverEntity(this.worldObj, this, this.xCoord+this.blockShift*this.mX(), this.yCoord, this.zCoord+this.blockShift*this.mZ()); + this.renderHelper.parent=this; + this.renderHelperExist=true; + } + } + + if(this.renderHelper!=null && !this.renderHelper.isDead && !this.worldObj.loadedEntityList.contains(this.renderHelper)) + { + this.worldObj.spawnEntityInWorld(this.renderHelper); + } + if(this.audioSource==null && this.renderHelper!=null)this.audioSource = IC2.audioManager.createSource(this.renderHelper, PositionSpec.Center, this.getShieldSoundFile(),true,false, 1.0F); + if(this.audioSource!=null) + { + this.audioSource.updatePosition(); + } + if(this.audioSource!=null) + { + if(this.getActive()) + { + if(this.startUpCounter>62) + { + this.startUpCounter=0; + } + if(this.startUpCounter==0) + { + this.stopAS.stop(); + this.startAS.play(); + } + if(this.startUpCounter<60) + { + this.startUpCounter++; + } + else if(this.startUpCounter==60) + { + this.startAS.stop(); + this.loopAS.play(); + this.audioSource.play(); + this.startUpCounter++; + } + } + else if(this.startUpCounter>0) + { + this.startAS.stop(); + this.loopAS.stop(); + this.audioSource.stop(); + this.stopAS.play(); + if(this.startUpCounter<100) + { + this.startUpCounter++; + } + else + { + this.startUpCounter=0; + this.stopAS.stop(); + } + } + } + else if(!this.getActive() && this.audioSource!=null) + { + this.audioSource.stop(); + } + } + + @Override + public void updateEntityServer() + { + if (IC2.platform.isSimulating()) + { + this.setOverclockRates(); + } + if(this.dischargeSlot.tier!=this.tier) + { + this.dischargeSlot.tier=this.tier; + } + int xyz[]={0,0,1,0,-1,-1,1,1,-1,2,0,2,1,2,-1,-2,0,-2,1,-2,-1,0}; + boolean freeToDigForward = true; + boolean freeToMoveBack = true; + if(this.blockShift>=127) + { + freeToDigForward = false; + if(this.autoReverseModeOn && !this.reverseModeOn && this.worldObj.isRemote) + { + this.reverseModeOn=true; + IC2.network.get().initiateClientTileEntityEvent(this, 0); + } + } + else + { + for(int i = 0;i<21;i++) + { + Block block = this.worldObj.getBlock(this.xCoord+xyz[i+1]*this.mZ()+(this.blockShift+1)*this.mX(), this.yCoord+xyz[i], this.zCoord+xyz[i+1]*this.mX()+(this.blockShift+1)*this.mZ()); + if(this.blockBlackList.contains(block)) + { + freeToDigForward = false; + } + } + } + + if(this.blockShift<=3) + { + freeToMoveBack = false; + } + else + { + for(int i = 0;i<21;i++) + { + Block block = this.worldObj.getBlock(this.xCoord+xyz[i+1]*this.mZ()+(this.blockShift-3)*this.mX(), this.yCoord+xyz[i], this.zCoord+xyz[i+1]*this.mX()+(this.blockShift-3)*this.mZ()); + if(block.getCollisionBoundingBoxFromPool(worldObj, this.xCoord+xyz[i+1]*this.mZ()+(this.blockShift-3)*this.mX(), this.yCoord+xyz[i], this.zCoord+xyz[i+1]*this.mX()+(this.blockShift-3)*this.mZ())!=null && !block.getMaterial().isLiquid() && !this.blockWhiteList.contains(block)) + { + freeToMoveBack = false; + } + block = this.worldObj.getBlock(this.xCoord+xyz[i+1]*this.mZ()+(this.blockShift-2)*this.mX(), this.yCoord+xyz[i], this.zCoord+xyz[i+1]*this.mX()+(this.blockShift-2)*this.mZ()); + if(block.getCollisionBoundingBoxFromPool(worldObj, this.xCoord+xyz[i+1]*this.mZ()+(this.blockShift-2)*this.mX(), this.yCoord+xyz[i], this.zCoord+xyz[i+1]*this.mX()+(this.blockShift-2)*this.mZ())!=null && !block.getMaterial().isLiquid() && !this.blockWhiteList.contains(block)) + { + freeToMoveBack = false; + } + } + } + + if(IC2.platform.isSimulating()) + { + if(this.hasShield && this.shaftDestroyedAtA==0 && this.energy>=this.energyConsume) + { + if(this.reverseModeOn && freeToMoveBack) + { + this.energy-=this.energyConsume; + this.setActive(true); + } + else if((!this.reverseModeOn) && freeToDigForward && this.miningPipesSlot.get()!=null && this.miningPipesSlot.get().stackSize>=1) + { + this.energy-=this.energyConsume; + this.setActive(true); + } + else + { + this.setActive(false); + } + } + else + { + this.setActive(false); + } + } + if(this.getActive()) + { + double velocity; + if(this.reverseModeOn && this.modelShift>0) + { + this.modelShift--; + List entityList = this.worldObj.getEntitiesWithinAABB(Entity.class, this.getShieldBladesZone(0D)); + velocity = -0.5D; + if(entityList.size()>0) + { + for(int i0=0;i0 entityLivingList = this.worldObj.getEntitiesWithinAABB(EntityLivingBase.class, this.getShieldBladesZone(2.5D)); + float damage = 800F/(this.modelShiftSpeed); + velocity = 0.1D*damage; + if(entityLivingList.size()>0) + { + for(int i1=0;i1 entityLivingList2 = this.worldObj.getEntitiesWithinAABB(EntityLiving.class, this.getShieldBladesZone(2.5D)); + velocity*=0.1D; + if(entityLivingList2.size()>0) + { + for(int i2=0;i2 1.0D) + { + double amount = this.dischargeSlot.discharge(this.getDemandedEnergy(), false); + this.energy += amount; + } + if(this.energy>this.maxStorage) + { + this.energy=this.maxStorage; + } + } + + if(this.blockShift!=this.lastBlockShift) + { + if(this.getActive() && this.reverseModeOn && IC2.platform.isSimulating() && this.lastModelShiftAmount-this.getModelShiftAmount()<1F) + { + if(this.miningPipesSlot.get()==null) + { + this.miningPipesSlot.put(Ic2Items.miningPipe.copy()); + } + else if(this.miningPipesSlot.get().stackSize<64) + { + this.miningPipesSlot.get().stackSize++; + } + else + { + this.worldObj.spawnEntityInWorld(new EntityItem(this.worldObj,this.xCoord,this.yCoord+1,this.zCoord,this.miningPipesSlot.get())); + this.miningPipesSlot.put(Ic2Items.miningPipe.copy()); + } + this.lastModelShiftAmount-=1F; + } + else if(this.getActive() && IC2.platform.isSimulating() && this.getModelShiftAmount()-this.lastModelShiftAmount>1F) + { + this.miningPipesSlot.consume(1); + this.lastModelShiftAmount+=1F; + IC2.network.get().updateTileEntityField(this, "modelShift"); + } + this.defineTechBlockVariables(); + if(this.getActive() && IC2.platform.isSimulating()) + { + if(this.reverseModeOn) + { + for(int i = 0;i<21;i++) + { + if(this.blockShift>=2) + { + this.worldObj.setBlockToAir(this.xCoord+xyz[i+1]*this.mZ()+(this.blockShift)*this.mX(), this.yCoord+xyz[i], this.zCoord+xyz[i+1]*this.mX()+(this.blockShift)*this.mZ()); + } + if(this.blockShift>=3) + { + Block block = this.worldObj.getBlock(this.xCoord+xyz[i+1]*this.mZ()+(this.blockShift-2)*this.mX(), this.yCoord+xyz[i], this.zCoord+xyz[i+1]*this.mX()+(this.blockShift-2)*this.mZ()); + if(block!=IHLMod.multiBlockSpacerBlock && block!=IHLMod.horizontalMiningPipeX && block!=IHLMod.horizontalMiningPipeZ) + { + this.harvestDrops(block, this.xCoord+xyz[i+1]*this.mZ()+(this.blockShift-2)*this.mX(), this.yCoord+xyz[i], this.zCoord+xyz[i+1]*this.mX()+(this.blockShift-2)*this.mZ(), this.xCoord+xyz[i+1]*this.mZ()+(this.blockShift-3)*this.mX(), this.yCoord+xyz[i], this.zCoord+xyz[i+1]*this.mX()+(this.blockShift-3)*this.mZ(), IHLMod.multiBlockSpacerBlock); + } + else + { + this.worldObj.setBlock(this.xCoord+xyz[i+1]*this.mZ()+(this.blockShift-2)*this.mX(), this.yCoord+xyz[i], this.zCoord+xyz[i+1]*this.mX()+(this.blockShift-2)*this.mZ(), IHLMod.multiBlockSpacerBlock); + } + } + } + } + else + { + for(int i = 0;i<21;i++) + { + Block block = this.worldObj.getBlock(this.xCoord+xyz[i+1]*this.mZ()+this.blockShift*this.mX(), this.yCoord+xyz[i], this.zCoord+xyz[i+1]*this.mX()+this.blockShift*this.mZ()); + this.harvestMiningDrops(block, this.xCoord+xyz[i+1]*this.mZ()+this.blockShift*this.mX(), this.yCoord+xyz[i], this.zCoord+xyz[i+1]*this.mX()+this.blockShift*this.mZ(), this.xCoord+xyz[i+1]*this.mZ()+(this.blockShift-2)*this.mX(), this.yCoord+xyz[i], this.zCoord+xyz[i+1]*this.mX()+(this.blockShift-2)*this.mZ(), IHLMod.multiBlockSpacerBlock); + if(this.blockShift>=3) + { + if(i!=0) + { + this.worldObj.setBlockToAir(this.xCoord+xyz[i+1]*this.mZ()+(this.blockShift-2)*this.mX(), this.yCoord+xyz[i], this.zCoord+xyz[i+1]*this.mX()+(this.blockShift-2)*this.mZ()); + } + else + { + this.worldObj.setBlock(this.xCoord+(this.blockShift-2)*this.mX(), this.yCoord, this.zCoord+(this.blockShift-2)*this.mZ(), this.mX()!=0?IHLMod.horizontalMiningPipeZ:IHLMod.horizontalMiningPipeX); + } + } + + } + } + } + this.lastBlockShift=this.blockShift; + } + } + + private void harvestMiningDrops(Block block, int x, int y, int z, int x1, int y1, int z1, Block setToBlock) + { + if(block!=null && block!=Blocks.air && block!=Blocks.bedrock) + { + int meta = worldObj.getBlockMetadata(x, y, z); + ArrayList drops = block.getDrops(this.worldObj, x, y, z, meta, 0); + if(drops.size()>0) + { + if(!this.advancedShield || !this.handleDropsByHydrotransport(drops)) + { + for(int i1=0;i1=this.xShaftMin && x<=this.xShaftMax && z>=this.zShaftMin && z<=this.zShaftMax) + { + int sda = (x-this.xCoord)*this.mX()+(z-this.zCoord)*this.mZ(); + if(this.shaftDestroyedAtA==0 && this.shaftDestroyedAtB==0) + { + this.shaftDestroyedAtA=this.shaftDestroyedAtB=sda; + } + else if(sda>this.shaftDestroyedAtB) + { + for(int i4=this.shaftDestroyedAtB;i4=this.yShieldMin && y<=this.yShieldMax && x>=this.xShieldMin && x<=this.xShieldMax && z>=this.zShieldMin && z<=this.zShieldMax) + { + if(onBlockBeak) + { + this.hasShield=false; + this.advancedShield=false; + this.modelShift=0; + this.lastBlockShift=this.blockShift=1; + this.shaftDestroyedAtA=0; + IC2.network.get().updateTileEntityField(this, "modelShift"); + IC2.network.get().updateTileEntityField(this, "advancedShield"); + this.cleanUp(); + } + return true; + } + return false; + } + + public boolean isShaftCoordNN(int x, int y, int z) + { + if(y==this.yShaft && x>=this.xShaftMin && x<=this.xShaftMax && z>=this.zShaftMin && z<=this.zShaftMax) + { + return true; + } + return false; + } + + public void cleanUp() + { + int xyz[]={0,0,1,0,-1,-1,1,1,-1,2,0,2,1,2,-1,-2,0,-2,1,-2,-1,0}; + for(int shift = 0;shift<=this.blockShift+1;shift++) + { + for(int i = 0;i<21;i++) + { + Block block = this.worldObj.getBlock(this.xCoord+xyz[i+1]*this.mZ()+shift*this.mX(), this.yCoord+xyz[i], this.zCoord+xyz[i+1]*this.mX()+shift*this.mZ()); + if(block instanceof HorizontalMiningPipe) + { + ArrayList drops = block.getDrops(this.worldObj, this.xCoord+xyz[i+1]*this.mZ()+shift*this.mX(), this.yCoord+xyz[i], this.zCoord+xyz[i+1]*this.mX()+shift*this.mZ(), 0, 3); + if(drops.size()>0) + { + for(int i1=0;i1 drops) + { + ForgeDirection direction = ForgeDirection.getOrientation(this.getFacing()).getOpposite(); + TileEntity te = worldObj.getTileEntity(xCoord+direction.offsetX, yCoord+direction.offsetY, zCoord+direction.offsetZ); + if(te instanceof HydrotransportPulpRegeneratorTileEntity) + { + HydrotransportPulpRegeneratorTileEntity hprte = (HydrotransportPulpRegeneratorTileEntity) te; + if(hprte.getActive()) + { + ArrayList drops1 = new ArrayList(); + Iterator dropsIterator = drops.iterator(); + while(dropsIterator.hasNext()) + { + ItemStack stack = dropsIterator.next(); + RecipeOutput maceratorOut = Recipes.macerator.getOutputFor(stack, false); + if(maceratorOut!=null && !maceratorOut.items.isEmpty()) + { + Iterator maceratorOutIterator = maceratorOut.items.iterator(); + while(maceratorOutIterator.hasNext()) + { + drops1.add(maceratorOutIterator.next().copy()); + } + } + else + { + drops1.add(stack); + } + } + if(hprte.outputSlot.canAdd(drops1)) + { + hprte.outputSlot.add(drops1); + } + else + { + for(int i1=0;i1 drops = block.getDrops(this.worldObj, x, y, z, meta, 0); + if(drops.size()>0) + { + for(int i1=0;i1= this.maxStorage) + { + return amount; + } + else + { + this.energy += amount; + return 0.0D; + } + } + + @Override + public double getEnergy() { + return this.energy; + } + + @Override + public boolean useEnergy(double amount) + { + if (this.energy >= amount) + { + this.energy -= amount; + return true; + } + else + { + return false; + } + } + + @Override + public boolean shouldRenderInPass(int pass) + { + return pass==0; + } + + @Override + public boolean getIsInvalid() + { + return this.isInvalid(); + } + + public void installAdvancedShield() + { + this.installShield(); + this.advancedShield=true; + IC2.network.get().updateTileEntityField(this, "advancedShield"); + } + + public ItemStack getOutput(int arg0) + { + return null; + } + + public int getOutputSize() { + return 0; + } + + public void setOutput(int arg0, ItemStack arg1) + { + } + + @Override + public Set getUpgradableProperties() + { + Set properties = new HashSet(); + properties.add(UpgradableProperty.EnergyStorage); + properties.add(UpgradableProperty.Processing); + properties.add(UpgradableProperty.Transformer); + return properties; + } +} -- cgit v1.2.3