From 4c4f534e5b9468daf8b94ac2736a9ea7b102dff4 Mon Sep 17 00:00:00 2001 From: "Aidan C. Brady" Date: Tue, 11 Feb 2014 21:12:54 -0500 Subject: [PATCH] Digital Miner overhaul - easier than I thought. Also cleanup. --- .../mekanism/client/gui/GuiDigitalMiner.java | 16 +- common/mekanism/common/item/ItemProxy.java | 16 +- .../common/miner/ThreadMinerSearch.java | 111 ---------- .../common/tile/TileEntityDigitalMiner.java | 191 ++++++++---------- .../mekanism/common/transporter/Finder.java | 2 +- 5 files changed, 103 insertions(+), 233 deletions(-) delete mode 100644 common/mekanism/common/miner/ThreadMinerSearch.java diff --git a/common/mekanism/client/gui/GuiDigitalMiner.java b/common/mekanism/client/gui/GuiDigitalMiner.java index 762495f49..9aa98c416 100644 --- a/common/mekanism/client/gui/GuiDigitalMiner.java +++ b/common/mekanism/client/gui/GuiDigitalMiner.java @@ -9,7 +9,6 @@ import mekanism.client.gui.GuiEnergyInfo.IInfoHandler; import mekanism.common.PacketHandler; import mekanism.common.PacketHandler.Transmission; import mekanism.common.inventory.container.ContainerDigitalMiner; -import mekanism.common.miner.ThreadMinerSearch.State; import mekanism.common.network.PacketDigitalMinerGui; import mekanism.common.network.PacketDigitalMinerGui.MinerGuiPacket; import mekanism.common.network.PacketTileEntity; @@ -67,21 +66,21 @@ public class GuiDigitalMiner extends GuiMekanism buttonList.clear(); startButton = new GuiButton(0, guiWidth + 69, guiHeight + 17, 60, 20, MekanismUtils.localize("gui.start")); - if(tileEntity.searcher.state != State.IDLE && tileEntity.running) + if(tileEntity.running) { startButton.enabled = false; } stopButton = new GuiButton(1, guiWidth + 69, guiHeight + 37, 60, 20, MekanismUtils.localize("gui.stop")); - if(tileEntity.searcher.state == State.IDLE || !tileEntity.running) + if(!tileEntity.running) { stopButton.enabled = false; } configButton = new GuiButton(2, guiWidth + 69, guiHeight + 57, 60, 20, MekanismUtils.localize("gui.config")); - if(tileEntity.searcher.state != State.IDLE) + if(tileEntity.running || tileEntity.searched > 0) { configButton.enabled = false; } @@ -121,7 +120,7 @@ public class GuiDigitalMiner extends GuiMekanism { super.updateScreen(); - if(tileEntity.searcher.state != State.IDLE && tileEntity.running) + if(tileEntity.running) { startButton.enabled = false; } @@ -129,7 +128,7 @@ public class GuiDigitalMiner extends GuiMekanism startButton.enabled = true; } - if(tileEntity.searcher.state == State.IDLE || !tileEntity.running) + if(!tileEntity.running) { stopButton.enabled = false; } @@ -137,7 +136,7 @@ public class GuiDigitalMiner extends GuiMekanism stopButton.enabled = true; } - if(tileEntity.searcher.state != State.IDLE) + if(tileEntity.running || tileEntity.searched > 0) { configButton.enabled = false; } @@ -156,14 +155,13 @@ public class GuiDigitalMiner extends GuiMekanism fontRenderer.drawString(MekanismUtils.localize("container.inventory"), 8, (ySize - 96) + 2, 0x404040); fontRenderer.drawString(tileEntity.running ? MekanismUtils.localize("gui.digitalMiner.running") : MekanismUtils.localize("gui.idle"), 9, 10, 0x00CD00); - fontRenderer.drawString(tileEntity.searcher.state.desc, 9, 19, 0x00CD00); fontRenderer.drawString(MekanismUtils.localize("gui.eject") + ": " + MekanismUtils.localize("gui." + (tileEntity.doEject ? "on" : "off")), 9, 30, 0x00CD00); fontRenderer.drawString(MekanismUtils.localize("gui.digitalMiner.pull") + ": " + MekanismUtils.localize("gui." + (tileEntity.doPull ? "on" : "off")), 9, 39, 0x00CD00); fontRenderer.drawString(MekanismUtils.localize("gui.digitalMiner.silk") + ": " + MekanismUtils.localize("gui." + (tileEntity.silkTouch ? "on" : "off")), 9, 48, 0x00CD00); fontRenderer.drawString(MekanismUtils.localize("gui.digitalMiner.toMine") + ":", 9, 59, 0x00CD00); - fontRenderer.drawString("" + tileEntity.clientToMine, 9, 68, 0x00CD00); + fontRenderer.drawString("" + tileEntity.searched, 9, 68, 0x00CD00); if(tileEntity.replaceStack != null) { diff --git a/common/mekanism/common/item/ItemProxy.java b/common/mekanism/common/item/ItemProxy.java index 882826f70..e53df3a6a 100644 --- a/common/mekanism/common/item/ItemProxy.java +++ b/common/mekanism/common/item/ItemProxy.java @@ -82,16 +82,20 @@ public class ItemProxy extends Item public void registerIcons(IconRegister register) {} @Override - public void onUpdate(ItemStack par1ItemStack, World par2World, Entity par3Entity, int par4, boolean par5) + public void onUpdate(ItemStack stack, World world, Entity entity, int par4, boolean flag) { - if (par3Entity instanceof EntityPlayer) + if(entity instanceof EntityPlayer) { - EntityPlayer player = (EntityPlayer) par3Entity; - for (Object o : player.inventoryContainer.inventorySlots) + EntityPlayer player = (EntityPlayer)entity; + + for(Object o : player.inventoryContainer.inventorySlots) { - Slot s = (Slot) o; - if (s.getStack() != null && s.getStack().getItem() == this) + Slot s = (Slot)o; + + if(s.getStack() != null && s.getStack().getItem() == this) + { player.inventory.decrStackSize(s.slotNumber, 64); + } } } } diff --git a/common/mekanism/common/miner/ThreadMinerSearch.java b/common/mekanism/common/miner/ThreadMinerSearch.java deleted file mode 100644 index 4d3fb943c..000000000 --- a/common/mekanism/common/miner/ThreadMinerSearch.java +++ /dev/null @@ -1,111 +0,0 @@ -package mekanism.common.miner; - -import java.util.Collections; - -import mekanism.api.Coord4D; -import mekanism.common.IBoundingBlock; -import mekanism.common.tile.TileEntityDigitalMiner; -import mekanism.common.util.MekanismUtils; -import net.minecraft.block.Block; -import net.minecraft.item.ItemStack; - -public class ThreadMinerSearch extends Thread -{ - public TileEntityDigitalMiner tileEntity; - - public State state = State.IDLE; - - public ThreadMinerSearch(TileEntityDigitalMiner tile) - { - tileEntity = tile; - } - - @Override - public void run() - { - state = State.SEARCHING; - - if(tileEntity.filters.isEmpty()) - { - state = State.FINISHED; - return; - } - - for(int y = tileEntity.maxY; y >= tileEntity.minY; y--) - { - for(int x = tileEntity.xCoord-tileEntity.radius; x <= tileEntity.xCoord+tileEntity.radius; x++) - { - for(int z = tileEntity.zCoord-tileEntity.radius; z <= tileEntity.zCoord+tileEntity.radius; z++) - { - if(tileEntity.isInvalid()) - { - return; - } - - if(Coord4D.get(tileEntity).equals(new Coord4D(x, y, z, tileEntity.worldObj.provider.dimensionId))) - { - continue; - } - - if(new Coord4D(x, y, z, tileEntity.worldObj.provider.dimensionId).getTileEntity(tileEntity.worldObj) instanceof IBoundingBlock) - { - continue; - } - - int blockID = tileEntity.worldObj.getBlockId(x, y, z); - int meta = tileEntity.worldObj.getBlockMetadata(x, y, z); - - if(blockID != 0 && blockID != Block.bedrock.blockID) - { - ItemStack stack = new ItemStack(blockID, 1, meta); - - if(tileEntity.replaceStack != null && tileEntity.replaceStack.isItemEqual(stack)) - { - continue; - } - - boolean hasFilter = false; - - for(MinerFilter filter : tileEntity.filters) - { - if(filter.canFilter(stack)) - { - hasFilter = true; - } - } - - if(tileEntity.inverse ? !hasFilter : hasFilter) - { - tileEntity.oresToMine.add(new Coord4D(x, y, z, tileEntity.worldObj.provider.dimensionId)); - } - } - } - } - } - - Collections.shuffle(tileEntity.oresToMine); - - state = State.FINISHED; - MekanismUtils.saveChunk(tileEntity); - } - - public void reset() - { - state = State.IDLE; - } - - public static enum State - { - IDLE("Not ready"), - SEARCHING("Searching"), - PAUSED("Paused"), - FINISHED("Ready"); - - public String desc; - - private State(String s) - { - desc = s; - } - } -} diff --git a/common/mekanism/common/tile/TileEntityDigitalMiner.java b/common/mekanism/common/tile/TileEntityDigitalMiner.java index 620bcb03d..709964898 100644 --- a/common/mekanism/common/tile/TileEntityDigitalMiner.java +++ b/common/mekanism/common/tile/TileEntityDigitalMiner.java @@ -1,15 +1,14 @@ package mekanism.common.tile; import java.util.ArrayList; -import java.util.HashSet; import java.util.Iterator; import java.util.List; -import java.util.Set; import mekanism.api.Coord4D; import mekanism.common.HashList; import mekanism.common.IActiveState; import mekanism.common.IAdvancedBoundingBlock; +import mekanism.common.IBoundingBlock; import mekanism.common.ILogisticalTransporter; import mekanism.common.IRedstoneControl; import mekanism.common.IUpgradeTile; @@ -22,8 +21,6 @@ import mekanism.common.inventory.container.ContainerNull; import mekanism.common.miner.MItemStackFilter; import mekanism.common.miner.MOreDictFilter; import mekanism.common.miner.MinerFilter; -import mekanism.common.miner.ThreadMinerSearch; -import mekanism.common.miner.ThreadMinerSearch.State; import mekanism.common.network.PacketTileEntity; import mekanism.common.tile.component.TileComponentUpgrade; import mekanism.common.transporter.InvStack; @@ -55,11 +52,11 @@ public class TileEntityDigitalMiner extends TileEntityElectricBlock implements I { public static int[] EJECT_INV; - public List oresToMine = new ArrayList(); + public int searched = 0; public HashList filters = new HashList(); - public ThreadMinerSearch searcher = new ThreadMinerSearch(this); + public Coord4D currentNode; public final double ENERGY_USAGE = Mekanism.digitalMinerUsage; @@ -75,8 +72,6 @@ public class TileEntityDigitalMiner extends TileEntityElectricBlock implements I public int delay; - public int clientToMine; - public ItemStack replaceStack; public boolean isActive; @@ -90,8 +85,6 @@ public class TileEntityDigitalMiner extends TileEntityElectricBlock implements I public int delayTicks; - public boolean initCalc = false; - public int numPowering; /** This machine's current RedstoneControl type. */ @@ -124,20 +117,9 @@ public class TileEntityDigitalMiner extends TileEntityElectricBlock implements I if(!worldObj.isRemote) { - if(!initCalc) - { - if(searcher.state == State.FINISHED) - { - reset(); - start(); - } - - initCalc = true; - } - ChargeUtils.discharge(27, this); - if(MekanismUtils.canFunction(this) && running && getEnergy() >= getPerTick() && searcher.state == State.FINISHED && oresToMine.size() > 0) + if(MekanismUtils.canFunction(this) && running && getEnergy() >= getPerTick()) { setActive(true); @@ -150,61 +132,48 @@ public class TileEntityDigitalMiner extends TileEntityElectricBlock implements I if(delay == 0) { - Set toRemove = new HashSet(); + currentNode = getNextBlock(); - for(Coord4D obj : oresToMine) + if(currentNode != null) { - if(!obj.exists(worldObj)) + searched++; + + if(!currentNode.isAirBlock(worldObj) && !Coord4D.get(this).equals(currentNode) && !(currentNode.getTileEntity(worldObj) instanceof IBoundingBlock)) { - continue; - } - - int id = obj.getBlockId(worldObj); - int meta = obj.getMetadata(worldObj); - - if(id == 0) - { - toRemove.add(obj); - continue; - } - - boolean hasFilter = false; - - for(MinerFilter filter : filters) - { - if(filter.canFilter(new ItemStack(id, 1, meta))) + int id = currentNode.getBlockId(worldObj); + int meta = currentNode.getMetadata(worldObj); + + boolean hasFilter = false; + + for(MinerFilter filter : filters) { - hasFilter = true; - break; + if(filter.canFilter(new ItemStack(id, 1, meta))) + { + hasFilter = true; + break; + } + } + + if(inverse ? !hasFilter : hasFilter) + { + List drops = MinerUtils.getDrops(worldObj, currentNode, silkTouch); + + if(canInsert(drops)) + { + add(drops); + + setReplace(currentNode); + + worldObj.playAuxSFXAtEntity(null, 2001, currentNode.xCoord, currentNode.yCoord, currentNode.zCoord, id + (meta << 12)); + + delay = getDelay(); + } } } - - if(inverse ? hasFilter : !hasFilter) - { - toRemove.add(obj); - continue; - } - - List drops = MinerUtils.getDrops(worldObj, obj, silkTouch); - - if(canInsert(drops)) - { - add(drops); - - setReplace(obj); - toRemove.add(obj); - - worldObj.playAuxSFXAtEntity(null, 2001, obj.xCoord, obj.yCoord, obj.zCoord, id + (meta << 12)); - - delay = getDelay(); - - break; - } } - - for(Coord4D obj : toRemove) - { - oresToMine.remove(obj); + else { + searched = 0; + stop(); } } } @@ -252,6 +221,30 @@ public class TileEntityDigitalMiner extends TileEntityElectricBlock implements I } } + public Coord4D getNextBlock() + { + if(currentNode == null) + { + return new Coord4D(xCoord-radius, maxY, zCoord-radius, worldObj.provider.dimensionId); + } + + if(currentNode.xCoord < xCoord+radius) + { + return currentNode.getFromSide(ForgeDirection.EAST); + } + else if(currentNode.zCoord < zCoord+radius) + { + return new Coord4D(xCoord-radius, currentNode.yCoord, currentNode.zCoord+1); + } + else if(currentNode.yCoord > minY) + { + return new Coord4D(xCoord-radius, currentNode.yCoord-1, zCoord-radius); + } + else { + return null; + } + } + public double getPerTick() { double ret = MekanismUtils.getEnergyPerTick(getSpeedMultiplier(), getEnergyMultiplier(), ENERGY_USAGE); @@ -272,7 +265,7 @@ public class TileEntityDigitalMiner extends TileEntityElectricBlock implements I public int getDelay() { - return MekanismUtils.getTicks(getSpeedMultiplier(), 80); + return MekanismUtils.getTicks(getSpeedMultiplier(), 60); } public void setReplace(Coord4D obj) @@ -450,11 +443,6 @@ public class TileEntityDigitalMiner extends TileEntityElectricBlock implements I public void start() { - if(searcher.state == State.IDLE) - { - searcher.start(); - } - running = true; MekanismUtils.saveChunk(this); @@ -462,26 +450,16 @@ public class TileEntityDigitalMiner extends TileEntityElectricBlock implements I public void stop() { - if(searcher.state == State.SEARCHING) - { - searcher.interrupt(); - reset(); - - return; - } - else if(searcher.state == State.FINISHED) - { - running = false; - } + running = false; MekanismUtils.saveChunk(this); } public void reset() { - searcher = new ThreadMinerSearch(this); + searched = 0; + currentNode = null; running = false; - oresToMine.clear(); MekanismUtils.saveChunk(this); } @@ -515,9 +493,14 @@ public class TileEntityDigitalMiner extends TileEntityElectricBlock implements I delay = nbtTags.getInteger("delay"); silkTouch = nbtTags.getBoolean("silkTouch"); numPowering = nbtTags.getInteger("numPowering"); - searcher.state = State.values()[nbtTags.getInteger("state")]; controlType = RedstoneControl.values()[nbtTags.getInteger("controlType")]; inverse = nbtTags.getBoolean("inverse"); + searched = nbtTags.getInteger("searched"); + + if(nbtTags.hasKey("currentNode")) + { + currentNode = Coord4D.read(nbtTags.getCompoundTag("currentNode")); + } if(nbtTags.hasKey("replaceStack")) { @@ -540,11 +523,6 @@ public class TileEntityDigitalMiner extends TileEntityElectricBlock implements I { super.writeToNBT(nbtTags); - if(searcher.state == State.SEARCHING) - { - reset(); - } - nbtTags.setInteger("radius", radius); nbtTags.setInteger("minY", minY); nbtTags.setInteger("maxY", maxY); @@ -555,9 +533,14 @@ public class TileEntityDigitalMiner extends TileEntityElectricBlock implements I nbtTags.setInteger("delay", delay); nbtTags.setBoolean("silkTouch", silkTouch); nbtTags.setInteger("numPowering", numPowering); - nbtTags.setInteger("state", searcher.state.ordinal()); nbtTags.setInteger("controlType", controlType.ordinal()); nbtTags.setBoolean("inverse", inverse); + nbtTags.setInteger("searched", searched); + + if(currentNode != null) + { + nbtTags.setCompoundTag("currentNode", currentNode.write(new NBTTagCompound())); + } if(replaceStack != null) { @@ -660,7 +643,6 @@ public class TileEntityDigitalMiner extends TileEntityElectricBlock implements I running = dataStream.readBoolean(); silkTouch = dataStream.readBoolean(); numPowering = dataStream.readInt(); - searcher.state = State.values()[dataStream.readInt()]; if(dataStream.readBoolean()) { @@ -670,9 +652,9 @@ public class TileEntityDigitalMiner extends TileEntityElectricBlock implements I replaceStack = null; } - clientToMine = dataStream.readInt(); controlType = RedstoneControl.values()[dataStream.readInt()]; inverse = dataStream.readBoolean(); + searched = dataStream.readInt(); filters.clear(); @@ -694,7 +676,6 @@ public class TileEntityDigitalMiner extends TileEntityElectricBlock implements I running = dataStream.readBoolean(); silkTouch = dataStream.readBoolean(); numPowering = dataStream.readInt(); - searcher.state = State.values()[dataStream.readInt()]; if(dataStream.readBoolean()) { @@ -704,9 +685,9 @@ public class TileEntityDigitalMiner extends TileEntityElectricBlock implements I replaceStack = null; } - clientToMine = dataStream.readInt(); controlType = RedstoneControl.values()[dataStream.readInt()]; inverse = dataStream.readBoolean(); + searched = dataStream.readInt(); } else if(type == 2) { @@ -723,7 +704,7 @@ public class TileEntityDigitalMiner extends TileEntityElectricBlock implements I { isActive = dataStream.readBoolean(); running = dataStream.readBoolean(); - clientToMine = dataStream.readInt(); + searched = dataStream.readInt(); } } @@ -743,7 +724,6 @@ public class TileEntityDigitalMiner extends TileEntityElectricBlock implements I data.add(running); data.add(silkTouch); data.add(numPowering); - data.add(searcher.state.ordinal()); if(replaceStack != null) { @@ -755,9 +735,9 @@ public class TileEntityDigitalMiner extends TileEntityElectricBlock implements I data.add(false); } - data.add(oresToMine.size()); data.add(controlType.ordinal()); data.add(inverse); + data.add(searched); data.add(filters.size()); @@ -777,7 +757,7 @@ public class TileEntityDigitalMiner extends TileEntityElectricBlock implements I data.add(isActive); data.add(running); - data.add(oresToMine.size()); + data.add(searched); return data; } @@ -797,7 +777,6 @@ public class TileEntityDigitalMiner extends TileEntityElectricBlock implements I data.add(running); data.add(silkTouch); data.add(numPowering); - data.add(searcher.state.ordinal()); if(replaceStack != null) { @@ -809,9 +788,9 @@ public class TileEntityDigitalMiner extends TileEntityElectricBlock implements I data.add(false); } - data.add(oresToMine.size()); data.add(controlType.ordinal()); data.add(inverse); + data.add(searched); return data; } diff --git a/common/mekanism/common/transporter/Finder.java b/common/mekanism/common/transporter/Finder.java index 2cb6d5c6b..98a539ff9 100644 --- a/common/mekanism/common/transporter/Finder.java +++ b/common/mekanism/common/transporter/Finder.java @@ -33,7 +33,7 @@ public abstract class Finder { List oreKeys = MekanismUtils.getOreDictName(stack); - if(oreKeys.isEmpty()) + if(!oreDictName.equals("*") && oreKeys.isEmpty()) { return false; }