diff --git a/src/cr0s/WarpDrive/machines/TileEntityAbstractMiner.java b/src/cr0s/WarpDrive/machines/TileEntityAbstractMiner.java new file mode 100644 index 00000000..b033ff00 --- /dev/null +++ b/src/cr0s/WarpDrive/machines/TileEntityAbstractMiner.java @@ -0,0 +1,556 @@ +package cr0s.WarpDrive.machines; + +import java.io.ByteArrayOutputStream; +import java.io.DataOutputStream; +import java.util.ArrayList; +import java.util.List; + +import cpw.mods.fml.common.FMLCommonHandler; +import cpw.mods.fml.relauncher.Side; + +import appeng.api.IAEItemStack; +import appeng.api.Util; +import appeng.api.WorldCoord; +import appeng.api.events.GridTileLoadEvent; +import appeng.api.events.GridTileUnloadEvent; +import appeng.api.me.tiles.IGridMachine; +import appeng.api.me.tiles.ITileCable; +import appeng.api.me.util.IGridInterface; +import appeng.api.me.util.IMEInventoryHandler; + +import net.minecraft.block.Block; +import net.minecraft.inventory.IInventory; +import net.minecraft.item.ItemStack; +import net.minecraft.nbt.NBTTagCompound; +import net.minecraft.network.packet.Packet250CustomPayload; +import net.minecraft.server.MinecraftServer; +import net.minecraft.tileentity.TileEntity; +import net.minecraft.world.ChunkCoordIntPair; +import net.minecraft.world.World; +import net.minecraftforge.common.MinecraftForge; +import cr0s.WarpDrive.Vector3; +import cr0s.WarpDrive.WarpDrive; +import cr0s.WarpDrive.WarpDriveConfig; + +public abstract class TileEntityAbstractMiner extends WarpChunkTE implements IGridMachine, ITileCable +{ + //FOR OVERRIDE + protected final boolean canSilktouch=true; + protected final int minFortune=0; + protected final int maxFortune=5; + protected final int laserBelow = 1; + + //FOR STORAGE + private boolean silkTouch = false; + private int fortuneLevel = 0; + + private TileEntityParticleBooster booster = null; + private Vector3 minerVector; + + Boolean powerStatus = false; + private IGridInterface grid; + private boolean isMEReady = false; + + public TileEntityAbstractMiner() + { + super(); + fixMinerVector(); + } + + private void fixMinerVector() + { + if(minerVector == null) + minerVector = new Vector3(); + minerVector.x = xCoord; + minerVector.y = yCoord - (laserBelow); + minerVector.z = zCoord; + minerVector = minerVector.translate(0.5); + } + + protected int toInt(double d) + { + return (int) Math.round(d); + } + + protected int toInt(Object o) + { + return toInt(toDouble(o)); + } + + protected double toDouble(Object o) + { + return Double.parseDouble(o.toString()); + } + + protected boolean toBool(Object o) + { + if(o.toString() == "true" || o.toString() == "1") + return true; + return false; + } + + private List getItemStackFromBlock(int i, int j, int k, int blockID, int blockMeta) + { + Block block = Block.blocksList[blockID]; + if (block == null) + return null; + if (silkTouch) + { + if (block.canSilkHarvest(worldObj, null, i, j, k, blockMeta)) + { + ArrayList t = new ArrayList(); + t.add(new ItemStack(blockID, 1, blockMeta)); + return t; + } + } + return block.getBlockDropped(worldObj, i, j, k, blockMeta, fortuneLevel); + } + + protected boolean isOnEarth() + { + return worldObj.provider.dimensionId == 0; + } + + private IInventory findChest() + { + TileEntity result = null; + for(int x=-1;x<=1;x++) + { + for(int y=-1;y<=1;y++) + { + for(int z=-1;z<=1;z++) + { + result = worldObj.getBlockTileEntity(xCoord+x, yCoord+y, zCoord+z); + if(result != null && !(result instanceof TileEntityAbstractMiner) && (result instanceof IInventory)) + return (IInventory) result; + } + } + } + return null; + } + + //GETTERSETTERS + + protected boolean silkTouch() + { + return silkTouch; + } + + protected int fortune() + { + return fortuneLevel; + } + + protected boolean silkTouch(boolean b) + { + silkTouch = canSilktouch && b; + return silkTouch(); + } + + protected int fortune(int f) + { + try + { + fortuneLevel = Math.min(maxFortune, Math.max(minFortune,f)); + } + catch(NumberFormatException e) + { + fortuneLevel = minFortune; + } + return fortune(); + } + + protected TileEntityParticleBooster booster() + { + if(booster == null) + findFirstBooster(); + return booster; + } + + protected int energy() + { + TileEntityParticleBooster a = booster(); + if(a != null) + return booster().getCurrentEnergyValue(); + return 0; + } + + //DATA RET + + protected boolean isRoomForHarvest() + { + if(grid != null && isMEReady) + return true; + + IInventory inv = findChest(); + if(inv != null) + { + int size = inv.getSizeInventory(); + for(int i=0;i stacks = getItemStackFromBlock(valuable.intX(), valuable.intY(), valuable.intZ(), blockID, blockMeta); + if (stacks != null) + { + for (ItemStack stack : stacks) + { + if (grid != null) + didPlace = didPlace && putInGrid(stack) == stack.stackSize; + else + didPlace = didPlace && putInChest(findChest(), stack) == stack.stackSize; + } + } + mineBlock(valuable,blockID,blockMeta); + return didPlace; + } + else if (blockID == Block.waterMoving.blockID || blockID == Block.waterStill.blockID) + // Evaporate water + worldObj.playSoundEffect((double)((float)valuable.intX() + 0.5F), (double)((float)valuable.intY() + 0.5F), (double)((float)valuable.intZ() + 0.5F), "random.fizz", 0.5F, 2.6F + (worldObj.rand.nextFloat() - worldObj.rand.nextFloat()) * 0.8F); + worldObj.setBlockToAir(valuable.intX(), valuable.intY(), valuable.intZ()); + return true; + } + + private int putInGrid(ItemStack itemStackSource) + { + int transferred = 0; + if(isMEReady && grid != null) + { + IMEInventoryHandler cellArray = grid.getCellArray(); + if (cellArray != null) + { + IAEItemStack ret = cellArray.addItems(Util.createItemStack(itemStackSource)); + if (ret != null) + transferred = (int) ret.getStackSize(); + } + } + return transferred; + } + + private int putInChest(IInventory inventory, ItemStack itemStackSource) + { + if (inventory == null || itemStackSource == null) + { + return 0; + } + + int transferred = 0; + + for (int i = 0; i < inventory.getSizeInventory(); i++) + { + if (!inventory.isItemValidForSlot(i, itemStackSource)) + { + continue; + } + + ItemStack itemStack = inventory.getStackInSlot(i); + + if (itemStack == null || !itemStack.isItemEqual(itemStackSource)) + { + continue; + } + + int transfer = Math.min(itemStackSource.stackSize - transferred, itemStack.getMaxStackSize() - itemStack.stackSize); + itemStack.stackSize += transfer; + transferred += transfer; + + if (transferred == itemStackSource.stackSize) + { + return transferred; + } + } + + for (int i = 0; i < inventory.getSizeInventory(); i++) + { + if (!inventory.isItemValidForSlot(i, itemStackSource)) + { + continue; + } + + ItemStack itemStack = inventory.getStackInSlot(i); + + if (itemStack != null) + { + continue; + } + + int transfer = Math.min(itemStackSource.stackSize - transferred, itemStackSource.getMaxStackSize()); + ItemStack dest = copyWithSize(itemStackSource, transfer); + inventory.setInventorySlotContents(i, dest); + transferred += transfer; + + if (transferred == itemStackSource.stackSize) + { + return transferred; + } + } + + return transferred; + } + + protected boolean collectEnergyPacketFromBooster(int packet, boolean test) + { + TileEntityParticleBooster b = booster(); + if (b != null) + if (test) + return packet <= b.getCurrentEnergyValue(); + else + return b.consumeEnergy(packet); + return false; + } + + private TileEntityParticleBooster findFirstBooster() + { + TileEntity result; + int[] xPos = {1,-1,0,0,0,0}; + int[] yPos = {0,0,-1,1,0,0}; + int[] zPos = {0,0,0,0,-1,1}; + + for(int i=0;i<6;i++) + { + result = worldObj.getBlockTileEntity(xCoord + xPos[i], yCoord + yPos[i], zCoord + zPos[i]); + + if (result != null && result instanceof TileEntityParticleBooster) + { + booster = (TileEntityParticleBooster) result; + return (TileEntityParticleBooster) result; + } + } + booster = null; + return null; + } + + protected void defineMiningArea(int xSize,int zSize) + { + int xmax, zmax, x1, x2, z1, z2; + int xmin, zmin; + x1 = xCoord + xSize / 2; + x2 = xCoord - xSize / 2; + + if (x1 < x2) + { + xmin = x1; + xmax = x2; + } + else + { + xmin = x2; + xmax = x1; + } + + z1 = zCoord + zSize / 2; + z2 = zCoord - zSize / 2; + + if (z1 < z2) + { + zmin = z1; + zmax = z2; + } + else + { + zmin = z2; + zmax = z1; + } + + defineMiningArea(xmin,zmin,xmax,zmax); + } + + protected void defineMiningArea(int minX, int minZ, int maxX, int maxZ) + { + ChunkCoordIntPair a = worldObj.getChunkFromBlockCoords(minX, minZ).getChunkCoordIntPair(); + ChunkCoordIntPair b = worldObj.getChunkFromBlockCoords(maxX, maxZ).getChunkCoordIntPair(); + if(a.equals(minChunk)) + if(b.equals(maxChunk)) + return; + if(b.equals(minChunk)) + if(a.equals(maxChunk)) + return; + minChunk = a; + maxChunk = b; + refreshLoading(true); + } + + private ItemStack copyWithSize(ItemStack itemStack, int newSize) + { + ItemStack ret = itemStack.copy(); + ret.stackSize = newSize; + return ret; + } + + //NBT DATA + public void readFromNBT(NBTTagCompound tag) + { + super.readFromNBT(tag); + silkTouch = tag.getBoolean("silkTouch"); + fortuneLevel = tag.getInteger("fortuneLevel"); + + minerVector.x = xCoord; + minerVector.y = yCoord - (laserBelow); + minerVector.z = zCoord; + minerVector = minerVector.translate(0.5); + } + + public void writeToNBT(NBTTagCompound tag) + { + super.writeToNBT(tag); + tag.setBoolean("silkTouch", silkTouch); + tag.setInteger("fortuneLevel", fortuneLevel); + } + + //AE INTERFACE + public void setNetworkReady( boolean isReady ) + { + isMEReady = isReady; + } + + public boolean isMachineActive() + { + return isMEReady; + } + + //OVERRIDES + @Override + public float getPowerDrainPerTick() + { + return 1; + } + + @Override + public void validate() + { + super.validate(); + MinecraftForge.EVENT_BUS.post(new GridTileLoadEvent(this, worldObj, getLocation())); + } + + @Override + public void invalidate() + { + super.invalidate(); + MinecraftForge.EVENT_BUS.post(new GridTileUnloadEvent(this, worldObj, getLocation())); + } + + @Override + public WorldCoord getLocation() + { + return new WorldCoord(xCoord, yCoord, zCoord); + } + + @Override + public boolean isValid() + { + return true; + } + + @Override + public void setPowerStatus(boolean hasPower) + { + powerStatus = hasPower; + } + + @Override + public boolean isPowered() + { + return powerStatus; + } + + @Override + public IGridInterface getGrid() + { + return grid; + } + + @Override + public void setGrid(IGridInterface gi) + { + grid = gi; + } + + @Override + public boolean coveredConnections() + { + return true; + } + + @Override + public World getWorld() + { + return worldObj; + } + +} diff --git a/src/cr0s/WarpDrive/machines/TileEntityMiningLaser.java b/src/cr0s/WarpDrive/machines/TileEntityMiningLaser.java index e82e16cf..9c862fc8 100644 --- a/src/cr0s/WarpDrive/machines/TileEntityMiningLaser.java +++ b/src/cr0s/WarpDrive/machines/TileEntityMiningLaser.java @@ -1,8 +1,5 @@ package cr0s.WarpDrive.machines; -import cpw.mods.fml.common.FMLCommonHandler; -import cpw.mods.fml.relauncher.Side; -import cpw.mods.fml.relauncher.SideOnly; import cr0s.WarpDrive.Vector3; import cr0s.WarpDrive.WarpDrive; import cr0s.WarpDrive.WarpDriveConfig; @@ -10,72 +7,36 @@ import dan200.computer.api.IComputerAccess; import dan200.computer.api.ILuaContext; import dan200.computer.api.IPeripheral; -import java.io.ByteArrayOutputStream; -import java.io.DataOutputStream; import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; - import net.minecraft.block.Block; -import net.minecraft.block.material.Material; -import net.minecraft.entity.player.EntityPlayer; -import net.minecraft.item.ItemStack; import net.minecraft.nbt.NBTTagCompound; -import net.minecraft.network.packet.Packet250CustomPayload; -import net.minecraft.network.packet.Packet62LevelSound; -import net.minecraft.server.MinecraftServer; -import net.minecraft.tileentity.TileEntity; -import net.minecraft.inventory.IInventory; -import net.minecraft.util.AxisAlignedBB; -import net.minecraft.util.DamageSource; -import net.minecraft.util.MovingObjectPosition; -import net.minecraft.world.World; -import net.minecraftforge.common.DimensionManager; -import net.minecraftforge.common.MinecraftForge; -import net.minecraftforge.common.ForgeDirection; -import net.minecraftforge.fluids.FluidRegistry; -import net.minecraftforge.oredict.OreDictionary; -import appeng.api.WorldCoord; -import appeng.api.IAEItemStack; -import appeng.api.Util; -import appeng.api.events.GridTileLoadEvent; -import appeng.api.events.GridTileUnloadEvent; -import appeng.api.me.tiles.IGridMachine; -import appeng.api.me.tiles.ITileCable; -import appeng.api.me.util.IGridInterface; -import appeng.api.me.util.IMEInventoryHandler; -public class TileEntityMiningLaser extends WarpChunkTE implements IPeripheral, IGridMachine, ITileCable +public class TileEntityMiningLaser extends TileEntityAbstractMiner implements IPeripheral { - Boolean powerStatus = false; - private IGridInterface grid; - - private final int MAX_BOOSTERS_NUMBER = 1; - + protected final int laserBelow = 0; + private int digX,digZ = 8; private final int CUBE_SIDE = 8; - - private int dx, dz, dy; private boolean isMining = false; private boolean isQuarry = false; private double speedMul = 1; - private boolean silkTouch = false; - private int fortuneLevel = 0; - private int miningDelay = 0; private int minLayer = 1; - public void setNetworkReady( boolean isReady ) + protected int calculateLayerCost() { - return ; + return isOnEarth() ? WarpDriveConfig.i.ML_EU_PER_LAYER_EARTH : WarpDriveConfig.i.ML_EU_PER_LAYER_SPACE; } - public boolean isMachineActive() + protected int calculateBlockCost() { - return true; + int enPerBlock = isOnEarth() ? WarpDriveConfig.i.ML_EU_PER_BLOCK_EARTH : WarpDriveConfig.i.ML_EU_PER_BLOCK_SPACE; + if(silkTouch()) + return (int) Math.round(enPerBlock * WarpDriveConfig.i.ML_EU_MUL_SILKTOUCH); + return (int) Math.round(enPerBlock * (Math.pow(WarpDriveConfig.i.ML_EU_MUL_FORTUNE, fortune()))); } private String[] methodsArray = @@ -103,13 +64,7 @@ public class TileEntityMiningLaser extends WarpChunkTE implements IPeripheral, I private int valuableIndex = 0; private int layerOffset = 1; - - private Vector3 minerVector; //private long uid = 0; - - TileEntityParticleBooster booster = null; - - private boolean isOnEarth = false; //int t = 20; @Override public void updateEntity() @@ -126,15 +81,6 @@ public class TileEntityMiningLaser extends WarpChunkTE implements IPeripheral, I speedMul = Math.max(WarpDriveConfig.i.ML_MIN_SPEED,Math.min(WarpDriveConfig.i.ML_MAX_SPEED,speedMul)); if (isMining) { - isOnEarth = (worldObj.provider.dimensionId == 0); - if (minerVector != null) - { - minerVector.x = xCoord; - minerVector.y = yCoord - 1; - minerVector.z = zCoord; - minerVector = minerVector.add(0.5); - } - if (currentMode == 0) { if (++delayTicksScan > (WarpDriveConfig.i.ML_SCAN_DELAY / speedMul)) @@ -177,8 +123,6 @@ public class TileEntityMiningLaser extends WarpChunkTE implements IPeripheral, I Vector3 valuable = valuablesInLayer.get(valuableIndex); // Mine valuable ore int blockID = worldObj.getBlockId(valuable.intX(), valuable.intY(), valuable.intZ()); - - // Skip if block is too hard or its empty block if (!canDig(blockID)) { WarpDrive.debugPrint("Cannot mine: " + blockID); @@ -190,8 +134,6 @@ public class TileEntityMiningLaser extends WarpChunkTE implements IPeripheral, I { if(collectEnergyPacketFromBooster(energyReq,false)) { - sendLaserPacket(minerVector, new Vector3(valuable.intX(), valuable.intY(), valuable.intZ()).add(0.5), 1, 1, 0, 2 * WarpDriveConfig.i.ML_MINE_DELAY, 0, 50); - worldObj.playSoundEffect(xCoord + 0.5f, yCoord, zCoord + 0.5f, "warpdrive:lowlaser", 4F, 1F); harvestBlock(valuable); valuableIndex++; miningDelay = 0; @@ -220,212 +162,6 @@ public class TileEntityMiningLaser extends WarpChunkTE implements IPeripheral, I } } - - private boolean canDig(int blockID) - { - if (Block.blocksList[blockID] != null) - return ((blockID == WarpDriveConfig.i.GT_Granite || blockID == WarpDriveConfig.i.GT_Ores || blockID == WarpDriveConfig.i.iridiumID || Block.blocksList[blockID].blockResistance <= Block.obsidian.blockResistance) && blockID != WarpDriveConfig.i.MFFS_Field && blockID != Block.bedrock.blockID); - else - return (blockID != WarpDriveConfig.i.MFFS_Field && blockID != Block.bedrock.blockID); - } - - private boolean harvestBlock(Vector3 valuable) - { - int blockID = worldObj.getBlockId(valuable.intX(), valuable.intY(), valuable.intZ()); - int blockMeta = worldObj.getBlockMetadata(valuable.intX(), valuable.intY(), valuable.intZ()); - if (blockID != Block.waterMoving.blockID && blockID != Block.waterStill.blockID && blockID != Block.lavaMoving.blockID && blockID != Block.lavaStill.blockID) - { - boolean didPlace = true; - List stacks = getItemStackFromBlock(valuable.intX(), valuable.intY(), valuable.intZ(), blockID, blockMeta); - if (stacks != null) - { - for (ItemStack stack : stacks) - { - if (grid != null) - didPlace = didPlace && putInGrid(stack) == stack.stackSize; - else - didPlace = didPlace && putInChest(findChest(), stack) == stack.stackSize; - } - } - worldObj.playAuxSFXAtEntity(null, 2001, valuable.intX(), valuable.intY(), valuable.intZ(), blockID + (blockMeta << 12)); - worldObj.setBlockToAir(valuable.intX(), valuable.intY(), valuable.intZ()); - return didPlace; - } - else if (blockID == Block.waterMoving.blockID || blockID == Block.waterStill.blockID) - // Evaporate water - worldObj.playSoundEffect((double)((float)valuable.intX() + 0.5F), (double)((float)valuable.intY() + 0.5F), (double)((float)valuable.intZ() + 0.5F), "random.fizz", 0.5F, 2.6F + (worldObj.rand.nextFloat() - worldObj.rand.nextFloat()) * 0.8F); - worldObj.setBlockToAir(valuable.intX(), valuable.intY(), valuable.intZ()); - return true; - } - - private IInventory findChest() - { - TileEntity result = null; - result = worldObj.getBlockTileEntity(xCoord + 1, yCoord, zCoord); - - if (result != null && result instanceof IInventory) - { - return (IInventory) result; - } - - result = worldObj.getBlockTileEntity(xCoord - 1, yCoord, zCoord); - - if (result != null && result instanceof IInventory) - { - return (IInventory) result; - } - - result = worldObj.getBlockTileEntity(xCoord, yCoord, zCoord + 1); - - if (result != null && result instanceof IInventory) - { - return (IInventory) result; - } - - result = worldObj.getBlockTileEntity(xCoord, yCoord, zCoord - 1); - - if (result != null && result instanceof IInventory) - { - return (IInventory) result; - } - - result = worldObj.getBlockTileEntity(xCoord, yCoord + 1, zCoord); - - if (result != null && result instanceof IInventory) - { - return (IInventory) result; - } - - return null; - } - - private int calculateLayerCost() - { - return isOnEarth ? WarpDriveConfig.i.ML_EU_PER_LAYER_EARTH : WarpDriveConfig.i.ML_EU_PER_LAYER_SPACE; - } - - private int calculateBlockCost() - { - int enPerBlock = isOnEarth ? WarpDriveConfig.i.ML_EU_PER_BLOCK_EARTH : WarpDriveConfig.i.ML_EU_PER_BLOCK_SPACE; - if(silkTouch) - return (int) Math.round(enPerBlock * WarpDriveConfig.i.ML_EU_MUL_SILKTOUCH * speedMul); - return (int) Math.round(enPerBlock * (Math.pow(WarpDriveConfig.i.ML_EU_MUL_FORTUNE, fortuneLevel)) * speedMul); - } - - private boolean isRoomForHarvest() - { - if(grid != null) - return true; - - IInventory inv = findChest(); - if(inv != null) - { - int size = inv.getSizeInventory(); - for(int i=0;i getItemStackFromBlock(int i, int j, int k, int blockID, int blockMeta) - { - Block block = Block.blocksList[blockID]; - if (block == null) - return null; - if (silkTouch) - { - if (block.canSilkHarvest(worldObj, null, i, j, k, blockMeta)) - { - ArrayList t = new ArrayList(); - t.add(new ItemStack(blockID, 1, blockMeta)); - return t; - } - } - return block.getBlockDropped(worldObj, i, j, k, blockMeta, fortuneLevel); - } - - public int putInGrid(ItemStack itemStackSource) - { - int transferred = 0; - IMEInventoryHandler cellArray = grid.getCellArray(); - if (cellArray != null) - { - IAEItemStack ret = cellArray.addItems(Util.createItemStack(itemStackSource)); - if (ret != null) - transferred = (int) ret.getStackSize(); - } - return transferred; - } - - public int putInChest(IInventory inventory, ItemStack itemStackSource) - { - if (inventory == null || itemStackSource == null) - { - return 0; - } - - int transferred = 0; - - for (int i = 0; i < inventory.getSizeInventory(); i++) - { - if (!inventory.isItemValidForSlot(i, itemStackSource)) - { - continue; - } - - ItemStack itemStack = inventory.getStackInSlot(i); - - if (itemStack == null || !itemStack.isItemEqual(itemStackSource)) - { - continue; - } - - int transfer = Math.min(itemStackSource.stackSize - transferred, itemStack.getMaxStackSize() - itemStack.stackSize); - itemStack.stackSize += transfer; - transferred += transfer; - - if (transferred == itemStackSource.stackSize) - { - return transferred; - } - } - - for (int i = 0; i < inventory.getSizeInventory(); i++) - { - if (!inventory.isItemValidForSlot(i, itemStackSource)) - { - continue; - } - - ItemStack itemStack = inventory.getStackInSlot(i); - - if (itemStack != null) - { - continue; - } - - int transfer = Math.min(itemStackSource.stackSize - transferred, itemStackSource.getMaxStackSize()); - ItemStack dest = copyWithSize(itemStackSource, transfer); - inventory.setInventorySlotContents(i, dest); - transferred += transfer; - - if (transferred == itemStackSource.stackSize) - { - return transferred; - } - } - - return transferred; - } - - public ItemStack copyWithSize(ItemStack itemStack, int newSize) - { - ItemStack ret = itemStack.copy(); - ret.stackSize = newSize; - return ret; - } - private void scanLayer() { //System.out.println("Scanning layer"); @@ -463,9 +199,7 @@ public class TileEntityMiningLaser extends WarpChunkTE implements IPeripheral, I //System.out.println("Layer: xmax: " + xmax + ", xmin: " + xmin); //System.out.println("Layer: zmax: " + zmax + ", zmin: " + zmin); - minChunk = worldObj.getChunkFromBlockCoords(xmin,zmin).getChunkCoordIntPair(); - maxChunk = worldObj.getChunkFromBlockCoords(xmax,zmax).getChunkCoordIntPair(); - refreshLoading(true); + defineMiningArea(xmin,zmin,xmax,zmax); // Search for valuable blocks for (int x = xmin; x <= xmax; x++) @@ -487,119 +221,67 @@ public class TileEntityMiningLaser extends WarpChunkTE implements IPeripheral, I //System.out.println("[ML] Found " + valuablesInLayer.size() + " valuables"); } - private boolean collectEnergyPacketFromBooster(int packet, boolean test) + + + private void scanLayer() { - if (booster == null) - booster = findFirstBooster(); - if (booster != null) - if (test) - return packet <= booster.getCurrentEnergyValue(); - else - return booster.consumeEnergy(packet); - return false; - } - - private TileEntityParticleBooster findFirstBooster() - { - TileEntity result; - result = worldObj.getBlockTileEntity(xCoord + 1, yCoord, zCoord); - - if (result != null && result instanceof TileEntityParticleBooster) + //System.out.println("Scanning layer"); + valuablesInLayer.clear(); + int xmax, zmax, x1, x2, z1, z2; + int xmin, zmin; + x1 = xCoord + digX / 2; + x2 = xCoord - digX / 2; + + if (x1 < x2) { - dx = 1; - dz = 0; - dy = 0; - return (TileEntityParticleBooster) result; + xmin = x1; + xmax = x2; } - - result = worldObj.getBlockTileEntity(xCoord - 1, yCoord, zCoord); - - if (result != null && result instanceof TileEntityParticleBooster) + else { - dx = -1; - dz = 0; - dy = 0; - return (TileEntityParticleBooster) result; + xmin = x2; + xmax = x1; } - - result = worldObj.getBlockTileEntity(xCoord, yCoord, zCoord + 1); - - if (result != null && result instanceof TileEntityParticleBooster) + + z1 = zCoord + digZ / 2; + z2 = zCoord - digZ / 2; + + if (z1 < z2) { - dx = 0; - dz = 1; - dy = 0; - return (TileEntityParticleBooster) result; + zmin = z1; + zmax = z2; } - - result = worldObj.getBlockTileEntity(xCoord, yCoord, zCoord - 1); - - if (result != null && result instanceof TileEntityParticleBooster) + else { - dx = 0; - dz = -1; - dy = 0; - return (TileEntityParticleBooster) result; + zmin = z2; + zmax = z1; } - - result = worldObj.getBlockTileEntity(xCoord, yCoord + 1, zCoord); - - if (result != null && result instanceof TileEntityParticleBooster) - { - dx = 0; - dz = 0; - dy = 1; - return (TileEntityParticleBooster) result; - } - - return null; - } - - public void sendLaserPacket(Vector3 source, Vector3 dest, float r, float g, float b, int age, int energy, int radius) - { - Side side = FMLCommonHandler.instance().getEffectiveSide(); - - if (side == Side.SERVER) - { - if (source == null || dest == null || worldObj == null) + + //System.out.println("Layer: xmax: " + xmax + ", xmin: " + xmin); + //System.out.println("Layer: zmax: " + zmax + ", zmin: " + zmin); + + minChunk = worldObj.getChunkFromBlockCoords(xmin,zmin).getChunkCoordIntPair(); + maxChunk = worldObj.getChunkFromBlockCoords(xmax,zmax).getChunkCoordIntPair(); + refreshLoading(true); + + // Search for valuable blocks + for (int x = xmin; x <= xmax; x++) + for (int z = zmin; z <= zmax; z++) { - return; + int blockID = worldObj.getBlockId(x, currentLayer, z); + if (canDig(blockID)) + if (isQuarry) // Quarry collects all blocks + { + if (!worldObj.isAirBlock(x, currentLayer, z) && blockID != Block.lavaMoving.blockID && blockID != Block.lavaStill.blockID) + valuablesInLayer.add(new Vector3(x, currentLayer, z)); + } + else // Not-quarry collect only valuables blocks + if (WarpDriveConfig.i.MinerOres.contains(worldObj.getBlockId(x, currentLayer, z))) + valuablesInLayer.add(new Vector3(x, currentLayer, z)); } - - ByteArrayOutputStream bos = new ByteArrayOutputStream(8); - DataOutputStream outputStream = new DataOutputStream(bos); - - try - { - // Write source vector - outputStream.writeDouble(source.x); - outputStream.writeDouble(source.y); - outputStream.writeDouble(source.z); - // Write target vector - outputStream.writeDouble(dest.x); - outputStream.writeDouble(dest.y); - outputStream.writeDouble(dest.z); - // Write r, g, b of laser - outputStream.writeFloat(r); - outputStream.writeFloat(g); - outputStream.writeFloat(b); - // Write age - outputStream.writeByte(age); - // Write energy value - outputStream.writeInt(0); - } - catch (Exception ex) - { - ex.printStackTrace(); - } - - Packet250CustomPayload packet = new Packet250CustomPayload(); - packet.channel = "WarpDriveBeam"; - packet.data = bos.toByteArray(); - packet.length = bos.size(); - MinecraftServer.getServer().getConfigurationManager().sendToAllNear(source.intX(), source.intY(), source.intZ(), radius, worldObj.provider.dimensionId, packet); - MinecraftServer.getServer().getConfigurationManager().sendToAllNear(dest.intX(), dest.intY(), dest.intZ(), radius, worldObj.provider.dimensionId, packet); - } + + valuableIndex = 0; + //System.out.println("[ML] Found " + valuablesInLayer.size() + " valuables"); } @Override @@ -614,11 +296,7 @@ public class TileEntityMiningLaser extends WarpChunkTE implements IPeripheral, I digX = tag.getInteger("digX"); digZ = tag.getInteger("digZ"); - silkTouch = tag.getBoolean("silkTouch"); - fortuneLevel = tag.getInteger("fortuneLevel"); speedMul = tag.getDouble("speedMul"); - - minerVector = new Vector3(xCoord, yCoord - 1, zCoord).add(0.5); } @Override @@ -633,8 +311,6 @@ public class TileEntityMiningLaser extends WarpChunkTE implements IPeripheral, I tag.setInteger("digX", digX); tag.setInteger("digZ", digZ); - tag.setBoolean("silkTouch", silkTouch); - tag.setInteger("fortuneLevel", fortuneLevel); tag.setDouble("speedMul", speedMul); } //CC @@ -650,11 +326,6 @@ public class TileEntityMiningLaser extends WarpChunkTE implements IPeripheral, I { return methodsArray; } - - private int toInt(Object o) - { - return (int) Math.round(Double.parseDouble(o.toString())); - } @Override public Object[] callMethod(IComputerAccess computer, ILuaContext context, int method, Object[] arguments) throws Exception @@ -664,7 +335,6 @@ public class TileEntityMiningLaser extends WarpChunkTE implements IPeripheral, I case 0: // Mine() if (isMining) return new Boolean[] { false }; - minerVector = new Vector3(xCoord, yCoord - 1, zCoord).add(0.5); currentLayer = yCoord - layerOffset; digX = CUBE_SIDE; digZ = CUBE_SIDE; @@ -672,21 +342,19 @@ public class TileEntityMiningLaser extends WarpChunkTE implements IPeripheral, I { if(arguments.length >= 2) { - try - { - digX = Math.min(toInt(arguments[0]),WarpDriveConfig.i.ML_MAX_SIZE); - digZ = Math.min(toInt(arguments[1]),WarpDriveConfig.i.ML_MAX_SIZE); - - isQuarry = false; - delayTicksScan = 0; - currentMode = 0; - isMining = true; - } - catch(NumberFormatException e) - { - - } + digX = Math.min(toInt(arguments[0]),WarpDriveConfig.i.ML_MAX_SIZE); + digZ = Math.min(toInt(arguments[1]),WarpDriveConfig.i.ML_MAX_SIZE); } + else + { + digX = CUBE_SIDE; + digZ = CUBE_SIDE; + } + isQuarry = false; + delayTicksScan = 0; + currentMode = 0; + isMining = true; + defineMiningArea(digX,digZ); } catch(NumberFormatException e) { @@ -710,7 +378,6 @@ public class TileEntityMiningLaser extends WarpChunkTE implements IPeripheral, I isQuarry = true; delayTicksScan = 0; currentMode = 0; - minerVector = new Vector3(xCoord, yCoord - 1, zCoord).add(0.5); currentLayer = yCoord - layerOffset; isMining = true; digX = CUBE_SIDE; @@ -722,6 +389,7 @@ public class TileEntityMiningLaser extends WarpChunkTE implements IPeripheral, I digX = Math.min(toInt(arguments[0]),WarpDriveConfig.i.ML_MAX_SIZE); digZ = Math.min(toInt(arguments[1]),WarpDriveConfig.i.ML_MAX_SIZE); } + defineMiningArea(digX,digZ); } catch(NumberFormatException e) { @@ -732,9 +400,6 @@ public class TileEntityMiningLaser extends WarpChunkTE implements IPeripheral, I return new Boolean[] { true }; case 4: // State is: state, energy, currentLayer, valuablesMined, valuablesInLayer = getMinerState() - int energy = 0; - if (booster != null) - energy = booster.getCurrentEnergyValue(); String state = "not mining"; int valuablesMined = 0; int valuablesInLayer = 0; @@ -743,11 +408,11 @@ public class TileEntityMiningLaser extends WarpChunkTE implements IPeripheral, I valuablesInLayer = this.valuablesInLayer.size(); valuablesMined = this.valuableIndex; state = "mining" + ((isQuarry) ? " (quarry mode)" : ""); - if (energy < 0) + if (energy() < 0) state = "out of energy"; } - return new Object[] {state, energy, currentLayer, valuablesMined, valuablesInLayer, - digX, digZ, speedMul, fortuneLevel, silkTouch}; + return new Object[] {state, energy(), currentLayer, valuablesMined, valuablesInLayer, + digX, digZ, speedMul, fortune(), silkTouch()}; case 5: // Offset if (arguments.length == 1) @@ -760,21 +425,12 @@ public class TileEntityMiningLaser extends WarpChunkTE implements IPeripheral, I return new Integer[] { layerOffset-1 }; case 6: // silktouch(1/boolean) if (arguments.length == 1) - silkTouch = arguments[0].toString() == "true" || arguments[0].toString() == "1"; - return new Boolean[] { silkTouch }; + silkTouch(toBool(arguments[0])); + return new Boolean[] { silkTouch() }; case 7: // fortune(int) if (arguments.length == 1) - { - try - { - fortuneLevel = (int) Math.round(Math.max(0, Math.min(Double.parseDouble(arguments[0].toString()),5))); - } - catch (NumberFormatException e) - { - fortuneLevel = 0; - } - } - return new Integer[] { fortuneLevel }; + fortune(toInt(arguments[0])); + return new Integer[] { fortune() }; case 8: // speedMul(double) if (arguments.length == 1) { @@ -838,74 +494,6 @@ public class TileEntityMiningLaser extends WarpChunkTE implements IPeripheral, I public void detach(IComputerAccess computer) { } -//AE - @Override - public float getPowerDrainPerTick() - { - return 1; - } - - @Override - public void validate() - { - super.validate(); - MinecraftForge.EVENT_BUS.post(new GridTileLoadEvent(this, worldObj, getLocation())); - } - - @Override - public void invalidate() - { - super.invalidate(); - MinecraftForge.EVENT_BUS.post(new GridTileUnloadEvent(this, worldObj, getLocation())); - } - - @Override - public WorldCoord getLocation() - { - return new WorldCoord(xCoord, yCoord, zCoord); - } - - @Override - public boolean isValid() - { - return true; - } - - @Override - public void setPowerStatus(boolean hasPower) - { - powerStatus = hasPower; - } - - @Override - public boolean isPowered() - { - return powerStatus; - } - - @Override - public IGridInterface getGrid() - { - return grid; - } - - @Override - public void setGrid(IGridInterface gi) - { - grid = gi; - } - - @Override - public World getWorld() - { - return worldObj; - } - - @Override - public boolean coveredConnections() - { - return true; - } @Override public boolean shouldChunkLoad() {