Progress towards #515
Fixes #512
Fixes #514
Fixes (possibly) #529
Fixes #535
Update to latest sponge
This commit is contained in:
boy0001
2015-08-11 20:45:13 +10:00
parent b77b550e56
commit 1a9ec84a4d
23 changed files with 391 additions and 167 deletions

View File

@ -526,6 +526,45 @@ public class PS {
public ArrayList<Plot> sortPlots(Collection<Plot> plots) {
return sortPlots(plots, SortType.DISTANCE_FROM_ORIGIN, null);
}
public ArrayList<Plot> sortPlotsByTemp(Collection<Plot> plots) {
int max = 0;
int overflowCount = 0;
for (Plot plot : plots) {
if (plot.temp > 0) {
if (plot.temp > max) {
max = plot.temp;
}
}
else {
overflowCount++;
}
}
Plot[] array = new Plot[max + 1];
List<Plot> overflow = new ArrayList<>(overflowCount);
for (Plot plot : plots) {
if (plot.temp <= 0) {
overflow.add(plot);
}
else {
array[plot.temp] = plot;
}
}
ArrayList<Plot> result = new ArrayList<>(plots.size());
for (Plot plot : array) {
if (plot != null) {
result.add(plot);
}
}
Collections.sort(overflow, new Comparator<Plot>() {
@Override
public int compare(Plot a, Plot b) {
return a.hashCode() - b.hashCode();
}
});
result.addAll(overflow);
return result;
}
/**
* Sort plots by hashcode
@ -834,7 +873,7 @@ public class PS {
}
}
public enum SortType { CREATION_DATE, DISTANCE_FROM_ORIGIN; }
public enum SortType { CREATION_DATE, CREATION_DATE_TIMESTAMP, DISTANCE_FROM_ORIGIN; }
/**
* Sort a collection of plots by world (with a priority world), then by hashcode
@ -887,6 +926,9 @@ public class PS {
for (String world : worlds) {
switch (type) {
case CREATION_DATE:
toReturn.addAll(sortPlotsByTemp(map.get(world)));
break;
case CREATION_DATE_TIMESTAMP:
toReturn.addAll(sortPlotsByTimestamp(map.get(world)));
break;
case DISTANCE_FROM_ORIGIN:
@ -1477,13 +1519,14 @@ public class PS {
public void disable() {
try {
TASK = null;
database = null;
// Validate that all data in the db is correct
DBFunc.validatePlots(getPlotsRaw());
// Close the connection
database.closeConnection();
DBFunc.close();
UUIDHandler.handleShutdown();
} catch (NullPointerException | SQLException e) {
} catch (NullPointerException e) {
log("&cCould not close database connection!");
}
}
@ -1630,7 +1673,7 @@ public class PS {
FlagManager.addFlag(new AbstractFlag("weather") {
public PlotWeather parseValueRaw(final String value) {
switch (value) {
switch (value.toLowerCase()) {
case "rain":
case "storm":
case "on":

View File

@ -51,8 +51,7 @@ public class Home extends SubCommand {
@Override
public boolean onCommand(final PlotPlayer plr, String[] args) {
final ArrayList<Plot> plots = PS.get().sortPlots(PS.get().getPlots(plr), SortType.CREATION_DATE, null);
final ArrayList<Plot> plots = PS.get().sortPlotsByTemp(PS.get().getPlots(plr));//PS.get().sortPlots(PS.get().getPlots(plr), SortType.CREATION_DATE, null);
if (plots.size() == 1) {
MainUtil.teleportPlayer(plr, plr.getLocation(), plots.get(0));
return true;

View File

@ -23,6 +23,7 @@ package com.intellectualcrafters.plot.commands;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map.Entry;
import java.util.UUID;
@ -147,14 +148,17 @@ public class Rate extends SubCommand {
}
};
if (plot.getSettings().ratings == null) {
TaskManager.runTaskAsync(new Runnable() {
@Override
public void run() {
plot.getSettings().ratings = DBFunc.getRatings(plot);
run.run();
}
});
return true;
if (!Settings.CACHE_RATINGS) {
TaskManager.runTaskAsync(new Runnable() {
@Override
public void run() {
plot.getSettings().ratings = DBFunc.getRatings(plot);
run.run();
}
});
return true;
}
plot.getSettings().ratings = new HashMap<UUID, Integer>();
}
run.run();
return true;
@ -196,14 +200,17 @@ public class Rate extends SubCommand {
}
};
if (plot.getSettings().ratings == null) {
TaskManager.runTaskAsync(new Runnable() {
@Override
public void run() {
plot.getSettings().ratings = DBFunc.getRatings(plot);
run.run();
}
});
return true;
if (!Settings.CACHE_RATINGS) {
TaskManager.runTaskAsync(new Runnable() {
@Override
public void run() {
plot.getSettings().ratings = DBFunc.getRatings(plot);
run.run();
}
});
return true;
}
plot.getSettings().ratings = new HashMap<UUID, Integer>();
}
run.run();
return true;

View File

@ -68,10 +68,10 @@ public class Visit extends SubCommand {
UUID user = UUIDHandler.getCachedUUID(args[0], null);
if (user != null ) {
// do plots by username
plots = PS.get().sortPlots(PS.get().getPlots(user), SortType.CREATION_DATE, null);
plots = PS.get().sortPlotsByTemp(PS.get().getPlots(user));
} else if (PS.get().isPlotWorld(args[0])) {
// do plots by world
plots = PS.get().sortPlots(PS.get().getPlotsInWorld(args[0]), SortType.CREATION_DATE, null);
plots = PS.get().sortPlots(PS.get().getPlotsInWorld(args[0]), SortType.DISTANCE_FROM_ORIGIN, null);
}
else {
Plot plot = MainUtil.getPlotFromString(plr, args[0], true);

View File

@ -138,7 +138,8 @@ public class list extends SubCommand {
MainUtil.sendMessage(plr, C.NO_PERMISSION, "plots.list.mine");
return false;
}
plots = new ArrayList<>(PS.get().getPlots(plr));
sort = false;
plots = PS.get().sortPlotsByTemp(PS.get().getPlots(plr));
break;
}
case "shared": {
@ -283,7 +284,8 @@ public class list extends SubCommand {
MainUtil.sendMessage(plr, C.NO_PERMISSION, "plots.list.player");
return false;
}
plots = new ArrayList<>(PS.get().getPlots(uuid));
sort = false;
plots = PS.get().sortPlotsByTemp(PS.get().getPlots(uuid));
break;
}
}

View File

@ -334,4 +334,6 @@ public interface AbstractDB {
* Don't fuck with this one, unless you enjoy it rough
*/
boolean deleteTables();
void close();
}

