diff --git a/src/main/java/com/intellectualcrafters/plot/PS.java b/src/main/java/com/intellectualcrafters/plot/PS.java index 260279ad3..8da8f4579 100644 --- a/src/main/java/com/intellectualcrafters/plot/PS.java +++ b/src/main/java/com/intellectualcrafters/plot/PS.java @@ -526,6 +526,45 @@ public class PS { public ArrayList sortPlots(Collection plots) { return sortPlots(plots, SortType.DISTANCE_FROM_ORIGIN, null); } + + public ArrayList sortPlotsByTemp(Collection 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 overflow = new ArrayList<>(overflowCount); + for (Plot plot : plots) { + if (plot.temp <= 0) { + overflow.add(plot); + } + else { + array[plot.temp] = plot; + } + } + ArrayList result = new ArrayList<>(plots.size()); + for (Plot plot : array) { + if (plot != null) { + result.add(plot); + } + } + Collections.sort(overflow, new Comparator() { + @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": diff --git a/src/main/java/com/intellectualcrafters/plot/commands/Home.java b/src/main/java/com/intellectualcrafters/plot/commands/Home.java index 052187415..99013162b 100644 --- a/src/main/java/com/intellectualcrafters/plot/commands/Home.java +++ b/src/main/java/com/intellectualcrafters/plot/commands/Home.java @@ -51,8 +51,7 @@ public class Home extends SubCommand { @Override public boolean onCommand(final PlotPlayer plr, String[] args) { - - final ArrayList plots = PS.get().sortPlots(PS.get().getPlots(plr), SortType.CREATION_DATE, null); + final ArrayList 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; diff --git a/src/main/java/com/intellectualcrafters/plot/commands/Rate.java b/src/main/java/com/intellectualcrafters/plot/commands/Rate.java index bf147c449..c73d6cb8c 100644 --- a/src/main/java/com/intellectualcrafters/plot/commands/Rate.java +++ b/src/main/java/com/intellectualcrafters/plot/commands/Rate.java @@ -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(); } 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(); } run.run(); return true; diff --git a/src/main/java/com/intellectualcrafters/plot/commands/Visit.java b/src/main/java/com/intellectualcrafters/plot/commands/Visit.java index b00498e6a..f6d982151 100644 --- a/src/main/java/com/intellectualcrafters/plot/commands/Visit.java +++ b/src/main/java/com/intellectualcrafters/plot/commands/Visit.java @@ -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); diff --git a/src/main/java/com/intellectualcrafters/plot/commands/list.java b/src/main/java/com/intellectualcrafters/plot/commands/list.java index 1d326ee79..eda129d24 100644 --- a/src/main/java/com/intellectualcrafters/plot/commands/list.java +++ b/src/main/java/com/intellectualcrafters/plot/commands/list.java @@ -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; } } diff --git a/src/main/java/com/intellectualcrafters/plot/database/AbstractDB.java b/src/main/java/com/intellectualcrafters/plot/database/AbstractDB.java index c97c13b6d..b308d9afb 100644 --- a/src/main/java/com/intellectualcrafters/plot/database/AbstractDB.java +++ b/src/main/java/com/intellectualcrafters/plot/database/AbstractDB.java @@ -334,4 +334,6 @@ public interface AbstractDB { * Don't fuck with this one, unless you enjoy it rough */ boolean deleteTables(); + + void close(); } diff --git a/src/main/java/com/intellectualcrafters/plot/database/DBFunc.java b/src/main/java/com/intellectualcrafters/plot/database/DBFunc.java index c6f359b04..c10ddf72e 100644 --- a/src/main/java/com/intellectualcrafters/plot/database/DBFunc.java +++ b/src/main/java/com/intellectualcrafters/plot/database/DBFunc.java @@ -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(); + } } diff --git a/src/main/java/com/intellectualcrafters/plot/database/SQLManager.java b/src/main/java/com/intellectualcrafters/plot/database/SQLManager.java index adc91bc6e..02b0c8ae3 100644 --- a/src/main/java/com/intellectualcrafters/plot/database/SQLManager.java +++ b/src/main/java/com/intellectualcrafters/plot/database/SQLManager.java @@ -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 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> database = getPlots(); ArrayList 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) {} } } \ No newline at end of file diff --git a/src/main/java/com/intellectualcrafters/plot/flag/Flag.java b/src/main/java/com/intellectualcrafters/plot/flag/Flag.java index 7054d0e90..72c4ff094 100644 --- a/src/main/java/com/intellectualcrafters/plot/flag/Flag.java +++ b/src/main/java/com/intellectualcrafters/plot/flag/Flag.java @@ -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 + ")"); } } diff --git a/src/main/java/com/intellectualcrafters/plot/object/Plot.java b/src/main/java/com/intellectualcrafters/plot/object/Plot.java index d9e45ccbd..7df6940ae 100644 --- a/src/main/java/com/intellectualcrafters/plot/object/Plot.java +++ b/src/main/java/com/intellectualcrafters/plot/object/Plot.java @@ -163,6 +163,7 @@ public class Plot { } } this.timestamp = timestamp; + this.temp = temp; } /** diff --git a/src/main/java/com/intellectualcrafters/plot/util/MainUtil.java b/src/main/java/com/intellectualcrafters/plot/util/MainUtil.java index 4560621de..40dabb1f4 100644 --- a/src/main/java/com/intellectualcrafters/plot/util/MainUtil.java +++ b/src/main/java/com/intellectualcrafters/plot/util/MainUtil.java @@ -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 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> 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); } diff --git a/src/main/java/com/plotsquared/bukkit/BukkitMain.java b/src/main/java/com/plotsquared/bukkit/BukkitMain.java index 38debfae2..6a860eee6 100644 --- a/src/main/java/com/plotsquared/bukkit/BukkitMain.java +++ b/src/main/java/com/plotsquared/bukkit/BukkitMain.java @@ -126,8 +126,8 @@ public class BukkitMain extends JavaPlugin implements Listener, IPlotMain { @Override public void onDisable() { - Bukkit.getScheduler().cancelTasks(this); PS.get().disable(); + Bukkit.getScheduler().cancelTasks(this); THIS = null; } diff --git a/src/main/java/com/plotsquared/bukkit/object/entity/EntityWrapper.java b/src/main/java/com/plotsquared/bukkit/object/entity/EntityWrapper.java index b6a259824..413f02f4d 100644 --- a/src/main/java/com/plotsquared/bukkit/object/entity/EntityWrapper.java +++ b/src/main/java/com/plotsquared/bukkit/object/entity/EntityWrapper.java @@ -178,6 +178,8 @@ public class EntityWrapper { this.x = loc.getX(); this.y = loc.getY(); this.z = loc.getZ(); + System.out.print("ENTITY: " + entity.getType()); + System.out.print("ENTITY: " + entity.getType().getTypeId()); this.id = entity.getType().getTypeId(); if (depth == 0) { return; diff --git a/src/main/java/com/plotsquared/bukkit/util/BukkitChunkManager.java b/src/main/java/com/plotsquared/bukkit/util/BukkitChunkManager.java index 56f26f715..f25cfae5d 100644 --- a/src/main/java/com/plotsquared/bukkit/util/BukkitChunkManager.java +++ b/src/main/java/com/plotsquared/bukkit/util/BukkitChunkManager.java @@ -494,12 +494,14 @@ public class BukkitChunkManager extends ChunkManager { public static void restoreEntities(final World world, final int x_offset, final int z_offset) { for (final EntityWrapper entity : entities) { try { + System.out.print("RESTORING ENTITIE!: " + EntityType.fromId(entity.id)); entity.spawn(world, x_offset, z_offset); } catch (final Exception e) { PS.debug("Failed to restore entity (e): " + entity.x + "," + entity.y + "," + entity.z + " : " + entity.id + " : " + EntityType.fromId(entity.id)); e.printStackTrace(); } } + entities.clear(); } public static void restoreBlocks(final World world, final int x_offset, final int z_offset) { diff --git a/src/main/java/com/plotsquared/bukkit/util/SendChunk.java b/src/main/java/com/plotsquared/bukkit/util/SendChunk.java index 4e9b31ede..8487d6d07 100644 --- a/src/main/java/com/plotsquared/bukkit/util/SendChunk.java +++ b/src/main/java/com/plotsquared/bukkit/util/SendChunk.java @@ -12,6 +12,7 @@ import org.bukkit.World; import com.intellectualcrafters.plot.PS; import com.intellectualcrafters.plot.object.ChunkLoc; +import com.intellectualcrafters.plot.util.TaskManager; import com.intellectualcrafters.plot.util.ReflectionUtils.RefClass; import com.intellectualcrafters.plot.util.ReflectionUtils.RefConstructor; import com.intellectualcrafters.plot.util.ReflectionUtils.RefField; @@ -87,12 +88,20 @@ public class SendChunk { } } if (unload) { - try { - chunk.unload(true, true); - } - catch (Exception e) { - e.printStackTrace(); - } + TaskManager.runTask(new Runnable() { + @Override + public void run() { + try { + chunk.unload(true, true); + } + catch (Exception e) { + String worldname = chunk.getWorld().getName(); + PS.debug("$4Could not save chunk: " + worldname + ";" + chunk.getX() + ";" + chunk.getZ()); + PS.debug("$3 - $4File may be open in another process (e.g. MCEdit)"); + PS.debug("$3 - $4" + worldname + "/level.dat or " + worldname + "level_old.dat may be corrupt (try repairing or removing these)"); + } + } + }); } } diff --git a/src/main/java/com/plotsquared/sponge/SpongeMain.java b/src/main/java/com/plotsquared/sponge/SpongeMain.java index 63bbbbdd2..e43607a0b 100644 --- a/src/main/java/com/plotsquared/sponge/SpongeMain.java +++ b/src/main/java/com/plotsquared/sponge/SpongeMain.java @@ -21,7 +21,7 @@ import org.spongepowered.api.Server; import org.spongepowered.api.block.BlockState; import org.spongepowered.api.block.BlockType; import org.spongepowered.api.block.BlockTypes; -import org.spongepowered.api.data.manipulator.block.StoneData; +import org.spongepowered.api.data.manipulator.mutable.block.StoneData; import org.spongepowered.api.entity.player.Player; import org.spongepowered.api.event.Subscribe; import org.spongepowered.api.event.entity.player.PlayerChatEvent; @@ -261,7 +261,7 @@ public class SpongeMain implements IPlotMain, PluginContainer { case 0: { this.modify = new WorldModify(generator, false); game.getRegistry().registerWorldGeneratorModifier(modify); - Optional builder = game.getRegistry().getWorldBuilder() + Optional builder = game.getRegistry().createWorldBuilder() .name(world) .enabled(true) .loadsOnStartup(true) @@ -277,7 +277,7 @@ public class SpongeMain implements IPlotMain, PluginContainer { default: { this.modify = new WorldModify(generator, true); game.getRegistry().registerWorldGeneratorModifier(modify); - Optional builder = game.getRegistry().getWorldBuilder() + Optional builder = game.getRegistry().createWorldBuilder() .name(world) .enabled(true) .loadsOnStartup(true) @@ -356,17 +356,16 @@ public class SpongeMain implements IPlotMain, PluginContainer { for (int i = 0; i < data_lines.size(); i++) { String classname = packaze + data_lines.get(i).trim(); try { - Class clazz = Class.forName(classname); - fields = clazz.getDeclaredFields(); - for (Field field : fields) { - CatalogType type = (CatalogType) field.get(null); - String minecraft_id = type.getId(); - BlockState state = states.get(minecraft_id + ":" + 0); - if (state == null) { - continue; + Class clazz = Class.forName(classname); + fields = clazz.getDeclaredFields(); + for (Field field : fields) { + CatalogType type = (CatalogType) field.get(null); + String minecraft_id = type.getId(); + BlockState state = states.get(minecraft_id + ":" + 0); + if (state == null) { + continue; + } } - state.getManipulator(StoneData.class); - } } catch (Throwable e) {} } diff --git a/src/main/java/com/plotsquared/sponge/generator/AugmentedPopulator.java b/src/main/java/com/plotsquared/sponge/generator/AugmentedPopulator.java index 4ea3fe1f6..0278d5b26 100644 --- a/src/main/java/com/plotsquared/sponge/generator/AugmentedPopulator.java +++ b/src/main/java/com/plotsquared/sponge/generator/AugmentedPopulator.java @@ -8,9 +8,13 @@ import java.util.Random; import org.spongepowered.api.block.BlockState; import org.spongepowered.api.block.BlockType; +import org.spongepowered.api.util.DiscreteTransform3; import org.spongepowered.api.world.Chunk; import org.spongepowered.api.world.World; +import org.spongepowered.api.world.extent.ImmutableBlockVolume; import org.spongepowered.api.world.extent.MutableBlockVolume; +import org.spongepowered.api.world.extent.StorageType; +import org.spongepowered.api.world.extent.UnmodifiableBlockVolume; import org.spongepowered.api.world.gen.GeneratorPopulator; import org.spongepowered.api.world.gen.Populator; import org.spongepowered.api.world.gen.WorldGenerator; @@ -283,6 +287,48 @@ public class AugmentedPopulator implements Populator { public void setBlockType(Vector3i v, BlockType t) { setBlockType(v.getX(), v.getY(), v.getZ(), t); } + + @Override + public MutableBlockVolume getBlockCopy() { + // TODO Auto-generated method stub + return this; + } + + @Override + public MutableBlockVolume getBlockCopy(StorageType arg0) { + // TODO Auto-generated method stub + return this; + } + + @Override + public ImmutableBlockVolume getImmutableBlockCopy() { + // TODO Auto-generated method stub + return null; + } + + @Override + public UnmodifiableBlockVolume getUnmodifiableBlockView() { + // TODO Auto-generated method stub + return null; + } + + @Override + public MutableBlockVolume getBlockView(DiscreteTransform3 arg0) { + // TODO Auto-generated method stub + return this; + } + + @Override + public MutableBlockVolume getBlockView(Vector3i arg0, Vector3i arg1) { + // TODO Auto-generated method stub + return this; + } + + @Override + public MutableBlockVolume getRelativeBlockView() { + // TODO Auto-generated method stub + return this; + } }; this.populator.populate(world, blocks , null); } diff --git a/src/main/java/com/plotsquared/sponge/listener/MainListener.java b/src/main/java/com/plotsquared/sponge/listener/MainListener.java index 3110d95af..78182fe9a 100644 --- a/src/main/java/com/plotsquared/sponge/listener/MainListener.java +++ b/src/main/java/com/plotsquared/sponge/listener/MainListener.java @@ -27,7 +27,6 @@ import org.spongepowered.api.event.block.BlockMoveEvent; import org.spongepowered.api.event.block.BlockRedstoneUpdateEvent; import org.spongepowered.api.event.block.FloraGrowEvent; import org.spongepowered.api.event.entity.EntityChangeBlockEvent; -import org.spongepowered.api.event.entity.EntityExplosionEvent; import org.spongepowered.api.event.entity.EntitySpawnEvent; import org.spongepowered.api.event.entity.EntityTeleportEvent; import org.spongepowered.api.event.entity.player.PlayerBreakBlockEvent; @@ -41,11 +40,13 @@ import org.spongepowered.api.event.entity.player.PlayerQuitEvent; import org.spongepowered.api.event.message.CommandEvent; import org.spongepowered.api.event.network.PlayerConnectionEvent; import org.spongepowered.api.event.world.ChunkPreGenerateEvent; +import org.spongepowered.api.event.world.WorldOnExplosionEvent; import org.spongepowered.api.network.PlayerConnection; import org.spongepowered.api.text.Text; import org.spongepowered.api.text.Texts; import org.spongepowered.api.util.command.CommandSource; import org.spongepowered.api.world.World; +import org.spongepowered.api.world.explosion.Explosion; import org.spongepowered.api.world.extent.Extent; import com.flowpowered.math.vector.Vector3d; @@ -110,7 +111,8 @@ public class MainListener { } final Location loc = SpongeUtil.getLocation(event.getLocation()); final String world = loc.getWorld(); - if (!PS.get().isPlotWorld(world)) { + PlotWorld plotworld = PS.get().getPlotWorld(world); + if (plotworld == null) { return; } Plot plot = MainUtil.getPlot(loc); @@ -132,7 +134,9 @@ public class MainListener { return; } - event.setCancelled(true); + if (!plotworld.MOB_SPAWNING) { + event.setCancelled(true); + } } @Subscribe @@ -164,7 +168,7 @@ public class MainListener { @Subscribe public void onBlockMove(BlockMoveEvent event) { - org.spongepowered.api.world.Location block = event.getBlocks().get(0); + org.spongepowered.api.world.Location block = event.getLocations().get(0); Extent extent = block.getExtent(); if (extent instanceof World) { World world = (World) extent; @@ -172,7 +176,7 @@ public class MainListener { if (!PS.get().isPlotWorld(worldname)) { return; } - event.filter(new Predicate() { + event.filterLocations(new Predicate() { @Override public boolean apply(org.spongepowered.api.world.Location loc) { if (MainUtil.isPlotRoad(SpongeUtil.getLocation(worldname, loc))) { @@ -186,7 +190,7 @@ public class MainListener { @Subscribe public void onFloraGrow(FloraGrowEvent event) { - org.spongepowered.api.world.Location block = event.getBlock(); + org.spongepowered.api.world.Location block = event.getLocation(); Extent extent = block.getExtent(); if (extent instanceof World) { World world = (World) extent; @@ -258,16 +262,19 @@ public class MainListener { } @Subscribe - public void onBigBoom(final EntityExplosionEvent event) { - Location loc = SpongeUtil.getLocation(event.getExplosionLocation()); - final String world = loc.getWorld(); + public void onBigBoom(final WorldOnExplosionEvent event) { + World worldObj = event.getWorld(); + final String world = worldObj.getName(); if (!PS.get().isPlotWorld(world)) { return; } + Explosion explosion = event.getExplosion(); + Vector3d origin = explosion.getOrigin(); + Location loc = new Location(world, origin.getFloorX(), origin.getFloorY(), origin.getFloorZ()); final Plot plot = MainUtil.getPlot(loc); if ((plot != null) && plot.hasOwner()) { if (FlagManager.isPlotFlagTrue(plot, "explosion")) { - event.filter(new Predicate() { + event.filterLocations(new Predicate() { @Override public boolean apply(org.spongepowered.api.world.Location loc) { if (!plot.equals(MainUtil.getPlot(SpongeUtil.getLocation(loc)))) { @@ -276,14 +283,35 @@ public class MainListener { return true; } }); + event.filterEntities(new Predicate() { + @Override + public boolean apply(Entity entity) { + if (!plot.equals(MainUtil.getPlot(SpongeUtil.getLocation(entity)))) { + return false; + } + return true; + } + }); return; } } if (MainUtil.isPlotArea(loc)) { - event.setYield(0); + explosion.shouldBreakBlocks(false); + explosion.canCauseFire(false); + explosion.setRadius(0); + event.filterEntities(new Predicate() { + @Override + public boolean apply(Entity entity) { + if (!plot.equals(MainUtil.getPlot(SpongeUtil.getLocation(entity)))) { + return false; + } + return true; + } + }); + return; } else { if (FlagManager.isPlotFlagTrue(plot, "explosion")) { - event.filter(new Predicate() { + event.filterLocations(new Predicate() { @Override public boolean apply(org.spongepowered.api.world.Location loc) { if (!plot.equals(MainUtil.getPlot(SpongeUtil.getLocation(loc)))) { @@ -292,6 +320,15 @@ public class MainListener { return true; } }); + event.filterEntities(new Predicate() { + @Override + public boolean apply(Entity entity) { + if (!plot.equals(MainUtil.getPlot(SpongeUtil.getLocation(entity)))) { + return false; + } + return true; + } + }); return; } } @@ -317,7 +354,7 @@ public class MainListener { @Subscribe public void onRedstoneEvent(BlockRedstoneUpdateEvent event) { - org.spongepowered.api.world.Location block = event.getBlock(); + org.spongepowered.api.world.Location block = event.getLocation(); Location loc = SpongeUtil.getLocation(block); if (loc == null || !PS.get().isPlotWorld(loc.getWorld())) { return; @@ -356,11 +393,11 @@ public class MainListener { Player player = event.getEntity(); World world = player.getWorld(); String worldname = world.getName(); - org.spongepowered.api.world.Location blockLoc = event.getBlock(); - final Location loc = SpongeUtil.getLocation(worldname, event.getBlock()); + org.spongepowered.api.world.Location blockLoc = event.getLocation(); + final Location loc = SpongeUtil.getLocation(worldname, blockLoc); final Plot plot = MainUtil.getPlot(loc); if (plot != null) { - if (event.getBlock().getY() == 0) { + if (blockLoc.getY() == 0) { event.setCancelled(true); return; } @@ -403,11 +440,11 @@ public class MainListener { Player player = event.getEntity(); World world = player.getWorld(); String worldname = world.getName(); - org.spongepowered.api.world.Location blockLoc = event.getBlock(); - final Location loc = SpongeUtil.getLocation(worldname, event.getBlock()); + org.spongepowered.api.world.Location blockLoc = event.getLocation(); + final Location loc = SpongeUtil.getLocation(worldname, blockLoc); final Plot plot = MainUtil.getPlot(loc); if (plot != null) { - if (event.getBlock().getY() == 0) { + if (blockLoc.getY() == 0) { event.setCancelled(true); return; } @@ -450,11 +487,11 @@ public class MainListener { Player player = event.getEntity(); World world = player.getWorld(); String worldname = world.getName(); - org.spongepowered.api.world.Location blockLoc = event.getBlock(); - final Location loc = SpongeUtil.getLocation(worldname, event.getBlock()); + org.spongepowered.api.world.Location blockLoc = event.getLocation(); + final Location loc = SpongeUtil.getLocation(worldname, blockLoc); final Plot plot = MainUtil.getPlot(loc); if (plot != null) { - if (event.getBlock().getY() == 0) { + if (blockLoc.getY() == 0) { event.setCancelled(true); return; } diff --git a/src/main/java/com/plotsquared/sponge/object/SpongePlayer.java b/src/main/java/com/plotsquared/sponge/object/SpongePlayer.java index f951aa001..04ed348ec 100644 --- a/src/main/java/com/plotsquared/sponge/object/SpongePlayer.java +++ b/src/main/java/com/plotsquared/sponge/object/SpongePlayer.java @@ -1,8 +1,12 @@ package com.plotsquared.sponge.object; +import java.util.Date; import java.util.HashSet; import java.util.UUID; +import org.spongepowered.api.data.key.Keys; +import org.spongepowered.api.data.manipulator.mutable.entity.GameModeData; +import org.spongepowered.api.data.value.mutable.Value; import org.spongepowered.api.entity.player.Player; import org.spongepowered.api.entity.player.gamemode.GameMode; import org.spongepowered.api.entity.player.gamemode.GameModes; @@ -47,7 +51,11 @@ public class SpongePlayer extends PlotPlayer { @Override public long getPreviousLogin() { - return (long) (player.getJoinData().getLastPlayed().getSeconds()) * 1000; + Value data = player.getJoinData().lastPlayed(); + if (data.exists()) { + return last = data.get().getSeconds() * 1000; + } + return 0; } @Override @@ -168,7 +176,7 @@ public class SpongePlayer extends PlotPlayer { @Override public PlotGamemode getGamemode() { // TODO Auto-generated method stub - GameMode gamemode = player.getGameModeData().getValue(); + GameMode gamemode = player.getGameModeData().type().get(); if (gamemode == GameModes.ADVENTURE) { return PlotGamemode.ADVENTURE; } @@ -187,20 +195,20 @@ public class SpongePlayer extends PlotPlayer { @Override public void setGamemode(PlotGamemode gamemode) { // TODO Auto-generated method stub - switch (gamemode) { - case ADVENTURE: - player.getGameModeData().setGameMode(GameModes.ADVENTURE); - return; - case CREATIVE: - player.getGameModeData().setGameMode(GameModes.CREATIVE); - return; - case SPECTATOR: - player.getGameModeData().setGameMode(GameModes.SPECTATOR); - return; - case SURVIVAL: - player.getGameModeData().setGameMode(GameModes.SURVIVAL); - return; - } +// switch (gamemode) { +// case ADVENTURE: +// player.offer(Keys.GAME_MODE, GameModes.ADVENTURE); +// return; +// case CREATIVE: +// player.offer(Keys.GAME_MODE, GameModes.CREATIVE); +// return; +// case SPECTATOR: +// player.offer(Keys.GAME_MODE, GameModes.SPECTATOR); +// return; +// case SURVIVAL: +// player.offer(Keys.GAME_MODE, GameModes.SURVIVAL); +// return; +// } } @Override diff --git a/src/main/java/com/plotsquared/sponge/util/SpongeBlockManager.java b/src/main/java/com/plotsquared/sponge/util/SpongeBlockManager.java index f312ec3e4..dff62fdc6 100644 --- a/src/main/java/com/plotsquared/sponge/util/SpongeBlockManager.java +++ b/src/main/java/com/plotsquared/sponge/util/SpongeBlockManager.java @@ -1,17 +1,21 @@ package com.plotsquared.sponge.util; +import java.util.ArrayList; import java.util.List; import org.spongepowered.api.block.BlockState; import org.spongepowered.api.block.BlockType; import org.spongepowered.api.block.BlockTypes; import org.spongepowered.api.block.tileentity.Sign; -import org.spongepowered.api.data.manipulator.tileentity.SignData; +import org.spongepowered.api.block.tileentity.TileEntity; +import org.spongepowered.api.data.key.Keys; +import org.spongepowered.api.data.manipulator.mutable.tileentity.SignData; import org.spongepowered.api.text.Text; import org.spongepowered.api.world.World; import org.spongepowered.api.world.biome.BiomeType; import org.spongepowered.api.world.biome.BiomeTypes; +import com.google.common.base.Optional; import com.intellectualcrafters.plot.object.Location; import com.intellectualcrafters.plot.object.PlotBlock; import com.intellectualcrafters.plot.object.schematic.PlotItem; @@ -132,13 +136,22 @@ public class SpongeBlockManager extends BlockManager { @Override public String[] getSign(Location loc) { - BlockState block = SpongeUtil.getWorld(loc.getWorld()).getBlock(loc.getX(), loc.getY(), loc.getZ()); - if (!(block instanceof Sign)) { + World world = SpongeUtil.getWorld(loc.getWorld()); + Optional block = world.getTileEntity(loc.getX(), loc.getY(), loc.getZ()); + if (!block.isPresent()) { + return null; + } + TileEntity tile = block.get(); + if (!(tile instanceof Sign)) { + return null; + } + Sign sign = (Sign) tile; + Optional> optional = tile.get(Keys.SIGN_LINES); + if (!optional.isPresent()) { return null; } - Sign sign = (Sign) block; String[] result = new String[4]; - List lines = sign.getData().get().getLines(); + List lines = optional.get(); for (int i = 0; i < 4; i++) { result[i] = lines.get(i).toString(); } @@ -161,15 +174,20 @@ public class SpongeBlockManager extends BlockManager { public void functionSetSign(String worldname, int x, int y, int z, String[] lines) { World world = SpongeUtil.getWorld(worldname); world.setBlock(x, y, z, BlockTypes.WALL_SIGN.getDefaultState()); - BlockState block = world.getBlock(x, y, z); - if (!(block instanceof Sign)) { + Optional block = world.getTileEntity(x, y, z); + if (!block.isPresent()) { return; } - Sign sign = (Sign) block; - SignData data = sign.getData().get(); - for (int i = 0; i < 4; i++) { - data.setLine(i, SpongeMain.THIS.getText(lines[i])); + TileEntity tile = block.get(); + if (!(tile instanceof Sign)) { + return; } + Sign sign = (Sign) tile; + List text = new ArrayList<>(4); + for (int i = 0; i < 4; i++) { + text.add(SpongeMain.THIS.getText(lines[i])); + } + sign.offer(Keys.SIGN_LINES, text); } @Override diff --git a/src/main/java/com/plotsquared/sponge/util/SpongeInventoryUtil.java b/src/main/java/com/plotsquared/sponge/util/SpongeInventoryUtil.java index 3b7e823b1..c3f3b5cdd 100644 --- a/src/main/java/com/plotsquared/sponge/util/SpongeInventoryUtil.java +++ b/src/main/java/com/plotsquared/sponge/util/SpongeInventoryUtil.java @@ -27,7 +27,7 @@ public class SpongeInventoryUtil extends InventoryUtil { public ItemStackBuilder builder; public SpongeInventoryUtil() { - this.builder = SpongeMain.THIS.getGame().getRegistry().getItemBuilder(); + this.builder = SpongeMain.THIS.getGame().getRegistry().createItemBuilder(); } @Override diff --git a/src/main/java/com/plotsquared/sponge/util/SpongeMetrics.java b/src/main/java/com/plotsquared/sponge/util/SpongeMetrics.java index 2db10002b..b9b544a22 100644 --- a/src/main/java/com/plotsquared/sponge/util/SpongeMetrics.java +++ b/src/main/java/com/plotsquared/sponge/util/SpongeMetrics.java @@ -184,7 +184,7 @@ public class SpongeMetrics { } // Begin hitting the server with glorious data - TaskBuilder builder = game.getScheduler().getTaskBuilder(); + TaskBuilder builder = game.getScheduler().createTaskBuilder(); builder.async() .interval(TimeUnit.MINUTES.toMillis(PING_INTERVAL)) .execute(new Runnable() { diff --git a/src/main/java/com/plotsquared/sponge/util/SpongeTaskManager.java b/src/main/java/com/plotsquared/sponge/util/SpongeTaskManager.java index 1e4837554..dace73de7 100644 --- a/src/main/java/com/plotsquared/sponge/util/SpongeTaskManager.java +++ b/src/main/java/com/plotsquared/sponge/util/SpongeTaskManager.java @@ -18,7 +18,7 @@ public class SpongeTaskManager extends TaskManager { @Override public int taskRepeat(Runnable r, int interval) { int val = i.incrementAndGet(); - TaskBuilder builder = SpongeMain.THIS.getGame().getScheduler().getTaskBuilder(); + TaskBuilder builder = SpongeMain.THIS.getGame().getScheduler().createTaskBuilder(); TaskBuilder built = builder.delay(interval).interval(interval).execute(r); Task task = built.submit(SpongeMain.THIS.getPlugin()); tasks.put(val, task); @@ -28,7 +28,7 @@ public class SpongeTaskManager extends TaskManager { @Override public int taskRepeatAsync(Runnable r, int interval) { int val = i.incrementAndGet(); - TaskBuilder builder = SpongeMain.THIS.getGame().getScheduler().getTaskBuilder(); + TaskBuilder builder = SpongeMain.THIS.getGame().getScheduler().createTaskBuilder(); TaskBuilder built = builder.delay(interval).async().interval(interval).execute(r); Task task = built.submit(SpongeMain.THIS.getPlugin()); tasks.put(val, task); @@ -37,25 +37,25 @@ public class SpongeTaskManager extends TaskManager { @Override public void taskAsync(Runnable r) { - TaskBuilder builder = SpongeMain.THIS.getGame().getScheduler().getTaskBuilder(); + TaskBuilder builder = SpongeMain.THIS.getGame().getScheduler().createTaskBuilder(); builder.async().execute(r).submit(SpongeMain.THIS.getPlugin()); } @Override public void task(Runnable r) { - TaskBuilder builder = SpongeMain.THIS.getGame().getScheduler().getTaskBuilder(); + TaskBuilder builder = SpongeMain.THIS.getGame().getScheduler().createTaskBuilder(); builder.execute(r).submit(SpongeMain.THIS.getPlugin()); } @Override public void taskLater(Runnable r, int delay) { - TaskBuilder builder = SpongeMain.THIS.getGame().getScheduler().getTaskBuilder(); + TaskBuilder builder = SpongeMain.THIS.getGame().getScheduler().createTaskBuilder(); builder.delay(delay).execute(r).submit(SpongeMain.THIS.getPlugin()); } @Override public void taskLaterAsync(Runnable r, int delay) { - TaskBuilder builder = SpongeMain.THIS.getGame().getScheduler().getTaskBuilder(); + TaskBuilder builder = SpongeMain.THIS.getGame().getScheduler().createTaskBuilder(); builder.async().delay(delay).execute(r).submit(SpongeMain.THIS.getPlugin()); }