Added comments to make things clearer

This commit is contained in:
tastybento 2017-06-10 11:54:32 -07:00
parent c44e432aa3
commit bcb13e2b6d
4 changed files with 44 additions and 30 deletions

View File

@ -36,11 +36,11 @@ public abstract class BSBDatabase {
} }
/** /**
* Gets a handler for this class type with this database connection * Gets a database handler that will store and retrieve classes of type dataObjectClass
* @param plugin * @param plugin
* @param type * @param dataObjectClass
* @return selector object * @return database handler
*/ */
public abstract AbstractDatabaseHandler<?> getHandler(BSkyBlock plugin, Class<?> type); public abstract AbstractDatabaseHandler<?> getHandler(BSkyBlock plugin, Class<?> dataObjectClass);
} }

View File

@ -8,7 +8,6 @@ import java.util.List;
import us.tastybento.bskyblock.BSkyBlock; import us.tastybento.bskyblock.BSkyBlock;
import us.tastybento.bskyblock.database.DatabaseConnecter; import us.tastybento.bskyblock.database.DatabaseConnecter;
import us.tastybento.bskyblock.database.objects.DataObject;
/** /**
* An abstract class that handles insert/select-operations into/from a database * An abstract class that handles insert/select-operations into/from a database

View File

@ -6,6 +6,7 @@ import java.util.Map.Entry;
import java.util.Set; import java.util.Set;
import java.util.TreeMap; import java.util.TreeMap;
import java.util.UUID; import java.util.UUID;
import java.util.WeakHashMap;
import org.bukkit.Location; import org.bukkit.Location;
@ -16,6 +17,8 @@ import us.tastybento.bskyblock.database.objects.Island;
/** /**
* The job of this class is manage all island related data. * The job of this class is manage all island related data.
* It also handles island ownership, including team, trustees, coops, etc.
* The data object that it uses is Island
* @author tastybento * @author tastybento
* *
*/ */
@ -24,8 +27,8 @@ public class IslandsManager {
private BSkyBlock plugin; private BSkyBlock plugin;
private BSBDatabase database; private BSBDatabase database;
private HashMap<Location, Island> islands; private WeakHashMap<Location, Island> islandsByLocation;
private HashMap<UUID, Island> islandsByUUID; private WeakHashMap<UUID, Island> islandsByUUID;
// 2D islandGrid of islands, x,z // 2D islandGrid of islands, x,z
private TreeMap<Integer, TreeMap<Integer, Island>> islandGrid = new TreeMap<Integer, TreeMap<Integer, Island>>(); private TreeMap<Integer, TreeMap<Integer, Island>> islandGrid = new TreeMap<Integer, TreeMap<Integer, Island>>();
@ -42,9 +45,10 @@ public class IslandsManager {
public IslandsManager(BSkyBlock plugin){ public IslandsManager(BSkyBlock plugin){
this.plugin = plugin; this.plugin = plugin;
database = BSBDatabase.getDatabase(); database = BSBDatabase.getDatabase();
// Set up the database handler to store and retrieve Island classes
handler = (AbstractDatabaseHandler<Island>) database.getHandler(plugin, Island.class); handler = (AbstractDatabaseHandler<Island>) database.getHandler(plugin, Island.class);
islands = new HashMap<Location, Island>(); islandsByLocation = new WeakHashMap<Location, Island>();
islandsByUUID = new HashMap<UUID, Island>(); islandsByUUID = new WeakHashMap<UUID, Island>();
spawn = null; spawn = null;
} }
@ -52,15 +56,13 @@ public class IslandsManager {
* Clear and reload all islands from database * Clear and reload all islands from database
*/ */
public void load(){ public void load(){
islands.clear(); islandsByLocation.clear();
islandsByUUID.clear(); islandsByUUID.clear();
spawn = null; spawn = null;
try { try {
for (Object island : handler.loadObjects()) { for (Island island : handler.loadObjects()) {
if (island instanceof Island) { islandsByLocation.put(island.getCenter(), island);
islands.put(((Island)island).getCenter(), (Island)island); islandsByUUID.put(island.getOwner(), island);
islandsByUUID.put(((Island)island).getOwner(), (Island)island);
}
} }
} catch (Exception e) { } catch (Exception e) {
// TODO Auto-generated catch block // TODO Auto-generated catch block
@ -74,7 +76,7 @@ public class IslandsManager {
@Override @Override
public void run() { public void run() {
for(Island island : islands.values()){ for(Island island : islandsByLocation.values()){
try { try {
handler.saveObject(island); handler.saveObject(island);
} catch (Exception e) { } catch (Exception e) {
@ -85,7 +87,7 @@ public class IslandsManager {
} }
}); });
} else { } else {
for(Island island : islands.values()){ for(Island island : islandsByLocation.values()){
try { try {
handler.saveObject(island); handler.saveObject(island);
} catch (Exception e) { } catch (Exception e) {
@ -98,19 +100,20 @@ public class IslandsManager {
public void shutdown(){ public void shutdown(){
save(false); save(false);
islands.clear(); islandsByLocation.clear();
islandsByUUID.clear();
} }
public int getCount(){ public int getCount(){
return islands.size(); return islandsByLocation.size();
} }
public boolean isIsland(Location location){ public boolean isIsland(Location location){
return islands.get(location) != null; return islandsByLocation.get(location) != null;
} }
public Island getIsland(Location location){ public Island getIsland(Location location){
return islands.get(location); return islandsByLocation.get(location);
} }
/** /**
@ -131,20 +134,26 @@ public class IslandsManager {
} }
/** /**
* Create an island with owner * Create an island with owner. Note this does not create the schematic. It just reates the island data object.
* @param location * @param location
* @param owner UUID * @param owner UUID
*/ */
public Island createIsland(Location location, UUID owner){ public Island createIsland(Location location, UUID owner){
Island island = new Island(location, owner, Settings.islandProtectionRange); Island island = new Island(location, owner, Settings.islandProtectionRange);
islands.put(location, island); islandsByLocation.put(location, island);
if (owner != null) if (owner != null)
islandsByUUID.put(owner, island); islandsByUUID.put(owner, island);
return island; return island;
} }
public void deleteIsland(Location location){ public void deleteIsland(Location location){
//TODO if (islandsByLocation.containsKey(location)) {
Island island = islandsByLocation.get(location);
if (island.getOwner() != null) {
islandsByUUID.remove(island.getOwner());
}
islandsByLocation.remove(location);
}
} }
public Island getSpawn(){ public Island getSpawn(){

View File

@ -546,6 +546,12 @@ public class Island extends DataObject {
return center.getBlockZ(); return center.getBlockZ();
} }
/**
* Checks if coords are in the island space
* @param x
* @param z
* @return true if in the island space
*/
public boolean inIslandSpace(int x, int z) { public boolean inIslandSpace(int x, int z) {
if (x >= center.getBlockX() - Settings.islandDistance / 2 && x < center.getBlockX() + Settings.islandDistance / 2 && z >= center.getBlockZ() - Settings.islandDistance / 2 if (x >= center.getBlockX() - Settings.islandDistance / 2 && x < center.getBlockX() + Settings.islandDistance / 2 && z >= center.getBlockZ() - Settings.islandDistance / 2
&& z < center.getBlockZ() + Settings.islandDistance / 2) { && z < center.getBlockZ() + Settings.islandDistance / 2) {