Cleanup of some database methods

Signed-off-by: matt <4009945+MattBDev@users.noreply.github.com>
This commit is contained in:
matt 2019-02-04 18:00:05 -05:00
parent dfbcecb081
commit e3d43509cf
4 changed files with 506 additions and 564 deletions

View File

@ -47,204 +47,200 @@ public class FileUUIDHandler extends UUIDHandlerImplementation {
} else { } else {
world = worlds.get(0).getName(); world = worlds.get(0).getName();
} }
TaskManager.runTaskAsync(new Runnable() { TaskManager.runTaskAsync(() -> {
@Override public void run() { PlotSquared.debug(C.PREFIX + "&6Starting player data caching for: " + world);
PlotSquared.debug(C.PREFIX + "&6Starting player data caching for: " + world); File uuidFile = new File(PlotSquared.get().IMP.getDirectory(), "uuids.txt");
File uuidFile = new File(PlotSquared.get().IMP.getDirectory(), "uuids.txt"); if (uuidFile.exists()) {
if (uuidFile.exists()) { try {
try { List<String> lines =
List<String> lines = Files.readAllLines(uuidFile.toPath(), StandardCharsets.UTF_8);
Files.readAllLines(uuidFile.toPath(), StandardCharsets.UTF_8); for (String line : lines) {
for (String line : lines) { try {
try { line = line.trim();
line = line.trim(); if (line.isEmpty()) {
if (line.isEmpty()) { continue;
continue;
}
line = line.replaceAll("[\\|][0-9]+[\\|][0-9]+[\\|]", "");
String[] split = line.split("\\|");
String name = split[0];
if (name.isEmpty() || (name.length() > 16) || !StringMan
.isAlphanumericUnd(name)) {
continue;
}
UUID uuid = FileUUIDHandler.this.uuidWrapper.getUUID(name);
if (uuid == null) {
continue;
}
UUIDHandler.add(new StringWrapper(name), uuid);
} catch (Exception e2) {
e2.printStackTrace();
} }
line = line.replaceAll("[\\|][0-9]+[\\|][0-9]+[\\|]", "");
String[] split = line.split("\\|");
String name = split[0];
if (name.isEmpty() || (name.length() > 16) || !StringMan
.isAlphanumericUnd(name)) {
continue;
}
UUID uuid = FileUUIDHandler.this.uuidWrapper.getUUID(name);
if (uuid == null) {
continue;
}
UUIDHandler.add(new StringWrapper(name), uuid);
} catch (Exception e2) {
e2.printStackTrace();
} }
} catch (IOException e) {
e.printStackTrace();
} }
} catch (IOException e) {
e.printStackTrace();
} }
HashBiMap<StringWrapper, UUID> toAdd = }
HashBiMap.create(new HashMap<StringWrapper, UUID>()); HashBiMap<StringWrapper, UUID> toAdd =
if (Settings.UUID.NATIVE_UUID_PROVIDER) { HashBiMap.create(new HashMap<StringWrapper, UUID>());
HashSet<UUID> all = UUIDHandler.getAllUUIDS(); if (Settings.UUID.NATIVE_UUID_PROVIDER) {
PlotSquared.debug("&aFast mode UUID caching enabled!"); HashSet<UUID> all = UUIDHandler.getAllUUIDS();
File playerDataFolder = PlotSquared.debug("&aFast mode UUID caching enabled!");
new File(container, world + File.separator + "playerdata"); File playerDataFolder =
String[] dat = playerDataFolder.list(new DatFileFilter()); new File(container, world + File.separator + "playerdata");
boolean check = all.isEmpty(); String[] dat = playerDataFolder.list(new DatFileFilter());
if (dat != null) { boolean check = all.isEmpty();
for (String current : dat) { if (dat != null) {
String s = current.replaceAll(".dat$", ""); for (String current : dat) {
try { String s = current.replaceAll(".dat$", "");
UUID uuid = UUID.fromString(s); try {
if (check || all.remove(uuid)) { UUID uuid = UUID.fromString(s);
File file = new File(playerDataFolder, current); if (check || all.remove(uuid)) {
NbtFactory.NbtCompound compound = NbtFactory File file = new File(playerDataFolder, current);
.fromStream(new FileInputStream(file), NbtFactory.NbtCompound compound = NbtFactory
NbtFactory.StreamOptions.GZIP_COMPRESSION); .fromStream(new FileInputStream(file),
if (!compound.containsKey("bukkit")) { NbtFactory.StreamOptions.GZIP_COMPRESSION);
PlotSquared.debug("ERROR: Player data (" + uuid.toString() if (!compound.containsKey("bukkit")) {
+ ".dat) does not contain the the key \"bukkit\""); PlotSquared.debug("ERROR: Player data (" + uuid.toString()
} else { + ".dat) does not contain the the key \"bukkit\"");
NbtFactory.NbtCompound bukkit = } else {
(NbtFactory.NbtCompound) compound.get("bukkit"); NbtFactory.NbtCompound bukkit =
String name = (String) bukkit.get("lastKnownName"); (NbtFactory.NbtCompound) compound.get("bukkit");
long last = (long) bukkit.get("lastPlayed"); String name = (String) bukkit.get("lastKnownName");
long first = (long) bukkit.get("firstPlayed"); long last = (long) bukkit.get("lastPlayed");
if (ExpireManager.IMP != null) { long first = (long) bukkit.get("firstPlayed");
ExpireManager.IMP.storeDate(uuid, last); if (ExpireManager.IMP != null) {
ExpireManager.IMP.storeAccountAge(uuid, last - first); ExpireManager.IMP.storeDate(uuid, last);
} ExpireManager.IMP.storeAccountAge(uuid, last - first);
toAdd.put(new StringWrapper(name), uuid);
} }
} toAdd.put(new StringWrapper(name), uuid);
} catch (Exception e) {
e.printStackTrace();
PlotSquared.debug(C.PREFIX + "Invalid playerdata: " + current);
}
}
}
add(toAdd);
if (all.isEmpty()) {
if (whenDone != null) {
whenDone.run();
}
return;
} else {
PlotSquared.debug("Failed to cache: " + all.size()
+ " uuids - slowly processing all files");
}
}
HashSet<String> worlds = Sets.newHashSet(world, "world");
HashSet<UUID> uuids = new HashSet<>();
HashSet<String> names = new HashSet<>();
File playerDataFolder = null;
for (String worldName : worlds) {
// Getting UUIDs
playerDataFolder =
new File(container, worldName + File.separator + "playerdata");
String[] dat = playerDataFolder.list(new DatFileFilter());
if ((dat != null) && (dat.length != 0)) {
for (String current : dat) {
String s = current.replaceAll(".dat$", "");
try {
UUID uuid = UUID.fromString(s);
uuids.add(uuid);
} catch (Exception ignored) {
PlotSquared.debug(C.PREFIX + "Invalid PlayerData: " + current);
}
}
break;
}
// Getting names
File playersFolder = new File(worldName + File.separator + "players");
dat = playersFolder.list(new DatFileFilter());
if ((dat != null) && (dat.length != 0)) {
for (String current : dat) {
names.add(current.replaceAll(".dat$", ""));
}
break;
}
}
for (UUID uuid : uuids) {
try {
File file =
new File(playerDataFolder + File.separator + uuid.toString() + ".dat");
if (!file.exists()) {
continue;
}
NbtFactory.NbtCompound compound = NbtFactory
.fromStream(new FileInputStream(file),
NbtFactory.StreamOptions.GZIP_COMPRESSION);
if (!compound.containsKey("bukkit")) {
PlotSquared.debug("ERROR: Player data (" + uuid.toString()
+ ".dat) does not contain the the key \"bukkit\"");
} else {
NbtFactory.NbtCompound bukkit =
(NbtFactory.NbtCompound) compound.get("bukkit");
String name = (String) bukkit.get("lastKnownName");
StringWrapper wrap = new StringWrapper(name);
if (!toAdd.containsKey(wrap)) {
long last = (long) bukkit.get("lastPlayed");
long first = (long) bukkit.get("firstPlayed");
if (Settings.UUID.OFFLINE) {
if (Settings.UUID.FORCE_LOWERCASE && !name.toLowerCase()
.equals(name)) {
uuid = FileUUIDHandler.this.uuidWrapper.getUUID(name);
} else {
long most = (long) compound.get("UUIDMost");
long least = (long) compound.get("UUIDLeast");
uuid = new UUID(most, least);
}
}
if (ExpireManager.IMP != null) {
ExpireManager.IMP.storeDate(uuid, last);
ExpireManager.IMP.storeAccountAge(uuid, last - first);
}
toAdd.put(wrap, uuid);
}
}
} catch (Exception ignored) {
PlotSquared
.debug(C.PREFIX + "&6Invalid PlayerData: " + uuid.toString() + ".dat");
}
}
for (String name : names) {
UUID uuid = FileUUIDHandler.this.uuidWrapper.getUUID(name);
StringWrapper nameWrap = new StringWrapper(name);
toAdd.put(nameWrap, uuid);
}
if (getUUIDMap().isEmpty()) {
for (OfflinePlotPlayer op : FileUUIDHandler.this.uuidWrapper
.getOfflinePlayers()) {
long last = op.getLastPlayed();
if (last != 0) {
String name = op.getName();
StringWrapper wrap = new StringWrapper(name);
if (!toAdd.containsKey(wrap)) {
UUID uuid = FileUUIDHandler.this.uuidWrapper.getUUID(op);
toAdd.put(wrap, uuid);
if (ExpireManager.IMP != null) {
ExpireManager.IMP.storeDate(uuid, last);
} }
} }
} catch (Exception e) {
e.printStackTrace();
PlotSquared.debug(C.PREFIX + "Invalid playerdata: " + current);
} }
} }
} }
add(toAdd); add(toAdd);
if (whenDone != null) { if (all.isEmpty()) {
whenDone.run(); if (whenDone != null) {
whenDone.run();
}
return;
} else {
PlotSquared.debug("Failed to cache: " + all.size()
+ " uuids - slowly processing all files");
} }
} }
HashSet<String> worlds1 = Sets.newHashSet(world, "world");
HashSet<UUID> uuids = new HashSet<>();
HashSet<String> names = new HashSet<>();
File playerDataFolder = null;
for (String worldName : worlds1) {
// Getting UUIDs
playerDataFolder =
new File(container, worldName + File.separator + "playerdata");
String[] dat = playerDataFolder.list(new DatFileFilter());
if ((dat != null) && (dat.length != 0)) {
for (String current : dat) {
String s = current.replaceAll(".dat$", "");
try {
UUID uuid = UUID.fromString(s);
uuids.add(uuid);
} catch (Exception ignored) {
PlotSquared.debug(C.PREFIX + "Invalid PlayerData: " + current);
}
}
break;
}
// Getting names
File playersFolder = new File(worldName + File.separator + "players");
dat = playersFolder.list(new DatFileFilter());
if ((dat != null) && (dat.length != 0)) {
for (String current : dat) {
names.add(current.replaceAll(".dat$", ""));
}
break;
}
}
for (UUID uuid : uuids) {
try {
File file =
new File(playerDataFolder + File.separator + uuid.toString() + ".dat");
if (!file.exists()) {
continue;
}
NbtFactory.NbtCompound compound = NbtFactory
.fromStream(new FileInputStream(file),
NbtFactory.StreamOptions.GZIP_COMPRESSION);
if (!compound.containsKey("bukkit")) {
PlotSquared.debug("ERROR: Player data (" + uuid.toString()
+ ".dat) does not contain the the key \"bukkit\"");
} else {
NbtFactory.NbtCompound bukkit =
(NbtFactory.NbtCompound) compound.get("bukkit");
String name = (String) bukkit.get("lastKnownName");
StringWrapper wrap = new StringWrapper(name);
if (!toAdd.containsKey(wrap)) {
long last = (long) bukkit.get("lastPlayed");
long first = (long) bukkit.get("firstPlayed");
if (Settings.UUID.OFFLINE) {
if (Settings.UUID.FORCE_LOWERCASE && !name.toLowerCase()
.equals(name)) {
uuid = FileUUIDHandler.this.uuidWrapper.getUUID(name);
} else {
long most = (long) compound.get("UUIDMost");
long least = (long) compound.get("UUIDLeast");
uuid = new UUID(most, least);
}
}
if (ExpireManager.IMP != null) {
ExpireManager.IMP.storeDate(uuid, last);
ExpireManager.IMP.storeAccountAge(uuid, last - first);
}
toAdd.put(wrap, uuid);
}
}
} catch (Exception ignored) {
PlotSquared
.debug(C.PREFIX + "&6Invalid PlayerData: " + uuid.toString() + ".dat");
}
}
for (String name : names) {
UUID uuid = FileUUIDHandler.this.uuidWrapper.getUUID(name);
StringWrapper nameWrap = new StringWrapper(name);
toAdd.put(nameWrap, uuid);
}
if (getUUIDMap().isEmpty()) {
for (OfflinePlotPlayer op : FileUUIDHandler.this.uuidWrapper
.getOfflinePlayers()) {
long last = op.getLastPlayed();
if (last != 0) {
String name = op.getName();
StringWrapper wrap = new StringWrapper(name);
if (!toAdd.containsKey(wrap)) {
UUID uuid = FileUUIDHandler.this.uuidWrapper.getUUID(op);
toAdd.put(wrap, uuid);
if (ExpireManager.IMP != null) {
ExpireManager.IMP.storeDate(uuid, last);
}
}
}
}
}
add(toAdd);
if (whenDone != null) {
whenDone.run();
}
}); });
return true; return true;
} }
@Override public void fetchUUID(final String name, final RunnableVal<UUID> ifFetch) { @Override public void fetchUUID(final String name, final RunnableVal<UUID> ifFetch) {
TaskManager.runTaskAsync(new Runnable() { TaskManager.runTaskAsync(() -> {
@Override public void run() { ifFetch.value = FileUUIDHandler.this.uuidWrapper.getUUID(name);
ifFetch.value = FileUUIDHandler.this.uuidWrapper.getUUID(name); TaskManager.runTask(ifFetch);
TaskManager.runTask(ifFetch);
}
}); });
} }
} }