View File

@ -420,4 +420,8 @@ public class DBFunc {
public static void setPosition(final PlotCluster cluster, final String position) {
dbManager.setPosition(cluster, position);
}
public static void close() {
dbManager.close();
}
}

View File

@ -108,6 +108,25 @@ public class SQLManager implements AbstractDB {
tasks = new ConcurrentLinkedQueue<>();
plotTasks.put(plot, tasks);
}
if (task == null) {
task = new UniqueStatement(plot.hashCode() + "") {
@Override
public PreparedStatement get() throws SQLException {
return null;
}
@Override
public void set(PreparedStatement stmt) throws SQLException {}
@Override
public void addBatch(PreparedStatement stmt) throws SQLException {}
@Override
public void execute(PreparedStatement stmt) throws SQLException {}
};
}
tasks.add(task);
}
@ -140,28 +159,27 @@ public class SQLManager implements AbstractDB {
TaskManager.runTaskAsync(new Runnable() {
@Override
public void run() {
commit();
long last = System.currentTimeMillis();
while (true) {
if (PS.get().getDatabase().getConnection() == null) {
if (PS.get().getDatabase() == null) {
break;
}
// schedule reconnect
// schedule reconnect
if (Settings.DB.USE_MYSQL && System.currentTimeMillis() - last > 550000) {
last = System.currentTimeMillis();
commit();
try {
connection.close();
close();
connection = PS.get().getDatabase().forceConnection();
} catch (SQLException | ClassNotFoundException e) {
e.printStackTrace();
}
}
sendBatch();
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
if (!sendBatch()) {
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
@ -182,14 +200,10 @@ public class SQLManager implements AbstractDB {
public boolean sendBatch() {
try {
try {
if (globalTasks.size() > 0) {
if (connection.getAutoCommit()) {
connection.setAutoCommit(false);
}
} catch (SQLException e) {
e.printStackTrace();
}
if (globalTasks.size() > 0) {
Runnable task = globalTasks.remove();
if (task != null) {
task.run();
@ -197,8 +211,12 @@ public class SQLManager implements AbstractDB {
commit();
return true;
}
int count = 0;
int count = -1;
if (plotTasks.size() > 0) {
count = 0;
if (connection.getAutoCommit()) {
connection.setAutoCommit(false);
}
String method = null;
PreparedStatement stmt = null;
UniqueStatement task = null;
@ -232,6 +250,10 @@ public class SQLManager implements AbstractDB {
}
}
if (clusterTasks.size() > 0) {
count = 0;
if (connection.getAutoCommit()) {
connection.setAutoCommit(false);
}
String method = null;
PreparedStatement stmt = null;
UniqueStatement task = null;
@ -239,11 +261,11 @@ public class SQLManager implements AbstractDB {
ArrayList<PlotCluster> keys = new ArrayList<>(clusterTasks.keySet());
for (int i = 0; i < keys.size(); i++) {
PlotCluster plot = keys.get(i);
if (plotTasks.get(plot).size() == 0) {
plotTasks.remove(plot);
if (clusterTasks.get(plot).size() == 0) {
clusterTasks.remove(plot);
continue;
}
task = plotTasks.get(plot).remove();
task = clusterTasks.get(plot).remove();
count++;
if (task != null) {
if (task._method == null || !task._method.equals(method)) {
@ -264,18 +286,20 @@ public class SQLManager implements AbstractDB {
stmt.close();
}
}
commit();
if (count != 0) {
if (count > 0) {
commit();
return true;
}
else if (count != -1) {
if (!connection.getAutoCommit()) {
connection.setAutoCommit(true);
}
}
if (clusterTasks.size() > 0) {
clusterTasks.clear();
commit();
}
}
if (plotTasks.size() > 0) {
plotTasks.clear();
commit();
}
}
catch (Exception e) {
@ -355,31 +379,32 @@ public class SQLManager implements AbstractDB {
// Populating structures
final PreparedStatement stmt = connection.prepareStatement(GET_ALL_PLOTS);
final ResultSet result = stmt.executeQuery();
while (result.next()) {
final int id = result.getInt("id");
int x = result.getInt("plot_id_x");
int y = result.getInt("plot_id_z");
PlotId plotId = new PlotId(x, y);
Plot plot = plotMap.get(plotId);
if (plot != null) {
settings.add(new SettingsPair(id, plot.getSettings()));
if (plot.getDenied() != null) {
for (UUID uuid : plot.getDenied()) {
denied.add(new UUIDPair(id, uuid));
try (ResultSet result = stmt.executeQuery()) {
while (result.next()) {
final int id = result.getInt("id");
int x = result.getInt("plot_id_x");
int y = result.getInt("plot_id_z");
PlotId plotId = new PlotId(x, y);
Plot plot = plotMap.get(plotId);
if (plot != null) {
settings.add(new SettingsPair(id, plot.getSettings()));
if (plot.getDenied() != null) {
for (UUID uuid : plot.getDenied()) {
denied.add(new UUIDPair(id, uuid));
}
}
if (plot.getMembers() != null) {
for (UUID uuid : plot.getMembers()) {
trusted.add(new UUIDPair(id, uuid));
}
}
if (plot.getTrusted() != null) {
for (UUID uuid : plot.getTrusted()) {
helpers.add(new UUIDPair(id, uuid));
}
}
}
if (plot.getMembers() != null) {
for (UUID uuid : plot.getMembers()) {
trusted.add(new UUIDPair(id, uuid));
}
}
if (plot.getTrusted() != null) {
for (UUID uuid : plot.getTrusted()) {
helpers.add(new UUIDPair(id, uuid));
}
}
}
}
}
createSettings(settings, new Runnable() {
@Override
@ -843,10 +868,10 @@ public class SQLManager implements AbstractDB {
public void commit() {
try {
if (this.connection.getAutoCommit()) {
this.connection.setAutoCommit(false);
if (!this.connection.getAutoCommit()) {
this.connection.commit();
this.connection.setAutoCommit(true);
}
this.connection.commit();
} catch (SQLException e) {
e.printStackTrace();
}
@ -1065,7 +1090,6 @@ public class SQLManager implements AbstractDB {
}
PreparedStatement stmt = null;
try {
commit();
commit();
if (plot.temp > 0) {
return plot.temp;
@ -2326,7 +2350,7 @@ public class SQLManager implements AbstractDB {
@Override
public void run() {
try {
SQLManager.this.connection.close();
close();
SQLManager.this.connection = PS.get().getDatabase().forceConnection();
final Statement stmt = connection.createStatement();
stmt.addBatch("DROP TABLE `" + prefix + "cluster_invited`");
@ -2382,6 +2406,13 @@ public class SQLManager implements AbstractDB {
break;
}
}
try {
if (connection.getAutoCommit()) {
connection.setAutoCommit(false);
}
} catch (SQLException e) {
e.printStackTrace();
}
ConcurrentHashMap<String, ConcurrentHashMap<PlotId, Plot>> database = getPlots();
ArrayList<Plot> toCreate = new ArrayList<>();
for (Plot plot : PS.get().getPlotsRaw()) {
@ -2473,8 +2504,8 @@ public class SQLManager implements AbstractDB {
setFlags(plot, pf.values());
}
}
// TODO comments
// TODO ratings
// TODO comments (null)
// TODO ratings (null)
// TODO alias
// TODO unconnected entries from helpers, trusted, denied, comments, settings, rating
}
@ -2487,11 +2518,15 @@ public class SQLManager implements AbstractDB {
}
}
}
PS.debug("$4Done!");
commit();
}
@Override
public void close() {
try {
connection.commit();
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
catch (SQLException e) {}
}
}

View File

@ -45,7 +45,7 @@ public class Flag {
this.key = key;
this.value = key.parseValueRaw(value);
if (this.value == null) {
throw new IllegalArgumentException(key.getValueDesc());
throw new IllegalArgumentException(key.getValueDesc() + " (" + value + ")");
}
}

View File

@ -163,6 +163,7 @@ public class Plot {
}
}
this.timestamp = timestamp;
this.temp = temp;
}
/**

View File

@ -1362,14 +1362,14 @@ public class MainUtil {
Plot p2 = PS.get().getPlot(world, newPlot);
if (p1 == null || p1.owner == null) {
if (p2 != null && p2.owner != null) {
moveData(p2, p1, whenDone);
moveData(p2, getPlot(world, current), whenDone);
return true;
}
return false;
}
if (p2 == null || p2.owner == null) {
if (p1 != null && p1.owner != null) {
moveData(p1, p2, whenDone);
moveData(p1, getPlot(world, newPlot), whenDone);
return true;
}
return false;
@ -1395,21 +1395,24 @@ public class MainUtil {
public static boolean moveData(final Plot plot1, final Plot plot2, final Runnable whenDone) {
if (plot1.owner == null) {
TaskManager.runTaskLater(whenDone, 1);
PS.debug(plot2 +" is unowned (single)");
TaskManager.runTask(whenDone);
return false;
}
final Plot pos1 = getBottomPlot(plot1);
final Plot pos2 = getTopPlot(plot1);
final PlotId size = MainUtil.getSize(plot1);
if (!MainUtil.isUnowned(plot2.world, plot2.id, new PlotId((plot2.id.x + size.x) - 1, (plot2.id.y + size.y) - 1))) {
TaskManager.runTaskLater(whenDone, 1);
PS.debug(plot2 +" is unowned (multi)");
TaskManager.runTask(whenDone);
return false;
}
final int offset_x = plot2.id.x - pos1.id.x;
final int offset_y = plot2.id.y - pos1.id.y;
final ArrayList<PlotId> selection = getPlotSelectionIds(pos1.id, pos2.id);
for (final PlotId id : selection) {
DBFunc.movePlot(getPlot(plot1.world, new PlotId(id.x, id.y)), getPlot(plot2.world, new PlotId(id.x + offset_x, id.y + offset_y)));
String worldOriginal = plot1.world;
PlotId idOriginal = new PlotId(id.x, id.y);
final Plot plot = PS.get().getPlot(plot1.world, id);
Map<String, ConcurrentHashMap<PlotId, Plot>> raw = PS.get().getAllPlotsRaw();
raw.get(plot1.world).remove(id);
@ -1417,6 +1420,7 @@ public class MainUtil {
plot.id.y += offset_y;
plot.id.recalculateHash();
raw.get(plot2.world).put(plot.id, plot);
DBFunc.movePlot(getPlot(worldOriginal, idOriginal), getPlot(plot2.world, new PlotId(id.x + offset_x, id.y + offset_y)));
}
TaskManager.runTaskLater(whenDone, 1);
return true;
@ -1760,6 +1764,9 @@ public class MainUtil {
if (plot.getSettings().ratings != null) {
rating = plot.getSettings().ratings;
}
else if (Settings.CACHE_RATINGS) {
rating = new HashMap<>();
}
else {
rating = DBFunc.getRatings(plot);
}
@ -1790,6 +1797,9 @@ public class MainUtil {
if (plot.getSettings().ratings != null) {
rating = plot.getSettings().ratings;
}
else if (Settings.CACHE_RATINGS) {
rating = new HashMap<>();
}
else {
rating = DBFunc.getRatings(plot);
}