0
0
mirror of https://github.com/Wurst-Imperium/Wurst7.git synced 2024-09-20 01:12:13 +02:00

Move ChunkSearcher to a separate file

This commit is contained in:
Alexander01998 2020-12-08 15:44:17 +01:00
parent 82e414ab05
commit baf12705aa
3 changed files with 182 additions and 210 deletions

View File

@ -20,7 +20,6 @@ import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.ForkJoinTask;
import java.util.concurrent.Future;
import java.util.stream.Collectors;
import org.lwjgl.opengl.GL11;
@ -36,6 +35,7 @@ import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.ChunkPos;
import net.minecraft.util.math.MathHelper;
import net.minecraft.world.chunk.Chunk;
import net.minecraft.world.chunk.EmptyChunk;
import net.wurstclient.Category;
import net.wurstclient.SearchTags;
import net.wurstclient.events.PacketInputListener;
@ -46,6 +46,7 @@ import net.wurstclient.settings.EnumSetting;
import net.wurstclient.settings.SliderSetting;
import net.wurstclient.util.BlockUtils;
import net.wurstclient.util.ChatUtils;
import net.wurstclient.util.ChunkSearcher;
import net.wurstclient.util.MinPriorityThreadFactory;
import net.wurstclient.util.RenderUtils;
import net.wurstclient.util.RotationUtils;
@ -167,11 +168,12 @@ public final class CaveFinderHack extends Hack
ChunkPos center = getPlayerChunkPos(eyesPos);
int range = area.getSelected().chunkRange;
int dimensionId = MC.world.getRegistryKey().toString().hashCode();
addSearchersInRange(center, range, currentBlock);
addSearchersInRange(center, range, currentBlock, dimensionId);
removeSearchersOutOfRange(center, range);
replaceSearchersWithDifferentBlock(currentBlock);
replaceSearchersWithChunkUpdate(currentBlock);
replaceSearchersWithDifferences(currentBlock, dimensionId);
replaceSearchersWithChunkUpdate(currentBlock, dimensionId);
if(!areAllChunkSearchersDone())
return;
@ -237,7 +239,7 @@ public final class CaveFinderHack extends Hack
}
private void addSearchersInRange(ChunkPos center, int chunkRange,
Block block)
Block block, int dimensionId)
{
ArrayList<Chunk> chunksInRange = getChunksInRange(center, chunkRange);
@ -246,7 +248,7 @@ public final class CaveFinderHack extends Hack
if(searchers.containsKey(chunk))
continue;
addSearcher(chunk, block);
addSearcher(chunk, block, dimensionId);
}
}
@ -256,7 +258,13 @@ public final class CaveFinderHack extends Hack
for(int x = center.x - chunkRange; x <= center.x + chunkRange; x++)
for(int z = center.z - chunkRange; z <= center.z + chunkRange; z++)
chunksInRange.add(MC.world.getChunk(x, z));
{
Chunk chunk = MC.world.getChunk(x, z);
if(chunk instanceof EmptyChunk)
continue;
chunksInRange.add(chunk);
}
return chunksInRange;
}
@ -265,27 +273,32 @@ public final class CaveFinderHack extends Hack
{
for(ChunkSearcher searcher : new ArrayList<>(searchers.values()))
{
if(Math.abs(searcher.chunk.getPos().x - center.x) <= chunkRange
&& Math.abs(searcher.chunk.getPos().z - center.z) <= chunkRange)
ChunkPos searcherPos = searcher.getChunk().getPos();
if(Math.abs(searcherPos.x - center.x) <= chunkRange
&& Math.abs(searcherPos.z - center.z) <= chunkRange)
continue;
removeSearcher(searcher);
}
}
private void replaceSearchersWithDifferentBlock(Block currentBlock)
private void replaceSearchersWithDifferences(Block currentBlock,
int dimensionId)
{
for(ChunkSearcher oldSearcher : new ArrayList<>(searchers.values()))
{
if(currentBlock.equals(oldSearcher.block))
if(currentBlock.equals(oldSearcher.getBlock())
&& dimensionId == oldSearcher.getDimensionId())
continue;
removeSearcher(oldSearcher);
addSearcher(oldSearcher.chunk, currentBlock);
addSearcher(oldSearcher.getChunk(), currentBlock, dimensionId);
}
}
private void replaceSearchersWithChunkUpdate(Block currentBlock)
private void replaceSearchersWithChunkUpdate(Block currentBlock,
int dimensionId)
{
synchronized(chunksToUpdate)
{
@ -301,17 +314,17 @@ public final class CaveFinderHack extends Hack
continue;
removeSearcher(oldSearcher);
addSearcher(chunk, currentBlock);
addSearcher(chunk, currentBlock, dimensionId);
itr.remove();
}
}
}
private void addSearcher(Chunk chunk, Block block)
private void addSearcher(Chunk chunk, Block block, int dimensionId)
{
stopPool2Tasks();
ChunkSearcher searcher = new ChunkSearcher(chunk, block);
ChunkSearcher searcher = new ChunkSearcher(chunk, block, dimensionId);
searchers.put(chunk, searcher);
searcher.startSearching(pool1);
}
@ -320,7 +333,7 @@ public final class CaveFinderHack extends Hack
{
stopPool2Tasks();
searchers.remove(searcher.chunk);
searchers.remove(searcher.getChunk());
searcher.cancelSearching();
}
@ -344,7 +357,7 @@ public final class CaveFinderHack extends Hack
private boolean areAllChunkSearchersDone()
{
for(ChunkSearcher searcher : searchers.values())
if(searcher.status != ChunkSearcher.Status.DONE)
if(searcher.getStatus() != ChunkSearcher.Status.DONE)
return false;
return true;
@ -366,7 +379,7 @@ public final class CaveFinderHack extends Hack
Callable<HashSet<BlockPos>> task =
() -> searchers.values().parallelStream()
.flatMap(searcher -> searcher.matchingBlocks.stream())
.flatMap(searcher -> searcher.getMatchingBlocks().stream())
.sorted(Comparator
.comparingInt(pos -> eyesPos.getManhattanDistance(pos)))
.limit(maxBlocks)
@ -496,92 +509,6 @@ public final class CaveFinderHack extends Hack
displayListUpToDate = true;
}
private static class ChunkSearcher
{
private final Chunk chunk;
private final Block block;
private final ArrayList<BlockPos> matchingBlocks = new ArrayList<>();
private Status status = Status.IDLE;
private Future<?> future;
public ChunkSearcher(Chunk chunk, Block block)
{
this.chunk = chunk;
this.block = block;
}
public void startSearching(ExecutorService pool)
{
if(status != Status.IDLE)
throw new IllegalStateException();
status = Status.SEARCHING;
future = pool.submit(() -> searchNow());
}
private void searchNow()
{
if(status == Status.IDLE || status == Status.DONE
|| !matchingBlocks.isEmpty())
throw new IllegalStateException();
ChunkPos chunkPos = chunk.getPos();
int minX = chunkPos.getStartX();
int minY = 0;
int minZ = chunkPos.getStartZ();
int maxX = chunkPos.getEndX();
int maxY = 255;
int maxZ = chunkPos.getEndZ();
for(int x = minX; x <= maxX; x++)
for(int y = minY; y <= maxY; y++)
for(int z = minZ; z <= maxZ; z++)
{
if(status == Status.INTERRUPTED || Thread.interrupted())
return;
BlockPos pos = new BlockPos(x, y, z);
Block block = BlockUtils.getBlock(pos);
if(!this.block.equals(block))
continue;
matchingBlocks.add(pos);
}
status = Status.DONE;
}
public void cancelSearching()
{
new Thread(() -> cancelNow(), "ChunkSearcher-canceller").start();
}
private void cancelNow()
{
if(future != null)
try
{
status = Status.INTERRUPTED;
future.get();
}catch(InterruptedException | ExecutionException e)
{
e.printStackTrace();
}
matchingBlocks.clear();
status = Status.IDLE;
}
private enum Status
{
IDLE,
SEARCHING,
INTERRUPTED,
DONE;
}
}
private enum Area
{
D3("3x3 chunks", 1),

View File

@ -20,7 +20,6 @@ import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.ForkJoinTask;
import java.util.concurrent.Future;
import java.util.stream.Collectors;
import org.lwjgl.opengl.GL11;
@ -45,8 +44,8 @@ import net.wurstclient.hack.Hack;
import net.wurstclient.settings.BlockSetting;
import net.wurstclient.settings.EnumSetting;
import net.wurstclient.settings.SliderSetting;
import net.wurstclient.util.BlockUtils;
import net.wurstclient.util.ChatUtils;
import net.wurstclient.util.ChunkSearcher;
import net.wurstclient.util.MinPriorityThreadFactory;
import net.wurstclient.util.RenderUtils;
import net.wurstclient.util.RotationUtils;
@ -178,10 +177,6 @@ public final class SearchHack extends Hack
ChunkPos center = getPlayerChunkPos(eyesPos);
int range = area.getSelected().chunkRange;
// 20w21a: Assuming class_5321 is the new Dimension and using its
// toString() as an ID. Not sure why it has two Identifiers, but the
// combination should be unique for every dimension.
int dimensionId = MC.world.getRegistryKey().toString().hashCode();
addSearchersInRange(center, range, currentBlock, dimensionId);
@ -278,8 +273,10 @@ public final class SearchHack extends Hack
for(int z = center.z - chunkRange; z <= center.z + chunkRange; z++)
{
Chunk chunk = MC.world.getChunk(x, z);
if(!(chunk instanceof EmptyChunk))
chunksInRange.add(chunk);
if(chunk instanceof EmptyChunk)
continue;
chunksInRange.add(chunk);
}
return chunksInRange;
@ -289,8 +286,10 @@ public final class SearchHack extends Hack
{
for(ChunkSearcher searcher : new ArrayList<>(searchers.values()))
{
if(Math.abs(searcher.chunk.getPos().x - center.x) <= chunkRange
&& Math.abs(searcher.chunk.getPos().z - center.z) <= chunkRange)
ChunkPos searcherPos = searcher.getChunk().getPos();
if(Math.abs(searcherPos.x - center.x) <= chunkRange
&& Math.abs(searcherPos.z - center.z) <= chunkRange)
continue;
removeSearcher(searcher);
@ -302,12 +301,12 @@ public final class SearchHack extends Hack
{
for(ChunkSearcher oldSearcher : new ArrayList<>(searchers.values()))
{
if(currentBlock.equals(oldSearcher.block)
&& dimensionId == oldSearcher.dimensionId)
if(currentBlock.equals(oldSearcher.getBlock())
&& dimensionId == oldSearcher.getDimensionId())
continue;
removeSearcher(oldSearcher);
addSearcher(oldSearcher.chunk, currentBlock, dimensionId);
addSearcher(oldSearcher.getChunk(), currentBlock, dimensionId);
}
}
@ -347,7 +346,7 @@ public final class SearchHack extends Hack
{
stopPool2Tasks();
searchers.remove(searcher.chunk);
searchers.remove(searcher.getChunk());
searcher.cancelSearching();
}
@ -371,7 +370,7 @@ public final class SearchHack extends Hack
private boolean areAllChunkSearchersDone()
{
for(ChunkSearcher searcher : searchers.values())
if(searcher.status != ChunkSearcher.Status.DONE)
if(searcher.getStatus() != ChunkSearcher.Status.DONE)
return false;
return true;
@ -393,7 +392,7 @@ public final class SearchHack extends Hack
Callable<HashSet<BlockPos>> task =
() -> searchers.values().parallelStream()
.flatMap(searcher -> searcher.matchingBlocks.stream())
.flatMap(searcher -> searcher.getMatchingBlocks().stream())
.sorted(Comparator
.comparingInt(pos -> eyesPos.getManhattanDistance(pos)))
.limit(maxBlocks)
@ -523,94 +522,6 @@ public final class SearchHack extends Hack
displayListUpToDate = true;
}
private static class ChunkSearcher
{
private final Chunk chunk;
private final Block block;
private final int dimensionId;
private final ArrayList<BlockPos> matchingBlocks = new ArrayList<>();
private Status status = Status.IDLE;
private Future<?> future;
public ChunkSearcher(Chunk chunk, Block block, int dimensionId)
{
this.chunk = chunk;
this.block = block;
this.dimensionId = dimensionId;
}
public void startSearching(ExecutorService pool)
{
if(status != Status.IDLE)
throw new IllegalStateException();
status = Status.SEARCHING;
future = pool.submit(() -> searchNow());
}
private void searchNow()
{
if(status == Status.IDLE || status == Status.DONE
|| !matchingBlocks.isEmpty())
throw new IllegalStateException();
ChunkPos chunkPos = chunk.getPos();
int minX = chunkPos.getStartX();
int minY = 0;
int minZ = chunkPos.getStartZ();
int maxX = chunkPos.getEndX();
int maxY = 255;
int maxZ = chunkPos.getEndZ();
for(int x = minX; x <= maxX; x++)
for(int y = minY; y <= maxY; y++)
for(int z = minZ; z <= maxZ; z++)
{
if(status == Status.INTERRUPTED || Thread.interrupted())
return;
BlockPos pos = new BlockPos(x, y, z);
Block block = BlockUtils.getBlock(pos);
if(!this.block.equals(block))
continue;
matchingBlocks.add(pos);
}
status = Status.DONE;
}
public void cancelSearching()
{
new Thread(() -> cancelNow(), "ChunkSearcher-canceller").start();
}
private void cancelNow()
{
if(future != null)
try
{
status = Status.INTERRUPTED;
future.get();
}catch(InterruptedException | ExecutionException e)
{
e.printStackTrace();
}
matchingBlocks.clear();
status = Status.IDLE;
}
private enum Status
{
IDLE,
SEARCHING,
INTERRUPTED,
DONE;
}
}
private enum Area
{
D3("3x3 chunks", 1),

View File

@ -0,0 +1,134 @@
/*
* Copyright (c) 2014-2020 Wurst-Imperium and contributors.
*
* This source code is subject to the terms of the GNU General Public
* License, version 3. If a copy of the GPL was not distributed with this
* file, You can obtain one at: https://www.gnu.org/licenses/gpl-3.0.txt
*/
package net.wurstclient.util;
import java.util.ArrayList;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;
import net.minecraft.block.Block;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.ChunkPos;
import net.minecraft.world.chunk.Chunk;
/**
* Searches a {@link Chunk} for a particular type of {@link Block}.
*/
public final class ChunkSearcher
{
private final Chunk chunk;
private final Block block;
private final int dimensionId;
private final ArrayList<BlockPos> matchingBlocks = new ArrayList<>();
private ChunkSearcher.Status status = Status.IDLE;
private Future<?> future;
public ChunkSearcher(Chunk chunk, Block block, int dimensionId)
{
this.chunk = chunk;
this.block = block;
this.dimensionId = dimensionId;
}
public void startSearching(ExecutorService pool)
{
if(status != Status.IDLE)
throw new IllegalStateException();
status = Status.SEARCHING;
future = pool.submit(() -> searchNow());
}
private void searchNow()
{
if(status == Status.IDLE || status == Status.DONE
|| !matchingBlocks.isEmpty())
throw new IllegalStateException();
ChunkPos chunkPos = chunk.getPos();
int minX = chunkPos.getStartX();
int minY = 0;
int minZ = chunkPos.getStartZ();
int maxX = chunkPos.getEndX();
int maxY = 255;
int maxZ = chunkPos.getEndZ();
for(int x = minX; x <= maxX; x++)
for(int y = minY; y <= maxY; y++)
for(int z = minZ; z <= maxZ; z++)
{
if(status == Status.INTERRUPTED || Thread.interrupted())
return;
BlockPos pos = new BlockPos(x, y, z);
Block block = BlockUtils.getBlock(pos);
if(!this.block.equals(block))
continue;
matchingBlocks.add(pos);
}
status = Status.DONE;
}
public void cancelSearching()
{
new Thread(() -> cancelNow(), "ChunkSearcher-canceller").start();
}
private void cancelNow()
{
if(future != null)
try
{
status = Status.INTERRUPTED;
future.get();
}catch(InterruptedException | ExecutionException e)
{
e.printStackTrace();
}
matchingBlocks.clear();
status = Status.IDLE;
}
public Chunk getChunk()
{
return chunk;
}
public Block getBlock()
{
return block;
}
public int getDimensionId()
{
return dimensionId;
}
public ArrayList<BlockPos> getMatchingBlocks()
{
return matchingBlocks;
}
public ChunkSearcher.Status getStatus()
{
return status;
}
public static enum Status
{
IDLE,
SEARCHING,
INTERRUPTED,
DONE;
}
}