View File

@ -30,25 +30,18 @@ import java.util.Map.Entry;
public static void insertPlots(final SQLManager manager, final List<Plot> plots, public static void insertPlots(final SQLManager manager, final List<Plot> plots,
final PlotPlayer player) { final PlotPlayer player) {
TaskManager.runTaskAsync(new Runnable() { TaskManager.runTaskAsync(() -> {
@Override public void run() { try {
try { ArrayList<Plot> ps = new ArrayList<>(plots);
ArrayList<Plot> ps = new ArrayList<>(); MainUtil.sendMessage(player, "&6Starting...");
for (Plot p : plots) { manager.createPlotsAndData(ps, () -> {
ps.add(p); MainUtil.sendMessage(player, "&6Database conversion finished!");
} manager.close();
MainUtil.sendMessage(player, "&6Starting..."); });
manager.createPlotsAndData(ps, new Runnable() { } catch (Exception e) {
@Override public void run() { MainUtil.sendMessage(player,
MainUtil.sendMessage(player, "&6Database conversion finished!"); "Failed to insert plot objects, see stacktrace for info");
manager.close(); e.printStackTrace();
}
});
} catch (Exception e) {
MainUtil.sendMessage(player,
"Failed to insert plot objects, see stacktrace for info");
e.printStackTrace();
}
} }
}); });
} }
@ -130,20 +123,13 @@ import java.util.Map.Entry;
plots.add(plot); plots.add(plot);
} }
} else { } else {
HashMap<PlotId, Plot> plotmap = HashMap<PlotId, Plot> plotmap = PlotSquared.get().plots_tmp
PlotSquared.get().plots_tmp.get(areaname); .computeIfAbsent(areaname, k -> new HashMap<>());
if (plotmap == null) {
plotmap = new HashMap<>();
PlotSquared.get().plots_tmp.put(areaname, plotmap);
}
plotmap.putAll(entry.getValue()); plotmap.putAll(entry.getValue());
} }
} }
DBFunc.createPlotsAndData(plots, new Runnable() { DBFunc.createPlotsAndData(plots,
@Override public void run() { () -> MainUtil.sendMessage(player, "&6Database conversion finished!"));
MainUtil.sendMessage(player, "&6Database conversion finished!");
}
});
return true; return true;
case "mysql": case "mysql":
if (args.length < 6) { if (args.length < 6) {

View File

@ -6,7 +6,6 @@ import com.github.intellectualsites.plotsquared.plot.config.Settings;
import com.github.intellectualsites.plotsquared.plot.config.Storage; import com.github.intellectualsites.plotsquared.plot.config.Storage;
import com.github.intellectualsites.plotsquared.plot.flag.Flag; import com.github.intellectualsites.plotsquared.plot.flag.Flag;
import com.github.intellectualsites.plotsquared.plot.flag.FlagManager; import com.github.intellectualsites.plotsquared.plot.flag.FlagManager;
import com.github.intellectualsites.plotsquared.plot.flag.StringFlag;
import com.github.intellectualsites.plotsquared.plot.object.*; import com.github.intellectualsites.plotsquared.plot.object.*;
import com.github.intellectualsites.plotsquared.plot.object.comment.PlotComment; import com.github.intellectualsites.plotsquared.plot.object.comment.PlotComment;
import com.github.intellectualsites.plotsquared.plot.util.MainUtil; import com.github.intellectualsites.plotsquared.plot.util.MainUtil;
@ -121,42 +120,40 @@ import java.util.concurrent.atomic.AtomicInteger;
} catch (SQLException e) { } catch (SQLException e) {
e.printStackTrace(); e.printStackTrace();
} }
TaskManager.runTaskAsync(new Runnable() { TaskManager.runTaskAsync(() -> {
@Override public void run() { long last = System.currentTimeMillis();
long last = System.currentTimeMillis(); while (true) {
while (true) { if (SQLManager.this.closed) {
if (SQLManager.this.closed) { break;
break; }
boolean hasTask =
!globalTasks.isEmpty() || !playerTasks.isEmpty() || !plotTasks.isEmpty()
|| !clusterTasks.isEmpty();
if (hasTask) {
if (SQLManager.this.mySQL && System.currentTimeMillis() - last > 550000
|| !isValid()) {
last = System.currentTimeMillis();
reconnect();
} }
boolean hasTask = if (!sendBatch()) {
!globalTasks.isEmpty() || !playerTasks.isEmpty() || !plotTasks.isEmpty()
|| !clusterTasks.isEmpty();
if (hasTask) {
if (SQLManager.this.mySQL && System.currentTimeMillis() - last > 550000
|| !isValid()) {
last = System.currentTimeMillis();
reconnect();
}
if (!sendBatch()) {
try {
if (!getNotifyTasks().isEmpty()) {
for (Runnable task : getNotifyTasks()) {
TaskManager.runTask(task);
}
getNotifyTasks().clear();
}
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
} else {
try { try {
Thread.sleep(1000); if (!getNotifyTasks().isEmpty()) {
for (Runnable task : getNotifyTasks()) {
TaskManager.runTask(task);
}
getNotifyTasks().clear();
}
Thread.sleep(50);
} catch (InterruptedException e) { } catch (InterruptedException e) {
e.printStackTrace(); e.printStackTrace();
} }
} }
} else {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
} }
} }
}); });
@ -171,7 +168,7 @@ import java.util.concurrent.atomic.AtomicInteger;
return false; return false;
} }
try (PreparedStatement stmt = this.connection.prepareStatement("SELECT 1")) { try (PreparedStatement stmt = this.connection.prepareStatement("SELECT 1")) {
stmt.executeQuery(); stmt.execute();
return true; return true;
} catch (Throwable e) { } catch (Throwable e) {
return false; return false;
@ -212,7 +209,7 @@ import java.util.concurrent.atomic.AtomicInteger;
return null; return null;
} }
@Override public void set(PreparedStatement stmt) { @Override public void set(PreparedStatement statement) {
} }
@Override public void addBatch(PreparedStatement statement) { @Override public void addBatch(PreparedStatement statement) {
@ -242,7 +239,7 @@ import java.util.concurrent.atomic.AtomicInteger;
return null; return null;
} }
@Override public void set(PreparedStatement stmt) { @Override public void set(PreparedStatement statement) {
} }
@Override public void addBatch(PreparedStatement statement) { @Override public void addBatch(PreparedStatement statement) {
@ -269,7 +266,7 @@ import java.util.concurrent.atomic.AtomicInteger;
return null; return null;
} }
@Override public void set(PreparedStatement stmt) { @Override public void set(PreparedStatement statement) {
} }
@Override public void addBatch(PreparedStatement statement) { @Override public void addBatch(PreparedStatement statement) {
@ -515,92 +512,88 @@ import java.util.concurrent.atomic.AtomicInteger;
} }
@Override public void createPlotsAndData(final List<Plot> myList, final Runnable whenDone) { @Override public void createPlotsAndData(final List<Plot> myList, final Runnable whenDone) {
addGlobalTask(new Runnable() { addGlobalTask(() -> {
@Override public void run() { try {
try { // Create the plots
// Create the plots createPlots(myList, () -> {
createPlots(myList, new Runnable() { try {
@Override public void run() { // Creating datastructures
try { HashMap<PlotId, Plot> plotMap = new HashMap<>();
// Creating datastructures for (Plot plot : myList) {
HashMap<PlotId, Plot> plotMap = new HashMap<>(); plotMap.put(plot.getId(), plot);
for (Plot plot : myList) { }
plotMap.put(plot.getId(), plot); ArrayList<SettingsPair> settings = new ArrayList<>();
} final ArrayList<UUIDPair> helpers = new ArrayList<>();
ArrayList<SettingsPair> settings = new ArrayList<>(); final ArrayList<UUIDPair> trusted = new ArrayList<>();
final ArrayList<UUIDPair> helpers = new ArrayList<>(); final ArrayList<UUIDPair> denied = new ArrayList<>();
final ArrayList<UUIDPair> trusted = new ArrayList<>();
final ArrayList<UUIDPair> denied = new ArrayList<>();
// Populating structures // Populating structures
try (PreparedStatement stmt = SQLManager.this.connection try (PreparedStatement stmt = SQLManager.this.connection
.prepareStatement(SQLManager.this.GET_ALL_PLOTS); .prepareStatement(SQLManager.this.GET_ALL_PLOTS);
ResultSet result = stmt.executeQuery()) { ResultSet result = stmt.executeQuery()) {
while (result.next()) { while (result.next()) {
int id = result.getInt("id"); int id = result.getInt("id");
int x = result.getInt("plot_id_x"); int x = result.getInt("plot_id_x");
int y = result.getInt("plot_id_z"); int y = result.getInt("plot_id_z");
PlotId plotId = new PlotId(x, y); PlotId plotId = new PlotId(x, y);
Plot plot = plotMap.get(plotId); Plot plot = plotMap.get(plotId);
if (plot != null) { if (plot != null) {
settings.add(new SettingsPair(id, plot.getSettings())); settings.add(new SettingsPair(id, plot.getSettings()));
for (UUID uuid : plot.getDenied()) { for (UUID uuid : plot.getDenied()) {
denied.add(new UUIDPair(id, uuid)); denied.add(new UUIDPair(id, uuid));
} }
for (UUID uuid : plot.getMembers()) { for (UUID uuid : plot.getMembers()) {
trusted.add(new UUIDPair(id, uuid)); trusted.add(new UUIDPair(id, uuid));
} }
for (UUID uuid : plot.getTrusted()) { for (UUID uuid : plot.getTrusted()) {
helpers.add(new UUIDPair(id, uuid)); helpers.add(new UUIDPair(id, uuid));
}
}
} }
} }
createSettings(settings, new Runnable() { }
}
createSettings(settings, new Runnable() {
@Override public void run() {
createTiers(helpers, "helpers", new Runnable() {
@Override public void run() { @Override public void run() {
createTiers(helpers, "helpers", new Runnable() { createTiers(trusted, "trusted", new Runnable() {
@Override public void run() { @Override public void run() {
createTiers(trusted, "trusted", new Runnable() { createTiers(denied, "denied",
@Override public void run() { new Runnable() {
createTiers(denied, "denied", @Override public void run() {
new Runnable() { try {
@Override public void run() { SQLManager.this.connection
try { .commit();
SQLManager.this.connection } catch (SQLException e) {
.commit(); e.printStackTrace();
} catch (SQLException e) { }
e.printStackTrace(); if (whenDone != null) {
} whenDone.run();
if (whenDone != null) { }
whenDone.run(); }
} });
}
});
}
});
} }
}); });
} }
}); });
} catch (SQLException e) {
e.printStackTrace();
PlotSquared.debug("&7[WARN] Failed to set all helpers for plots");
try {
SQLManager.this.connection.commit();
} catch (SQLException e1) {
e1.printStackTrace();
}
} }
});
} catch (SQLException e) {
e.printStackTrace();
PlotSquared.debug("&7[WARN] Failed to set all helpers for plots");
try {
SQLManager.this.connection.commit();
} catch (SQLException e1) {
e1.printStackTrace();
} }
});
} catch (Exception e) {
e.printStackTrace();
PlotSquared.debug("&7[WARN] Failed to set all helpers for plots");
try {
SQLManager.this.connection.commit();
} catch (SQLException e1) {
e1.printStackTrace();
} }
});
} catch (Exception e) {
e.printStackTrace();
PlotSquared.debug("&7[WARN] Failed to set all helpers for plots");
try {
SQLManager.this.connection.commit();
} catch (SQLException e1) {
e1.printStackTrace();
} }
} }
}); });
@ -929,11 +922,7 @@ import java.util.concurrent.atomic.AtomicInteger;
stmt.setInt(1, pair.id); stmt.setInt(1, pair.id);
} }
}; };
addGlobalTask(new Runnable() { addGlobalTask(() -> setBulk(myList, mod, whenDone));
@Override public void run() {
setBulk(myList, mod, whenDone);
}
});
} }
public void createEmptySettings(final ArrayList<Integer> myList, final Runnable whenDone) { public void createEmptySettings(final ArrayList<Integer> myList, final Runnable whenDone) {
@ -977,25 +966,21 @@ import java.util.concurrent.atomic.AtomicInteger;
stmt.setInt(1, id); stmt.setInt(1, id);
} }
}; };
addGlobalTask(new Runnable() { addGlobalTask(() -> setBulk(myList, mod, whenDone));
@Override public void run() {
setBulk(myList, mod, whenDone);
}
});
} }
public void createPlotSafe(final Plot plot, final Runnable success, final Runnable failure) { public void createPlotSafe(final Plot plot, final Runnable success, final Runnable failure) {
final long timestamp = plot.getTimestamp(); final long timestamp = plot.getTimestamp();
addPlotTask(plot, new UniqueStatement("createPlotSafe_" + plot.hashCode()) { addPlotTask(plot, new UniqueStatement("createPlotSafe_" + plot.hashCode()) {
@Override public void set(PreparedStatement stmt) throws SQLException { @Override public void set(PreparedStatement statement) throws SQLException {
stmt.setInt(1, plot.getId().x); statement.setInt(1, plot.getId().x);
stmt.setInt(2, plot.getId().y); statement.setInt(2, plot.getId().y);
stmt.setString(3, plot.owner.toString()); statement.setString(3, plot.owner.toString());
stmt.setString(4, plot.getArea().toString()); statement.setString(4, plot.getArea().toString());
stmt.setTimestamp(5, new Timestamp(plot.getTimestamp())); statement.setTimestamp(5, new Timestamp(plot.getTimestamp()));
stmt.setString(6, plot.getArea().toString()); statement.setString(6, plot.getArea().toString());
stmt.setInt(7, plot.getId().x); statement.setInt(7, plot.getId().x);
stmt.setInt(8, plot.getId().y); statement.setInt(8, plot.getId().y);
} }
@Override public PreparedStatement get() throws SQLException { @Override public PreparedStatement get() throws SQLException {
@ -1015,9 +1000,9 @@ import java.util.concurrent.atomic.AtomicInteger;
plot.temp = keys.getInt(1); plot.temp = keys.getInt(1);
addPlotTask(plot, new UniqueStatement( addPlotTask(plot, new UniqueStatement(
"createPlotAndSettings_settings_" + plot.hashCode()) { "createPlotAndSettings_settings_" + plot.hashCode()) {
@Override public void set(PreparedStatement stmt) @Override public void set(PreparedStatement statement)
throws SQLException { throws SQLException {
stmt.setInt(1, getId(plot)); statement.setInt(1, getId(plot));
} }
@Override public PreparedStatement get() throws SQLException { @Override public PreparedStatement get() throws SQLException {
@ -1054,12 +1039,12 @@ import java.util.concurrent.atomic.AtomicInteger;
@Override public void createPlotAndSettings(final Plot plot, Runnable whenDone) { @Override public void createPlotAndSettings(final Plot plot, Runnable whenDone) {
addPlotTask(plot, new UniqueStatement("createPlotAndSettings_" + plot.hashCode()) { addPlotTask(plot, new UniqueStatement("createPlotAndSettings_" + plot.hashCode()) {
@Override public void set(PreparedStatement stmt) throws SQLException { @Override public void set(PreparedStatement statement) throws SQLException {
stmt.setInt(1, plot.getId().x); statement.setInt(1, plot.getId().x);
stmt.setInt(2, plot.getId().y); statement.setInt(2, plot.getId().y);
stmt.setString(3, plot.owner.toString()); statement.setString(3, plot.owner.toString());
stmt.setString(4, plot.getArea().toString()); statement.setString(4, plot.getArea().toString());
stmt.setTimestamp(5, new Timestamp(plot.getTimestamp())); statement.setTimestamp(5, new Timestamp(plot.getTimestamp()));
} }
@Override public PreparedStatement get() throws SQLException { @Override public PreparedStatement get() throws SQLException {
@ -1080,8 +1065,8 @@ import java.util.concurrent.atomic.AtomicInteger;
} }
}); });
addPlotTask(plot, new UniqueStatement("createPlotAndSettings_settings_" + plot.hashCode()) { addPlotTask(plot, new UniqueStatement("createPlotAndSettings_settings_" + plot.hashCode()) {
@Override public void set(PreparedStatement stmt) throws SQLException { @Override public void set(PreparedStatement statement) throws SQLException {
stmt.setInt(1, getId(plot)); statement.setInt(1, getId(plot));
} }
@Override public PreparedStatement get() throws SQLException { @Override public PreparedStatement get() throws SQLException {
@ -1248,8 +1233,8 @@ import java.util.concurrent.atomic.AtomicInteger;
@Override public void deleteSettings(final Plot plot) { @Override public void deleteSettings(final Plot plot) {
addPlotTask(plot, new UniqueStatement("delete_plot_settings") { addPlotTask(plot, new UniqueStatement("delete_plot_settings") {
@Override public void set(PreparedStatement stmt) throws SQLException { @Override public void set(PreparedStatement statement) throws SQLException {
stmt.setInt(1, getId(plot)); statement.setInt(1, getId(plot));
} }
@Override public PreparedStatement get() throws SQLException { @Override public PreparedStatement get() throws SQLException {
@ -1265,8 +1250,8 @@ import java.util.concurrent.atomic.AtomicInteger;
return; return;
} }
addPlotTask(plot, new UniqueStatement("delete_plot_helpers") { addPlotTask(plot, new UniqueStatement("delete_plot_helpers") {
@Override public void set(PreparedStatement stmt) throws SQLException { @Override public void set(PreparedStatement statement) throws SQLException {
stmt.setInt(1, getId(plot)); statement.setInt(1, getId(plot));
} }
@Override public PreparedStatement get() throws SQLException { @Override public PreparedStatement get() throws SQLException {
@ -1282,8 +1267,8 @@ import java.util.concurrent.atomic.AtomicInteger;
return; return;
} }
addPlotTask(plot, new UniqueStatement("delete_plot_trusted") { addPlotTask(plot, new UniqueStatement("delete_plot_trusted") {
@Override public void set(PreparedStatement stmt) throws SQLException { @Override public void set(PreparedStatement statement) throws SQLException {
stmt.setInt(1, getId(plot)); statement.setInt(1, getId(plot));
} }
@Override public PreparedStatement get() throws SQLException { @Override public PreparedStatement get() throws SQLException {
@ -1299,8 +1284,8 @@ import java.util.concurrent.atomic.AtomicInteger;
return; return;
} }
addPlotTask(plot, new UniqueStatement("delete_plot_denied") { addPlotTask(plot, new UniqueStatement("delete_plot_denied") {
@Override public void set(PreparedStatement stmt) throws SQLException { @Override public void set(PreparedStatement statement) throws SQLException {
stmt.setInt(1, getId(plot)); statement.setInt(1, getId(plot));
} }
@Override public PreparedStatement get() throws SQLException { @Override public PreparedStatement get() throws SQLException {
@ -1313,9 +1298,9 @@ import java.util.concurrent.atomic.AtomicInteger;
@Override public void deleteComments(final Plot plot) { @Override public void deleteComments(final Plot plot) {
addPlotTask(plot, new UniqueStatement("delete_plot_comments") { addPlotTask(plot, new UniqueStatement("delete_plot_comments") {
@Override public void set(PreparedStatement stmt) throws SQLException { @Override public void set(PreparedStatement statement) throws SQLException {
stmt.setString(1, plot.getArea().toString()); statement.setString(1, plot.getArea().toString());
stmt.setInt(2, plot.hashCode()); statement.setInt(2, plot.hashCode());
} }
@Override public PreparedStatement get() throws SQLException { @Override public PreparedStatement get() throws SQLException {
@ -1331,8 +1316,8 @@ import java.util.concurrent.atomic.AtomicInteger;
return; return;
} }
addPlotTask(plot, new UniqueStatement("delete_plot_ratings") { addPlotTask(plot, new UniqueStatement("delete_plot_ratings") {
@Override public void set(PreparedStatement stmt) throws SQLException { @Override public void set(PreparedStatement statement) throws SQLException {
stmt.setInt(1, getId(plot)); statement.setInt(1, getId(plot));
} }
@Override public PreparedStatement get() throws SQLException { @Override public PreparedStatement get() throws SQLException {
@ -1359,8 +1344,8 @@ import java.util.concurrent.atomic.AtomicInteger;
deleteComments(plot); deleteComments(plot);
deleteRatings(plot); deleteRatings(plot);
addPlotTask(plot, new UniqueStatement("delete_plot") { addPlotTask(plot, new UniqueStatement("delete_plot") {
@Override public void set(PreparedStatement stmt) throws SQLException { @Override public void set(PreparedStatement statement) throws SQLException {
stmt.setInt(1, getId(plot)); statement.setInt(1, getId(plot));
} }
@Override public PreparedStatement get() throws SQLException { @Override public PreparedStatement get() throws SQLException {
@ -1381,8 +1366,8 @@ import java.util.concurrent.atomic.AtomicInteger;
"Creating plot... Id: " + plot.getId() + " World: " + plot.getWorldName() + " Owner: " "Creating plot... Id: " + plot.getId() + " World: " + plot.getWorldName() + " Owner: "
+ plot.owner + " Index: " + id); + plot.owner + " Index: " + id);
addPlotTask(plot, new UniqueStatement("createPlotSettings") { addPlotTask(plot, new UniqueStatement("createPlotSettings") {
@Override public void set(PreparedStatement stmt) throws SQLException { @Override public void set(PreparedStatement statement) throws SQLException {
stmt.setInt(1, id); statement.setInt(1, id);
} }
@Override public PreparedStatement get() throws SQLException { @Override public PreparedStatement get() throws SQLException {
@ -1685,8 +1670,8 @@ import java.util.concurrent.atomic.AtomicInteger;
time = System.currentTimeMillis() + id; time = System.currentTimeMillis() + id;
} }
} }
Plot p = new Plot(plot_id, user, new HashSet<UUID>(), new HashSet<UUID>(), Plot p = new Plot(plot_id, user, new HashSet<>(), new HashSet<>(),
new HashSet<UUID>(), "", null, null, null, new HashSet<>(), "", null, null, null,
new boolean[] {false, false, false, false}, time, id); new boolean[] {false, false, false, false}, time, id);
HashMap<PlotId, Plot> map = newPlots.get(areaid); HashMap<PlotId, Plot> map = newPlots.get(areaid);
if (map != null) { if (map != null) {
@ -1845,7 +1830,7 @@ import java.util.concurrent.atomic.AtomicInteger;
} catch (Exception ignored) { } catch (Exception ignored) {
} }
} }
Integer m = resultSet.getInt("merged"); int m = resultSet.getInt("merged");
boolean[] merged = new boolean[4]; boolean[] merged = new boolean[4];
for (int i = 0; i < 4; i++) { for (int i = 0; i < 4; i++) {
merged[3 - i] = (m & 1 << i) != 0; merged[3 - i] = (m & 1 << i) != 0;
@ -1878,13 +1863,6 @@ import java.util.concurrent.atomic.AtomicInteger;
if (StringMan if (StringMan
.isAlpha(element.replaceAll("_", "").replaceAll("-", ""))) { .isAlpha(element.replaceAll("_", "").replaceAll("-", ""))) {
Flag flag = FlagManager.getOrCreateFlag(element); Flag flag = FlagManager.getOrCreateFlag(element);
if (flag == null) {
flag = new StringFlag(element) {
@Override public String getValueDescription() {
return "Generic Filler Flag";
}
};
}
flags.put(flag, flag.parseValue("")); flags.put(flag, flag.parseValue(""));
} else { } else {
PlotSquared.debug("INVALID FLAG: " + element); PlotSquared.debug("INVALID FLAG: " + element);
@ -1936,10 +1914,10 @@ import java.util.concurrent.atomic.AtomicInteger;
@Override public void setMerged(final Plot plot, final boolean[] merged) { @Override public void setMerged(final Plot plot, final boolean[] merged) {
plot.getSettings().setMerged(merged); plot.getSettings().setMerged(merged);
addPlotTask(plot, new UniqueStatement("setMerged") { addPlotTask(plot, new UniqueStatement("setMerged") {
@Override public void set(PreparedStatement stmt) throws SQLException { @Override public void set(PreparedStatement statement) throws SQLException {
int hash = MainUtil.hash(merged); int hash = MainUtil.hash(merged);
stmt.setInt(1, hash); statement.setInt(1, hash);
stmt.setInt(2, getId(plot)); statement.setInt(2, getId(plot));
} }
@Override public PreparedStatement get() throws SQLException { @Override public PreparedStatement get() throws SQLException {
@ -1956,10 +1934,10 @@ import java.util.concurrent.atomic.AtomicInteger;
final PlotId pos1 = plot1.getId(); final PlotId pos1 = plot1.getId();
final PlotId pos2 = plot2.getId(); final PlotId pos2 = plot2.getId();
addPlotTask(plot1, new UniqueStatement("swapPlots") { addPlotTask(plot1, new UniqueStatement("swapPlots") {
@Override public void set(PreparedStatement stmt) throws SQLException { @Override public void set(PreparedStatement statement) throws SQLException {
stmt.setInt(1, pos2.x); statement.setInt(1, pos2.x);
stmt.setInt(2, pos2.y); statement.setInt(2, pos2.y);
stmt.setInt(3, id1); statement.setInt(3, id1);
} }
@Override public PreparedStatement get() throws SQLException { @Override public PreparedStatement get() throws SQLException {
@ -1969,10 +1947,10 @@ import java.util.concurrent.atomic.AtomicInteger;
} }
}); });
addPlotTask(plot2, new UniqueStatement("swapPlots") { addPlotTask(plot2, new UniqueStatement("swapPlots") {
@Override public void set(PreparedStatement stmt) throws SQLException { @Override public void set(PreparedStatement statement) throws SQLException {
stmt.setInt(1, pos1.x); statement.setInt(1, pos1.x);
stmt.setInt(2, pos1.y); statement.setInt(2, pos1.y);
stmt.setInt(3, id2); statement.setInt(3, id2);
} }
@Override public PreparedStatement get() throws SQLException { @Override public PreparedStatement get() throws SQLException {
@ -1985,11 +1963,11 @@ import java.util.concurrent.atomic.AtomicInteger;
@Override public void movePlot(final Plot original, final Plot newPlot) { @Override public void movePlot(final Plot original, final Plot newPlot) {
addPlotTask(original, new UniqueStatement("movePlot") { addPlotTask(original, new UniqueStatement("movePlot") {
@Override public void set(PreparedStatement stmt) throws SQLException { @Override public void set(PreparedStatement statement) throws SQLException {
stmt.setInt(1, newPlot.getId().x); statement.setInt(1, newPlot.getId().x);
stmt.setInt(2, newPlot.getId().y); statement.setInt(2, newPlot.getId().y);
stmt.setString(3, newPlot.getArea().toString()); statement.setString(3, newPlot.getArea().toString());
stmt.setInt(4, getId(original)); statement.setInt(4, getId(original));
} }
@Override public PreparedStatement get() throws SQLException { @Override public PreparedStatement get() throws SQLException {
@ -2004,9 +1982,9 @@ import java.util.concurrent.atomic.AtomicInteger;
@Override public void setFlags(final Plot plot, HashMap<Flag<?>, Object> flags) { @Override public void setFlags(final Plot plot, HashMap<Flag<?>, Object> flags) {
final String flag_string = FlagManager.toString(flags); final String flag_string = FlagManager.toString(flags);
addPlotTask(plot, new UniqueStatement("setFlags") { addPlotTask(plot, new UniqueStatement("setFlags") {
@Override public void set(PreparedStatement stmt) throws SQLException { @Override public void set(PreparedStatement statement) throws SQLException {
stmt.setString(1, flag_string); statement.setString(1, flag_string);
stmt.setInt(2, getId(plot)); statement.setInt(2, getId(plot));
} }
@Override public PreparedStatement get() throws SQLException { @Override public PreparedStatement get() throws SQLException {
@ -2019,9 +1997,9 @@ import java.util.concurrent.atomic.AtomicInteger;
@Override public void setAlias(final Plot plot, final String alias) { @Override public void setAlias(final Plot plot, final String alias) {
addPlotTask(plot, new UniqueStatement("setAlias") { addPlotTask(plot, new UniqueStatement("setAlias") {
@Override public void set(PreparedStatement stmt) throws SQLException { @Override public void set(PreparedStatement statement) throws SQLException {
stmt.setString(1, alias); statement.setString(1, alias);
stmt.setInt(2, getId(plot)); statement.setInt(2, getId(plot));
} }
@Override public PreparedStatement get() throws SQLException { @Override public PreparedStatement get() throws SQLException {
@ -2040,8 +2018,7 @@ import java.util.concurrent.atomic.AtomicInteger;
@Override public void run() { @Override public void run() {
if (!uniqueIds.isEmpty()) { if (!uniqueIds.isEmpty()) {
try { try {
ArrayList<Integer> uniqueIdsList = new ArrayList<Integer>(uniqueIds); ArrayList<Integer> uniqueIdsList = new ArrayList<>(uniqueIds);
String stmt_prefix = "";
int size = uniqueIdsList.size(); int size = uniqueIdsList.size();
int packet = 990; int packet = 990;
int amount = size / packet; int amount = size / packet;
@ -2054,8 +2031,8 @@ import java.util.concurrent.atomic.AtomicInteger;
if (subList.isEmpty()) { if (subList.isEmpty()) {
break; break;
} }
StringBuilder idstr2 = new StringBuilder(""); StringBuilder idstr2 = new StringBuilder();
stmt_prefix = ""; String stmt_prefix = "";
for (Integer id : subList) { for (Integer id : subList) {
idstr2.append(stmt_prefix).append(id); idstr2.append(stmt_prefix).append(id);
stmt_prefix = " OR `id` = "; stmt_prefix = " OR `id` = ";
@ -2105,43 +2082,41 @@ import java.util.concurrent.atomic.AtomicInteger;
} }
@Override public void purge(final PlotArea area, final Set<PlotId> plots) { @Override public void purge(final PlotArea area, final Set<PlotId> plots) {
addGlobalTask(new Runnable() { addGlobalTask(() -> {
@Override public void run() { try (PreparedStatement stmt = SQLManager.this.connection.prepareStatement(
try (PreparedStatement stmt = SQLManager.this.connection.prepareStatement( "SELECT `id`, `plot_id_x`, `plot_id_z` FROM `" + SQLManager.this.prefix
"SELECT `id`, `plot_id_x`, `plot_id_z` FROM `" + SQLManager.this.prefix + "plot` WHERE `world` = ?")) {
+ "plot` WHERE `world` = ?")) { stmt.setString(1, area.toString());
stmt.setString(1, area.toString()); Set<Integer> ids;
Set<Integer> ids; try (ResultSet r = stmt.executeQuery()) {
try (ResultSet r = stmt.executeQuery()) { ids = new HashSet<>();
ids = new HashSet<>(); while (r.next()) {
while (r.next()) { PlotId plot_id =
PlotId plot_id = new PlotId(r.getInt("plot_id_x"), r.getInt("plot_id_z"));
new PlotId(r.getInt("plot_id_x"), r.getInt("plot_id_z")); if (plots.contains(plot_id)) {
if (plots.contains(plot_id)) { ids.add(r.getInt("id"));
ids.add(r.getInt("id"));
}
} }
} }
purgeIds(ids);
} catch (SQLException e) {
e.printStackTrace();
PlotSquared.debug("&c[ERROR] FAILED TO PURGE AREA '" + area + "'!");
}
for (Iterator<PlotId> iterator = plots.iterator(); iterator.hasNext(); ) {
PlotId plotId = iterator.next();
iterator.remove();
PlotId id = new PlotId(plotId.x, plotId.y);
area.removePlot(id);
} }
purgeIds(ids);
} catch (SQLException e) {
e.printStackTrace();
PlotSquared.debug("&c[ERROR] FAILED TO PURGE AREA '" + area + "'!");
}
for (Iterator<PlotId> iterator = plots.iterator(); iterator.hasNext(); ) {
PlotId plotId = iterator.next();
iterator.remove();
PlotId id = new PlotId(plotId.x, plotId.y);
area.removePlot(id);
} }
}); });
} }
@Override public void setPosition(final Plot plot, final String position) { @Override public void setPosition(final Plot plot, final String position) {
addPlotTask(plot, new UniqueStatement("setPosition") { addPlotTask(plot, new UniqueStatement("setPosition") {
@Override public void set(PreparedStatement stmt) throws SQLException { @Override public void set(PreparedStatement statement) throws SQLException {
stmt.setString(1, position == null ? "" : position); statement.setString(1, position == null ? "" : position);
stmt.setInt(2, getId(plot)); statement.setInt(2, getId(plot));
} }
@Override public PreparedStatement get() throws SQLException { @Override public PreparedStatement get() throws SQLException {
@ -2407,8 +2382,8 @@ import java.util.concurrent.atomic.AtomicInteger;
@Override public void delete(PlotCluster cluster) { @Override public void delete(PlotCluster cluster) {
final int id = getClusterId(cluster); final int id = getClusterId(cluster);
addClusterTask(cluster, new UniqueStatement("delete_cluster_settings") { addClusterTask(cluster, new UniqueStatement("delete_cluster_settings") {
@Override public void set(PreparedStatement stmt) throws SQLException { @Override public void set(PreparedStatement statement) throws SQLException {
stmt.setInt(1, id); statement.setInt(1, id);
} }
@Override public PreparedStatement get() throws SQLException { @Override public PreparedStatement get() throws SQLException {
@ -2418,8 +2393,8 @@ import java.util.concurrent.atomic.AtomicInteger;
} }
}); });
addClusterTask(cluster, new UniqueStatement("delete_cluster_helpers") { addClusterTask(cluster, new UniqueStatement("delete_cluster_helpers") {
@Override public void set(PreparedStatement stmt) throws SQLException { @Override public void set(PreparedStatement statement) throws SQLException {
stmt.setInt(1, id); statement.setInt(1, id);
} }
@Override public PreparedStatement get() throws SQLException { @Override public PreparedStatement get() throws SQLException {
@ -2429,8 +2404,8 @@ import java.util.concurrent.atomic.AtomicInteger;
} }
}); });
addClusterTask(cluster, new UniqueStatement("delete_cluster_invited") { addClusterTask(cluster, new UniqueStatement("delete_cluster_invited") {
@Override public void set(PreparedStatement stmt) throws SQLException { @Override public void set(PreparedStatement statement) throws SQLException {
stmt.setInt(1, id); statement.setInt(1, id);
} }
@Override public PreparedStatement get() throws SQLException { @Override public PreparedStatement get() throws SQLException {
@ -2440,8 +2415,8 @@ import java.util.concurrent.atomic.AtomicInteger;
} }
}); });
addClusterTask(cluster, new UniqueStatement("delete_cluster") { addClusterTask(cluster, new UniqueStatement("delete_cluster") {
@Override public void set(PreparedStatement stmt) throws SQLException { @Override public void set(PreparedStatement statement) throws SQLException {
stmt.setInt(1, id); statement.setInt(1, id);
} }
@Override public PreparedStatement get() throws SQLException { @Override public PreparedStatement get() throws SQLException {
@ -2454,15 +2429,15 @@ import java.util.concurrent.atomic.AtomicInteger;
@Override public void addPersistentMeta(final UUID uuid, final String key, final byte[] meta, @Override public void addPersistentMeta(final UUID uuid, final String key, final byte[] meta,
final boolean replace) { final boolean replace) {
addPlayerTask(uuid, new UniqueStatement("addPersistentMeta") { addPlayerTask(uuid, new UniqueStatement("addPersistentMeta") {
@Override public void set(PreparedStatement stmt) throws SQLException { @Override public void set(PreparedStatement statement) throws SQLException {
if (replace) { if (replace) {
stmt.setBytes(1, meta); statement.setBytes(1, meta);
stmt.setString(2, uuid.toString()); statement.setString(2, uuid.toString());
stmt.setString(3, key); statement.setString(3, key);
} else { } else {
stmt.setString(1, uuid.toString()); statement.setString(1, uuid.toString());
stmt.setString(2, key); statement.setString(2, key);
stmt.setBytes(3, meta); statement.setBytes(3, meta);
} }
} }
@ -2482,9 +2457,9 @@ import java.util.concurrent.atomic.AtomicInteger;
@Override public void removePersistentMeta(final UUID uuid, final String key) { @Override public void removePersistentMeta(final UUID uuid, final String key) {
addPlayerTask(uuid, new UniqueStatement("removePersistentMeta") { addPlayerTask(uuid, new UniqueStatement("removePersistentMeta") {
@Override public void set(PreparedStatement stmt) throws SQLException { @Override public void set(PreparedStatement statement) throws SQLException {
stmt.setString(1, uuid.toString()); statement.setString(1, uuid.toString());
stmt.setString(2, key); statement.setString(2, key);
} }
@Override public PreparedStatement get() throws SQLException { @Override public PreparedStatement get() throws SQLException {
@ -2498,8 +2473,8 @@ import java.util.concurrent.atomic.AtomicInteger;
@Override @Override
public void getPersistentMeta(final UUID uuid, final RunnableVal<Map<String, byte[]>> result) { public void getPersistentMeta(final UUID uuid, final RunnableVal<Map<String, byte[]>> result) {
addPlayerTask(uuid, new UniqueStatement("getPersistentMeta") { addPlayerTask(uuid, new UniqueStatement("getPersistentMeta") {
@Override public void set(PreparedStatement stmt) throws SQLException { @Override public void set(PreparedStatement statement) throws SQLException {
stmt.setString(1, uuid.toString()); statement.setString(1, uuid.toString());
} }
@Override public PreparedStatement get() throws SQLException { @Override public PreparedStatement get() throws SQLException {
@ -2523,11 +2498,7 @@ import java.util.concurrent.atomic.AtomicInteger;
} }
resultSet.close(); resultSet.close();
TaskManager.runTaskAsync(new Runnable() { TaskManager.runTaskAsync(() -> result.run(metaMap));
@Override public void run() {
result.run(metaMap);
}
});
} }
}); });
@ -2591,11 +2562,7 @@ import java.util.concurrent.atomic.AtomicInteger;
} }
cluster = new PlotCluster(null, pos1, pos2, user, id); cluster = new PlotCluster(null, pos1, pos2, user, id);
clusters.put(id, cluster); clusters.put(id, cluster);
Set<PlotCluster> set = newClusters.get(areaid); Set<PlotCluster> set = newClusters.computeIfAbsent(areaid, k -> new HashSet<>());
if (set == null) {
set = new HashSet<>();
newClusters.put(areaid, set);
}
set.add(cluster); set.add(cluster);
} }
//Getting helpers //Getting helpers
@ -2693,9 +2660,9 @@ import java.util.concurrent.atomic.AtomicInteger;
@Override public void setClusterName(final PlotCluster cluster, final String name) { @Override public void setClusterName(final PlotCluster cluster, final String name) {
addClusterTask(cluster, new UniqueStatement("setClusterName") { addClusterTask(cluster, new UniqueStatement("setClusterName") {
@Override public void set(PreparedStatement stmt) throws SQLException { @Override public void set(PreparedStatement statement) throws SQLException {
stmt.setString(1, name); statement.setString(1, name);
stmt.setInt(2, getClusterId(cluster)); statement.setInt(2, getClusterId(cluster));
} }
@Override public PreparedStatement get() throws SQLException { @Override public PreparedStatement get() throws SQLException {
@ -2739,13 +2706,13 @@ import java.util.concurrent.atomic.AtomicInteger;
@Override public void createCluster(final PlotCluster cluster) { @Override public void createCluster(final PlotCluster cluster) {
addClusterTask(cluster, new UniqueStatement("createCluster_" + cluster.hashCode()) { addClusterTask(cluster, new UniqueStatement("createCluster_" + cluster.hashCode()) {
@Override public void set(PreparedStatement stmt) throws SQLException { @Override public void set(PreparedStatement statement) throws SQLException {
stmt.setInt(1, cluster.getP1().x); statement.setInt(1, cluster.getP1().x);
stmt.setInt(2, cluster.getP1().y); statement.setInt(2, cluster.getP1().y);
stmt.setInt(3, cluster.getP2().x); statement.setInt(3, cluster.getP2().x);
stmt.setInt(4, cluster.getP2().y); statement.setInt(4, cluster.getP2().y);
stmt.setString(5, cluster.owner.toString()); statement.setString(5, cluster.owner.toString());
stmt.setString(6, cluster.area.toString()); statement.setString(6, cluster.area.toString());
} }
@Override public PreparedStatement get() throws SQLException { @Override public PreparedStatement get() throws SQLException {
@ -2767,9 +2734,9 @@ import java.util.concurrent.atomic.AtomicInteger;
}); });
addClusterTask(cluster, addClusterTask(cluster,
new UniqueStatement("createCluster_settings_" + cluster.hashCode()) { new UniqueStatement("createCluster_settings_" + cluster.hashCode()) {
@Override public void set(PreparedStatement stmt) throws SQLException { @Override public void set(PreparedStatement statement) throws SQLException {
stmt.setInt(1, getClusterId(cluster)); statement.setInt(1, getClusterId(cluster));
stmt.setString(2, cluster.settings.getAlias()); statement.setString(2, cluster.settings.getAlias());
} }
@Override public PreparedStatement get() throws SQLException { @Override public PreparedStatement get() throws SQLException {
@ -2787,12 +2754,12 @@ import java.util.concurrent.atomic.AtomicInteger;
current.setP2(max); current.setP2(max);
addClusterTask(current, new UniqueStatement("resizeCluster") { addClusterTask(current, new UniqueStatement("resizeCluster") {
@Override public void set(PreparedStatement stmt) throws SQLException { @Override public void set(PreparedStatement statement) throws SQLException {
stmt.setInt(1, pos1.x); statement.setInt(1, pos1.x);
stmt.setInt(2, pos1.y); statement.setInt(2, pos1.y);
stmt.setInt(3, pos2.x); statement.setInt(3, pos2.x);
stmt.setInt(4, pos2.y); statement.setInt(4, pos2.y);
stmt.setInt(5, getClusterId(current)); statement.setInt(5, getClusterId(current));
} }
@Override public PreparedStatement get() throws SQLException { @Override public PreparedStatement get() throws SQLException {
@ -2805,9 +2772,9 @@ import java.util.concurrent.atomic.AtomicInteger;
@Override public void setPosition(final PlotCluster cluster, final String position) { @Override public void setPosition(final PlotCluster cluster, final String position) {
addClusterTask(cluster, new UniqueStatement("setPosition") { addClusterTask(cluster, new UniqueStatement("setPosition") {
@Override public void set(PreparedStatement stmt) throws SQLException { @Override public void set(PreparedStatement statement) throws SQLException {
stmt.setString(1, position); statement.setString(1, position);
stmt.setInt(2, getClusterId(cluster)); statement.setInt(2, getClusterId(cluster));
} }
@Override public PreparedStatement get() throws SQLException { @Override public PreparedStatement get() throws SQLException {
@ -3008,87 +2975,83 @@ import java.util.concurrent.atomic.AtomicInteger;
@Override @Override
public void replaceWorld(final String oldWorld, final String newWorld, final PlotId min, public void replaceWorld(final String oldWorld, final String newWorld, final PlotId min,
final PlotId max) { final PlotId max) {
addGlobalTask(new Runnable() { addGlobalTask(() -> {
@Override public void run() { if (min == null) {
if (min == null) { try (PreparedStatement stmt = SQLManager.this.connection.prepareStatement(
try (PreparedStatement stmt = SQLManager.this.connection.prepareStatement( "UPDATE `" + SQLManager.this.prefix
"UPDATE `" + SQLManager.this.prefix + "plot` SET `world` = ? WHERE `world` = ?")) {
+ "plot` SET `world` = ? WHERE `world` = ?")) { stmt.setString(1, newWorld);
stmt.setString(1, newWorld); stmt.setString(2, oldWorld);
stmt.setString(2, oldWorld); stmt.executeUpdate();
stmt.executeUpdate(); } catch (SQLException e) {
} catch (SQLException e) { e.printStackTrace();
e.printStackTrace(); }
} try (PreparedStatement stmt = SQLManager.this.connection.prepareStatement(
try (PreparedStatement stmt = SQLManager.this.connection.prepareStatement( "UPDATE `" + SQLManager.this.prefix
"UPDATE `" + SQLManager.this.prefix + "cluster` SET `world` = ? WHERE `world` = ?")) {
+ "cluster` SET `world` = ? WHERE `world` = ?")) { stmt.setString(1, newWorld);
stmt.setString(1, newWorld); stmt.setString(2, oldWorld);
stmt.setString(2, oldWorld); stmt.executeUpdate();
stmt.executeUpdate(); } catch (SQLException e) {
} catch (SQLException e) { e.printStackTrace();
e.printStackTrace(); }
} } else {
} else { try (PreparedStatement stmt = SQLManager.this.connection.prepareStatement(
try (PreparedStatement stmt = SQLManager.this.connection.prepareStatement( "UPDATE `" + SQLManager.this.prefix
"UPDATE `" + SQLManager.this.prefix + "plot` SET `world` = ? WHERE `world` = ? AND `plot_id_x` BETWEEN ? AND ? AND `plot_id_z` BETWEEN ? AND ?")) {
+ "plot` SET `world` = ? WHERE `world` = ? AND `plot_id_x` BETWEEN ? AND ? AND `plot_id_z` BETWEEN ? AND ?")) { stmt.setString(1, newWorld);
stmt.setString(1, newWorld); stmt.setString(2, oldWorld);
stmt.setString(2, oldWorld); stmt.setInt(3, min.x);
stmt.setInt(3, min.x); stmt.setInt(4, max.x);
stmt.setInt(4, max.x); stmt.setInt(5, min.y);
stmt.setInt(5, min.y); stmt.setInt(6, max.y);
stmt.setInt(6, max.y); stmt.executeUpdate();
stmt.executeUpdate(); } catch (SQLException e) {
} catch (SQLException e) { e.printStackTrace();
e.printStackTrace(); }
} try (PreparedStatement stmt = SQLManager.this.connection.prepareStatement(
try (PreparedStatement stmt = SQLManager.this.connection.prepareStatement( "UPDATE `" + SQLManager.this.prefix
"UPDATE `" + SQLManager.this.prefix + "cluster` SET `world` = ? WHERE `world` = ? AND `pos1_x` <= ? AND `pos1_z` <= ? AND `pos2_x` >= ? AND `pos2_z` >= ?")) {
+ "cluster` SET `world` = ? WHERE `world` = ? AND `pos1_x` <= ? AND `pos1_z` <= ? AND `pos2_x` >= ? AND `pos2_z` >= ?")) { stmt.setString(1, newWorld);
stmt.setString(1, newWorld); stmt.setString(2, oldWorld);
stmt.setString(2, oldWorld); stmt.setInt(3, max.x);
stmt.setInt(3, max.x); stmt.setInt(4, max.y);
stmt.setInt(4, max.y); stmt.setInt(5, min.x);
stmt.setInt(5, min.x); stmt.setInt(6, min.y);
stmt.setInt(6, min.y); stmt.executeUpdate();
stmt.executeUpdate(); } catch (SQLException e) {
} catch (SQLException e) { e.printStackTrace();
e.printStackTrace();
}
} }
} }
}); });
} }
@Override public void replaceUUID(final UUID old, final UUID now) { @Override public void replaceUUID(final UUID old, final UUID now) {
addGlobalTask(new Runnable() { addGlobalTask(() -> {
@Override public void run() { try (Statement stmt = SQLManager.this.connection.createStatement()) {
try (Statement stmt = SQLManager.this.connection.createStatement()) { stmt.executeUpdate(
stmt.executeUpdate( "UPDATE `" + SQLManager.this.prefix + "cluster` SET `owner` = '" + now
"UPDATE `" + SQLManager.this.prefix + "cluster` SET `owner` = '" + now .toString() + "' WHERE `owner` = '" + old.toString() + '\'');
.toString() + "' WHERE `owner` = '" + old.toString() + '\''); stmt.executeUpdate(
stmt.executeUpdate( "UPDATE `" + SQLManager.this.prefix + "cluster_helpers` SET `user_uuid` = '"
"UPDATE `" + SQLManager.this.prefix + "cluster_helpers` SET `user_uuid` = '" + now.toString() + "' WHERE `user_uuid` = '" + old.toString() + '\'');
+ now.toString() + "' WHERE `user_uuid` = '" + old.toString() + '\''); stmt.executeUpdate(
stmt.executeUpdate( "UPDATE `" + SQLManager.this.prefix + "cluster_invited` SET `user_uuid` = '"
"UPDATE `" + SQLManager.this.prefix + "cluster_invited` SET `user_uuid` = '" + now.toString() + "' WHERE `user_uuid` = '" + old.toString() + '\'');
+ now.toString() + "' WHERE `user_uuid` = '" + old.toString() + '\''); stmt.executeUpdate(
stmt.executeUpdate( "UPDATE `" + SQLManager.this.prefix + "plot` SET `owner` = '" + now
"UPDATE `" + SQLManager.this.prefix + "plot` SET `owner` = '" + now .toString() + "' WHERE `owner` = '" + old.toString() + '\'');
.toString() + "' WHERE `owner` = '" + old.toString() + '\''); stmt.executeUpdate(
stmt.executeUpdate( "UPDATE `" + SQLManager.this.prefix + "plot_denied` SET `user_uuid` = '"
"UPDATE `" + SQLManager.this.prefix + "plot_denied` SET `user_uuid` = '" + now.toString() + "' WHERE `user_uuid` = '" + old.toString() + '\'');
+ now.toString() + "' WHERE `user_uuid` = '" + old.toString() + '\''); stmt.executeUpdate(
stmt.executeUpdate( "UPDATE `" + SQLManager.this.prefix + "plot_helpers` SET `user_uuid` = '"
"UPDATE `" + SQLManager.this.prefix + "plot_helpers` SET `user_uuid` = '" + now.toString() + "' WHERE `user_uuid` = '" + old.toString() + '\'');
+ now.toString() + "' WHERE `user_uuid` = '" + old.toString() + '\''); stmt.executeUpdate(
stmt.executeUpdate( "UPDATE `" + SQLManager.this.prefix + "plot_trusted` SET `user_uuid` = '"
"UPDATE `" + SQLManager.this.prefix + "plot_trusted` SET `user_uuid` = '" + now.toString() + "' WHERE `user_uuid` = '" + old.toString() + '\'');
+ now.toString() + "' WHERE `user_uuid` = '" + old.toString() + '\''); } catch (SQLException e) {
} catch (SQLException e) { e.printStackTrace();
e.printStackTrace();
}
} }
}); });
} }
@ -3120,7 +3083,7 @@ import java.util.concurrent.atomic.AtomicInteger;
public abstract PreparedStatement get() throws SQLException; public abstract PreparedStatement get() throws SQLException;
public abstract void set(PreparedStatement stmt) throws SQLException; public abstract void set(PreparedStatement statement) throws SQLException;
} }

View File

@ -8,6 +8,7 @@ import com.github.intellectualsites.plotsquared.plot.util.Permissions;
import com.google.common.collect.ImmutableSet; import com.google.common.collect.ImmutableSet;
import java.util.*; import java.util.*;
import java.util.stream.Collectors;
/** /**
* Flag Manager Utility. * Flag Manager Utility.
@ -234,13 +235,9 @@ public class FlagManager {
* @return a list of flags the specified player can use * @return a list of flags the specified player can use
*/ */
public static List<Flag> getFlags(PlotPlayer player) { public static List<Flag> getFlags(PlotPlayer player) {
List<Flag> returnFlags = new ArrayList<>(); List<Flag> returnFlags = Flags.getFlags().stream().filter(flag -> Permissions
for (Flag flag : Flags.getFlags()) { .hasPermission(player, "plots.set.flag." + flag.getName().toLowerCase()))
if (Permissions .collect(Collectors.toList());
.hasPermission(player, "plots.set.flag." + flag.getName().toLowerCase())) {
returnFlags.add(flag);
}
}
return returnFlags; return returnFlags;
} }