Relocate Source to proper directory.
This commit is contained in:
@@ -0,0 +1,304 @@
|
||||
package appeng.services;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.HashMap;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.Future;
|
||||
import java.util.concurrent.ThreadFactory;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import net.minecraft.block.Block;
|
||||
import net.minecraft.world.World;
|
||||
import net.minecraft.world.chunk.Chunk;
|
||||
import appeng.api.AEApi;
|
||||
import appeng.api.util.DimensionalCoord;
|
||||
import appeng.services.helpers.CompassReader;
|
||||
import appeng.services.helpers.ICompassCallback;
|
||||
|
||||
public class CompassService implements ThreadFactory
|
||||
{
|
||||
|
||||
int jobSize = 0;
|
||||
|
||||
private class CMUpdatePost implements Runnable
|
||||
{
|
||||
|
||||
public final World world;
|
||||
|
||||
public final int chunkX, chunkZ;
|
||||
public final int doubleChunkY; // 32 blocks instead of 16.
|
||||
public final boolean value;
|
||||
|
||||
public CMUpdatePost(World w, int cx, int cz, int dcy, boolean val) {
|
||||
world = w;
|
||||
chunkX = cx;
|
||||
doubleChunkY = dcy;
|
||||
chunkZ = cz;
|
||||
value = val;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run()
|
||||
{
|
||||
jobSize--;
|
||||
|
||||
CompassReader cr = getReader( world );
|
||||
cr.setHasBeacon( chunkX, chunkZ, doubleChunkY, value );
|
||||
|
||||
if ( jobSize() < 2 )
|
||||
cleanUp();
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
private class CMDirectionRequest implements Runnable
|
||||
{
|
||||
|
||||
public final int maxRange;
|
||||
public final DimensionalCoord coord;
|
||||
public final ICompassCallback callback;
|
||||
|
||||
public CMDirectionRequest(DimensionalCoord coord, int getMaxRange, ICompassCallback cc) {
|
||||
this.coord = coord;
|
||||
this.maxRange = getMaxRange;
|
||||
callback = cc;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run()
|
||||
{
|
||||
jobSize--;
|
||||
|
||||
int cx = coord.x >> 4;
|
||||
int cz = coord.z >> 4;
|
||||
|
||||
CompassReader cr = getReader( coord.getWorld() );
|
||||
|
||||
// Am I standing on it?
|
||||
if ( cr.hasBeacon( cx, cz ) )
|
||||
{
|
||||
callback.calculatedDirection( true, true, -999, 0 );
|
||||
|
||||
if ( jobSize() < 2 )
|
||||
cleanUp();
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
// spiral outward...
|
||||
for (int offset = 1; offset < maxRange; offset++)
|
||||
{
|
||||
int minx = cx - offset;
|
||||
int minz = cz - offset;
|
||||
int maxx = cx + offset;
|
||||
int maxz = cz + offset;
|
||||
|
||||
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 ) )
|
||||
{
|
||||
int closeness = dist( cx, cz, minx, z );
|
||||
if ( closeness < closest )
|
||||
{
|
||||
closest = closeness;
|
||||
chosen_x = minx;
|
||||
chosen_z = z;
|
||||
}
|
||||
}
|
||||
|
||||
if ( cr.hasBeacon( maxx, z ) )
|
||||
{
|
||||
int closeness = 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 ) )
|
||||
{
|
||||
int closeness = dist( cx, cz, x, minz );
|
||||
if ( closeness < closest )
|
||||
{
|
||||
closest = closeness;
|
||||
chosen_x = x;
|
||||
chosen_z = minz;
|
||||
}
|
||||
}
|
||||
|
||||
if ( cr.hasBeacon( x, maxz ) )
|
||||
{
|
||||
int closeness = dist( cx, cz, x, maxz );
|
||||
if ( closeness < closest )
|
||||
{
|
||||
closest = closeness;
|
||||
chosen_x = x;
|
||||
chosen_z = maxz;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if ( closest < Integer.MAX_VALUE )
|
||||
{
|
||||
callback.calculatedDirection( true, false, rad( cx, cz, chosen_x, chosen_z ), dist( cx, cz, chosen_x, chosen_z ) );
|
||||
|
||||
if ( jobSize() < 2 )
|
||||
cleanUp();
|
||||
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// didn't find shit...
|
||||
callback.calculatedDirection( false, true, -999, 999 );
|
||||
|
||||
if ( jobSize() < 2 )
|
||||
cleanUp();
|
||||
}
|
||||
};
|
||||
|
||||
public Future<?> getCompassDirection(DimensionalCoord coord, int maxRange, ICompassCallback cc)
|
||||
{
|
||||
jobSize++;
|
||||
return executor.submit( new CMDirectionRequest( coord, maxRange, cc ) );
|
||||
}
|
||||
|
||||
public int jobSize()
|
||||
{
|
||||
return jobSize;
|
||||
}
|
||||
|
||||
public void cleanUp()
|
||||
{
|
||||
for (CompassReader cr : worldSet.values())
|
||||
cr.close();
|
||||
}
|
||||
|
||||
public void updateArea(World w, int chunkX, int chunkZ)
|
||||
{
|
||||
int x = chunkX << 4;
|
||||
int z = chunkZ << 4;
|
||||
|
||||
updateArea( w, x, 16, z );
|
||||
updateArea( w, x, 16 + 32, z );
|
||||
updateArea( w, x, 16 + 64, z );
|
||||
updateArea( w, x, 16 + 96, z );
|
||||
|
||||
updateArea( w, x, 16 + 128, z );
|
||||
updateArea( w, x, 16 + 160, z );
|
||||
updateArea( w, x, 16 + 192, z );
|
||||
updateArea( w, x, 16 + 224, z );
|
||||
}
|
||||
|
||||
public Future<?> updateArea(World w, int x, int y, int z)
|
||||
{
|
||||
jobSize++;
|
||||
|
||||
int cx = x >> 4;
|
||||
int cdy = y >> 5;
|
||||
int cz = z >> 4;
|
||||
|
||||
int low_y = cdy << 5;
|
||||
int hi_y = low_y + 32;
|
||||
|
||||
Block skystone = AEApi.instance().blocks().blockSkyStone.block();
|
||||
|
||||
// lower level...
|
||||
Chunk c = w.getChunkFromBlockCoords( x, z );
|
||||
|
||||
for (int i = 0; i < 16; i++)
|
||||
{
|
||||
for (int j = 0; j < 16; j++)
|
||||
{
|
||||
for (int k = low_y; k < hi_y; k++)
|
||||
{
|
||||
Block blk = c.getBlock( i, k, j );
|
||||
if ( blk == skystone && c.getBlockMetadata( i, k, j ) == 0 )
|
||||
{
|
||||
return executor.submit( new CMUpdatePost( w, cx, cz, cdy, true ) );
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return executor.submit( new CMUpdatePost( w, cx, cz, cdy, false ) );
|
||||
}
|
||||
|
||||
HashMap<World, CompassReader> worldSet = new HashMap();
|
||||
ExecutorService executor;
|
||||
|
||||
final File rootFolder;
|
||||
|
||||
public CompassService(File aEFolder) {
|
||||
rootFolder = aEFolder;
|
||||
executor = Executors.newSingleThreadExecutor( this );
|
||||
jobSize = 0;
|
||||
}
|
||||
|
||||
private CompassReader getReader(World w)
|
||||
{
|
||||
CompassReader cr = worldSet.get( w );
|
||||
|
||||
if ( cr == null )
|
||||
{
|
||||
cr = new CompassReader( w, rootFolder );
|
||||
worldSet.put( w, cr );
|
||||
}
|
||||
|
||||
return cr;
|
||||
}
|
||||
|
||||
private int dist(int ax, int az, int bx, int bz)
|
||||
{
|
||||
int up = (bz - az) * 16;
|
||||
int side = (bx - ax) * 16;
|
||||
|
||||
return up * up + side * side;
|
||||
}
|
||||
|
||||
private double rad(int ax, int az, int bx, int bz)
|
||||
{
|
||||
int up = bz - az;
|
||||
int side = bx - ax;
|
||||
|
||||
return Math.atan2( -up, side ) - Math.PI / 2.0;
|
||||
}
|
||||
|
||||
public void kill()
|
||||
{
|
||||
executor.shutdown();
|
||||
|
||||
try
|
||||
{
|
||||
executor.awaitTermination( 6, TimeUnit.MINUTES );
|
||||
jobSize = 0;
|
||||
|
||||
for (CompassReader cr : worldSet.values())
|
||||
{
|
||||
cr.close();
|
||||
}
|
||||
|
||||
worldSet.clear();
|
||||
}
|
||||
catch (InterruptedException e)
|
||||
{
|
||||
// wrap this up..
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Thread newThread(Runnable job)
|
||||
{
|
||||
return new Thread( job, "AE Compass Service" );
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,131 @@
|
||||
package appeng.services;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.InputStreamReader;
|
||||
import java.net.URL;
|
||||
import java.net.URLConnection;
|
||||
import java.util.Date;
|
||||
|
||||
import net.minecraft.nbt.NBTTagCompound;
|
||||
import appeng.core.AEConfig;
|
||||
import appeng.core.AELog;
|
||||
import appeng.core.AppEng;
|
||||
|
||||
import com.google.gson.JsonArray;
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonObject;
|
||||
import com.google.gson.JsonParser;
|
||||
|
||||
import cpw.mods.fml.common.event.FMLInterModComms;
|
||||
|
||||
public class VersionChecker implements Runnable
|
||||
{
|
||||
|
||||
public static VersionChecker instance = null;
|
||||
|
||||
private long delay = 0;
|
||||
private boolean VersionChecker = true;
|
||||
|
||||
public VersionChecker()
|
||||
{
|
||||
long now = (new Date()).getTime();
|
||||
delay = (1000 * 3600 * 5) - (now - AEConfig.instance.latestTimeStamp);
|
||||
if ( delay < 1 )
|
||||
delay = 1;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run()
|
||||
{
|
||||
try
|
||||
{
|
||||
sleep( delay );
|
||||
}
|
||||
catch (InterruptedException e)
|
||||
{
|
||||
// :(
|
||||
}
|
||||
|
||||
while (true)
|
||||
{
|
||||
Thread.yield();
|
||||
|
||||
try
|
||||
{
|
||||
String MCVersion = cpw.mods.fml.common.Loader.instance().getMCVersionString().replace( "Minecraft ", "" );
|
||||
URL url = new URL( "http://feeds.ae-mod.info/latest.json?VersionMC=" + MCVersion + "&Channel=" + AEConfig.CHANNEL + "&CurrentVersion="
|
||||
+ AEConfig.VERSION );
|
||||
|
||||
URLConnection yc = url.openConnection();
|
||||
yc.setRequestProperty( "User-Agent", "AE2/" + AEConfig.VERSION + " (Channel:" + AEConfig.CHANNEL + "," + MCVersion.replace( " ", ":" ) + ")" );
|
||||
BufferedReader in = new BufferedReader( new InputStreamReader( yc.getInputStream() ) );
|
||||
|
||||
String Version = "";
|
||||
String inputLine;
|
||||
|
||||
while ((inputLine = in.readLine()) != null)
|
||||
Version += inputLine;
|
||||
|
||||
in.close();
|
||||
|
||||
if ( Version.length() > 2 )
|
||||
{
|
||||
JsonElement element = (new JsonParser()).parse( Version );
|
||||
|
||||
int version = element.getAsJsonObject().get( "FormatVersion" ).getAsInt();
|
||||
if ( version == 1 )
|
||||
{
|
||||
JsonObject Meta = element.getAsJsonObject().get( "Meta" ).getAsJsonObject();
|
||||
JsonArray Versions = element.getAsJsonObject().get( "Versions" ).getAsJsonArray();
|
||||
if ( Versions.size() > 0 )
|
||||
{
|
||||
JsonObject Latest = Versions.get( 0 ).getAsJsonObject();
|
||||
|
||||
AEConfig.instance.latestVersion = Latest.get( "Version" ).getAsString();
|
||||
AEConfig.instance.latestTimeStamp = (new Date()).getTime();
|
||||
AEConfig.instance.save();
|
||||
|
||||
if ( VersionChecker && !AEConfig.VERSION.equals( AEConfig.instance.latestVersion ) )
|
||||
{
|
||||
NBTTagCompound versionInf = new NBTTagCompound();
|
||||
versionInf.setString( "modDisplayName", "Applied Energistics 2" );
|
||||
versionInf.setString( "oldVersion", AEConfig.VERSION );
|
||||
versionInf.setString( "newVersion", AEConfig.instance.latestVersion );
|
||||
versionInf.setString( "updateUrl", Latest.get( "UserBuild" ).getAsString() );
|
||||
versionInf.setBoolean( "isDirectLink", true );
|
||||
|
||||
JsonElement changeLog = Latest.get( "ChangeLog" );
|
||||
if ( changeLog == null )
|
||||
versionInf.setString( "changeLog", "For full change log please see: " + Meta.get( "DownloadLink" ).getAsString() );
|
||||
else
|
||||
versionInf.setString( "changeLog", changeLog.getAsString() );
|
||||
|
||||
versionInf.setString( "newFileName", "appliedenergistics2-" + AEConfig.instance.latestVersion + ".jar" );
|
||||
FMLInterModComms.sendRuntimeMessage( AppEng.instance, "VersionChecker", "addUpdate", versionInf );
|
||||
VersionChecker = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
sleep( 1000 * 3600 * 4 );
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
try
|
||||
{
|
||||
sleep( 1000 * 3600 * 4 );
|
||||
}
|
||||
catch (InterruptedException e1)
|
||||
{
|
||||
AELog.error( e );
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void sleep(long i) throws InterruptedException
|
||||
{
|
||||
Thread.sleep( i );
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package appeng.services.helpers;
|
||||
|
||||
public class CompassException extends RuntimeException
|
||||
{
|
||||
|
||||
private static final long serialVersionUID = 8825268683203860877L;
|
||||
|
||||
public final Throwable inner;
|
||||
|
||||
public CompassException(Throwable t) {
|
||||
inner = t;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
package appeng.services.helpers;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.HashMap;
|
||||
|
||||
import net.minecraft.world.World;
|
||||
|
||||
public class CompassReader
|
||||
{
|
||||
|
||||
HashMap<Long, CompassRegion> regions = new HashMap();
|
||||
final int id;
|
||||
final File rootFolder;
|
||||
|
||||
public void close()
|
||||
{
|
||||
for (CompassRegion r : regions.values())
|
||||
r.close();
|
||||
|
||||
regions.clear();
|
||||
}
|
||||
|
||||
public CompassReader(World w, File rootFolder) {
|
||||
id = w.provider.dimensionId;
|
||||
this.rootFolder = rootFolder;
|
||||
}
|
||||
|
||||
public void setHasBeacon(int cx, int cz, int cdy, boolean hasBeacon)
|
||||
{
|
||||
CompassRegion r = getRegion( cx, cz );
|
||||
r.setHasBeacon( cx, cz, cdy, hasBeacon );
|
||||
}
|
||||
|
||||
public boolean hasBeacon(int cx, int cz)
|
||||
{
|
||||
CompassRegion r = getRegion( cx, cz );
|
||||
return r.hasBeacon( cx, cz );
|
||||
}
|
||||
|
||||
private CompassRegion getRegion(int cx, int cz)
|
||||
{
|
||||
long pos = cx >> 10;
|
||||
pos = pos << 32;
|
||||
pos = pos | (cz >> 10);
|
||||
|
||||
CompassRegion cr = regions.get( pos );
|
||||
if ( cr == null )
|
||||
{
|
||||
cr = new CompassRegion( cx, cz, id, rootFolder );
|
||||
regions.put( pos, cr );
|
||||
}
|
||||
|
||||
return cr;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,172 @@
|
||||
package appeng.services.helpers;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.RandomAccessFile;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.channels.FileChannel;
|
||||
|
||||
import appeng.core.AELog;
|
||||
|
||||
public class CompassRegion
|
||||
{
|
||||
|
||||
final int low_x;
|
||||
final int low_z;
|
||||
|
||||
final int hi_x;
|
||||
final int hi_z;
|
||||
|
||||
final int world;
|
||||
|
||||
boolean hasFile = false;
|
||||
final File rootFolder;
|
||||
RandomAccessFile raf = null;
|
||||
ByteBuffer buffer;
|
||||
|
||||
public void close()
|
||||
{
|
||||
try
|
||||
{
|
||||
if ( hasFile )
|
||||
{
|
||||
buffer = null;
|
||||
raf.close();
|
||||
raf = null;
|
||||
hasFile = false;
|
||||
}
|
||||
}
|
||||
catch (Throwable t)
|
||||
{
|
||||
throw new CompassException( t );
|
||||
}
|
||||
}
|
||||
|
||||
public CompassRegion(int cx, int cz, int worldID, File rootFolder) {
|
||||
|
||||
world = worldID;
|
||||
this.rootFolder = rootFolder;
|
||||
|
||||
int region_x = cx >> 10;
|
||||
int region_z = cz >> 10;
|
||||
|
||||
low_x = region_x << 10;
|
||||
low_z = region_z << 10;
|
||||
|
||||
hi_x = low_x + 1024;
|
||||
hi_z = low_z + 1024;
|
||||
|
||||
openFile( false );
|
||||
}
|
||||
|
||||
public boolean hasBeacon(int cx, int cz)
|
||||
{
|
||||
if ( hasFile )
|
||||
{
|
||||
cx = cx & 0x3FF;
|
||||
cz = cz & 0x3FF;
|
||||
|
||||
int val = read( cx, cz );
|
||||
if ( val != 0 )
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public void setHasBeacon(int cx, int cz, int cdy, boolean hasBeacon)
|
||||
{
|
||||
cx &= 0x3FF;
|
||||
cz &= 0x3FF;
|
||||
|
||||
openFile( hasBeacon );
|
||||
|
||||
if ( hasFile )
|
||||
{
|
||||
int val = read( cx, cz );
|
||||
int originalVal = val;
|
||||
|
||||
if ( hasBeacon )
|
||||
val |= 1 << cdy;
|
||||
else
|
||||
val &= ~(1 << cdy);
|
||||
|
||||
if ( originalVal != val )
|
||||
write( cx, cz, val );
|
||||
}
|
||||
}
|
||||
|
||||
private void write(int cx, int cz, int val)
|
||||
{
|
||||
try
|
||||
{
|
||||
buffer.put( cx + cz * 0x400, (byte) val );
|
||||
// raf.seek( cx + cz * 0x400 );
|
||||
// raf.writeByte( val );
|
||||
}
|
||||
catch (Throwable t)
|
||||
{
|
||||
throw new CompassException( t );
|
||||
}
|
||||
}
|
||||
|
||||
private int read(int cx, int cz)
|
||||
{
|
||||
try
|
||||
{
|
||||
return buffer.get( cx + cz * 0x400 );
|
||||
// raf.seek( cx + cz * 0x400 );
|
||||
// return raf.readByte();
|
||||
}
|
||||
catch (IndexOutOfBoundsException outofBounds)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
catch (Throwable t)
|
||||
{
|
||||
throw new CompassException( t );
|
||||
}
|
||||
}
|
||||
|
||||
private void openFile(boolean create)
|
||||
{
|
||||
File fName = getFileName();
|
||||
if ( hasFile )
|
||||
return;
|
||||
|
||||
if ( create || fileExists( fName ) )
|
||||
{
|
||||
try
|
||||
{
|
||||
raf = new RandomAccessFile( fName, "rw" );
|
||||
FileChannel fc = raf.getChannel();
|
||||
buffer = fc.map( FileChannel.MapMode.READ_WRITE, 0, 0x400 * 0x400 );// fc.size() );
|
||||
hasFile = true;
|
||||
}
|
||||
catch (Throwable t)
|
||||
{
|
||||
throw new CompassException( t );
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private boolean fileExists(File name)
|
||||
{
|
||||
return name.exists() && name.isFile();
|
||||
}
|
||||
|
||||
private File getFileName()
|
||||
{
|
||||
String folder = rootFolder.getPath() + File.separatorChar + "compass";
|
||||
File folderFile = new File( folder );
|
||||
|
||||
if ( !folderFile.exists() || !folderFile.isDirectory() )
|
||||
{
|
||||
if ( !folderFile.mkdir() )
|
||||
AELog.info( "Failed to created AE2/compass/" );
|
||||
}
|
||||
|
||||
return new File( folder + File.separatorChar + world + "_" + low_x + "_" + low_z + ".dat" );
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package appeng.services.helpers;
|
||||
|
||||
public interface ICompassCallback
|
||||
{
|
||||
|
||||
/**
|
||||
* Called from another thread.
|
||||
*
|
||||
* @param hasResult
|
||||
* @param spin
|
||||
* @param radians
|
||||
*/
|
||||
public void calculatedDirection(boolean hasResult, boolean spin, double radians, double dist);
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user