* OPT: The map database is now thread-safe.

This commit is contained in:
Prokopyl 2015-03-31 20:48:44 +02:00
parent b192214609
commit 860fed25e5
5 changed files with 44 additions and 45 deletions

View File

@ -86,31 +86,32 @@ public class ImageRendererExecutor extends Worker
static private ImageMap RenderSingle(final BufferedImage image, final UUID playerUUID) throws Throwable static private ImageMap RenderSingle(final BufferedImage image, final UUID playerUUID) throws Throwable
{ {
final short mapID = instance.submitToMainThread(new Callable<Short>() Future<Short> futureMapID = instance.submitToMainThread(new Callable<Short>()
{ {
@Override @Override
public Short call() throws Exception public Short call() throws Exception
{ {
return MapManager.getNewMapsIds(1)[0]; return MapManager.getNewMapsIds(1)[0];
} }
}).get(); });
final BufferedImage finalImage = ResizeImage(image, ImageMap.WIDTH, ImageMap.HEIGHT); final BufferedImage finalImage = ResizeImage(image, ImageMap.WIDTH, ImageMap.HEIGHT);
final short mapID = futureMapID.get();
ImageIOExecutor.saveImage(mapID, finalImage); ImageIOExecutor.saveImage(mapID, finalImage);
final ImageMap newMap = instance.submitToMainThread(new Callable<ImageMap>() instance.submitToMainThread(new Callable<Void>()
{ {
@Override @Override
public ImageMap call() throws Exception public Void call() throws Exception
{ {
Renderer.installRenderer(finalImage, mapID); Renderer.installRenderer(finalImage, mapID);
return MapManager.createMap(playerUUID, mapID); return null;
} }
}).get(); });
return newMap; return MapManager.createMap(playerUUID, mapID);
} }
static private ImageMap RenderPoster(final BufferedImage image, final UUID playerUUID) throws Throwable static private ImageMap RenderPoster(final BufferedImage image, final UUID playerUUID) throws Throwable
@ -133,18 +134,18 @@ public class ImageRendererExecutor extends Worker
ImageIOExecutor.saveImage(mapsIDs, poster); ImageIOExecutor.saveImage(mapsIDs, poster);
final ImageMap newMap = instance.submitToMainThread(new Callable<ImageMap>() instance.submitToMainThread(new Callable<Void>()
{ {
@Override @Override
public ImageMap call() throws Exception public Void call() throws Exception
{ {
Renderer.installRenderer(poster, mapsIDs); Renderer.installRenderer(poster, mapsIDs);
return MapManager.createMap(poster, playerUUID, mapsIDs); return null;
} }
}).get(); });
return newMap; return MapManager.createMap(poster, playerUUID, mapsIDs);
} }
static private BufferedImage ResizeImage(BufferedImage source, int destinationW, int destinationH) static private BufferedImage ResizeImage(BufferedImage source, int destinationW, int destinationH)

View File

@ -119,9 +119,9 @@ public abstract class ImageMap implements ConfigurationSerializable
public Map<String, Object> serialize() public Map<String, Object> serialize()
{ {
Map<String, Object> map = new HashMap<String, Object>(); Map<String, Object> map = new HashMap<String, Object>();
map.put("id", id); map.put("id", getId());
map.put("type", mapType.toString()); map.put("type", mapType.toString());
map.put("name", name); map.put("name", getName());
this.postSerialize(map); this.postSerialize(map);
return map; return map;
} }
@ -153,17 +153,17 @@ public abstract class ImageMap implements ConfigurationSerializable
return userUUID; return userUUID;
} }
public String getName() public synchronized String getName()
{ {
return name; return name;
} }
public String getId() public synchronized String getId()
{ {
return id; return id;
} }
public void rename(String id, String name) public synchronized void rename(String id, String name)
{ {
this.id = id; this.id = id;
this.name = name; this.name = name;
@ -171,8 +171,7 @@ public abstract class ImageMap implements ConfigurationSerializable
public void rename(String name) public void rename(String name)
{ {
if(this.name.equals(name)) return; if(getName().equals(name)) return;
this.id = MapManager.getNextAvailableMapID(name, userUUID); rename(MapManager.getNextAvailableMapID(name, getUserUUID()), name);
this.name = name;
} }
} }

View File

