pick 97420a31d The big reformat of 2020
This commit is contained in:
@@ -18,12 +18,12 @@
|
||||
|
||||
package appeng.services;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.*;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
|
||||
import appeng.api.AEApi;
|
||||
import appeng.api.util.DimensionalCoord;
|
||||
import appeng.services.compass.CompassReader;
|
||||
import appeng.services.compass.ICompassCallback;
|
||||
import appeng.util.Platform;
|
||||
import net.minecraft.block.Block;
|
||||
import net.minecraft.server.MinecraftServer;
|
||||
import net.minecraft.util.math.BlockPos;
|
||||
@@ -34,327 +34,281 @@ import net.minecraft.world.server.ServerWorld;
|
||||
import net.minecraftforge.event.world.WorldEvent;
|
||||
import net.minecraftforge.eventbus.api.SubscribeEvent;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.*;
|
||||
import appeng.api.AEApi;
|
||||
import appeng.api.util.DimensionalCoord;
|
||||
import appeng.services.compass.CompassReader;
|
||||
import appeng.services.compass.ICompassCallback;
|
||||
import appeng.util.Platform;
|
||||
|
||||
public final class CompassService {
|
||||
private static final int CHUNK_SIZE = 16;
|
||||
|
||||
public final class CompassService
|
||||
{
|
||||
private static final int CHUNK_SIZE = 16;
|
||||
private final MinecraftServer server;
|
||||
private final Map<DimensionType, CompassReader> worldSet = new HashMap<>(10);
|
||||
private final ExecutorService executor;
|
||||
|
||||
private final MinecraftServer server;
|
||||
private final Map<DimensionType, CompassReader> worldSet = new HashMap<>( 10 );
|
||||
private final ExecutorService executor;
|
||||
private int jobSize;
|
||||
|
||||
private int jobSize;
|
||||
public CompassService(MinecraftServer server, @Nonnull final ThreadFactory factory) {
|
||||
this.server = server;
|
||||
this.executor = Executors.newSingleThreadExecutor(factory);
|
||||
this.jobSize = 0;
|
||||
}
|
||||
|
||||
public CompassService(MinecraftServer server, @Nonnull final ThreadFactory factory)
|
||||
{
|
||||
this.server = server;
|
||||
this.executor = Executors.newSingleThreadExecutor( factory );
|
||||
this.jobSize = 0;
|
||||
}
|
||||
public Future<?> getCompassDirection(final DimensionalCoord coord, final int maxRange, final ICompassCallback cc) {
|
||||
this.jobSize++;
|
||||
return this.executor.submit(new CMDirectionRequest(coord, maxRange, cc));
|
||||
}
|
||||
|
||||
public Future<?> getCompassDirection( final DimensionalCoord coord, final int maxRange, final ICompassCallback cc )
|
||||
{
|
||||
this.jobSize++;
|
||||
return this.executor.submit( new CMDirectionRequest( coord, maxRange, cc ) );
|
||||
}
|
||||
/**
|
||||
* Ensure the a compass service is removed once a world gets unloaded by forge.
|
||||
*
|
||||
* @param event the event containing the unloaded world.
|
||||
*/
|
||||
// FIXME this is never registered
|
||||
@SubscribeEvent
|
||||
public void unloadWorld(final WorldEvent.Unload event) {
|
||||
DimensionType dim = event.getWorld().getDimension().getType();
|
||||
|
||||
/**
|
||||
* Ensure the a compass service is removed once a world gets unloaded by forge.
|
||||
*
|
||||
* @param event the event containing the unloaded world.
|
||||
*/
|
||||
// FIXME this is never registered
|
||||
@SubscribeEvent
|
||||
public void unloadWorld( final WorldEvent.Unload event )
|
||||
{
|
||||
DimensionType dim = event.getWorld().getDimension().getType();
|
||||
if (Platform.isServer() && this.worldSet.containsKey(dim)) {
|
||||
final CompassReader compassReader = this.worldSet.remove(dim);
|
||||
compassReader.close();
|
||||
}
|
||||
}
|
||||
|
||||
if( Platform.isServer() && this.worldSet.containsKey( dim ) )
|
||||
{
|
||||
final CompassReader compassReader = this.worldSet.remove( dim );
|
||||
compassReader.close();
|
||||
}
|
||||
}
|
||||
private int jobSize() {
|
||||
return this.jobSize;
|
||||
}
|
||||
|
||||
private int jobSize()
|
||||
{
|
||||
return this.jobSize;
|
||||
}
|
||||
private void cleanUp() {
|
||||
for (final CompassReader cr : this.worldSet.values()) {
|
||||
cr.close();
|
||||
}
|
||||
}
|
||||
|
||||
private void cleanUp()
|
||||
{
|
||||
for( final CompassReader cr : this.worldSet.values() )
|
||||
{
|
||||
cr.close();
|
||||
}
|
||||
}
|
||||
public void updateArea(final IWorld w, final int chunkX, final int chunkZ) {
|
||||
final int x = chunkX << 4;
|
||||
final int z = chunkZ << 4;
|
||||
|
||||
public void updateArea( final IWorld w, final int chunkX, final int chunkZ )
|
||||
{
|
||||
final int x = chunkX << 4;
|
||||
final int z = chunkZ << 4;
|
||||
this.updateArea(w, x, CHUNK_SIZE, z);
|
||||
this.updateArea(w, x, CHUNK_SIZE + 32, z);
|
||||
this.updateArea(w, x, CHUNK_SIZE + 64, z);
|
||||
this.updateArea(w, x, CHUNK_SIZE + 96, z);
|
||||
|
||||
this.updateArea( w, x, CHUNK_SIZE, z );
|
||||
this.updateArea( w, x, CHUNK_SIZE + 32, z );
|
||||
this.updateArea( w, x, CHUNK_SIZE + 64, z );
|
||||
this.updateArea( w, x, CHUNK_SIZE + 96, z );
|
||||
this.updateArea(w, x, CHUNK_SIZE + 128, z);
|
||||
this.updateArea(w, x, CHUNK_SIZE + 160, z);
|
||||
this.updateArea(w, x, CHUNK_SIZE + 192, z);
|
||||
this.updateArea(w, x, CHUNK_SIZE + 224, z);
|
||||
}
|
||||
|
||||
this.updateArea( w, x, CHUNK_SIZE + 128, z );
|
||||
this.updateArea( w, x, CHUNK_SIZE + 160, z );
|
||||
this.updateArea( w, x, CHUNK_SIZE + 192, z );
|
||||
this.updateArea( w, x, CHUNK_SIZE + 224, z );
|
||||
}
|
||||
public Future<?> updateArea(final IWorld w, final int x, final int y, final int z) {
|
||||
this.jobSize++;
|
||||
|
||||
public Future<?> updateArea( final IWorld w, final int x, final int y, final int z )
|
||||
{
|
||||
this.jobSize++;
|
||||
final int cx = x >> 4;
|
||||
final int cdy = y >> 5;
|
||||
final int cz = z >> 4;
|
||||
|
||||
final int cx = x >> 4;
|
||||
final int cdy = y >> 5;
|
||||
final int cz = z >> 4;
|
||||
final int low_y = cdy << 5;
|
||||
final int hi_y = low_y + 32;
|
||||
|
||||
final int low_y = cdy << 5;
|
||||
final int hi_y = low_y + 32;
|
||||
// lower level...
|
||||
final IChunk c = w.getChunk(cx, cz);
|
||||
|
||||
// lower level...
|
||||
final IChunk c = w.getChunk( cx, cz );
|
||||
DimensionType dim = w.getDimension().getType();
|
||||
|
||||
DimensionType dim = w.getDimension().getType();
|
||||
Block skyStoneBlock = AEApi.instance().definitions().blocks().skyStoneBlock().block();
|
||||
BlockPos.Mutable pos = new BlockPos.Mutable();
|
||||
for (int i = 0; i < CHUNK_SIZE; i++) {
|
||||
pos.setX(i);
|
||||
for (int j = 0; j < CHUNK_SIZE; j++) {
|
||||
pos.setZ(j);
|
||||
for (int k = low_y; k < hi_y; k++) {
|
||||
pos.setY(k);
|
||||
final Block blk = c.getBlockState(pos).getBlock();
|
||||
if (blk == skyStoneBlock) {
|
||||
return this.executor.submit(new CMUpdatePost(dim, cx, cz, cdy, true));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Block skyStoneBlock = AEApi.instance().definitions().blocks().skyStoneBlock().block();
|
||||
BlockPos.Mutable pos = new BlockPos.Mutable();
|
||||
for( int i = 0; i < CHUNK_SIZE; i++ )
|
||||
{
|
||||
pos.setX(i);
|
||||
for( int j = 0; j < CHUNK_SIZE; j++ )
|
||||
{
|
||||
pos.setZ(j);
|
||||
for( int k = low_y; k < hi_y; k++ )
|
||||
{
|
||||
pos.setY(k);
|
||||
final Block blk = c.getBlockState( pos ).getBlock();
|
||||
if( blk == skyStoneBlock )
|
||||
{
|
||||
return this.executor.submit( new CMUpdatePost( dim, cx, cz, cdy, true ) );
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return this.executor.submit(new CMUpdatePost(dim, cx, cz, cdy, false));
|
||||
}
|
||||
|
||||
return this.executor.submit( new CMUpdatePost( dim, cx, cz, cdy, false ) );
|
||||
}
|
||||
public void kill() {
|
||||
this.executor.shutdown();
|
||||
|
||||
public void kill()
|
||||
{
|
||||
this.executor.shutdown();
|
||||
try {
|
||||
this.executor.awaitTermination(6, TimeUnit.MINUTES);
|
||||
this.jobSize = 0;
|
||||
|
||||
try
|
||||
{
|
||||
this.executor.awaitTermination( 6, TimeUnit.MINUTES );
|
||||
this.jobSize = 0;
|
||||
for (final CompassReader cr : this.worldSet.values()) {
|
||||
cr.close();
|
||||
}
|
||||
|
||||
for( final CompassReader cr : this.worldSet.values() )
|
||||
{
|
||||
cr.close();
|
||||
}
|
||||
this.worldSet.clear();
|
||||
} catch (final InterruptedException e) {
|
||||
// wrap this up..
|
||||
}
|
||||
}
|
||||
|
||||
this.worldSet.clear();
|
||||
}
|
||||
catch( final InterruptedException e )
|
||||
{
|
||||
// wrap this up..
|
||||
}
|
||||
}
|
||||
private CompassReader getReader(final DimensionType dim) {
|
||||
CompassReader cr = this.worldSet.get(dim);
|
||||
|
||||
private CompassReader getReader( final DimensionType dim )
|
||||
{
|
||||
CompassReader cr = this.worldSet.get( dim );
|
||||
if (cr == null) {
|
||||
ServerWorld sw = server.getWorld(dim);
|
||||
cr = new CompassReader(sw);
|
||||
this.worldSet.put(dim, cr);
|
||||
}
|
||||
|
||||
if( cr == null )
|
||||
{
|
||||
ServerWorld sw = server.getWorld(dim);
|
||||
cr = new CompassReader(sw);
|
||||
this.worldSet.put( dim, cr );
|
||||
}
|
||||
return cr;
|
||||
}
|
||||
|
||||
return cr;
|
||||
}
|
||||
private int dist(final int ax, final int az, final int bx, final int bz) {
|
||||
final int up = (bz - az) * CHUNK_SIZE;
|
||||
final int side = (bx - ax) * CHUNK_SIZE;
|
||||
|
||||
private int dist( final int ax, final int az, final int bx, final int bz )
|
||||
{
|
||||
final int up = ( bz - az ) * CHUNK_SIZE;
|
||||
final int side = ( bx - ax ) * CHUNK_SIZE;
|
||||
return up * up + side * side;
|
||||
}
|
||||
|
||||
return up * up + side * side;
|
||||
}
|
||||
private double rad(final int ax, final int az, final int bx, final int bz) {
|
||||
final int up = bz - az;
|
||||
final int side = bx - ax;
|
||||
|
||||
private double rad( final int ax, final int az, final int bx, final int bz )
|
||||
{
|
||||
final int up = bz - az;
|
||||
final int side = bx - ax;
|
||||
return Math.atan2(-up, side) - Math.PI / 2.0;
|
||||
}
|
||||
|
||||
return Math.atan2( -up, side ) - Math.PI / 2.0;
|
||||
}
|
||||
private class CMUpdatePost implements Runnable {
|
||||
|
||||
private class CMUpdatePost implements Runnable
|
||||
{
|
||||
public final DimensionType dim;
|
||||
|
||||
public final DimensionType dim;
|
||||
public final int chunkX;
|
||||
public final int chunkZ;
|
||||
public final int doubleChunkY; // 32 blocks instead of 16.
|
||||
public final boolean value;
|
||||
|
||||
public final int chunkX;
|
||||
public final int chunkZ;
|
||||
public final int doubleChunkY; // 32 blocks instead of 16.
|
||||
public final boolean value;
|
||||
public CMUpdatePost(final DimensionType dim, final int cx, final int cz, final int dcy, final boolean val) {
|
||||
this.dim = dim;
|
||||
this.chunkX = cx;
|
||||
this.doubleChunkY = dcy;
|
||||
this.chunkZ = cz;
|
||||
this.value = val;
|
||||
}
|
||||
|
||||
public CMUpdatePost(final DimensionType dim, final int cx, final int cz, final int dcy, final boolean val )
|
||||
{
|
||||
this.dim = dim;
|
||||
this.chunkX = cx;
|
||||
this.doubleChunkY = dcy;
|
||||
this.chunkZ = cz;
|
||||
this.value = val;
|
||||
}
|
||||
@Override
|
||||
public void run() {
|
||||
CompassService.this.jobSize--;
|
||||
|
||||
@Override
|
||||
public void run()
|
||||
{
|
||||
CompassService.this.jobSize--;
|
||||
final CompassReader cr = CompassService.this.getReader(this.dim);
|
||||
cr.setHasBeacon(this.chunkX, this.chunkZ, this.doubleChunkY, this.value);
|
||||
|
||||
final CompassReader cr = CompassService.this.getReader( this.dim );
|
||||
cr.setHasBeacon( this.chunkX, this.chunkZ, this.doubleChunkY, this.value );
|
||||
if (CompassService.this.jobSize() < 2) {
|
||||
CompassService.this.cleanUp();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if( CompassService.this.jobSize() < 2 )
|
||||
{
|
||||
CompassService.this.cleanUp();
|
||||
}
|
||||
}
|
||||
}
|
||||
private class CMDirectionRequest implements Runnable {
|
||||
|
||||
private class CMDirectionRequest implements Runnable
|
||||
{
|
||||
public final int maxRange;
|
||||
public final DimensionalCoord coord;
|
||||
public final ICompassCallback callback;
|
||||
|
||||
public final int maxRange;
|
||||
public final DimensionalCoord coord;
|
||||
public final ICompassCallback callback;
|
||||
public CMDirectionRequest(final DimensionalCoord coord, final int getMaxRange, final ICompassCallback cc) {
|
||||
this.coord = coord;
|
||||
this.maxRange = getMaxRange;
|
||||
this.callback = cc;
|
||||
}
|
||||
|
||||
public CMDirectionRequest( final DimensionalCoord coord, final int getMaxRange, final ICompassCallback cc )
|
||||
{
|
||||
this.coord = coord;
|
||||
this.maxRange = getMaxRange;
|
||||
this.callback = cc;
|
||||
}
|
||||
@Override
|
||||
public void run() {
|
||||
CompassService.this.jobSize--;
|
||||
|
||||
@Override
|
||||
public void run()
|
||||
{
|
||||
CompassService.this.jobSize--;
|
||||
final int cx = this.coord.x >> 4;
|
||||
final int cz = this.coord.z >> 4;
|
||||
|
||||
final int cx = this.coord.x >> 4;
|
||||
final int cz = this.coord.z >> 4;
|
||||
final CompassReader cr = CompassService.this.getReader(this.coord.getWorld().getDimension().getType());
|
||||
|
||||
final CompassReader cr = CompassService.this.getReader( this.coord.getWorld().getDimension().getType() );
|
||||
// Am I standing on it?
|
||||
if (cr.hasBeacon(cx, cz)) {
|
||||
this.callback.calculatedDirection(true, true, -999, 0);
|
||||
|
||||
// Am I standing on it?
|
||||
if( cr.hasBeacon( cx, cz ) )
|
||||
{
|
||||
this.callback.calculatedDirection( true, true, -999, 0 );
|
||||
if (CompassService.this.jobSize() < 2) {
|
||||
CompassService.this.cleanUp();
|
||||
}
|
||||
|
||||
if( CompassService.this.jobSize() < 2 )
|
||||
{
|
||||
CompassService.this.cleanUp();
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
// spiral outward...
|
||||
for (int offset = 1; offset < this.maxRange; offset++) {
|
||||
final int minX = cx - offset;
|
||||
final int minZ = cz - offset;
|
||||
final int maxX = cx + offset;
|
||||
final int maxZ = cz + offset;
|
||||
|
||||
// spiral outward...
|
||||
for( int offset = 1; offset < this.maxRange; offset++ )
|
||||
{
|
||||
final int minX = cx - offset;
|
||||
final int minZ = cz - offset;
|
||||
final int maxX = cx + offset;
|
||||
final int maxZ = cz + offset;
|
||||
int closest = Integer.MAX_VALUE;
|
||||
int chosen_x = cx;
|
||||
int chosen_z = cz;
|
||||
|
||||
int closest = Integer.MAX_VALUE;
|
||||
int chosen_x = cx;
|
||||
int chosen_z = cz;
|
||||
for (int z = minZ; z <= maxZ; z++) {
|
||||
if (cr.hasBeacon(minX, z)) {
|
||||
final int closeness = CompassService.this.dist(cx, cz, minX, z);
|
||||
if (closeness < closest) {
|
||||
closest = closeness;
|
||||
chosen_x = minX;
|
||||
chosen_z = z;
|
||||
}
|
||||
}
|
||||
|
||||
for( int z = minZ; z <= maxZ; z++ )
|
||||
{
|
||||
if( cr.hasBeacon( minX, z ) )
|
||||
{
|
||||
final int closeness = CompassService.this.dist( cx, cz, minX, z );
|
||||
if( closeness < closest )
|
||||
{
|
||||
closest = closeness;
|
||||
chosen_x = minX;
|
||||
chosen_z = z;
|
||||
}
|
||||
}
|
||||
if (cr.hasBeacon(maxX, z)) {
|
||||
final int closeness = CompassService.this.dist(cx, cz, maxX, z);
|
||||
if (closeness < closest) {
|
||||
closest = closeness;
|
||||
chosen_x = maxX;
|
||||
chosen_z = z;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if( cr.hasBeacon( maxX, z ) )
|
||||
{
|
||||
final int closeness = CompassService.this.dist( cx, cz, maxX, z );
|
||||
if( closeness < closest )
|
||||
{
|
||||
closest = closeness;
|
||||
chosen_x = maxX;
|
||||
chosen_z = z;
|
||||
}
|
||||
}
|
||||
}
|
||||
for (int x = minX + 1; x < maxX; x++) {
|
||||
if (cr.hasBeacon(x, minZ)) {
|
||||
final int closeness = CompassService.this.dist(cx, cz, x, minZ);
|
||||
if (closeness < closest) {
|
||||
closest = closeness;
|
||||
chosen_x = x;
|
||||
chosen_z = minZ;
|
||||
}
|
||||
}
|
||||
|
||||
for( int x = minX + 1; x < maxX; x++ )
|
||||
{
|
||||
if( cr.hasBeacon( x, minZ ) )
|
||||
{
|
||||
final int closeness = CompassService.this.dist( cx, cz, x, minZ );
|
||||
if( closeness < closest )
|
||||
{
|
||||
closest = closeness;
|
||||
chosen_x = x;
|
||||
chosen_z = minZ;
|
||||
}
|
||||
}
|
||||
if (cr.hasBeacon(x, maxZ)) {
|
||||
final int closeness = CompassService.this.dist(cx, cz, x, maxZ);
|
||||
if (closeness < closest) {
|
||||
closest = closeness;
|
||||
chosen_x = x;
|
||||
chosen_z = maxZ;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if( cr.hasBeacon( x, maxZ ) )
|
||||
{
|
||||
final int closeness = CompassService.this.dist( cx, cz, x, maxZ );
|
||||
if( closeness < closest )
|
||||
{
|
||||
closest = closeness;
|
||||
chosen_x = x;
|
||||
chosen_z = maxZ;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (closest < Integer.MAX_VALUE) {
|
||||
this.callback.calculatedDirection(true, false, CompassService.this.rad(cx, cz, chosen_x, chosen_z),
|
||||
CompassService.this.dist(cx, cz, chosen_x, chosen_z));
|
||||
|
||||
if( closest < Integer.MAX_VALUE )
|
||||
{
|
||||
this.callback.calculatedDirection( true, false, CompassService.this.rad( cx, cz, chosen_x, chosen_z ),
|
||||
CompassService.this.dist( cx, cz, chosen_x, chosen_z ) );
|
||||
if (CompassService.this.jobSize() < 2) {
|
||||
CompassService.this.cleanUp();
|
||||
}
|
||||
|
||||
if( CompassService.this.jobSize() < 2 )
|
||||
{
|
||||
CompassService.this.cleanUp();
|
||||
}
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
}
|
||||
// didn't find shit...
|
||||
this.callback.calculatedDirection(false, true, -999, 999);
|
||||
|
||||
// didn't find shit...
|
||||
this.callback.calculatedDirection( false, true, -999, 999 );
|
||||
|
||||
if( CompassService.this.jobSize() < 2 )
|
||||
{
|
||||
CompassService.this.cleanUp();
|
||||
}
|
||||
}
|
||||
}
|
||||
if (CompassService.this.jobSize() < 2) {
|
||||
CompassService.this.cleanUp();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,16 +18,13 @@
|
||||
|
||||
package appeng.services.compass;
|
||||
|
||||
public class CompassException extends RuntimeException {
|
||||
|
||||
public class CompassException extends RuntimeException
|
||||
{
|
||||
private static final long serialVersionUID = 8825268683203860877L;
|
||||
|
||||
private static final long serialVersionUID = 8825268683203860877L;
|
||||
private final Throwable inner;
|
||||
|
||||
private final Throwable inner;
|
||||
|
||||
public CompassException( final Throwable t )
|
||||
{
|
||||
this.inner = t;
|
||||
}
|
||||
public CompassException(final Throwable t) {
|
||||
this.inner = t;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,62 +18,53 @@
|
||||
|
||||
package appeng.services.compass;
|
||||
|
||||
|
||||
import com.google.common.base.Preconditions;
|
||||
import net.minecraft.world.server.ServerWorld;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import com.google.common.base.Preconditions;
|
||||
|
||||
public final class CompassReader
|
||||
{
|
||||
private final Map<Long, CompassRegion> regions = new HashMap<>( 100 );
|
||||
private final ServerWorld world;
|
||||
import net.minecraft.world.server.ServerWorld;
|
||||
|
||||
public CompassReader( ServerWorld world )
|
||||
{
|
||||
this.world = Preconditions.checkNotNull( world );
|
||||
}
|
||||
public final class CompassReader {
|
||||
private final Map<Long, CompassRegion> regions = new HashMap<>(100);
|
||||
private final ServerWorld world;
|
||||
|
||||
public void close()
|
||||
{
|
||||
for( final CompassRegion r : this.regions.values() )
|
||||
{
|
||||
r.close();
|
||||
}
|
||||
public CompassReader(ServerWorld world) {
|
||||
this.world = Preconditions.checkNotNull(world);
|
||||
}
|
||||
|
||||
this.regions.clear();
|
||||
}
|
||||
public void close() {
|
||||
for (final CompassRegion r : this.regions.values()) {
|
||||
r.close();
|
||||
}
|
||||
|
||||
public void setHasBeacon( final int cx, final int cz, final int cdy, final boolean hasBeacon )
|
||||
{
|
||||
final CompassRegion r = this.getRegion( cx, cz );
|
||||
this.regions.clear();
|
||||
}
|
||||
|
||||
r.setHasBeacon( cx, cz, cdy, hasBeacon );
|
||||
}
|
||||
public void setHasBeacon(final int cx, final int cz, final int cdy, final boolean hasBeacon) {
|
||||
final CompassRegion r = this.getRegion(cx, cz);
|
||||
|
||||
public boolean hasBeacon( final int cx, final int cz )
|
||||
{
|
||||
final CompassRegion r = this.getRegion( cx, cz );
|
||||
r.setHasBeacon(cx, cz, cdy, hasBeacon);
|
||||
}
|
||||
|
||||
return r.hasBeacon( cx, cz );
|
||||
}
|
||||
public boolean hasBeacon(final int cx, final int cz) {
|
||||
final CompassRegion r = this.getRegion(cx, cz);
|
||||
|
||||
private CompassRegion getRegion( final int cx, final int cz )
|
||||
{
|
||||
long pos = cx >> 10;
|
||||
pos <<= 32;
|
||||
pos |= ( cz >> 10 );
|
||||
return r.hasBeacon(cx, cz);
|
||||
}
|
||||
|
||||
CompassRegion cr = this.regions.get( pos );
|
||||
private CompassRegion getRegion(final int cx, final int cz) {
|
||||
long pos = cx >> 10;
|
||||
pos <<= 32;
|
||||
pos |= (cz >> 10);
|
||||
|
||||
if( cr == null )
|
||||
{
|
||||
cr = new CompassRegion( world, cx, cz );
|
||||
this.regions.put( pos, cr );
|
||||
}
|
||||
CompassRegion cr = this.regions.get(pos);
|
||||
|
||||
return cr;
|
||||
}
|
||||
if (cr == null) {
|
||||
cr = new CompassRegion(world, cx, cz);
|
||||
this.regions.put(pos, cr);
|
||||
}
|
||||
|
||||
return cr;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,145 +18,126 @@
|
||||
|
||||
package appeng.services.compass;
|
||||
|
||||
|
||||
import com.google.common.base.Preconditions;
|
||||
|
||||
import net.minecraft.nbt.CompoundNBT;
|
||||
import net.minecraft.world.server.ServerWorld;
|
||||
import net.minecraft.world.storage.WorldSavedData;
|
||||
|
||||
final class CompassRegion
|
||||
{
|
||||
private final int lowX;
|
||||
private final int lowZ;
|
||||
private final ServerWorld world;
|
||||
private SaveData data;
|
||||
final class CompassRegion {
|
||||
private final int lowX;
|
||||
private final int lowZ;
|
||||
private final ServerWorld world;
|
||||
private SaveData data;
|
||||
|
||||
public CompassRegion(ServerWorld world, final int cx, final int cz )
|
||||
{
|
||||
Preconditions.checkNotNull( world );
|
||||
public CompassRegion(ServerWorld world, final int cx, final int cz) {
|
||||
Preconditions.checkNotNull(world);
|
||||
|
||||
this.world = world;
|
||||
this.world = world;
|
||||
|
||||
final int region_x = cx >> 10;
|
||||
final int region_z = cz >> 10;
|
||||
final int region_x = cx >> 10;
|
||||
final int region_z = cz >> 10;
|
||||
|
||||
this.lowX = region_x << 10;
|
||||
this.lowZ = region_z << 10;
|
||||
this.lowX = region_x << 10;
|
||||
this.lowZ = region_z << 10;
|
||||
|
||||
this.openData(false);
|
||||
}
|
||||
this.openData(false);
|
||||
}
|
||||
|
||||
void close()
|
||||
{
|
||||
if( this.data != null )
|
||||
{
|
||||
this.data = null;
|
||||
}
|
||||
}
|
||||
void close() {
|
||||
if (this.data != null) {
|
||||
this.data = null;
|
||||
}
|
||||
}
|
||||
|
||||
boolean hasBeacon( int cx, int cz )
|
||||
{
|
||||
if( this.data != null )
|
||||
{
|
||||
cx &= 0x3FF;
|
||||
cz &= 0x3FF;
|
||||
boolean hasBeacon(int cx, int cz) {
|
||||
if (this.data != null) {
|
||||
cx &= 0x3FF;
|
||||
cz &= 0x3FF;
|
||||
|
||||
final int val = this.read( cx, cz );
|
||||
return val != 0;
|
||||
}
|
||||
final int val = this.read(cx, cz);
|
||||
return val != 0;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void setHasBeacon( int cx, int cz, final int cdy, final boolean hasBeacon )
|
||||
{
|
||||
cx &= 0x3FF;
|
||||
cz &= 0x3FF;
|
||||
void setHasBeacon(int cx, int cz, final int cdy, final boolean hasBeacon) {
|
||||
cx &= 0x3FF;
|
||||
cz &= 0x3FF;
|
||||
|
||||
this.openData( hasBeacon );
|
||||
this.openData(hasBeacon);
|
||||
|
||||
if( this.data != null )
|
||||
{
|
||||
int val = this.read( cx, cz );
|
||||
final int originalVal = val;
|
||||
if (this.data != null) {
|
||||
int val = this.read(cx, cz);
|
||||
final int originalVal = val;
|
||||
|
||||
if( hasBeacon )
|
||||
{
|
||||
val |= 1 << cdy;
|
||||
}
|
||||
else
|
||||
{
|
||||
val &= ~( 1 << cdy );
|
||||
}
|
||||
if (hasBeacon) {
|
||||
val |= 1 << cdy;
|
||||
} else {
|
||||
val &= ~(1 << cdy);
|
||||
}
|
||||
|
||||
if( originalVal != val )
|
||||
{
|
||||
this.write( cx, cz, val );
|
||||
}
|
||||
}
|
||||
}
|
||||
if (originalVal != val) {
|
||||
this.write(cx, cz, val);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void openData(final boolean create )
|
||||
{
|
||||
if( this.data != null )
|
||||
{
|
||||
return;
|
||||
}
|
||||
private void openData(final boolean create) {
|
||||
if (this.data != null) {
|
||||
return;
|
||||
}
|
||||
|
||||
String name = this.lowX + "_" + this.lowZ;
|
||||
String name = this.lowX + "_" + this.lowZ;
|
||||
|
||||
if (create) {
|
||||
this.data = world.getSavedData().getOrCreate(() -> new SaveData(name), name);
|
||||
if (this.data.bitmap == null) {
|
||||
this.data.bitmap = new byte[SaveData.BITMAP_LENGTH];
|
||||
}
|
||||
} else {
|
||||
this.data = world.getSavedData().get(() -> new SaveData(name), name);
|
||||
}
|
||||
}
|
||||
if (create) {
|
||||
this.data = world.getSavedData().getOrCreate(() -> new SaveData(name), name);
|
||||
if (this.data.bitmap == null) {
|
||||
this.data.bitmap = new byte[SaveData.BITMAP_LENGTH];
|
||||
}
|
||||
} else {
|
||||
this.data = world.getSavedData().get(() -> new SaveData(name), name);
|
||||
}
|
||||
}
|
||||
|
||||
private int read( final int cx, final int cz )
|
||||
{
|
||||
try
|
||||
{
|
||||
return this.data.bitmap[cx + cz * 0x400];
|
||||
}
|
||||
catch( final IndexOutOfBoundsException outOfBounds )
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
private int read(final int cx, final int cz) {
|
||||
try {
|
||||
return this.data.bitmap[cx + cz * 0x400];
|
||||
} catch (final IndexOutOfBoundsException outOfBounds) {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
private void write( final int cx, final int cz, final int val )
|
||||
{
|
||||
this.data.bitmap[cx + cz * 0x400] = (byte) val;
|
||||
this.data.markDirty();
|
||||
}
|
||||
private void write(final int cx, final int cz, final int val) {
|
||||
this.data.bitmap[cx + cz * 0x400] = (byte) val;
|
||||
this.data.markDirty();
|
||||
}
|
||||
|
||||
private static class SaveData extends WorldSavedData {
|
||||
private static class SaveData extends WorldSavedData {
|
||||
|
||||
private static final int BITMAP_LENGTH = 0x400 * 0x400;
|
||||
private static final int BITMAP_LENGTH = 0x400 * 0x400;
|
||||
|
||||
private byte[] bitmap;
|
||||
private byte[] bitmap;
|
||||
|
||||
public SaveData(String name) {
|
||||
super(name);
|
||||
}
|
||||
public SaveData(String name) {
|
||||
super(name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void read(CompoundNBT nbt) {
|
||||
this.bitmap = nbt.getByteArray("b");
|
||||
if (this.bitmap.length != BITMAP_LENGTH) {
|
||||
throw new IllegalStateException("Invalid bitmap length: " + bitmap.length);
|
||||
}
|
||||
}
|
||||
@Override
|
||||
public void read(CompoundNBT nbt) {
|
||||
this.bitmap = nbt.getByteArray("b");
|
||||
if (this.bitmap.length != BITMAP_LENGTH) {
|
||||
throw new IllegalStateException("Invalid bitmap length: " + bitmap.length);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public CompoundNBT write(CompoundNBT compound) {
|
||||
compound.putByteArray("b", bitmap);
|
||||
return compound;
|
||||
}
|
||||
@Override
|
||||
public CompoundNBT write(CompoundNBT compound) {
|
||||
compound.putByteArray("b", bitmap);
|
||||
return compound;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -18,26 +18,22 @@
|
||||
|
||||
package appeng.services.compass;
|
||||
|
||||
|
||||
import java.util.concurrent.ThreadFactory;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
|
||||
import com.google.common.base.Preconditions;
|
||||
|
||||
|
||||
/**
|
||||
* @author thatsIch
|
||||
* @version rv3 - 31.05.2015
|
||||
* @since rv3 31.05.2015
|
||||
*/
|
||||
public final class CompassThreadFactory implements ThreadFactory
|
||||
{
|
||||
@Override
|
||||
public Thread newThread( @Nonnull final Runnable job )
|
||||
{
|
||||
Preconditions.checkNotNull( job );
|
||||
public final class CompassThreadFactory implements ThreadFactory {
|
||||
@Override
|
||||
public Thread newThread(@Nonnull final Runnable job) {
|
||||
Preconditions.checkNotNull(job);
|
||||
|
||||
return new Thread( job, "AE Compass Service" );
|
||||
}
|
||||
return new Thread(job, "AE Compass Service");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,17 +18,15 @@
|
||||
|
||||
package appeng.services.compass;
|
||||
|
||||
public interface ICompassCallback {
|
||||
|
||||
public interface ICompassCallback
|
||||
{
|
||||
|
||||
/**
|
||||
* Called from another thread.
|
||||
*
|
||||
* @param hasResult true if found a target
|
||||
* @param spin true if should spin
|
||||
* @param radians radians
|
||||
* @param dist distance
|
||||
*/
|
||||
void calculatedDirection( boolean hasResult, boolean spin, double radians, double dist );
|
||||
/**
|
||||
* Called from another thread.
|
||||
*
|
||||
* @param hasResult true if found a target
|
||||
* @param spin true if should spin
|
||||
* @param radians radians
|
||||
* @param dist distance
|
||||
*/
|
||||
void calculatedDirection(boolean hasResult, boolean spin, double radians, double dist);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user