2013-12-27 23:59:59 +01:00
|
|
|
package appeng.core;
|
|
|
|
|
|
|
|
import java.io.File;
|
2014-03-05 08:27:42 +01:00
|
|
|
import java.io.IOException;
|
2013-12-27 23:59:59 +01:00
|
|
|
import java.lang.ref.WeakReference;
|
2014-03-05 08:27:42 +01:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
2014-01-27 05:00:36 +01:00
|
|
|
import java.util.Map.Entry;
|
2013-12-27 23:59:59 +01:00
|
|
|
import java.util.WeakHashMap;
|
|
|
|
|
2014-03-05 08:27:42 +01:00
|
|
|
import net.minecraft.entity.player.EntityPlayerMP;
|
2013-12-27 23:59:59 +01:00
|
|
|
import net.minecraftforge.common.DimensionManager;
|
2014-02-09 02:34:52 +01:00
|
|
|
import net.minecraftforge.common.config.ConfigCategory;
|
2014-02-09 06:08:27 +01:00
|
|
|
import net.minecraftforge.common.config.Configuration;
|
2014-02-09 02:34:52 +01:00
|
|
|
import net.minecraftforge.common.config.Property;
|
2013-12-27 23:59:59 +01:00
|
|
|
import appeng.api.util.WorldCoord;
|
2014-03-05 08:27:42 +01:00
|
|
|
import appeng.core.sync.network.NetworkHandler;
|
|
|
|
import appeng.core.sync.packets.PacketNewStorageDimension;
|
2013-12-27 23:59:59 +01:00
|
|
|
import appeng.me.GridStorage;
|
|
|
|
import appeng.me.GridStorageSearch;
|
2014-03-02 09:35:11 +01:00
|
|
|
import appeng.services.CompassService;
|
2013-12-27 23:59:59 +01:00
|
|
|
|
|
|
|
public class WorldSettings extends Configuration
|
|
|
|
{
|
|
|
|
|
|
|
|
private static WorldSettings instance;
|
|
|
|
|
|
|
|
long lastGridStorage = 0;
|
2014-01-27 05:00:36 +01:00
|
|
|
int lastPlayer = 0;
|
2013-12-27 23:59:59 +01:00
|
|
|
|
2014-03-02 09:35:11 +01:00
|
|
|
private CompassService compass;
|
|
|
|
|
|
|
|
File AEFolder;
|
|
|
|
|
|
|
|
public WorldSettings(File aeFolder) {
|
|
|
|
super( new File( aeFolder.getPath() + File.separatorChar + "settings.cfg" ) );
|
|
|
|
AEFolder = aeFolder;
|
|
|
|
|
|
|
|
compass = new CompassService( AEFolder );
|
|
|
|
(new Thread( compass, "AE Compass Service" )).start();
|
|
|
|
|
2013-12-27 23:59:59 +01:00
|
|
|
try
|
|
|
|
{
|
|
|
|
lastGridStorage = Long.parseLong( get( "Counters", "lastGridStorage", 0 ).getString() );
|
2014-01-27 05:00:36 +01:00
|
|
|
lastPlayer = get( "Counters", "lastPlayer", 0 ).getInt();
|
2013-12-27 23:59:59 +01:00
|
|
|
}
|
|
|
|
catch (NumberFormatException err)
|
|
|
|
{
|
|
|
|
lastGridStorage = 0;
|
2014-01-27 05:00:36 +01:00
|
|
|
lastPlayer = 0;
|
2013-12-27 23:59:59 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-05 08:27:42 +01:00
|
|
|
List<Integer> storageCellDims = new ArrayList();
|
|
|
|
|
|
|
|
public void addStorageCellDim(int newDim)
|
|
|
|
{
|
|
|
|
storageCellDims.add( newDim );
|
|
|
|
DimensionManager.registerDimension( newDim, AEConfig.instance.storageProviderID );
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
NetworkHandler.instance.sendToAll( new PacketNewStorageDimension( newDim ) );
|
|
|
|
}
|
|
|
|
catch (IOException e)
|
|
|
|
{
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
|
|
|
|
String[] values = new String[storageCellDims.size()];
|
|
|
|
|
|
|
|
for (int x = 0; x < values.length; x++)
|
|
|
|
values[x] = "" + storageCellDims.get( x );
|
|
|
|
|
|
|
|
get( "DimensionManager", "StorageCells", new int[0] ).set( values );
|
|
|
|
save();
|
|
|
|
}
|
|
|
|
|
2014-03-02 09:35:11 +01:00
|
|
|
public CompassService getCompass()
|
|
|
|
{
|
|
|
|
return compass;
|
|
|
|
}
|
|
|
|
|
2013-12-27 23:59:59 +01:00
|
|
|
public static WorldSettings getInstance()
|
|
|
|
{
|
|
|
|
if ( instance == null )
|
|
|
|
{
|
2014-03-04 05:56:08 +01:00
|
|
|
File world = DimensionManager.getCurrentSaveRootDirectory();
|
|
|
|
|
2014-03-02 09:35:11 +01:00
|
|
|
File f = new File( world.getPath() + File.separatorChar + "AE2" );
|
|
|
|
|
|
|
|
if ( !f.exists() || !f.isDirectory() )
|
|
|
|
f.mkdir();
|
|
|
|
|
2013-12-27 23:59:59 +01:00
|
|
|
instance = new WorldSettings( f );
|
|
|
|
}
|
|
|
|
|
|
|
|
return instance;
|
|
|
|
}
|
|
|
|
|
2014-03-05 08:27:42 +01:00
|
|
|
public void sendToPlayer(EntityPlayerMP player)
|
2013-12-27 23:59:59 +01:00
|
|
|
{
|
2014-03-05 08:27:42 +01:00
|
|
|
for (int newDim : get( "DimensionManager", "StorageCells", new int[0] ).getIntList())
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
NetworkHandler.instance.sendTo( new PacketNewStorageDimension( newDim ), player );
|
|
|
|
}
|
|
|
|
catch (IOException e)
|
|
|
|
{
|
|
|
|
AELog.error( e );
|
|
|
|
}
|
|
|
|
}
|
2013-12-27 23:59:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public void init()
|
|
|
|
{
|
|
|
|
save();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void shutdown()
|
|
|
|
{
|
|
|
|
save();
|
2014-03-02 09:35:11 +01:00
|
|
|
compass.kill();
|
2013-12-27 23:59:59 +01:00
|
|
|
instance = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
private WeakHashMap<GridStorageSearch, WeakReference<GridStorageSearch>> loadedStorage = new WeakHashMap();
|
|
|
|
|
|
|
|
public WorldCoord getStoredSize(int dim)
|
|
|
|
{
|
|
|
|
int x = get( "StorageCell" + dim, "scaleX", 0 ).getInt();
|
|
|
|
int y = get( "StorageCell" + dim, "scaleY", 0 ).getInt();
|
|
|
|
int z = get( "StorageCell" + dim, "scaleZ", 0 ).getInt();
|
|
|
|
return new WorldCoord( x, y, z );
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setStoredSize(int dim, int targetX, int targetY, int targetZ)
|
|
|
|
{
|
|
|
|
get( "StorageCell" + dim, "scaleX", 0 ).set( targetX );
|
|
|
|
get( "StorageCell" + dim, "scaleY", 0 ).set( targetY );
|
|
|
|
get( "StorageCell" + dim, "scaleZ", 0 ).set( targetZ );
|
|
|
|
save();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* lazy loading, can load any id, even ones that don't exist anymore.
|
|
|
|
*
|
|
|
|
* @param storageID
|
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
public GridStorage getGridStorage(long storageID)
|
|
|
|
{
|
|
|
|
GridStorageSearch gss = new GridStorageSearch( storageID );
|
|
|
|
WeakReference<GridStorageSearch> result = loadedStorage.get( gss );
|
|
|
|
|
|
|
|
if ( result == null || result.get() == null )
|
|
|
|
{
|
|
|
|
String Data = get( "gridstorage", "" + storageID, "" ).getString();
|
|
|
|
GridStorage thisStorage = new GridStorage( Data, storageID, gss );
|
|
|
|
gss.gridStorage = new WeakReference<GridStorage>( thisStorage );
|
|
|
|
loadedStorage.put( gss, new WeakReference<GridStorageSearch>( gss ) );
|
|
|
|
return thisStorage;
|
|
|
|
}
|
|
|
|
return result.get().gridStorage.get();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* create a new storage
|
|
|
|
*/
|
|
|
|
public GridStorage getNewGridStorage()
|
|
|
|
{
|
|
|
|
long storageID = nextGridStorage();
|
|
|
|
GridStorageSearch gss = new GridStorageSearch( storageID );
|
|
|
|
GridStorage newStorage = new GridStorage( storageID, gss );
|
|
|
|
gss.gridStorage = new WeakReference<GridStorage>( newStorage );
|
|
|
|
loadedStorage.put( gss, new WeakReference<GridStorageSearch>( gss ) );
|
|
|
|
return newStorage;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void destroyGridStorage(long id)
|
|
|
|
{
|
|
|
|
this.getCategory( "gridstorage" ).remove( "" + id );
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void save()
|
|
|
|
{
|
|
|
|
// populate new data
|
|
|
|
for (GridStorageSearch gs : loadedStorage.keySet())
|
|
|
|
{
|
|
|
|
GridStorage thisStorage = gs.gridStorage.get();
|
|
|
|
if ( thisStorage != null && thisStorage.getGrid() != null && !thisStorage.getGrid().isEmpty() )
|
|
|
|
{
|
|
|
|
String value = thisStorage.getValue();
|
|
|
|
get( "gridstorage", "" + thisStorage.getID(), value ).set( value );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// save to files
|
|
|
|
if ( hasChanged() )
|
|
|
|
super.save();
|
|
|
|
}
|
|
|
|
|
|
|
|
private long nextGridStorage()
|
|
|
|
{
|
|
|
|
long r = lastGridStorage++;
|
|
|
|
get( "Counters", "lastGridStorage", lastGridStorage ).set( Long.toString( lastGridStorage ) );
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2014-01-27 05:00:36 +01:00
|
|
|
private long nextPlayer()
|
|
|
|
{
|
|
|
|
long r = lastPlayer++;
|
|
|
|
get( "Counters", "lastPlayer", lastPlayer ).set( lastPlayer );
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getUsername(int id)
|
|
|
|
{
|
|
|
|
ConfigCategory playerList = this.getCategory( "players" );
|
|
|
|
for (Entry<String, Property> fish : playerList.entrySet())
|
|
|
|
{
|
|
|
|
if ( fish.getValue().isIntValue() && fish.getValue().getInt() == id )
|
|
|
|
return fish.getKey();
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
public int getPlayerID(String username)
|
|
|
|
{
|
|
|
|
ConfigCategory playerList = this.getCategory( "players" );
|
2014-02-02 08:32:10 +01:00
|
|
|
if ( playerList == null || username == null || username.length() == 0 )
|
2014-01-27 05:00:36 +01:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
Property prop = playerList.get( username );
|
|
|
|
if ( prop != null && prop.isIntValue() )
|
|
|
|
return prop.getInt();
|
|
|
|
else
|
|
|
|
{
|
|
|
|
playerList.put( username, prop = new Property( username, "" + nextPlayer(), Property.Type.INTEGER ) );
|
|
|
|
save();
|
|
|
|
return prop.getInt();
|
|
|
|
}
|
|
|
|
}
|
2014-03-02 09:35:11 +01:00
|
|
|
|
2013-12-27 23:59:59 +01:00
|
|
|
}
|