@ -45,7 +45,7 @@ public class PlayerMapStore implements ConfigurationSerializable
this.playerUUID = playerUUID; this.playerUUID = playerUUID;
} }
public boolean managesMap(short mapID) public synchronized boolean managesMap(short mapID)
{ {
for(ImageMap map : mapList) for(ImageMap map : mapList)
{ {
@ -54,7 +54,7 @@ public class PlayerMapStore implements ConfigurationSerializable
return false; return false;
} }
public boolean managesMap(ItemStack item) public synchronized boolean managesMap(ItemStack item)
{ {
for(ImageMap map : mapList) for(ImageMap map : mapList)
{ {
@ -63,19 +63,19 @@ public class PlayerMapStore implements ConfigurationSerializable
return false; return false;
} }
public void addMap(ImageMap map) public synchronized void addMap(ImageMap map)
{ {
mapList.add(map); mapList.add(map);
notifyModification(); notifyModification();
} }
public void deleteMap(ImageMap map) public synchronized void deleteMap(ImageMap map)
{ {
mapList.remove(map); mapList.remove(map);
notifyModification(); notifyModification();
} }
public boolean mapExists(String id) public synchronized boolean mapExists(String id)
{ {
for(ImageMap map : mapList) for(ImageMap map : mapList)
{ {
@ -98,12 +98,12 @@ public class PlayerMapStore implements ConfigurationSerializable
return mapId + "-" + id; return mapId + "-" + id;
} }
public List<ImageMap> getMapList() public synchronized List<ImageMap> getMapList()
{ {
return new ArrayList(mapList); return new ArrayList(mapList);
} }
public ImageMap getMap(String mapId) public synchronized ImageMap getMap(String mapId)
{ {
for(ImageMap map : mapList) for(ImageMap map : mapList)
{ {
@ -120,12 +120,12 @@ public class PlayerMapStore implements ConfigurationSerializable
return playerUUID; return playerUUID;
} }
public boolean isModified() public synchronized boolean isModified()
{ {
return modified; return modified;
} }
public void notifyModification() public synchronized void notifyModification()
{ {
this.modified = true; this.modified = true;
} }
@ -137,7 +137,7 @@ public class PlayerMapStore implements ConfigurationSerializable
{ {
Map<String, Object> map = new HashMap<String, Object>(); Map<String, Object> map = new HashMap<String, Object>();
ArrayList<Map> list = new ArrayList<Map>(); ArrayList<Map> list = new ArrayList<Map>();
synchronized(mapList) synchronized(this)
{ {
for(ImageMap tMap : mapList) for(ImageMap tMap : mapList)
{ {
@ -153,13 +153,13 @@ public class PlayerMapStore implements ConfigurationSerializable
if(section == null) return; if(section == null) return;
List<Map<String, Object>> list = (List<Map<String, Object>>) section.getList("mapList"); List<Map<String, Object>> list = (List<Map<String, Object>>) section.getList("mapList");
if(list == null) return; if(list == null) return;
synchronized(mapList)
{
for(Map<String, Object> tMap : list) for(Map<String, Object> tMap : list)
{ {
try try
{ {
mapList.add(ImageMap.fromConfig(tMap, playerUUID)); ImageMap newMap = ImageMap.fromConfig(tMap, playerUUID);
synchronized(this) {mapList.add(newMap);}
} }
catch(InvalidConfigurationException ex) catch(InvalidConfigurationException ex)
{ {
@ -167,7 +167,6 @@ public class PlayerMapStore implements ConfigurationSerializable
} }
} }
} }
}
/* ****** Configuration Files management ***** */ /* ****** Configuration Files management ***** */
@ -205,6 +204,6 @@ public class PlayerMapStore implements ConfigurationSerializable
PluginLogger.LogError("Could not save maps file for player " + playerUUID.toString(), ex); PluginLogger.LogError("Could not save maps file for player " + playerUUID.toString(), ex);
} }
PluginLogger.LogInfo("Saving maps file for " + playerUUID.toString()); PluginLogger.LogInfo("Saving maps file for " + playerUUID.toString());
modified = false; synchronized(this) {modified = false;}
} }
} }

View File

@ -25,9 +25,9 @@ import org.bukkit.configuration.InvalidConfigurationException;
public class PosterMap extends ImageMap public class PosterMap extends ImageMap
{ {
protected short[] mapsIDs; protected final short[] mapsIDs;
protected int columnCount; protected final int columnCount;
protected int rowCount; protected final int rowCount;
public PosterMap(UUID userUUID, short[] mapsIDs, int columnCount, int rowCount) public PosterMap(UUID userUUID, short[] mapsIDs, int columnCount, int rowCount)
{ {

View File

@ -24,7 +24,7 @@ import org.bukkit.configuration.InvalidConfigurationException;
public class SingleMap extends ImageMap public class SingleMap extends ImageMap
{ {
protected short mapID; protected final short mapID;
public SingleMap(UUID ownerUUID, short mapID) public SingleMap(UUID ownerUUID, short mapID)
{ {