mirror of
https://github.com/Thorfusion/Mekanism-Community-Edition.git
synced 2026-11-22 04:52:25 +00:00
Make ThreadMinerSearch use a chunk cache.
This commit is contained in:
1 parent
29a3477776
commit
c2144495bd
2 files changed
+37
-25
No files matched your search
@@ -13,7 +13,7 @@ import net.minecraft.block.BlockLiquid;
|
||||
import net.minecraft.block.state.IBlockState;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.util.math.BlockPos;
|
||||
import net.minecraft.world.World;
|
||||
import net.minecraft.world.ChunkCache;
|
||||
import net.minecraftforge.fluids.IFluidBlock;
|
||||
|
||||
public class ThreadMinerSearch extends Thread {
|
||||
@@ -25,6 +25,7 @@ public class ThreadMinerSearch extends Thread {
|
||||
private Map<Chunk3D, BitSet> oresToMine = new HashMap<>();
|
||||
private Map<Integer, MinerFilter> replaceMap = new HashMap<>();
|
||||
private Map<BlockInfo, MinerFilter> acceptedItems = new HashMap<>();
|
||||
private ChunkCache chunkCache;
|
||||
|
||||
public int found = 0;
|
||||
|
||||
@@ -32,6 +33,10 @@ public class ThreadMinerSearch extends Thread {
|
||||
tileEntity = tile;
|
||||
}
|
||||
|
||||
public void setChunkCache(ChunkCache chunkCache) {
|
||||
this.chunkCache = chunkCache;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
state = State.SEARCHING;
|
||||
@@ -44,7 +49,6 @@ public class ThreadMinerSearch extends Thread {
|
||||
int size = tileEntity.getTotalSize();
|
||||
BlockInfo info = new BlockInfo(null, 0);
|
||||
BlockPos minerPos = tileEntity.getPos();
|
||||
World world = tileEntity.getWorld();
|
||||
|
||||
for (int i = 0; i < size; i++) {
|
||||
if (tileEntity.isInvalid()) {
|
||||
@@ -60,48 +64,48 @@ public class ThreadMinerSearch extends Thread {
|
||||
}
|
||||
|
||||
BlockPos testPos = new BlockPos(x, y, z);
|
||||
if (!world.isBlockLoaded(testPos) || world.getTileEntity(testPos) instanceof TileEntityBoundingBlock) {
|
||||
if (chunkCache.getTileEntity(testPos) instanceof TileEntityBoundingBlock) {
|
||||
//If it is not loaded or it is a bounding block skip it
|
||||
continue;
|
||||
}
|
||||
|
||||
IBlockState state = world.getBlockState(testPos);
|
||||
IBlockState state = chunkCache.getBlockState(testPos);
|
||||
info.block = state.getBlock();
|
||||
info.meta = state.getBlock().getMetaFromState(state);
|
||||
|
||||
if (info.block == null || info.block instanceof BlockLiquid || info.block instanceof IFluidBlock || info.block.isAir(state, world, testPos)) {
|
||||
if (info.block == null || info.block instanceof BlockLiquid || info.block instanceof IFluidBlock || info.block.isAir(state, chunkCache, testPos)) {
|
||||
//Skip air and liquids
|
||||
continue;
|
||||
}
|
||||
|
||||
if (state.getBlockHardness(world, testPos) >= 0) {
|
||||
MinerFilter filterFound = null;
|
||||
if (acceptedItems.containsKey(info)) {
|
||||
filterFound = acceptedItems.get(info);
|
||||
} else {
|
||||
ItemStack stack = new ItemStack(info.block, 1, info.meta);
|
||||
if (tileEntity.isReplaceStack(stack)) {
|
||||
continue;
|
||||
}
|
||||
for (MinerFilter filter : tileEntity.filters) {
|
||||
if (filter.canFilter(stack)) {
|
||||
filterFound = filter;
|
||||
break;
|
||||
}
|
||||
}
|
||||
acceptedItems.put(info, filterFound);
|
||||
MinerFilter filterFound = null;
|
||||
if (acceptedItems.containsKey(info)) {
|
||||
filterFound = acceptedItems.get(info);
|
||||
} else {
|
||||
ItemStack stack = new ItemStack(info.block, 1, info.meta);
|
||||
if (tileEntity.isReplaceStack(stack)) {
|
||||
continue;
|
||||
}
|
||||
if (tileEntity.inverse == (filterFound == null)) {
|
||||
set(i, new Coord4D(x, y, z, world.provider.getDimension()));
|
||||
replaceMap.put(i, filterFound);
|
||||
found++;
|
||||
for (MinerFilter filter : tileEntity.filters) {
|
||||
if (filter.canFilter(stack)) {
|
||||
filterFound = filter;
|
||||
break;
|
||||
}
|
||||
}
|
||||
acceptedItems.put(info, filterFound);
|
||||
}
|
||||
if (tileEntity.inverse == (filterFound == null)) {
|
||||
set(i, new Coord4D(x, y, z, tileEntity.getWorld().provider.getDimension()));//I Really hope this is threadsafe..
|
||||
replaceMap.put(i, filterFound);
|
||||
found++;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
state = State.FINISHED;
|
||||
tileEntity.oresToMine = oresToMine;
|
||||
tileEntity.replaceMap = replaceMap;
|
||||
chunkCache = null;
|
||||
MekanismUtils.saveChunk(tileEntity);
|
||||
}
|
||||
|
||||
@@ -113,6 +117,7 @@ public class ThreadMinerSearch extends Thread {
|
||||
|
||||
public void reset() {
|
||||
state = State.IDLE;
|
||||
chunkCache = null;
|
||||
}
|
||||
|
||||
public enum State {
|
||||
|
||||
@@ -70,6 +70,7 @@ import net.minecraft.util.math.AxisAlignedBB;
|
||||
import net.minecraft.util.math.BlockPos;
|
||||
import net.minecraft.util.math.ChunkPos;
|
||||
import net.minecraft.util.math.Vec3i;
|
||||
import net.minecraft.world.ChunkCache;
|
||||
import net.minecraft.world.WorldServer;
|
||||
import net.minecraftforge.common.MinecraftForge;
|
||||
import net.minecraftforge.common.capabilities.Capability;
|
||||
@@ -361,6 +362,11 @@ public class TileEntityDigitalMiner extends TileEntityElectricBlock implements I
|
||||
|
||||
private boolean canMine(Coord4D coord) {
|
||||
IBlockState state = coord.getBlockState(world);
|
||||
//Check if the block is breakable, to avoid blocks like bedrock being being mined.
|
||||
if(state.getBlockHardness(world, coord.getPos()) < 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
EntityPlayer dummy = Objects.requireNonNull(Mekanism.proxy.getDummyPlayer((WorldServer) world, pos).get());
|
||||
BlockEvent.BreakEvent event = new BlockEvent.BreakEvent(world, coord.getPos(), state, dummy);
|
||||
MinecraftForge.EVENT_BUS.post(event);
|
||||
@@ -473,6 +479,7 @@ public class TileEntityDigitalMiner extends TileEntityElectricBlock implements I
|
||||
|
||||
public void start() {
|
||||
if (searcher.state == State.IDLE) {
|
||||
searcher.setChunkCache(new ChunkCache(getWorld(), getStartingCoord().getPos(), getStartingCoord().getPos().add(radius, maxY - minY, radius), 0));
|
||||
searcher.start();
|
||||
}
|
||||
running = true;
|
||||
|
||||
Reference in new issue
Block a user