Skip to content

Commit

Permalink
Added island tracking via yml database instead of filesystem.
Browse files Browse the repository at this point in the history
  • Loading branch information
tastybento committed Jan 27, 2015
1 parent 9c32280 commit 0ef0121
Show file tree
Hide file tree
Showing 8 changed files with 523 additions and 38 deletions.
2 changes: 1 addition & 1 deletion plugin.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: ASkyBlock
main: com.wasteofplastic.askyblock.ASkyBlock
version: 2.8
version: 2.8pre
description: A SkyBlock Plugin
author: Tastybento
depend: [Vault]
Expand Down
43 changes: 24 additions & 19 deletions src/com/wasteofplastic/askyblock/ASkyBlock.java
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ public class ASkyBlock extends JavaPlugin {
HashSet<UUID> fallingPlayers = new HashSet<UUID>();
// Biome chooser object
Biomes biomes;
// Island grid manager
private GridManager grid;

private boolean debug = false;
//public boolean flag = false;
Expand Down Expand Up @@ -509,7 +511,7 @@ protected boolean homeTeleport(final Player player) {
//home.getWorld().refreshChunk(home.getChunk().getX(), home.getChunk().getZ());
// Removing this line because it appears to cause artifacts of hovering blocks
//home.getWorld().loadChunk(home.getChunk());
getLogger().info("DEBUG: " + home.toString());
//getLogger().info("DEBUG: " + home.toString());
// This next line should help players with long ping times
// http://bukkit.org/threads/workaround-for-playing-falling-after-teleport-when-lagging.293035/
//getLogger().info("DEBUG: home = " + home.toString());
Expand Down Expand Up @@ -538,27 +540,20 @@ protected boolean islandAtLocation(final Location loc) {
return true;
}
//getLogger().info("DEBUG checking islandAtLocation for location " + loc.toString());
// Immediate check
if (loc.getBlock().getType().equals(Material.BEDROCK)) {
//getLogger().info("DEBUG found bedrock at island height");
// Check the island grid
if (grid.getIslandAt(loc) != null) {
//getLogger().info("DEBUG: Island at " + loc.toString());
return true;
}
// Near spawn?
if (spawn.isAtSpawn(loc)) {
//getLogger().info("DEBUG: too close to spawn");
return true;
}
/*
Vector v = loc.toVector();
v.multiply(new Vector(1,0,1));
if ((getSpawn().getBedrock() != null && v.distanceSquared(getSpawn().getBedrock().toVector().multiply(new Vector(1,0,1))) < (double)((double)spawn.getRange()) * spawn.getRange())) {
//plugin.getLogger().info("Too near spawn");
return true;
}*/
// Check the file system
String checkName = loc.getBlockX() + "," + loc.getBlockZ() + ".yml";
final File islandFile = new File(plugin.getDataFolder() + File.separator + "islands" + File.separator + checkName);
if (islandFile.exists()) {
//plugin.getLogger().info("File exists");
// Bedrock check
if (loc.getBlock().getType().equals(Material.BEDROCK)) {
getLogger().info("Found bedrock at island height - adding to islands.yml " + loc.getBlockX() + "," + loc.getBlockZ());
grid.addIsland(loc.getBlockX(), loc.getBlockZ());
return true;
}
// Look around
Expand All @@ -568,7 +563,8 @@ protected boolean islandAtLocation(final Location loc) {
for (int y = 10; y <= 255; y++) {
for (int z = -5; z <= 5; z++) {
if (loc.getWorld().getBlockAt(x + px, y, z + pz).getType().equals(Material.BEDROCK)) {
//plugin.getLogger().info("Bedrock found during long search");
plugin.getLogger().info("Bedrock found during long search - adding to islands.yml");
grid.addIsland(loc.getBlockX(), loc.getBlockZ());
return true;
}
}
Expand Down Expand Up @@ -1580,6 +1576,7 @@ public void onDisable() {
// Remove players from memory
players.removeAllPlayers();
//saveConfig();
grid.saveGrid();
saveWarpList();
saveMessages();
} catch (final Exception e) {
Expand Down Expand Up @@ -1672,6 +1669,7 @@ public void run() {
public void run() {
// load the list
loadTopTen();
grid = new GridManager(plugin);
}
});
// This part will kill monsters if they fall into the water because it
Expand Down Expand Up @@ -2023,7 +2021,7 @@ protected void loadTopTen() {
* @param level
*/
protected void updateTopTen(UUID ownerUUID, int level) {
getLogger().info("DEBUG: updating TopTen");
//getLogger().info("DEBUG: updating TopTen");
topTenList.put(ownerUUID, level);
topTenList = MapUtil.sortByValue(topTenList);
}
Expand Down Expand Up @@ -2397,7 +2395,7 @@ protected void resetPlayer(Player player) {
// Reset the island level
players.setIslandLevel(player.getUniqueId(), 0);
players.save(player.getUniqueId());
createTopTen();
updateTopTen(player.getUniqueId(),0);
// Update the inventory
player.updateInventory();
/*
Expand Down Expand Up @@ -2691,4 +2689,11 @@ public void setCalculatingLevel(boolean calculatingLevel) {
this.calculatingLevel = calculatingLevel;
}

/**
* @return the grid
*/
public GridManager getGrid() {
return grid;
}

}
5 changes: 4 additions & 1 deletion src/com/wasteofplastic/askyblock/DeleteIslandChunk.java
Original file line number Diff line number Diff line change
Expand Up @@ -82,14 +82,17 @@ protected DeleteIslandChunk(ASkyBlock plugin, final Location loc) {
}
}
// Remove from file system
/*
String checkName = loc.getBlockX() + "," + loc.getBlockZ() + ".yml";
final File islandFile = new File(plugin.getDataFolder() + File.separator + "islands" + File.separator + checkName);
if (islandFile.exists()) {
//plugin.getLogger().info("File exists");
if (!islandFile.delete()) {
plugin.getLogger().severe("Could not delete island file " + checkName + "!");
}
}
}*/
// Remove from grid
plugin.getGrid().deleteIsland(loc.getBlockX(), loc.getBlockZ());

/*
plugin.getServer().getScheduler().runTask(plugin, new Runnable() {
Expand Down
213 changes: 213 additions & 0 deletions src/com/wasteofplastic/askyblock/GridManager.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,213 @@
/**
*
*/
package com.wasteofplastic.askyblock;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Map.Entry;
import java.util.TreeMap;
import java.util.UUID;

import org.bukkit.Location;
import org.bukkit.configuration.file.YamlConfiguration;

/**
* @author tastybento
* This class manages the island grid. It knows where every island is, and where new
* ones should go. It can handle any size of island or protection size
* The grid is stored in a YML file.
*/
public class GridManager {
private ASkyBlock plugin = ASkyBlock.getPlugin();
// 2D grid of islands, x,z
private TreeMap<Integer,TreeMap<Integer,Island>> grid = new TreeMap<Integer,TreeMap<Integer,Island>>();
private File islandFile;


/**
* @param plugin
*/
public GridManager(ASkyBlock plugin) {
this.plugin = plugin;
loadGrid();
}

protected void loadGrid() {
islandFile = new File(plugin.getDataFolder(),"islands.yml");
if (!islandFile.exists()) {
plugin.getLogger().info("islands.yml does not exist. Converting...");
convert();
} else {
plugin.getLogger().info("Loading islands.yml");
YamlConfiguration islandYaml = ASkyBlock.loadYamlFile("islands.yml");
List <String> islandList = new ArrayList<String>();
if (islandYaml.contains(Settings.worldName)) {
islandList = islandYaml.getStringList(Settings.worldName);
for (String island : islandList) {
addIsland(island);
}
} else {
plugin.getLogger().severe("Could not find any islands for this world...");
}

}
}

private void convert() {
final File islandFolder = new File(plugin.getDataFolder() + File.separator + "islands");
for (File f: islandFolder.listFiles()) {
// Need to remove the .yml suffix
String fileName = f.getName();
int comma = fileName.indexOf(",");
if (fileName.endsWith(".yml") && comma != -1) {
try {
// Parse to an island value
int x = Integer.parseInt(fileName.substring(0, comma));
int z = Integer.parseInt(fileName.substring(comma +1 , fileName.indexOf(".")));
// Note that this is the CENTER of the island
addIsland(x,z);
//plugin.getLogger().info("DEBUG: added island " + x + "," +z);
} catch (Exception e) {
e.printStackTrace();
}
}
}
// Now save the grid
saveGrid();

}

protected void saveGrid() {
final File islandFile = new File(plugin.getDataFolder(), "islands.yml");
YamlConfiguration islandYaml = new YamlConfiguration();
List <String> islandList = new ArrayList<String>();
for (int x : grid.keySet()) {
for (int z : grid.get(x).keySet()) {
Island island = grid.get(x).get(z);
islandList.add(island.serialize());
}
}
islandYaml.set(Settings.worldName, islandList);
// Save the file
try {
islandYaml.save(islandFile);
} catch (Exception e) {
plugin.getLogger().severe("Could not save islands.yml!");
e.printStackTrace();
}
}

/**
* Returns the island at the location or null if there is none
* @param location
* @return Island object
*/
protected Island getIslandAt(Location location) {
int x = location.getBlockX();
Entry<Integer, TreeMap<Integer,Island>> en = grid.lowerEntry(x);
if (en != null) {
int z = location.getBlockZ();
Entry<Integer, Island> ent = en.getValue().lowerEntry(z);
if (ent != null) {
return ent.getValue();
}
}
return null;
}

/**
* Returns the owner of the island at location
* @param location
* @return UUID of owner
*/
protected UUID getOwnerOfIslandAt(Location location) {
Island island = getIslandAt(location);
if (island != null) {
return island.getOwner();
}
return null;
}

// grid manipulation methods
/**
* Adds an island to the grid
* @param x
* @param z
* @return true if successful, false if the spot is already taken
*/
protected boolean addIsland(int x, int z) {
return addIsland(x, z, null);
}

/**
* Adds an island to the grid with owner UUID
* @param x
* @param z
* @param owner
* @return true if successful, false if the spot is already taken
*/
protected boolean addIsland(int x, int z, UUID owner) {
if (grid.containsKey(x)) {
TreeMap<Integer,Island> zEntry = grid.get(x);
if (zEntry.containsKey(z)) {
// Island already exists
return false;
} else {
// Add island
zEntry.put(z, new Island(x,z, owner));
grid.put(x, zEntry);
return true;
}
} else {
// Add island
TreeMap<Integer,Island> zEntry = new TreeMap<Integer,Island>();
zEntry.put(z,new Island(x,z, owner));
grid.put(x, zEntry);
return true;
}
}
protected boolean addIsland(String islandSerialized) {
Island newIsland = new Island(islandSerialized);
if (grid.containsKey(newIsland.getMinX())) {
TreeMap<Integer,Island> zEntry = grid.get(newIsland.getMinX());
if (zEntry.containsKey(newIsland.getMinZ())) {
// Island already exists
plugin.getLogger().warning("Duplicate island found in island.yml: " + newIsland.getMinX() + "," + newIsland.getMinZ());
return false;
} else {
// Add island
zEntry.put(newIsland.getMinZ(), newIsland);
grid.put(newIsland.getMinX(), zEntry);
//plugin.getLogger().info("Debug: " + newIsland.toString());
return true;
}
} else {
// Add island
TreeMap<Integer,Island> zEntry = new TreeMap<Integer,Island>();
zEntry.put(newIsland.getMinZ(), newIsland);
grid.put(newIsland.getMinX(), zEntry);
//plugin.getLogger().info("Debug: " + newIsland.toString());
return true;
}
}
/**
* Delete island from the grid
* @param x
* @param z
* @return true if successful, false if there was no island to delete
*/
protected boolean deleteIsland(int x, int z) {
if (grid.containsKey(x)) {
TreeMap<Integer,Island> zEntry = grid.get(x);
if (zEntry.containsKey(z)) {
// Island exists - delete it
zEntry.remove(z);
grid.put(x, zEntry);
return true;
}
}
return false;
}
}
Loading

0 comments on commit 0ef0121

Please sign in to comment.