From 9a91fb896b57a46672be51cf81ffc219da69dd8a Mon Sep 17 00:00:00 2001 From: LordTuxn Date: Wed, 5 Jan 2022 01:31:37 +0100 Subject: [PATCH 01/21] Implement IPlotWorld interface and PlotWorld class --- .../core/system/plot/world/IPlotWorld.java | 63 +++++++++++++ .../core/system/plot/world/PlotWorld.java | 91 +++++++++++++++++++ 2 files changed, 154 insertions(+) create mode 100644 src/main/java/com/alpsbte/plotsystem/core/system/plot/world/IPlotWorld.java create mode 100644 src/main/java/com/alpsbte/plotsystem/core/system/plot/world/PlotWorld.java diff --git a/src/main/java/com/alpsbte/plotsystem/core/system/plot/world/IPlotWorld.java b/src/main/java/com/alpsbte/plotsystem/core/system/plot/world/IPlotWorld.java new file mode 100644 index 00000000..789ecebb --- /dev/null +++ b/src/main/java/com/alpsbte/plotsystem/core/system/plot/world/IPlotWorld.java @@ -0,0 +1,63 @@ +package com.alpsbte.plotsystem.core.system.plot.world; + +import com.alpsbte.plotsystem.core.system.Builder; +import com.alpsbte.plotsystem.core.system.plot.generator.AbstractPlotGenerator; +import com.sk89q.worldguard.bukkit.RegionContainer; +import org.bukkit.World; + +public interface IPlotWorld { + /** + * Generates a new plot world with the required settings and plot schematic + * @param plotOwner - plow owner of the plot + * @return - true if world was generated successfully + */ + boolean generate(Builder plotOwner, Class generator); + + /** + * Deletes the world file and entry in the config file + * @return - true if world was deleted successfully + */ + boolean delete(); + + /** + * Loads the plot world to memory to be used. Plot has to be generated. + * @return - true if world was loaded successfully + */ + boolean load(); + + /** + * Unloads the plot world from memory. Plot cannot be used anymore. Plot has to be generated. + * @param movePlayers - if true, players will get teleported to the spawn location. Otherwise, plot will not get unloaded. + * @return - true if world was loaded successfully + */ + boolean unload(boolean movePlayers); + + /** + * @return - Bukkit plot world + */ + World getBukkit(); + + /** + * Returns plot world name in the format (for example: P-23) + * @return - world name of the plot + */ + String getName(); + + /** + * Loads the protected plot world region from WorldGuard config + * @return - protected WorldGuard region + */ + RegionContainer getProtectedRegion(); + + /** + * Checks if the plot world is loaded to memory. If the plot world has not yet been generated, it will return false. + * @return - true if world is loaded + */ + boolean isLoaded(); + + /** + * Checks if the plot world is generated. + * @return - true if world is generated + */ + boolean isGenerated(); +} diff --git a/src/main/java/com/alpsbte/plotsystem/core/system/plot/world/PlotWorld.java b/src/main/java/com/alpsbte/plotsystem/core/system/plot/world/PlotWorld.java new file mode 100644 index 00000000..4fc938fe --- /dev/null +++ b/src/main/java/com/alpsbte/plotsystem/core/system/plot/world/PlotWorld.java @@ -0,0 +1,91 @@ +package com.alpsbte.plotsystem.core.system.plot.world; + +import com.alpsbte.plotsystem.PlotSystem; +import com.alpsbte.plotsystem.core.system.Builder; +import com.alpsbte.plotsystem.core.system.plot.Plot; +import com.alpsbte.plotsystem.core.system.plot.generator.AbstractPlotGenerator; +import com.alpsbte.plotsystem.core.system.plot.generator.DefaultPlotGenerator; +import com.alpsbte.plotsystem.core.system.plot.generator.RawPlotGenerator; +import com.alpsbte.plotsystem.utils.Utils; +import com.sk89q.worldguard.bukkit.RegionContainer; +import org.bukkit.Bukkit; +import org.bukkit.World; +import org.bukkit.entity.Player; + +public class PlotWorld implements IPlotWorld { + + private final Plot plot; + + public PlotWorld(Plot plot) { + this.plot = plot; + } + + @Override + public boolean generate(Builder plotOwner, Class generator) { + if (!isGenerated()) { + if (generator.isInstance(DefaultPlotGenerator.class)) { + new DefaultPlotGenerator(plot, plotOwner); + } else if (generator.isInstance(RawPlotGenerator.class)) { + new RawPlotGenerator(plot, plotOwner); + } else return false; + return true; + } + return false; + } + + @Override + public boolean delete() { + if (isGenerated() && unload(true)) + return PlotSystem.DependencyManager.getMultiverseCore().getMVWorldManager().deleteWorld(getName(), true, true) && + PlotSystem.DependencyManager.getMultiverseCore().saveWorldConfig(); + return false; + } + + @Override + public boolean load() { + if (isGenerated()) + return PlotSystem.DependencyManager.getMultiverseCore().getMVWorldManager().loadWorld(getName()) || isLoaded(); + return false; + } + + @Override + public boolean unload(boolean movePlayers) { + if(isLoaded()) { + if (movePlayers && !getBukkit().getPlayers().isEmpty()) { + for (Player player : getBukkit().getPlayers()) { + player.teleport(Utils.getSpawnLocation()); + } + } else if (!movePlayers && getBukkit().getPlayers().isEmpty()) return false; + + Bukkit.getScheduler().scheduleSyncDelayedTask(PlotSystem.getPlugin(), (() -> + Bukkit.unloadWorld(getBukkit(), true)), 60L); + return !isLoaded(); + } + return false; + } + + @Override + public World getBukkit() { + return Bukkit.getWorld(getName()); + } + + @Override + public String getName() { + return "P-" + plot.getID(); + } + + @Override + public RegionContainer getProtectedRegion() { + return null; + } + + @Override + public boolean isLoaded() { + return getBukkit() == null; + } + + @Override + public boolean isGenerated() { + return PlotSystem.DependencyManager.getMultiverseCore().getMVWorldManager().isMVWorld(getName()); + } +} From ac5dff9a087ac9e206754ab78c8f55e1948bdfdb Mon Sep 17 00:00:00 2001 From: LordTuxn Date: Wed, 5 Jan 2022 01:32:11 +0100 Subject: [PATCH 02/21] Start fixing errors and refactoring --- .../plotsystem/core/system/plot/Plot.java | 10 ++++--- .../core/system/plot/PlotHandler.java | 4 ++- .../plot/generator/AbstractPlotGenerator.java | 30 +++++++++---------- 3 files changed, 24 insertions(+), 20 deletions(-) diff --git a/src/main/java/com/alpsbte/plotsystem/core/system/plot/Plot.java b/src/main/java/com/alpsbte/plotsystem/core/system/plot/Plot.java index 7142e2bd..4ddeea62 100644 --- a/src/main/java/com/alpsbte/plotsystem/core/system/plot/Plot.java +++ b/src/main/java/com/alpsbte/plotsystem/core/system/plot/Plot.java @@ -26,6 +26,7 @@ import com.alpsbte.plotsystem.core.system.CityProject; import com.alpsbte.plotsystem.core.system.Review; +import com.alpsbte.plotsystem.core.system.plot.world.PlotWorld; import com.alpsbte.plotsystem.utils.conversion.CoordinateConversion; import com.sk89q.worldedit.Vector; import com.alpsbte.plotsystem.core.database.DatabaseConnection; @@ -53,10 +54,12 @@ public class Plot extends PlotPermissions { private final int ID; + private final PlotWorld plotWorld; public Plot(int ID) throws SQLException { super(ID); this.ID = ID; + this.plotWorld = new PlotWorld(this); } public int getID() { @@ -273,11 +276,10 @@ public String getGoogleEarthLink() throws SQLException { return "https://earth.google.com/web/@" + getGeoCoordinatesNumeric() + ",0a,1000d,20y,-0h,0t,0r"; } - public String getWorldName() { - return "P-" + getID(); - } - public World getPlotWorld() { return Bukkit.getWorld(getWorldName()); } + public PlotWorld getPlotWorld() { + return plotWorld; + } public void setPlotOwner(String UUID) throws SQLException { if (UUID == null) { diff --git a/src/main/java/com/alpsbte/plotsystem/core/system/plot/PlotHandler.java b/src/main/java/com/alpsbte/plotsystem/core/system/plot/PlotHandler.java index a034a418..b4f97604 100644 --- a/src/main/java/com/alpsbte/plotsystem/core/system/plot/PlotHandler.java +++ b/src/main/java/com/alpsbte/plotsystem/core/system/plot/PlotHandler.java @@ -30,6 +30,8 @@ import com.alpsbte.plotsystem.core.database.DatabaseConnection; import com.alpsbte.plotsystem.core.menus.CompanionMenu; import com.alpsbte.plotsystem.core.system.Server; +import com.alpsbte.plotsystem.core.system.plot.generator.DefaultPlotGenerator; +import com.alpsbte.plotsystem.core.system.plot.world.PlotWorld; import com.alpsbte.plotsystem.utils.Utils; import com.alpsbte.plotsystem.utils.enums.Status; import com.alpsbte.plotsystem.utils.ftp.FTPManager; @@ -55,7 +57,7 @@ public static void teleportPlayer(Plot plot, Player player) throws SQLException loadPlot(plot); player.teleport(getPlotSpawnPoint(plot)); - + new PlotWorld(plot).generate(plot.getPlotOwner(), DefaultPlotGenerator.class); player.playSound(player.getLocation(), Utils.TeleportSound, 1, 1); player.setAllowFlight(true); player.setFlying(true); diff --git a/src/main/java/com/alpsbte/plotsystem/core/system/plot/generator/AbstractPlotGenerator.java b/src/main/java/com/alpsbte/plotsystem/core/system/plot/generator/AbstractPlotGenerator.java index 6b5cbf4c..879b272d 100644 --- a/src/main/java/com/alpsbte/plotsystem/core/system/plot/generator/AbstractPlotGenerator.java +++ b/src/main/java/com/alpsbte/plotsystem/core/system/plot/generator/AbstractPlotGenerator.java @@ -91,7 +91,7 @@ public AbstractPlotGenerator(@NotNull Plot plot, @NotNull Builder builder) { generateWorld(); generateOutlines(plot.getOutlinesSchematic()); createMultiverseWorld(); - configureWorld(worldManager.getMVWorld(plot.getPlotWorld())); + configureWorld(worldManager.getMVWorld(plot.getPlotWorld().getBukkit())); createProtection(); } catch (Exception ex) { exception = ex; @@ -104,7 +104,7 @@ public AbstractPlotGenerator(@NotNull Plot plot, @NotNull Builder builder) { } if (exception != null) { - if (worldManager.isMVWorld(plot.getWorldName())) PlotHandler.abandonPlot(plot); + if (worldManager.isMVWorld(plot.getPlotWorld().getName())) PlotHandler.abandonPlot(plot); onException(exception); } }); @@ -123,7 +123,7 @@ public AbstractPlotGenerator(@NotNull Plot plot, @NotNull Builder builder) { protected void generateWorld() { if (PlotManager.plotExists(plot.getID())) PlotHandler.abandonPlot(plot); - worldCreator = new WorldCreator(plot.getWorldName()); + worldCreator = new WorldCreator(plot.getPlotWorld().getName()); worldCreator.environment(org.bukkit.World.Environment.NORMAL); worldCreator.type(WorldType.FLAT); worldCreator.generatorSettings("2;0;1;"); @@ -136,7 +136,7 @@ protected void generateWorld() { protected void createMultiverseWorld() { // Check if world creator is configured and add new world to multiverse world manager if (worldCreator != null) { - worldManager.addWorld(plot.getWorldName(), worldCreator.environment(), null, worldCreator.type(), false, + worldManager.addWorld(plot.getPlotWorld().getName(), worldCreator.environment(), null, worldCreator.type(), false, "VoidGen:{\"caves\":false,\"decoration\":false,\"mobs\":false,\"structures\":false}"); } else { throw new RuntimeException("World Creator is not configured"); @@ -152,7 +152,7 @@ protected void generateOutlines(File plotSchematic) { if (plotSchematic != null) { Vector buildingOutlinesCoordinates = PlotManager.getPlotCenter(); - com.sk89q.worldedit.world.World weWorld = new BukkitWorld(plot.getPlotWorld()); + com.sk89q.worldedit.world.World weWorld = new BukkitWorld(plot.getPlotWorld().getBukkit()); Clipboard clipboard = ClipboardFormat.SCHEMATIC.getReader(new FileInputStream(plotSchematic)).read(weWorld.getWorldData()); // Place the bottom part of the schematic 5 blocks above 0 @@ -166,7 +166,7 @@ protected void generateOutlines(File plotSchematic) { Operations.complete(operation); editSession.flushQueue(); - plot.getPlotWorld().setSpawnLocation(PlotHandler.getPlotSpawnPoint(plot)); + plot.getPlotWorld().getBukkit().setSpawnLocation(PlotHandler.getPlotSpawnPoint(plot)); } } catch (IOException | WorldEditException ex) { Bukkit.getLogger().log(Level.SEVERE, "An error occurred while generating plot outlines!", ex); @@ -180,15 +180,15 @@ protected void generateOutlines(File plotSchematic) { */ protected void configureWorld(@NotNull MultiverseWorld mvWorld) { // Set Bukkit world game rules - plot.getPlotWorld().setGameRuleValue("randomTickSpeed", "0"); - plot.getPlotWorld().setGameRuleValue("doDaylightCycle", "false"); - plot.getPlotWorld().setGameRuleValue("doFireTick", "false"); - plot.getPlotWorld().setGameRuleValue("doWeatherCycle", "false"); - plot.getPlotWorld().setGameRuleValue("keepInventory", "true"); - plot.getPlotWorld().setGameRuleValue("announceAdvancements", "false"); + plot.getPlotWorld().getBukkit().setGameRuleValue("randomTickSpeed", "0"); + plot.getPlotWorld().getBukkit().setGameRuleValue("doDaylightCycle", "false"); + plot.getPlotWorld().getBukkit().setGameRuleValue("doFireTick", "false"); + plot.getPlotWorld().getBukkit().setGameRuleValue("doWeatherCycle", "false"); + plot.getPlotWorld().getBukkit().setGameRuleValue("keepInventory", "true"); + plot.getPlotWorld().getBukkit().setGameRuleValue("announceAdvancements", "false"); // Set world time to midday - plot.getPlotWorld().setTime(6000); + plot.getPlotWorld().getBukkit().setTime(6000); // Configure multiverse world mvWorld.setAllowFlight(true); @@ -210,7 +210,7 @@ protected void createProtection() { BlockVector max = BlockVector.toBlockPoint(PlotManager.PLOT_SIZE, 256, PlotManager.PLOT_SIZE); RegionContainer container = PlotSystem.DependencyManager.getWorldGuard().getRegionContainer(); - RegionManager regionManager = container.get(plot.getPlotWorld()); + RegionManager regionManager = container.get(plot.getPlotWorld().getBukkit()); // Create protected region for world GlobalProtectedRegion globalRegion = new GlobalProtectedRegion("__global__"); @@ -218,7 +218,7 @@ protected void createProtection() { globalRegion.setFlag(DefaultFlag.ENTRY.getRegionGroupFlag(), RegionGroup.ALL); // Create protected region for plot - ProtectedRegion protectedPlotRegion = new ProtectedCuboidRegion(plot.getWorldName(), min, max); + ProtectedRegion protectedPlotRegion = new ProtectedCuboidRegion(plot.getPlotWorld().getName(), min, max); protectedPlotRegion.setPriority(100); // Add and save regions From 4bd64141f48d432ff2d4f31156d7a9a0f581ab78 Mon Sep 17 00:00:00 2001 From: LordTuxn Date: Thu, 6 Jan 2022 01:33:03 +0100 Subject: [PATCH 03/21] Rework plot class and implement IPlot interface --- .../commands/review/CMD_EditPlot.java | 6 +- .../plotsystem/core/menus/ReviewPlotMenu.java | 2 +- .../plotsystem/core/system/Review.java | 2 +- .../plotsystem/core/system/plot/IPlot.java | 180 +++++++++++++ .../plotsystem/core/system/plot/Plot.java | 247 ++++++++++-------- .../core/system/plot/PlotHandler.java | 8 +- .../core/system/plot/PlotPermissions.java | 27 +- 7 files changed, 337 insertions(+), 135 deletions(-) create mode 100644 src/main/java/com/alpsbte/plotsystem/core/system/plot/IPlot.java diff --git a/src/main/java/com/alpsbte/plotsystem/commands/review/CMD_EditPlot.java b/src/main/java/com/alpsbte/plotsystem/commands/review/CMD_EditPlot.java index b5f6b5df..9c1b420c 100644 --- a/src/main/java/com/alpsbte/plotsystem/commands/review/CMD_EditPlot.java +++ b/src/main/java/com/alpsbte/plotsystem/commands/review/CMD_EditPlot.java @@ -60,11 +60,11 @@ public boolean onCommand(CommandSender sender, Command cmd, String s, String[] a return true; } - if(plot.hasReviewerPerms()) { - plot.removeReviewerPerms().save(); + if(plot.getPermissions().hasReviewerPerms()) { + plot.getPermissions().removeReviewerPerms().save(); sender.sendMessage(Utils.getInfoMessageFormat("§6Disabled §abuild permissions for Reviewers on Plot §6#" + plot.getID())); } else { - plot.addReviewerPerms().save(); + plot.getPermissions().addReviewerPerms().save(); sender.sendMessage(Utils.getInfoMessageFormat("§6Enabled §abuild permissions for Reviewers on Plot §6#" + plot.getID())); } } catch (SQLException ex) { diff --git a/src/main/java/com/alpsbte/plotsystem/core/menus/ReviewPlotMenu.java b/src/main/java/com/alpsbte/plotsystem/core/menus/ReviewPlotMenu.java index 17514504..dfb7726b 100644 --- a/src/main/java/com/alpsbte/plotsystem/core/menus/ReviewPlotMenu.java +++ b/src/main/java/com/alpsbte/plotsystem/core/menus/ReviewPlotMenu.java @@ -292,7 +292,7 @@ protected void setItemClickEventsAsync() { double totalRatingWithMultiplier = totalRating * PlotManager.getMultiplierByDifficulty(plot.getDifficulty()); totalRating = (int) Math.floor(totalRatingWithMultiplier); - plot.setScore(totalRating); + plot.setTotalScore(totalRating); String reviewerConfirmationMessage; clickPlayer.closeInventory(); diff --git a/src/main/java/com/alpsbte/plotsystem/core/system/Review.java b/src/main/java/com/alpsbte/plotsystem/core/system/Review.java index af288c24..3dd02fcc 100644 --- a/src/main/java/com/alpsbte/plotsystem/core/system/Review.java +++ b/src/main/java/com/alpsbte/plotsystem/core/system/Review.java @@ -208,7 +208,7 @@ public static void undoReview(Review review) throws SQLException { plot.getPlotOwner().addScore(-plot.getSharedScore()); plot.getPlotOwner().addCompletedBuild(-1); - plot.setScore(-1); + plot.setTotalScore(-1); plot.setStatus(Status.unreviewed); plot.setPasted(false); diff --git a/src/main/java/com/alpsbte/plotsystem/core/system/plot/IPlot.java b/src/main/java/com/alpsbte/plotsystem/core/system/plot/IPlot.java new file mode 100644 index 00000000..05aab5ab --- /dev/null +++ b/src/main/java/com/alpsbte/plotsystem/core/system/plot/IPlot.java @@ -0,0 +1,180 @@ +package com.alpsbte.plotsystem.core.system.plot; + +import com.alpsbte.plotsystem.core.system.Builder; +import com.alpsbte.plotsystem.core.system.CityProject; +import com.alpsbte.plotsystem.core.system.Review; +import com.alpsbte.plotsystem.core.system.plot.world.PlotWorld; +import com.alpsbte.plotsystem.utils.enums.PlotDifficulty; +import com.alpsbte.plotsystem.utils.enums.Slot; +import com.alpsbte.plotsystem.utils.enums.Status; +import com.sk89q.worldedit.Vector; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import java.io.File; +import java.sql.SQLException; +import java.util.Date; +import java.util.List; + +public interface IPlot { + /** + * @return plot id + */ + int getID(); + + /** + * @return city project + * @throws SQLException SQL database exception + */ + CityProject getCity() throws SQLException; + + /** + * @return plot difficulty + * @throws SQLException SQL database exception + */ + PlotDifficulty getDifficulty() throws SQLException; + + /** + * @return builder who has claimed the plot + * @throws SQLException SQL database exception + */ + Builder getPlotOwner() throws SQLException; + + /** + * Sets the given builder to the plot owner + * @param UUID UUID of the plot owner, if null remove plot owner + * @throws SQLException SQL database exception + */ + void setPlotOwner(@Nullable String UUID) throws SQLException; + + /** + * @return builders who have been added by the owner as plot members + * @throws SQLException SQL database exception + */ + List getPlotMembers() throws SQLException; + + /** + * Sets the given builders to plot members on the plot + * @param plotMembers plot members, if empty no plot members will get assigned + * @throws SQLException SQL database exception + */ + void setPlotMembers(@NotNull List plotMembers) throws SQLException; + + /** + * @return plot review of the completed plot + * @throws SQLException SQL database exception + */ + Review getReview() throws SQLException; + + /** + * @return plot world, can be null if it has not yet been generated + */ + PlotWorld getWorld(); + + /** + * @return plot permission manager to add or remove build rights + */ + PlotPermissions getPermissions(); + + /** + * @return total points given for the plot + * @throws SQLException SQL database exception + */ + int getTotalScore() throws SQLException; + + /** + * When building with plot members, the total score is shared + * @return shared points + * @throws SQLException SQL database exception + */ + int getSharedScore() throws SQLException; + + /** + * Sets the given total score of the plot + * @param score given points after the review, if -1 set score to null + * @throws SQLException SQL database exception + */ + void setTotalScore(int score) throws SQLException; + + /** + * @return current status of the plot + * @throws SQLException SQL database exception + */ + Status getStatus() throws SQLException; + + /** + * Sets the given status of the plot + * @param status current status + * @throws SQLException SQL database exception + */ + void setStatus(@NotNull Status status) throws SQLException; + + /** + * @return last date on which the plot owner teleported to the plot + * @throws SQLException SQL database exception + */ + Date getLastActivity() throws SQLException; + + /** + * Sets the last activity to the current date and time + * @param setNull if true, set last activity to null + * @throws SQLException SQL database exception + */ + void setLastActivity(boolean setNull) throws SQLException; + + /** + * @return date when the plot was created on the Terra121 server + * @throws SQLException SQL database exception + */ + Date getCreateDate() throws SQLException; + + /** + * @return builder who created the plot on the Terra121 server + * @throws SQLException SQL database exception + */ + Builder getPlotCreator() throws SQLException; + + /** + * @return get the plot slot of the plot owner + * @throws SQLException SQL database exception + */ + Slot getSlot() throws SQLException; + + /** + * @return schematic file with outlines only + */ + File getOutlinesSchematic(); + + /** + * @return schematic file of the completed plot + */ + File getFinishedSchematic(); + + /** + * Returns geographic coordinates in numeric format + * @return WG84 EPSG:4979 coordinates as double array {lon,lat} in degrees + * @see com.alpsbte.plotsystem.utils.conversion.CoordinateConversion#convertToGeo(double, double) + * @throws SQLException SQL database exception + */ + String getGeoCoordinates() throws SQLException; + + /** + * Returns in-game Minecraft coordinates on a Terra121 world + * @return the in-game coordinates (x, z) + * @see com.alpsbte.plotsystem.utils.conversion.CoordinateConversion#convertFromGeo(double, double) + * @throws SQLException SQL database exception + */ + Vector getMinecraftCoordinates() throws SQLException; + + /** + * @param pasted if true, plot has been pasted on the Terra121 server + * @throws SQLException SQL database exception + */ + void setPasted(boolean pasted) throws SQLException; + + /** + * @return if {@link #getReview()} is null, it will return false + * @throws SQLException SQL database exception + */ + boolean isReviewed() throws SQLException; +} diff --git a/src/main/java/com/alpsbte/plotsystem/core/system/plot/Plot.java b/src/main/java/com/alpsbte/plotsystem/core/system/plot/Plot.java index 4ddeea62..8b899d17 100644 --- a/src/main/java/com/alpsbte/plotsystem/core/system/plot/Plot.java +++ b/src/main/java/com/alpsbte/plotsystem/core/system/plot/Plot.java @@ -37,7 +37,7 @@ import com.alpsbte.plotsystem.utils.enums.Status; import com.alpsbte.plotsystem.utils.ftp.FTPManager; import org.bukkit.Bukkit; -import org.bukkit.World; +import org.jetbrains.annotations.NotNull; import java.io.File; import java.net.URISyntaxException; @@ -51,21 +51,22 @@ import java.util.logging.Level; import java.util.stream.Collectors; -public class Plot extends PlotPermissions { - +public class Plot implements IPlot { private final int ID; - private final PlotWorld plotWorld; + + private PlotWorld plotWorld; + private PlotPermissions plotPermissions; public Plot(int ID) throws SQLException { - super(ID); this.ID = ID; - this.plotWorld = new PlotWorld(this); } + @Override public int getID() { return ID; } + @Override public CityProject getCity() throws SQLException { try (ResultSet rs = DatabaseConnection.createStatement("SELECT city_project_id FROM plotsystem_plots WHERE id = ?") .setValue(this.ID).executeQuery()) { @@ -75,6 +76,7 @@ public CityProject getCity() throws SQLException { } } + @Override public PlotDifficulty getDifficulty() throws SQLException { try (ResultSet rs = DatabaseConnection.createStatement("SELECT difficulty_id FROM plotsystem_plots WHERE id = ?") .setValue(this.ID).executeQuery()) { @@ -84,37 +86,7 @@ public PlotDifficulty getDifficulty() throws SQLException { } } - public File getOutlinesSchematic() { - try { - return CompletableFuture.supplyAsync(() -> { - try { - File file = Paths.get(PlotManager.getDefaultSchematicPath(), String.valueOf(getCity().getCountry().getServer().getID()), String.valueOf(getCity().getID()), getID() + ".schematic").toFile(); - - if(!file.exists()) { - if (getCity().getCountry().getServer().getFTPConfiguration() != null) { - FTPManager.downloadSchematic(FTPManager.getFTPUrl(getCity().getCountry().getServer(), getCity().getID()), file); - } - } - return file; - } catch (SQLException | URISyntaxException ex) { - Bukkit.getLogger().log(Level.SEVERE, "A SQL error occurred!", ex); - } - return null; - }).get(); - } catch (InterruptedException | ExecutionException ex) { - return null; - } - } - - public File getFinishedSchematic() { - try { - return Paths.get(PlotManager.getDefaultSchematicPath(), String.valueOf(getCity().getCountry().getServer().getID()), "finishedSchematics", String.valueOf(getCity().getID()), getID() + ".schematic").toFile(); - } catch (SQLException ex) { - Bukkit.getLogger().log(Level.SEVERE, "A SQL error occurred!", ex); - } - return null; - } - + @Override public Builder getPlotOwner() throws SQLException { if(getStatus() != Status.unclaimed) { try (ResultSet rs = DatabaseConnection.createStatement("SELECT owner_uuid FROM plotsystem_plots WHERE id = ?") @@ -126,6 +98,18 @@ public Builder getPlotOwner() throws SQLException { return null; } + @Override + public void setPlotOwner(String UUID) throws SQLException { + if (UUID == null) { + DatabaseConnection.createStatement("UPDATE plotsystem_plots SET owner_uuid = DEFAULT(owner_uuid) WHERE id = ?") + .setValue(this.ID).executeUpdate(); + } else { + DatabaseConnection.createStatement("UPDATE plotsystem_plots SET owner_uuid = ? WHERE id = ?") + .setValue(UUID).setValue(this.ID).executeUpdate(); + } + } + + @Override public List getPlotMembers() throws SQLException { List builders = new ArrayList<>(); @@ -146,6 +130,22 @@ public List getPlotMembers() throws SQLException { return builders; } + @Override + public void setPlotMembers(@NotNull List plotMembers) throws SQLException { + // Convert plot member list to string + String plotMemberAsString = plotMembers.stream().map(member -> member.getUUID().toString()).collect(Collectors.joining(",")); + Bukkit.getLogger().log(Level.SEVERE,plotMemberAsString); + + if(!plotMembers.isEmpty()) { + DatabaseConnection.createStatement("UPDATE plotsystem_plots SET member_uuids = ? WHERE id = ?") + .setValue(plotMemberAsString).setValue(this.ID).executeUpdate(); + } else { + DatabaseConnection.createStatement("UPDATE plotsystem_plots SET member_uuids = DEFAULT(member_uuids) WHERE id = ?") + .setValue(this.ID).executeUpdate(); + } + } + + @Override public Review getReview() throws SQLException { if(getStatus() == Status.completed || isRejected()) { try (ResultSet rs = DatabaseConnection.createStatement("SELECT review_id FROM plotsystem_plots WHERE id = ?") @@ -159,29 +159,19 @@ public Review getReview() throws SQLException { return null; } - public String getGeoCoordinatesNumeric() throws SQLException { - // Convert MC coordinates to geo coordinates - Vector mcCoordinates = getMinecraftCoordinates(); - try { - return CoordinateConversion.formatGeoCoordinatesNumeric(CoordinateConversion.convertToGeo(mcCoordinates.getX(), mcCoordinates.getZ())); - } catch (OutOfProjectionBoundsException ex) { - Bukkit.getLogger().log(Level.SEVERE, "Could not convert MC coordinates to geo coordinates!", ex); - } - return null; + @Override + public PlotWorld getWorld() { + if (plotWorld == null) plotWorld = new PlotWorld(this); + return plotWorld; } - public Vector getMinecraftCoordinates() throws SQLException { - try (ResultSet rs = DatabaseConnection.createStatement("SELECT mc_coordinates FROM plotsystem_plots WHERE id = ?") - .setValue(this.ID).executeQuery()) { - - if (rs.next()) { - String[] mcLocation = rs.getString(1).split(","); - return new Vector(Double.parseDouble(mcLocation[0]), Double.parseDouble(mcLocation[1]), Double.parseDouble(mcLocation[2])); - } - return null; - } + @Override + public PlotPermissions getPermissions() { + if (plotPermissions == null) plotPermissions = new PlotPermissions(getWorld()); + return plotPermissions; } + @Override public int getTotalScore() throws SQLException { try (ResultSet rs = DatabaseConnection.createStatement("SELECT score FROM plotsystem_plots WHERE id = ?") .setValue(this.ID).executeQuery()) { @@ -196,6 +186,7 @@ public int getTotalScore() throws SQLException { } } + @Override public int getSharedScore() throws SQLException { int score = getTotalScore(); if (score != -1 && !getPlotMembers().isEmpty()) { @@ -204,6 +195,18 @@ public int getSharedScore() throws SQLException { return score; } + @Override + public void setTotalScore(int score) throws SQLException { + if (score == -1) { + DatabaseConnection.createStatement("UPDATE plotsystem_plots SET score = DEFAULT(score) WHERE id = ?") + .setValue(this.ID).executeUpdate(); + } else { + DatabaseConnection.createStatement("UPDATE plotsystem_plots SET score = ? WHERE id = ?") + .setValue(score).setValue(this.ID).executeUpdate(); + } + } + + @Override public Status getStatus() throws SQLException { try (ResultSet rs = DatabaseConnection.createStatement("SELECT status FROM plotsystem_plots WHERE id = ?") .setValue(this.ID).executeQuery()) { @@ -215,6 +218,13 @@ public Status getStatus() throws SQLException { } } + @Override + public void setStatus(@NotNull Status status) throws SQLException { + DatabaseConnection.createStatement("UPDATE plotsystem_plots SET status = ? WHERE id = ?") + .setValue(status.name()).setValue(this.ID).executeUpdate(); + } + + @Override public Date getLastActivity() throws SQLException { try (ResultSet rs = DatabaseConnection.createStatement("SELECT last_activity FROM plotsystem_plots WHERE id = ?") .setValue(this.ID).executeQuery()) { @@ -226,6 +236,18 @@ public Date getLastActivity() throws SQLException { } } + @Override + public void setLastActivity(boolean setNull) throws SQLException { + if(setNull) { + DatabaseConnection.createStatement("UPDATE plotsystem_plots SET last_activity = DEFAULT(last_activity) WHERE id = ?") + .setValue(this.ID).executeUpdate(); + } else { + DatabaseConnection.createStatement("UPDATE plotsystem_plots SET last_activity = ? WHERE id = ?") + .setValue(java.sql.Date.valueOf(LocalDate.now())).setValue(this.ID).executeUpdate(); + } + } + + @Override public Date getCreateDate() throws SQLException { try (ResultSet rs = DatabaseConnection.createStatement("SELECT create_date FROM plotsystem_plots WHERE id = ?") .setValue(this.ID).executeQuery()) { @@ -237,6 +259,7 @@ public Date getCreateDate() throws SQLException { } } + @Override public Builder getPlotCreator() throws SQLException { try (ResultSet rs = DatabaseConnection.createStatement("SELECT create_player FROM plotsystem_plots WHERE id = ?") .setValue(this.ID).executeQuery()) { @@ -248,6 +271,7 @@ public Builder getPlotCreator() throws SQLException { } } + @Override public Slot getSlot() throws SQLException { try (ResultSet rs = DatabaseConnection.createStatement("SELECT first_slot, second_slot, third_slot FROM plotsystem_builders WHERE uuid = ?") .setValue(this.getPlotOwner().getUUID().toString()).executeQuery()) { @@ -264,77 +288,82 @@ public Slot getSlot() throws SQLException { } } - public String getOSMMapsLink() throws SQLException { - return "https://www.openstreetmap.org/#map=19/" + getGeoCoordinatesNumeric().replace(",", "/"); - } + @Override + public File getOutlinesSchematic() { + try { + return CompletableFuture.supplyAsync(() -> { + try { + File file = Paths.get(PlotManager.getDefaultSchematicPath(), String.valueOf(getCity().getCountry().getServer().getID()), String.valueOf(getCity().getID()), getID() + ".schematic").toFile(); - public String getGoogleMapsLink() throws SQLException { - return "https://www.google.com/maps/place/"+ getGeoCoordinatesNumeric(); + if(!file.exists()) { + if (getCity().getCountry().getServer().getFTPConfiguration() != null) { + FTPManager.downloadSchematic(FTPManager.getFTPUrl(getCity().getCountry().getServer(), getCity().getID()), file); + } + } + return file; + } catch (SQLException | URISyntaxException ex) { + Bukkit.getLogger().log(Level.SEVERE, "A SQL error occurred!", ex); + } + return null; + }).get(); + } catch (InterruptedException | ExecutionException ex) { + return null; + } } - public String getGoogleEarthLink() throws SQLException { - return "https://earth.google.com/web/@" + getGeoCoordinatesNumeric() + ",0a,1000d,20y,-0h,0t,0r"; + @Override + public File getFinishedSchematic() { + try { + return Paths.get(PlotManager.getDefaultSchematicPath(), String.valueOf(getCity().getCountry().getServer().getID()), "finishedSchematics", String.valueOf(getCity().getID()), getID() + ".schematic").toFile(); + } catch (SQLException ex) { + Bukkit.getLogger().log(Level.SEVERE, "A SQL error occurred!", ex); + } + return null; } - - public PlotWorld getPlotWorld() { - return plotWorld; + @Override + public String getGeoCoordinates() throws SQLException { + // Convert MC coordinates to geo coordinates + Vector mcCoordinates = getMinecraftCoordinates(); + try { + return CoordinateConversion.formatGeoCoordinatesNumeric(CoordinateConversion.convertToGeo(mcCoordinates.getX(), mcCoordinates.getZ())); + } catch (OutOfProjectionBoundsException ex) { + Bukkit.getLogger().log(Level.SEVERE, "Could not convert MC coordinates to geo coordinates!", ex); + } + return null; } - public void setPlotOwner(String UUID) throws SQLException { - if (UUID == null) { - DatabaseConnection.createStatement("UPDATE plotsystem_plots SET owner_uuid = DEFAULT(owner_uuid) WHERE id = ?") - .setValue(this.ID).executeUpdate(); - } else { - DatabaseConnection.createStatement("UPDATE plotsystem_plots SET owner_uuid = ? WHERE id = ?") - .setValue(UUID).setValue(this.ID).executeUpdate(); + @Override + public Vector getMinecraftCoordinates() throws SQLException { + try (ResultSet rs = DatabaseConnection.createStatement("SELECT mc_coordinates FROM plotsystem_plots WHERE id = ?") + .setValue(this.ID).executeQuery()) { + + if (rs.next()) { + String[] mcLocation = rs.getString(1).split(","); + return new Vector(Double.parseDouble(mcLocation[0]), Double.parseDouble(mcLocation[1]), Double.parseDouble(mcLocation[2])); + } + return null; } } - public void setScore(int score) throws SQLException { - if (score == -1) { - DatabaseConnection.createStatement("UPDATE plotsystem_plots SET score = DEFAULT(score) WHERE id = ?") - .setValue(this.ID).executeUpdate(); - } else { - DatabaseConnection.createStatement("UPDATE plotsystem_plots SET score = ? WHERE id = ?") - .setValue(score).setValue(this.ID).executeUpdate(); - } + public String getOSMMapsLink() throws SQLException { + return "https://www.openstreetmap.org/#map=19/" + getGeoCoordinates().replace(",", "/"); } - public void setStatus(Status status) throws SQLException { - DatabaseConnection.createStatement("UPDATE plotsystem_plots SET status = ? WHERE id = ?") - .setValue(status.name()).setValue(this.ID).executeUpdate(); + public String getGoogleMapsLink() throws SQLException { + return "https://www.google.com/maps/place/"+ getGeoCoordinates(); } - public void setLastActivity(boolean setNull) throws SQLException { - if(setNull) { - DatabaseConnection.createStatement("UPDATE plotsystem_plots SET last_activity = DEFAULT(last_activity) WHERE id = ?") - .setValue(this.ID).executeUpdate(); - } else { - DatabaseConnection.createStatement("UPDATE plotsystem_plots SET last_activity = ? WHERE id = ?") - .setValue(java.sql.Date.valueOf(LocalDate.now())).setValue(this.ID).executeUpdate(); - } + public String getGoogleEarthLink() throws SQLException { + return "https://earth.google.com/web/@" + getGeoCoordinates() + ",0a,1000d,20y,-0h,0t,0r"; } + @Override public void setPasted(boolean pasted) throws SQLException { DatabaseConnection.createStatement("UPDATE plotsystem_plots SET pasted = ? WHERE id = ?") .setValue(pasted).setValue(this.ID).executeUpdate(); } - private void setPlotMembers(List plotMembers) throws SQLException { - // Convert plot member list to string - String plotMemberAsString = plotMembers.stream().map(member -> member.getUUID().toString()).collect(Collectors.joining(",")); - Bukkit.getLogger().log(Level.SEVERE,plotMemberAsString); - - if(!plotMembers.isEmpty()) { - DatabaseConnection.createStatement("UPDATE plotsystem_plots SET member_uuids = ? WHERE id = ?") - .setValue(plotMemberAsString).setValue(this.ID).executeUpdate(); - } else { - DatabaseConnection.createStatement("UPDATE plotsystem_plots SET member_uuids = DEFAULT(member_uuids) WHERE id = ?") - .setValue(this.ID).executeUpdate(); - } - } - public void addPlotMember(Builder member) throws SQLException { List members = getPlotMembers(); if (members.size() < 3 && members.stream().noneMatch(m -> m.getUUID().equals(member.getUUID()))) { @@ -344,7 +373,7 @@ public void addPlotMember(Builder member) throws SQLException { setPlotMembers(members); member.setPlot(this.ID, slot); - addBuilderPerms(member.getUUID()); + getPermissions().addBuilderPerms(member.getUUID()); } } } @@ -359,8 +388,7 @@ public void removePlotMember(Builder member) throws SQLException { if (slot != null) { member.removePlot(slot); } - - removeBuilderPerms(member.getUUID()); + getPermissions().removeBuilderPerms(member.getUUID()); } } @@ -368,6 +396,7 @@ public boolean isReviewed() throws SQLException { return getReview() != null; } + // TODO: Move to Review class public boolean isRejected() throws SQLException { return (getStatus() == Status.unfinished || getStatus() == Status.unreviewed) && getTotalScore() != -1; // -1 == null } diff --git a/src/main/java/com/alpsbte/plotsystem/core/system/plot/PlotHandler.java b/src/main/java/com/alpsbte/plotsystem/core/system/plot/PlotHandler.java index b4f97604..60f8894a 100644 --- a/src/main/java/com/alpsbte/plotsystem/core/system/plot/PlotHandler.java +++ b/src/main/java/com/alpsbte/plotsystem/core/system/plot/PlotHandler.java @@ -85,10 +85,10 @@ public static void submitPlot(Plot plot) throws SQLException { } } - plot.removeBuilderPerms(plot.getPlotOwner().getUUID()).save(); + plot.getPermissions().removeBuilderPerms(plot.getPlotOwner().getUUID()).save(); if (plot.getPlotMembers().size() != 0) { for (Builder builder : plot.getPlotMembers()) { - plot.removeBuilderPerms(builder.getUUID()); + plot.getPermissions().removeBuilderPerms(builder.getUUID()); } } } @@ -96,7 +96,7 @@ public static void submitPlot(Plot plot) throws SQLException { public static void undoSubmit(Plot plot) throws SQLException { plot.setStatus(Status.unfinished); - plot.addBuilderPerms(plot.getPlotOwner().getUUID()).save(); + plot.getPermissions().addBuilderPerms(plot.getPlotOwner().getUUID()).save(); } public static void abandonPlot(Plot plot) { @@ -129,7 +129,7 @@ public static void abandonPlot(Plot plot) { plot.getPlotOwner().removePlot(plot.getSlot()); plot.setPlotOwner(null); plot.setLastActivity(true); - plot.setScore(-1); + plot.setTotalScore(-1); plot.setStatus(Status.unclaimed); FileUtils.deleteDirectory(new File(PlotManager.getMultiverseInventoriesConfigPath(plot.getWorldName()))); diff --git a/src/main/java/com/alpsbte/plotsystem/core/system/plot/PlotPermissions.java b/src/main/java/com/alpsbte/plotsystem/core/system/plot/PlotPermissions.java index e4a7cf5d..b207c14f 100644 --- a/src/main/java/com/alpsbte/plotsystem/core/system/plot/PlotPermissions.java +++ b/src/main/java/com/alpsbte/plotsystem/core/system/plot/PlotPermissions.java @@ -24,54 +24,47 @@ package com.alpsbte.plotsystem.core.system.plot; -import com.sk89q.worldguard.bukkit.RegionContainer; -import com.sk89q.worldguard.protection.managers.RegionManager; -import com.sk89q.worldguard.protection.regions.ProtectedRegion; -import com.alpsbte.plotsystem.PlotSystem; -import org.bukkit.Bukkit; +import com.alpsbte.plotsystem.core.system.plot.world.PlotWorld; -import java.sql.SQLException; import java.util.UUID; -import java.util.logging.Level; public class PlotPermissions { - private final int plotID; + private final PlotWorld plotWorld; - public PlotPermissions(int plotID) { - this.plotID = plotID; + public PlotPermissions(PlotWorld plotWorld) { + this.plotWorld = plotWorld; } public PlotPermissions addBuilderPerms(UUID builder) { - getPlotRegion().getOwners().addPlayer(builder); + plotWorld.getProtectedRegion().getOwners().addPlayer(builder); return this; } public PlotPermissions removeBuilderPerms(UUID builder) { - getPlotRegion().getOwners().removePlayer(builder); + plotWorld.getProtectedRegion().getOwners().removePlayer(builder); return this; } public PlotPermissions addReviewerPerms() { - getPlotRegion().getOwners().addGroup("staff"); + plotWorld.getProtectedRegion().getOwners().addGroup("staff"); return this; } public PlotPermissions removeReviewerPerms() { - getPlotRegion().getOwners().removeGroup("staff"); + plotWorld.getProtectedRegion().getOwners().removeGroup("staff"); return this; } public PlotPermissions clearAllPerms() { - getPlotRegion().getOwners().removeAll(); + plotWorld.getProtectedRegion().getOwners().removeAll(); return this; } public boolean hasReviewerPerms() { - return getPlotRegion().getOwners().getGroups().contains("staff"); + return plotWorld.getProtectedRegion().getOwners().getGroups().contains("staff"); } - public void save() { try { PlotHandler.unloadPlot(new Plot(plotID)); From 8357ddbca3c2edf1b88c1ced619644075365e28f Mon Sep 17 00:00:00 2001 From: LordTuxn Date: Thu, 6 Jan 2022 01:34:50 +0100 Subject: [PATCH 04/21] Fix some bugs and issues in PlotWorld class --- .../plotsystem/core/EventListener.java | 6 ++-- .../core/system/plot/PlotHandler.java | 36 ++++++------------- .../core/system/plot/PlotManager.java | 6 ++-- .../core/system/plot/PlotPermissions.java | 20 +---------- .../plot/generator/AbstractPlotGenerator.java | 30 ++++++++-------- .../plot/generator/RawPlotGenerator.java | 2 +- .../core/system/plot/world/IPlotWorld.java | 10 +++--- .../core/system/plot/world/PlotWorld.java | 32 ++++++++++++----- 8 files changed, 62 insertions(+), 80 deletions(-) diff --git a/src/main/java/com/alpsbte/plotsystem/core/EventListener.java b/src/main/java/com/alpsbte/plotsystem/core/EventListener.java index d76920ba..2dc41172 100644 --- a/src/main/java/com/alpsbte/plotsystem/core/EventListener.java +++ b/src/main/java/com/alpsbte/plotsystem/core/EventListener.java @@ -206,7 +206,7 @@ public void onPlayerInteractAtEntity(PlayerInteractAtEntityEvent event) throws S @EventHandler public void onPlayerQuitEvent(PlayerQuitEvent event) throws SQLException { if(PlotManager.isPlotWorld(event.getPlayer().getWorld())) { - PlotHandler.unloadPlot(PlotManager.getPlotByWorld(event.getPlayer().getWorld())); + PlotManager.getPlotByWorld(event.getPlayer().getWorld()).getWorld().unload(false); } DefaultPlotGenerator.playerPlotGenerationHistory.remove(event.getPlayer().getUniqueId()); } @@ -214,14 +214,14 @@ public void onPlayerQuitEvent(PlayerQuitEvent event) throws SQLException { @EventHandler public void onPlayerTeleportEvent(PlayerTeleportEvent event) throws SQLException { if(PlotManager.isPlotWorld(event.getPlayer().getWorld()) && !event.getFrom().getWorld().equals(event.getTo().getWorld())) { - PlotHandler.unloadPlot(PlotManager.getPlotByWorld(event.getFrom().getWorld())); + PlotManager.getPlotByWorld(event.getFrom().getWorld()).getWorld().unload(false); } } @EventHandler public void onPlayerChangedWorldEvent(PlayerChangedWorldEvent event) throws SQLException { if (PlotManager.isPlotWorld(event.getFrom())) { - PlotHandler.unloadPlot(PlotManager.getPlotByWorld(event.getFrom())); + PlotManager.getPlotByWorld(event.getFrom()).getWorld().unload(false); } if (PlotManager.isPlotWorld(event.getPlayer().getWorld())) { diff --git a/src/main/java/com/alpsbte/plotsystem/core/system/plot/PlotHandler.java b/src/main/java/com/alpsbte/plotsystem/core/system/plot/PlotHandler.java index 60f8894a..7de55be7 100644 --- a/src/main/java/com/alpsbte/plotsystem/core/system/plot/PlotHandler.java +++ b/src/main/java/com/alpsbte/plotsystem/core/system/plot/PlotHandler.java @@ -30,8 +30,6 @@ import com.alpsbte.plotsystem.core.database.DatabaseConnection; import com.alpsbte.plotsystem.core.menus.CompanionMenu; import com.alpsbte.plotsystem.core.system.Server; -import com.alpsbte.plotsystem.core.system.plot.generator.DefaultPlotGenerator; -import com.alpsbte.plotsystem.core.system.plot.world.PlotWorld; import com.alpsbte.plotsystem.utils.Utils; import com.alpsbte.plotsystem.utils.enums.Status; import com.alpsbte.plotsystem.utils.ftp.FTPManager; @@ -55,9 +53,8 @@ public class PlotHandler { public static void teleportPlayer(Plot plot, Player player) throws SQLException { player.sendMessage(Utils.getInfoMessageFormat("Teleporting to plot §6#" + plot.getID())); - loadPlot(plot); + plot.getWorld().load(); player.teleport(getPlotSpawnPoint(plot)); - new PlotWorld(plot).generate(plot.getPlotOwner(), DefaultPlotGenerator.class); player.playSound(player.getLocation(), Utils.TeleportSound, 1, 1); player.setAllowFlight(true); player.setFlying(true); @@ -79,8 +76,8 @@ public static void teleportPlayer(Plot plot, Player player) throws SQLException public static void submitPlot(Plot plot) throws SQLException { plot.setStatus(Status.unreviewed); - if(plot.getPlotWorld() != null) { - for(Player player : plot.getPlotWorld().getPlayers()) { + if(plot.getWorld() != null) { + for(Player player : plot.getWorld().getBukkitWorld().getPlayers()) { player.teleport(Utils.getSpawnLocation()); } } @@ -102,13 +99,13 @@ public static void undoSubmit(Plot plot) throws SQLException { public static void abandonPlot(Plot plot) { try { if (PlotManager.plotExists(plot.getID())) { - loadPlot(plot); // Load Plot to be listed by Multiverse + plot.getWorld().load(); // Load Plot to be listed by Multiverse - for (Player player : plot.getPlotWorld().getPlayers()) { + for (Player player : plot.getWorld().getBukkitWorld().getPlayers()) { player.teleport(Utils.getSpawnLocation()); } - PlotSystem.DependencyManager.getMultiverseCore().getMVWorldManager().deleteWorld(plot.getWorldName(), true, true); + PlotSystem.DependencyManager.getMultiverseCore().getMVWorldManager().deleteWorld(plot.getWorld().getName(), true, true); PlotSystem.DependencyManager.getMultiverseCore().saveWorldConfig(); } @@ -132,8 +129,8 @@ public static void abandonPlot(Plot plot) { plot.setTotalScore(-1); plot.setStatus(Status.unclaimed); - FileUtils.deleteDirectory(new File(PlotManager.getMultiverseInventoriesConfigPath(plot.getWorldName()))); - FileUtils.deleteDirectory(new File(PlotManager.getWorldGuardConfigPath(plot.getWorldName()))); + FileUtils.deleteDirectory(new File(PlotManager.getMultiverseInventoriesConfigPath(plot.getWorld().getName()))); + FileUtils.deleteDirectory(new File(PlotManager.getWorldGuardConfigPath(plot.getWorld().getName()))); } catch (SQLException ex) { Bukkit.getLogger().log(Level.SEVERE, "A SQL error occurred!", ex); } catch (IOException ex) { @@ -174,28 +171,15 @@ public static void deletePlot(Plot plot) throws SQLException { }).join() == null) throw new SQLException(); } - public static void loadPlot(Plot plot) { - if(plot.getPlotWorld() == null) { - PlotSystem.DependencyManager.getMultiverseCore().getMVWorldManager().loadWorld(plot.getWorldName()); - } - } - - public static void unloadPlot(Plot plot) { - if(plot.getPlotWorld() != null && plot.getPlotWorld().getPlayers().isEmpty()) { - Bukkit.getScheduler().scheduleSyncDelayedTask(PlotSystem.getPlugin(), (() -> - Bukkit.unloadWorld(plot.getWorldName(), true)), 60L); - } - } - public static Location getPlotSpawnPoint(Plot plot) { - Location spawnLocation = new Location(plot.getPlotWorld(), + Location spawnLocation = new Location(plot.getWorld().getBukkitWorld(), PlotManager.getPlotCenter().getX() + 0.5, 30, PlotManager.getPlotCenter().getZ() + 0.5, -90, 90); // Set spawn point 1 block above the highest center point - spawnLocation.setY(plot.getPlotWorld().getHighestBlockYAt((int) spawnLocation.getX(), (int) spawnLocation.getZ()) + 1); + spawnLocation.setY(plot.getWorld().getBukkitWorld().getHighestBlockYAt((int) spawnLocation.getX(), (int) spawnLocation.getZ()) + 1); return spawnLocation; } diff --git a/src/main/java/com/alpsbte/plotsystem/core/system/plot/PlotManager.java b/src/main/java/com/alpsbte/plotsystem/core/system/plot/PlotManager.java index 91edbdf5..505222d7 100644 --- a/src/main/java/com/alpsbte/plotsystem/core/system/plot/PlotManager.java +++ b/src/main/java/com/alpsbte/plotsystem/core/system/plot/PlotManager.java @@ -26,7 +26,6 @@ import com.alpsbte.plotsystem.core.config.ConfigPaths; import com.alpsbte.plotsystem.core.system.CityProject; -import com.alpsbte.plotsystem.core.system.Country; import com.sk89q.worldedit.EditSession; import com.sk89q.worldedit.Vector; import com.sk89q.worldedit.WorldEditException; @@ -194,8 +193,8 @@ public static CompletableFuture savePlotAsSchematic(Plot plot) throws IOEx // Load finished plot region as cuboid region - PlotHandler.loadPlot(plot); - CuboidRegion region = new CuboidRegion(new BukkitWorld(plot.getPlotWorld()), schematicMinPoint, schematicMaxPoint); + plot.getWorld().load(); + CuboidRegion region = new CuboidRegion(new BukkitWorld(plot.getWorld().getBukkitWorld()), schematicMinPoint, schematicMaxPoint); // Copy finished plot region to clipboard @@ -325,6 +324,7 @@ public static Plot getPlotByWorld(World plotWorld) throws SQLException { return new Plot(Integer.parseInt(plotWorld.getName().substring(2))); } + @Deprecated public static boolean plotExists(int ID) { String worldName = "P-" + ID; return (PlotSystem.DependencyManager.getMultiverseCore().getMVWorldManager().getMVWorld(worldName) != null) || PlotSystem.DependencyManager.getMultiverseCore().getMVWorldManager().getUnloadedWorlds().contains(worldName); diff --git a/src/main/java/com/alpsbte/plotsystem/core/system/plot/PlotPermissions.java b/src/main/java/com/alpsbte/plotsystem/core/system/plot/PlotPermissions.java index b207c14f..82822f14 100644 --- a/src/main/java/com/alpsbte/plotsystem/core/system/plot/PlotPermissions.java +++ b/src/main/java/com/alpsbte/plotsystem/core/system/plot/PlotPermissions.java @@ -66,24 +66,6 @@ public boolean hasReviewerPerms() { } public void save() { - try { - PlotHandler.unloadPlot(new Plot(plotID)); - } catch (SQLException ex) { - Bukkit.getLogger().log(Level.SEVERE, "A SQL error occurred!", ex); - } - } - - public ProtectedRegion getPlotRegion() { - RegionContainer container = PlotSystem.DependencyManager.getWorldGuard().getRegionContainer(); - - String worldName = "P-" + plotID; - try { - PlotHandler.loadPlot(new Plot(plotID)); - } catch (SQLException ex) { - Bukkit.getLogger().log(Level.SEVERE, "A SQL error occurred!", ex); - } - - RegionManager regionManager = container.get(Bukkit.getWorld(worldName)); - return regionManager.getRegion("p-" + plotID); + plotWorld.unload(true); } } diff --git a/src/main/java/com/alpsbte/plotsystem/core/system/plot/generator/AbstractPlotGenerator.java b/src/main/java/com/alpsbte/plotsystem/core/system/plot/generator/AbstractPlotGenerator.java index 879b272d..46642fcd 100644 --- a/src/main/java/com/alpsbte/plotsystem/core/system/plot/generator/AbstractPlotGenerator.java +++ b/src/main/java/com/alpsbte/plotsystem/core/system/plot/generator/AbstractPlotGenerator.java @@ -91,7 +91,7 @@ public AbstractPlotGenerator(@NotNull Plot plot, @NotNull Builder builder) { generateWorld(); generateOutlines(plot.getOutlinesSchematic()); createMultiverseWorld(); - configureWorld(worldManager.getMVWorld(plot.getPlotWorld().getBukkit())); + configureWorld(worldManager.getMVWorld(plot.getWorld().getBukkitWorld())); createProtection(); } catch (Exception ex) { exception = ex; @@ -104,7 +104,7 @@ public AbstractPlotGenerator(@NotNull Plot plot, @NotNull Builder builder) { } if (exception != null) { - if (worldManager.isMVWorld(plot.getPlotWorld().getName())) PlotHandler.abandonPlot(plot); + if (worldManager.isMVWorld(plot.getWorld().getName())) PlotHandler.abandonPlot(plot); onException(exception); } }); @@ -123,7 +123,7 @@ public AbstractPlotGenerator(@NotNull Plot plot, @NotNull Builder builder) { protected void generateWorld() { if (PlotManager.plotExists(plot.getID())) PlotHandler.abandonPlot(plot); - worldCreator = new WorldCreator(plot.getPlotWorld().getName()); + worldCreator = new WorldCreator(plot.getWorld().getName()); worldCreator.environment(org.bukkit.World.Environment.NORMAL); worldCreator.type(WorldType.FLAT); worldCreator.generatorSettings("2;0;1;"); @@ -136,7 +136,7 @@ protected void generateWorld() { protected void createMultiverseWorld() { // Check if world creator is configured and add new world to multiverse world manager if (worldCreator != null) { - worldManager.addWorld(plot.getPlotWorld().getName(), worldCreator.environment(), null, worldCreator.type(), false, + worldManager.addWorld(plot.getWorld().getName(), worldCreator.environment(), null, worldCreator.type(), false, "VoidGen:{\"caves\":false,\"decoration\":false,\"mobs\":false,\"structures\":false}"); } else { throw new RuntimeException("World Creator is not configured"); @@ -152,7 +152,7 @@ protected void generateOutlines(File plotSchematic) { if (plotSchematic != null) { Vector buildingOutlinesCoordinates = PlotManager.getPlotCenter(); - com.sk89q.worldedit.world.World weWorld = new BukkitWorld(plot.getPlotWorld().getBukkit()); + com.sk89q.worldedit.world.World weWorld = new BukkitWorld(plot.getWorld().getBukkitWorld()); Clipboard clipboard = ClipboardFormat.SCHEMATIC.getReader(new FileInputStream(plotSchematic)).read(weWorld.getWorldData()); // Place the bottom part of the schematic 5 blocks above 0 @@ -166,7 +166,7 @@ protected void generateOutlines(File plotSchematic) { Operations.complete(operation); editSession.flushQueue(); - plot.getPlotWorld().getBukkit().setSpawnLocation(PlotHandler.getPlotSpawnPoint(plot)); + plot.getWorld().getBukkitWorld().setSpawnLocation(PlotHandler.getPlotSpawnPoint(plot)); } } catch (IOException | WorldEditException ex) { Bukkit.getLogger().log(Level.SEVERE, "An error occurred while generating plot outlines!", ex); @@ -180,15 +180,15 @@ protected void generateOutlines(File plotSchematic) { */ protected void configureWorld(@NotNull MultiverseWorld mvWorld) { // Set Bukkit world game rules - plot.getPlotWorld().getBukkit().setGameRuleValue("randomTickSpeed", "0"); - plot.getPlotWorld().getBukkit().setGameRuleValue("doDaylightCycle", "false"); - plot.getPlotWorld().getBukkit().setGameRuleValue("doFireTick", "false"); - plot.getPlotWorld().getBukkit().setGameRuleValue("doWeatherCycle", "false"); - plot.getPlotWorld().getBukkit().setGameRuleValue("keepInventory", "true"); - plot.getPlotWorld().getBukkit().setGameRuleValue("announceAdvancements", "false"); + plot.getWorld().getBukkitWorld().setGameRuleValue("randomTickSpeed", "0"); + plot.getWorld().getBukkitWorld().setGameRuleValue("doDaylightCycle", "false"); + plot.getWorld().getBukkitWorld().setGameRuleValue("doFireTick", "false"); + plot.getWorld().getBukkitWorld().setGameRuleValue("doWeatherCycle", "false"); + plot.getWorld().getBukkitWorld().setGameRuleValue("keepInventory", "true"); + plot.getWorld().getBukkitWorld().setGameRuleValue("announceAdvancements", "false"); // Set world time to midday - plot.getPlotWorld().getBukkit().setTime(6000); + plot.getWorld().getBukkitWorld().setTime(6000); // Configure multiverse world mvWorld.setAllowFlight(true); @@ -210,7 +210,7 @@ protected void createProtection() { BlockVector max = BlockVector.toBlockPoint(PlotManager.PLOT_SIZE, 256, PlotManager.PLOT_SIZE); RegionContainer container = PlotSystem.DependencyManager.getWorldGuard().getRegionContainer(); - RegionManager regionManager = container.get(plot.getPlotWorld().getBukkit()); + RegionManager regionManager = container.get(plot.getWorld().getBukkitWorld()); // Create protected region for world GlobalProtectedRegion globalRegion = new GlobalProtectedRegion("__global__"); @@ -218,7 +218,7 @@ protected void createProtection() { globalRegion.setFlag(DefaultFlag.ENTRY.getRegionGroupFlag(), RegionGroup.ALL); // Create protected region for plot - ProtectedRegion protectedPlotRegion = new ProtectedCuboidRegion(plot.getPlotWorld().getName(), min, max); + ProtectedRegion protectedPlotRegion = new ProtectedCuboidRegion(plot.getWorld().getName(), min, max); protectedPlotRegion.setPriority(100); // Add and save regions diff --git a/src/main/java/com/alpsbte/plotsystem/core/system/plot/generator/RawPlotGenerator.java b/src/main/java/com/alpsbte/plotsystem/core/system/plot/generator/RawPlotGenerator.java index eee43eeb..270281f6 100644 --- a/src/main/java/com/alpsbte/plotsystem/core/system/plot/generator/RawPlotGenerator.java +++ b/src/main/java/com/alpsbte/plotsystem/core/system/plot/generator/RawPlotGenerator.java @@ -60,7 +60,7 @@ protected void generateOutlines(@NotNull File plotSchematic) {} @Override protected void createProtection() { RegionContainer container = PlotSystem.DependencyManager.getWorldGuard().getRegionContainer(); - RegionManager regionManager = container.get(getPlot().getPlotWorld()); + RegionManager regionManager = container.get(getPlot().getWorld().getBukkitWorld()); if (regionManager != null) { for (String regionID : regionManager.getRegions().keySet()) { diff --git a/src/main/java/com/alpsbte/plotsystem/core/system/plot/world/IPlotWorld.java b/src/main/java/com/alpsbte/plotsystem/core/system/plot/world/IPlotWorld.java index 789ecebb..05ba624a 100644 --- a/src/main/java/com/alpsbte/plotsystem/core/system/plot/world/IPlotWorld.java +++ b/src/main/java/com/alpsbte/plotsystem/core/system/plot/world/IPlotWorld.java @@ -2,16 +2,18 @@ import com.alpsbte.plotsystem.core.system.Builder; import com.alpsbte.plotsystem.core.system.plot.generator.AbstractPlotGenerator; -import com.sk89q.worldguard.bukkit.RegionContainer; +import com.sk89q.worldguard.protection.regions.ProtectedRegion; import org.bukkit.World; +import org.jetbrains.annotations.NotNull; public interface IPlotWorld { /** * Generates a new plot world with the required settings and plot schematic * @param plotOwner - plow owner of the plot + * @param generator - generator type as class * @return - true if world was generated successfully */ - boolean generate(Builder plotOwner, Class generator); + boolean generate(@NotNull Builder plotOwner, @NotNull Class generator); /** * Deletes the world file and entry in the config file @@ -35,7 +37,7 @@ public interface IPlotWorld { /** * @return - Bukkit plot world */ - World getBukkit(); + World getBukkitWorld(); /** * Returns plot world name in the format (for example: P-23) @@ -47,7 +49,7 @@ public interface IPlotWorld { * Loads the protected plot world region from WorldGuard config * @return - protected WorldGuard region */ - RegionContainer getProtectedRegion(); + ProtectedRegion getProtectedRegion(); /** * Checks if the plot world is loaded to memory. If the plot world has not yet been generated, it will return false. diff --git a/src/main/java/com/alpsbte/plotsystem/core/system/plot/world/PlotWorld.java b/src/main/java/com/alpsbte/plotsystem/core/system/plot/world/PlotWorld.java index 4fc938fe..47b32d6d 100644 --- a/src/main/java/com/alpsbte/plotsystem/core/system/plot/world/PlotWorld.java +++ b/src/main/java/com/alpsbte/plotsystem/core/system/plot/world/PlotWorld.java @@ -3,14 +3,21 @@ import com.alpsbte.plotsystem.PlotSystem; import com.alpsbte.plotsystem.core.system.Builder; import com.alpsbte.plotsystem.core.system.plot.Plot; +import com.alpsbte.plotsystem.core.system.plot.PlotManager; import com.alpsbte.plotsystem.core.system.plot.generator.AbstractPlotGenerator; import com.alpsbte.plotsystem.core.system.plot.generator.DefaultPlotGenerator; import com.alpsbte.plotsystem.core.system.plot.generator.RawPlotGenerator; import com.alpsbte.plotsystem.utils.Utils; import com.sk89q.worldguard.bukkit.RegionContainer; +import com.sk89q.worldguard.protection.managers.RegionManager; +import com.sk89q.worldguard.protection.regions.ProtectedRegion; import org.bukkit.Bukkit; import org.bukkit.World; import org.bukkit.entity.Player; +import org.jetbrains.annotations.NotNull; + +import java.util.Locale; +import java.util.logging.Level; public class PlotWorld implements IPlotWorld { @@ -21,7 +28,7 @@ public PlotWorld(Plot plot) { } @Override - public boolean generate(Builder plotOwner, Class generator) { + public boolean generate(@NotNull Builder plotOwner, @NotNull Class generator) { if (!isGenerated()) { if (generator.isInstance(DefaultPlotGenerator.class)) { new DefaultPlotGenerator(plot, plotOwner); @@ -51,21 +58,21 @@ public boolean load() { @Override public boolean unload(boolean movePlayers) { if(isLoaded()) { - if (movePlayers && !getBukkit().getPlayers().isEmpty()) { - for (Player player : getBukkit().getPlayers()) { + if (movePlayers && !getBukkitWorld().getPlayers().isEmpty()) { + for (Player player : getBukkitWorld().getPlayers()) { player.teleport(Utils.getSpawnLocation()); } - } else if (!movePlayers && getBukkit().getPlayers().isEmpty()) return false; + } else if (!movePlayers && !getBukkitWorld().getPlayers().isEmpty()) return false; Bukkit.getScheduler().scheduleSyncDelayedTask(PlotSystem.getPlugin(), (() -> - Bukkit.unloadWorld(getBukkit(), true)), 60L); + Bukkit.unloadWorld(getBukkitWorld(), true)), 60L); return !isLoaded(); } return false; } @Override - public World getBukkit() { + public World getBukkitWorld() { return Bukkit.getWorld(getName()); } @@ -75,17 +82,24 @@ public String getName() { } @Override - public RegionContainer getProtectedRegion() { + public ProtectedRegion getProtectedRegion() { + RegionContainer container = PlotSystem.DependencyManager.getWorldGuard().getRegionContainer(); + if (load()) { + RegionManager regionManager = container.get(getBukkitWorld()); + if (regionManager != null) { + return regionManager.getRegion(getName().toLowerCase(Locale.ROOT)); + } else Bukkit.getLogger().log(Level.WARNING, "Region manager is null"); + } return null; } @Override public boolean isLoaded() { - return getBukkit() == null; + return getBukkitWorld() != null; } @Override public boolean isGenerated() { - return PlotSystem.DependencyManager.getMultiverseCore().getMVWorldManager().isMVWorld(getName()); + return PlotSystem.DependencyManager.getMultiverseCore().getMVWorldManager().getMVWorld(getName()) != null || PlotSystem.DependencyManager.getMultiverseCore().getMVWorldManager().getUnloadedWorlds().contains(getName()); } } From cd8fc4eb0d8eb6313460d90e5e9e83f1b5dceb8e Mon Sep 17 00:00:00 2001 From: LordTuxn Date: Thu, 6 Jan 2022 21:47:48 +0100 Subject: [PATCH 05/21] add regenerator function to PlotWorld --- .../core/system/plot/world/IPlotWorld.java | 29 ++++++++++++------- .../core/system/plot/world/PlotWorld.java | 15 +++++++++- 2 files changed, 32 insertions(+), 12 deletions(-) diff --git a/src/main/java/com/alpsbte/plotsystem/core/system/plot/world/IPlotWorld.java b/src/main/java/com/alpsbte/plotsystem/core/system/plot/world/IPlotWorld.java index 05ba624a..5e35df4a 100644 --- a/src/main/java/com/alpsbte/plotsystem/core/system/plot/world/IPlotWorld.java +++ b/src/main/java/com/alpsbte/plotsystem/core/system/plot/world/IPlotWorld.java @@ -9,57 +9,64 @@ public interface IPlotWorld { /** * Generates a new plot world with the required settings and plot schematic - * @param plotOwner - plow owner of the plot - * @param generator - generator type as class - * @return - true if world was generated successfully + * @param plotOwner plow owner of the plot + * @param generator generator type as class + * @return true if world was generated successfully */ boolean generate(@NotNull Builder plotOwner, @NotNull Class generator); + /** + * Regenerates the current plot with an optional new generator type + * @param generator generator type as class + * @return true if world was regenerated successfully + */ + boolean regenerate(@NotNull Class generator); + /** * Deletes the world file and entry in the config file - * @return - true if world was deleted successfully + * @return true if world was deleted successfully */ boolean delete(); /** * Loads the plot world to memory to be used. Plot has to be generated. - * @return - true if world was loaded successfully + * @return true if world was loaded successfully */ boolean load(); /** * Unloads the plot world from memory. Plot cannot be used anymore. Plot has to be generated. * @param movePlayers - if true, players will get teleported to the spawn location. Otherwise, plot will not get unloaded. - * @return - true if world was loaded successfully + * @return true if world was loaded successfully */ boolean unload(boolean movePlayers); /** - * @return - Bukkit plot world + * @return Bukkit plot world */ World getBukkitWorld(); /** * Returns plot world name in the format (for example: P-23) - * @return - world name of the plot + * @return world name of the plot */ String getName(); /** * Loads the protected plot world region from WorldGuard config - * @return - protected WorldGuard region + * @return protected WorldGuard region */ ProtectedRegion getProtectedRegion(); /** * Checks if the plot world is loaded to memory. If the plot world has not yet been generated, it will return false. - * @return - true if world is loaded + * @return true if world is loaded */ boolean isLoaded(); /** * Checks if the plot world is generated. - * @return - true if world is generated + * @return true if world is generated */ boolean isGenerated(); } diff --git a/src/main/java/com/alpsbte/plotsystem/core/system/plot/world/PlotWorld.java b/src/main/java/com/alpsbte/plotsystem/core/system/plot/world/PlotWorld.java index 47b32d6d..a6d1e4a5 100644 --- a/src/main/java/com/alpsbte/plotsystem/core/system/plot/world/PlotWorld.java +++ b/src/main/java/com/alpsbte/plotsystem/core/system/plot/world/PlotWorld.java @@ -3,7 +3,6 @@ import com.alpsbte.plotsystem.PlotSystem; import com.alpsbte.plotsystem.core.system.Builder; import com.alpsbte.plotsystem.core.system.plot.Plot; -import com.alpsbte.plotsystem.core.system.plot.PlotManager; import com.alpsbte.plotsystem.core.system.plot.generator.AbstractPlotGenerator; import com.alpsbte.plotsystem.core.system.plot.generator.DefaultPlotGenerator; import com.alpsbte.plotsystem.core.system.plot.generator.RawPlotGenerator; @@ -16,6 +15,7 @@ import org.bukkit.entity.Player; import org.jetbrains.annotations.NotNull; +import java.sql.SQLException; import java.util.Locale; import java.util.logging.Level; @@ -40,6 +40,19 @@ public boolean generate(@NotNull Builder plotO return false; } + @Override + public boolean regenerate(@NotNull Class generator) { + if (isGenerated() && unload(true)) { + try { + if (delete() && generate(plot.getPlotOwner(), generator)) + return true; + } catch (SQLException ex) { + Bukkit.getLogger().log(Level.SEVERE, "A SQL error occurred!", ex); + } + } + return false; + } + @Override public boolean delete() { if (isGenerated() && unload(true)) From b6aa1292edfb46a86f4a181040214769f51149f7 Mon Sep 17 00:00:00 2001 From: LordTuxn Date: Thu, 6 Jan 2022 23:00:25 +0100 Subject: [PATCH 06/21] move teleportPlayer and getSpawnPoint to PlotWorld --- .../commands/plot/CMD_Plot_Teleport.java | 2 +- .../core/menus/PlayerPlotsMenu.java | 2 +- .../core/menus/PlotActionsMenu.java | 6 +- .../plotsystem/core/menus/ReviewMenu.java | 2 +- .../plotsystem/core/system/plot/Plot.java | 13 ++-- .../core/system/plot/PlotHandler.java | 58 ++--------------- .../plot/generator/AbstractPlotGenerator.java | 2 +- .../plot/generator/DefaultPlotGenerator.java | 2 +- .../core/system/plot/world/IPlotWorld.java | 15 +++++ .../core/system/plot/world/PlotWorld.java | 64 ++++++++++++++++++- 10 files changed, 95 insertions(+), 71 deletions(-) diff --git a/src/main/java/com/alpsbte/plotsystem/commands/plot/CMD_Plot_Teleport.java b/src/main/java/com/alpsbte/plotsystem/commands/plot/CMD_Plot_Teleport.java index 4b7ed378..88eac000 100644 --- a/src/main/java/com/alpsbte/plotsystem/commands/plot/CMD_Plot_Teleport.java +++ b/src/main/java/com/alpsbte/plotsystem/commands/plot/CMD_Plot_Teleport.java @@ -53,7 +53,7 @@ public void onCommand(CommandSender sender, String[] args) { if (args.length > 0 && Utils.TryParseInt(args[0]) != null) { int plotID = Integer.parseInt(args[0]); if (PlotManager.plotExists(plotID)) { - PlotHandler.teleportPlayer(new Plot(plotID), getPlayer(sender)); + new Plot(plotID).getWorld().teleportPlayer(getPlayer(sender)); } else { if (sender.hasPermission("plotsystem.admin") && PlotManager.plotExists(plotID, true)) { new DefaultPlotGenerator(new Plot(plotID), new Builder(getPlayer(sender).getUniqueId())); diff --git a/src/main/java/com/alpsbte/plotsystem/core/menus/PlayerPlotsMenu.java b/src/main/java/com/alpsbte/plotsystem/core/menus/PlayerPlotsMenu.java index e34c13d0..ad48bcb9 100644 --- a/src/main/java/com/alpsbte/plotsystem/core/menus/PlayerPlotsMenu.java +++ b/src/main/java/com/alpsbte/plotsystem/core/menus/PlayerPlotsMenu.java @@ -183,7 +183,7 @@ private List getDescription(Plot plot) throws SQLException { } } lines.add(""); - if (plot.isRejected()) lines.add("§c§lRejected"); + if (plot.isReviewed() && plot.isRejected()) lines.add("§c§lRejected"); lines.add("§6§lStatus: §7§l" + plot.getStatus().name().substring(0, 1).toUpperCase() + plot.getStatus().name().substring(1)); return lines; } diff --git a/src/main/java/com/alpsbte/plotsystem/core/menus/PlotActionsMenu.java b/src/main/java/com/alpsbte/plotsystem/core/menus/PlotActionsMenu.java index ba344ce6..f90fca24 100644 --- a/src/main/java/com/alpsbte/plotsystem/core/menus/PlotActionsMenu.java +++ b/src/main/java/com/alpsbte/plotsystem/core/menus/PlotActionsMenu.java @@ -152,11 +152,7 @@ protected void setItemClickEventsAsync() { // Set click event for teleport to plot item getMenu().getSlot(hasFeedback ? 12 : 13).setClickHandler((clickPlayer, clickInformation) -> { clickPlayer.closeInventory(); - try { - PlotHandler.teleportPlayer(plot, clickPlayer); - } catch (SQLException ex) { - Bukkit.getLogger().log(Level.SEVERE, "A SQL error occurred!", ex); - } + plot.getWorld().teleportPlayer(clickPlayer); }); // Set click event for abandon plot item diff --git a/src/main/java/com/alpsbte/plotsystem/core/menus/ReviewMenu.java b/src/main/java/com/alpsbte/plotsystem/core/menus/ReviewMenu.java index ed0fa4b1..6562f06f 100644 --- a/src/main/java/com/alpsbte/plotsystem/core/menus/ReviewMenu.java +++ b/src/main/java/com/alpsbte/plotsystem/core/menus/ReviewMenu.java @@ -121,7 +121,7 @@ protected void setItemClickEventsAsync() { if(plot.getStatus() == Status.unreviewed) { if (!plot.getPlotOwner().getUUID().toString().equals(getMenuPlayer().getUniqueId().toString())){ getMenuPlayer().closeInventory(); - PlotHandler.teleportPlayer(plot, getMenuPlayer()); + plot.getWorld().teleportPlayer(getMenuPlayer()); } else { getMenuPlayer().sendMessage(Utils.getErrorMessageFormat("You cannot review your own builds!")); } diff --git a/src/main/java/com/alpsbte/plotsystem/core/system/plot/Plot.java b/src/main/java/com/alpsbte/plotsystem/core/system/plot/Plot.java index 8b899d17..895fdd19 100644 --- a/src/main/java/com/alpsbte/plotsystem/core/system/plot/Plot.java +++ b/src/main/java/com/alpsbte/plotsystem/core/system/plot/Plot.java @@ -147,13 +147,12 @@ public void setPlotMembers(@NotNull List plotMembers) throws SQLExcepti @Override public Review getReview() throws SQLException { - if(getStatus() == Status.completed || isRejected()) { - try (ResultSet rs = DatabaseConnection.createStatement("SELECT review_id FROM plotsystem_plots WHERE id = ?") - .setValue(this.ID).executeQuery()) { + if (getStatus() == Status.completed) + try (ResultSet rs = DatabaseConnection.createStatement("SELECT review_id FROM plotsystem_plots WHERE id = ?") + .setValue(this.ID).executeQuery()) { - if (rs.next()) { - return new Review(rs.getInt(1)); - } + if (rs.next()) { + return new Review(rs.getInt(1)); } } return null; @@ -392,11 +391,11 @@ public void removePlotMember(Builder member) throws SQLException { } } + @Override public boolean isReviewed() throws SQLException { return getReview() != null; } - // TODO: Move to Review class public boolean isRejected() throws SQLException { return (getStatus() == Status.unfinished || getStatus() == Status.unreviewed) && getTotalScore() != -1; // -1 == null } diff --git a/src/main/java/com/alpsbte/plotsystem/core/system/plot/PlotHandler.java b/src/main/java/com/alpsbte/plotsystem/core/system/plot/PlotHandler.java index 7de55be7..10b48e31 100644 --- a/src/main/java/com/alpsbte/plotsystem/core/system/plot/PlotHandler.java +++ b/src/main/java/com/alpsbte/plotsystem/core/system/plot/PlotHandler.java @@ -24,21 +24,16 @@ package com.alpsbte.plotsystem.core.system.plot; -import com.alpsbte.plotsystem.PlotSystem; -import com.alpsbte.plotsystem.core.menus.ReviewMenu; import com.alpsbte.plotsystem.core.system.Builder; import com.alpsbte.plotsystem.core.database.DatabaseConnection; -import com.alpsbte.plotsystem.core.menus.CompanionMenu; import com.alpsbte.plotsystem.core.system.Server; import com.alpsbte.plotsystem.utils.Utils; import com.alpsbte.plotsystem.utils.enums.Status; import com.alpsbte.plotsystem.utils.ftp.FTPManager; import net.md_5.bungee.api.chat.*; -import org.apache.commons.io.FileUtils; import org.bukkit.*; import org.bukkit.entity.Player; -import java.io.File; import java.io.IOException; import java.net.URISyntaxException; import java.nio.file.Files; @@ -50,29 +45,6 @@ public class PlotHandler { - public static void teleportPlayer(Plot plot, Player player) throws SQLException { - player.sendMessage(Utils.getInfoMessageFormat("Teleporting to plot §6#" + plot.getID())); - - plot.getWorld().load(); - player.teleport(getPlotSpawnPoint(plot)); - player.playSound(player.getLocation(), Utils.TeleportSound, 1, 1); - player.setAllowFlight(true); - player.setFlying(true); - - player.getInventory().setItem(8, CompanionMenu.getMenuItem()); - - if(player.hasPermission("plotsystem.review")) { - player.getInventory().setItem(7, ReviewMenu.getMenuItem()); - } - - sendLinkMessages(plot, player); - sendGroupTipMessage(plot, player); - - if(plot.getPlotOwner().getUUID().equals(player.getUniqueId())) { - plot.setLastActivity(false); - } - } - public static void submitPlot(Plot plot) throws SQLException { plot.setStatus(Status.unreviewed); @@ -98,15 +70,14 @@ public static void undoSubmit(Plot plot) throws SQLException { public static void abandonPlot(Plot plot) { try { - if (PlotManager.plotExists(plot.getID())) { + if (plot.getWorld().isGenerated()) { plot.getWorld().load(); // Load Plot to be listed by Multiverse for (Player player : plot.getWorld().getBukkitWorld().getPlayers()) { player.teleport(Utils.getSpawnLocation()); } - PlotSystem.DependencyManager.getMultiverseCore().getMVWorldManager().deleteWorld(plot.getWorld().getName(), true, true); - PlotSystem.DependencyManager.getMultiverseCore().saveWorldConfig(); + plot.getWorld().delete(); } for (Builder builder : plot.getPlotMembers()) { @@ -128,13 +99,8 @@ public static void abandonPlot(Plot plot) { plot.setLastActivity(true); plot.setTotalScore(-1); plot.setStatus(Status.unclaimed); - - FileUtils.deleteDirectory(new File(PlotManager.getMultiverseInventoriesConfigPath(plot.getWorld().getName()))); - FileUtils.deleteDirectory(new File(PlotManager.getWorldGuardConfigPath(plot.getWorld().getName()))); } catch (SQLException ex) { Bukkit.getLogger().log(Level.SEVERE, "A SQL error occurred!", ex); - } catch (IOException ex) { - Bukkit.getLogger().log(Level.SEVERE, "A error occurred while deleting plot world and configs!", ex); } }).exceptionally(ex -> { Bukkit.getLogger().log(Level.SEVERE, "Failed to abandon plot!", ex); @@ -146,14 +112,14 @@ public static void abandonPlot(Plot plot) { } public static void deletePlot(Plot plot) throws SQLException { - if (PlotManager.plotExists(plot.getID())) abandonPlot(plot); + if (plot.getWorld().isGenerated()) abandonPlot(plot); if (CompletableFuture.supplyAsync(() -> { try { - Files.deleteIfExists(Paths.get(PlotManager.getDefaultSchematicPath(), String.valueOf(plot.getCity().getCountry().getServer().getID()), "finishedSchematics", String.valueOf(plot.getCity().getID()), plot.getID() + ".schematic")); - Files.deleteIfExists(Paths.get(PlotManager.getDefaultSchematicPath(), String.valueOf(plot.getCity().getCountry().getServer().getID()), String.valueOf(plot.getCity().getID()), plot.getID() + ".schematic")); - Server plotServer = plot.getCity().getCountry().getServer(); + + Files.deleteIfExists(Paths.get(PlotManager.getDefaultSchematicPath(), String.valueOf(plotServer.getID()), "finishedSchematics", String.valueOf(plot.getCity().getID()), plot.getID() + ".schematic")); + Files.deleteIfExists(Paths.get(PlotManager.getDefaultSchematicPath(), String.valueOf(plotServer.getID()), String.valueOf(plot.getCity().getID()), plot.getID() + ".schematic")); if (plotServer.getFTPConfiguration() != null) { return FTPManager.deleteSchematics(FTPManager.getFTPUrl(plotServer, plot.getCity().getID()), plot.getID() + ".schematic", false); } @@ -171,18 +137,6 @@ public static void deletePlot(Plot plot) throws SQLException { }).join() == null) throw new SQLException(); } - public static Location getPlotSpawnPoint(Plot plot) { - Location spawnLocation = new Location(plot.getWorld().getBukkitWorld(), - PlotManager.getPlotCenter().getX() + 0.5, - 30, - PlotManager.getPlotCenter().getZ() + 0.5, - -90, - 90); - // Set spawn point 1 block above the highest center point - spawnLocation.setY(plot.getWorld().getBukkitWorld().getHighestBlockYAt((int) spawnLocation.getX(), (int) spawnLocation.getZ()) + 1); - return spawnLocation; - } - public static void sendLinkMessages(Plot plot, Player player){ TextComponent[] tc = new TextComponent[3]; tc[0] = new TextComponent(); diff --git a/src/main/java/com/alpsbte/plotsystem/core/system/plot/generator/AbstractPlotGenerator.java b/src/main/java/com/alpsbte/plotsystem/core/system/plot/generator/AbstractPlotGenerator.java index 46642fcd..81acb82c 100644 --- a/src/main/java/com/alpsbte/plotsystem/core/system/plot/generator/AbstractPlotGenerator.java +++ b/src/main/java/com/alpsbte/plotsystem/core/system/plot/generator/AbstractPlotGenerator.java @@ -166,7 +166,7 @@ protected void generateOutlines(File plotSchematic) { Operations.complete(operation); editSession.flushQueue(); - plot.getWorld().getBukkitWorld().setSpawnLocation(PlotHandler.getPlotSpawnPoint(plot)); + plot.getWorld().getBukkitWorld().setSpawnLocation(plot.getWorld().getSpawnPoint()); } } catch (IOException | WorldEditException ex) { Bukkit.getLogger().log(Level.SEVERE, "An error occurred while generating plot outlines!", ex); diff --git a/src/main/java/com/alpsbte/plotsystem/core/system/plot/generator/DefaultPlotGenerator.java b/src/main/java/com/alpsbte/plotsystem/core/system/plot/generator/DefaultPlotGenerator.java index 39154c1e..1b8c57c6 100644 --- a/src/main/java/com/alpsbte/plotsystem/core/system/plot/generator/DefaultPlotGenerator.java +++ b/src/main/java/com/alpsbte/plotsystem/core/system/plot/generator/DefaultPlotGenerator.java @@ -89,7 +89,7 @@ protected void onComplete(boolean failed) throws SQLException { if (!failed) { Bukkit.getScheduler().runTask(PlotSystem.getPlugin(), () -> { try { - PlotHandler.teleportPlayer(getPlot(), getBuilder().getPlayer()); + getPlot().getWorld().teleportPlayer(getBuilder().getPlayer()); Bukkit.broadcastMessage(Utils.getInfoMessageFormat("Created new plot§a for §6" + getPlot().getPlotOwner().getName() + "§a!")); } catch (SQLException ex) { ex.printStackTrace(); diff --git a/src/main/java/com/alpsbte/plotsystem/core/system/plot/world/IPlotWorld.java b/src/main/java/com/alpsbte/plotsystem/core/system/plot/world/IPlotWorld.java index 5e35df4a..ffb6d455 100644 --- a/src/main/java/com/alpsbte/plotsystem/core/system/plot/world/IPlotWorld.java +++ b/src/main/java/com/alpsbte/plotsystem/core/system/plot/world/IPlotWorld.java @@ -3,7 +3,9 @@ import com.alpsbte.plotsystem.core.system.Builder; import com.alpsbte.plotsystem.core.system.plot.generator.AbstractPlotGenerator; import com.sk89q.worldguard.protection.regions.ProtectedRegion; +import org.bukkit.Location; import org.bukkit.World; +import org.bukkit.entity.Player; import org.jetbrains.annotations.NotNull; public interface IPlotWorld { @@ -41,6 +43,19 @@ public interface IPlotWorld { */ boolean unload(boolean movePlayers); + /** + * Teleports a player to the spawn point of the plot + * @param player bukkit player + * @return true if player was teleported successfully + */ + boolean teleportPlayer(Player player); + + /** + * Returns the spawn point of the plot + * @return center coordinates of the plot + */ + Location getSpawnPoint(); + /** * @return Bukkit plot world */ diff --git a/src/main/java/com/alpsbte/plotsystem/core/system/plot/world/PlotWorld.java b/src/main/java/com/alpsbte/plotsystem/core/system/plot/world/PlotWorld.java index a6d1e4a5..21a775f3 100644 --- a/src/main/java/com/alpsbte/plotsystem/core/system/plot/world/PlotWorld.java +++ b/src/main/java/com/alpsbte/plotsystem/core/system/plot/world/PlotWorld.java @@ -1,8 +1,12 @@ package com.alpsbte.plotsystem.core.system.plot.world; import com.alpsbte.plotsystem.PlotSystem; +import com.alpsbte.plotsystem.core.menus.CompanionMenu; +import com.alpsbte.plotsystem.core.menus.ReviewMenu; import com.alpsbte.plotsystem.core.system.Builder; import com.alpsbte.plotsystem.core.system.plot.Plot; +import com.alpsbte.plotsystem.core.system.plot.PlotHandler; +import com.alpsbte.plotsystem.core.system.plot.PlotManager; import com.alpsbte.plotsystem.core.system.plot.generator.AbstractPlotGenerator; import com.alpsbte.plotsystem.core.system.plot.generator.DefaultPlotGenerator; import com.alpsbte.plotsystem.core.system.plot.generator.RawPlotGenerator; @@ -10,11 +14,15 @@ import com.sk89q.worldguard.bukkit.RegionContainer; import com.sk89q.worldguard.protection.managers.RegionManager; import com.sk89q.worldguard.protection.regions.ProtectedRegion; +import org.apache.commons.io.FileUtils; import org.bukkit.Bukkit; +import org.bukkit.Location; import org.bukkit.World; import org.bukkit.entity.Player; import org.jetbrains.annotations.NotNull; +import java.io.File; +import java.io.IOException; import java.sql.SQLException; import java.util.Locale; import java.util.logging.Level; @@ -56,8 +64,17 @@ public boolean regenerate(@NotNull Class ge @Override public boolean delete() { if (isGenerated() && unload(true)) - return PlotSystem.DependencyManager.getMultiverseCore().getMVWorldManager().deleteWorld(getName(), true, true) && - PlotSystem.DependencyManager.getMultiverseCore().saveWorldConfig(); + if (PlotSystem.DependencyManager.getMultiverseCore().getMVWorldManager().deleteWorld(getName(), true, true) && + PlotSystem.DependencyManager.getMultiverseCore().saveWorldConfig()) { + try { + FileUtils.deleteDirectory(new File(PlotManager.getMultiverseInventoriesConfigPath(plot.getWorld().getName()))); + FileUtils.deleteDirectory(new File(PlotManager.getWorldGuardConfigPath(plot.getWorld().getName()))); + } catch (IOException ex) { + Bukkit.getLogger().log(Level.WARNING, "An error occurred while deleting world configs of plot #" + plot.getID()); + return false; + } + return true; + } return false; } @@ -84,6 +101,49 @@ public boolean unload(boolean movePlayers) { return false; } + @Override + public boolean teleportPlayer(Player player) { + if (load()) { + try { + player.sendMessage(Utils.getInfoMessageFormat("Teleporting to plot §6#" + plot.getID())); + + player.teleport(getSpawnPoint()); + player.playSound(player.getLocation(), Utils.TeleportSound, 1, 1); + player.setAllowFlight(true); + player.setFlying(true); + + player.getInventory().setItem(8, CompanionMenu.getMenuItem()); + if(player.hasPermission("plotsystem.review")) { + player.getInventory().setItem(7, ReviewMenu.getMenuItem()); + } + + PlotHandler.sendLinkMessages(plot, player); + PlotHandler.sendGroupTipMessage(plot, player); + + if(plot.getPlotOwner().getUUID().equals(player.getUniqueId())) { + plot.setLastActivity(false); + } + return true; + } catch (SQLException ex) { + Bukkit.getLogger().log(Level.SEVERE, "A SQL error occurred!", ex); + } + } + return false; + } + + @Override + public Location getSpawnPoint() { + Location spawnLocation = new Location(plot.getWorld().getBukkitWorld(), + PlotManager.getPlotCenter().getX() + 0.5, + 30, + PlotManager.getPlotCenter().getZ() + 0.5, + -90, + 90); + // Set spawn point 1 block above the highest center point + spawnLocation.setY(plot.getWorld().getBukkitWorld().getHighestBlockYAt((int) spawnLocation.getX(), (int) spawnLocation.getZ()) + 1); + return spawnLocation; + } + @Override public World getBukkitWorld() { return Bukkit.getWorld(getName()); From 3ee33ebbb01f5127bc5200f27668cf398b387d79 Mon Sep 17 00:00:00 2001 From: LordTuxn Date: Thu, 6 Jan 2022 23:40:14 +0100 Subject: [PATCH 07/21] fix NullPointerException when submitting plot --- .../com/alpsbte/plotsystem/core/system/plot/PlotHandler.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/alpsbte/plotsystem/core/system/plot/PlotHandler.java b/src/main/java/com/alpsbte/plotsystem/core/system/plot/PlotHandler.java index 10b48e31..5b36c92d 100644 --- a/src/main/java/com/alpsbte/plotsystem/core/system/plot/PlotHandler.java +++ b/src/main/java/com/alpsbte/plotsystem/core/system/plot/PlotHandler.java @@ -48,7 +48,7 @@ public class PlotHandler { public static void submitPlot(Plot plot) throws SQLException { plot.setStatus(Status.unreviewed); - if(plot.getWorld() != null) { + if(plot.getWorld().isLoaded()) { for(Player player : plot.getWorld().getBukkitWorld().getPlayers()) { player.teleport(Utils.getSpawnLocation()); } From dd0b2f6e74e7457c4a44dbd176e64ca58b9990bb Mon Sep 17 00:00:00 2001 From: LordTuxn Date: Fri, 7 Jan 2022 13:41:56 +0100 Subject: [PATCH 08/21] add mvCore variable --- .../plotsystem/core/system/plot/world/PlotWorld.java | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/alpsbte/plotsystem/core/system/plot/world/PlotWorld.java b/src/main/java/com/alpsbte/plotsystem/core/system/plot/world/PlotWorld.java index 21a775f3..5450ef2b 100644 --- a/src/main/java/com/alpsbte/plotsystem/core/system/plot/world/PlotWorld.java +++ b/src/main/java/com/alpsbte/plotsystem/core/system/plot/world/PlotWorld.java @@ -11,6 +11,7 @@ import com.alpsbte.plotsystem.core.system.plot.generator.DefaultPlotGenerator; import com.alpsbte.plotsystem.core.system.plot.generator.RawPlotGenerator; import com.alpsbte.plotsystem.utils.Utils; +import com.onarandombox.MultiverseCore.MultiverseCore; import com.sk89q.worldguard.bukkit.RegionContainer; import com.sk89q.worldguard.protection.managers.RegionManager; import com.sk89q.worldguard.protection.regions.ProtectedRegion; @@ -30,9 +31,11 @@ public class PlotWorld implements IPlotWorld { private final Plot plot; + private final MultiverseCore mvCore; public PlotWorld(Plot plot) { this.plot = plot; + this.mvCore = PlotSystem.DependencyManager.getMultiverseCore(); } @Override @@ -64,8 +67,7 @@ public boolean regenerate(@NotNull Class ge @Override public boolean delete() { if (isGenerated() && unload(true)) - if (PlotSystem.DependencyManager.getMultiverseCore().getMVWorldManager().deleteWorld(getName(), true, true) && - PlotSystem.DependencyManager.getMultiverseCore().saveWorldConfig()) { + if (mvCore.getMVWorldManager().deleteWorld(getName(), true, true) && mvCore.saveWorldConfig()) { try { FileUtils.deleteDirectory(new File(PlotManager.getMultiverseInventoriesConfigPath(plot.getWorld().getName()))); FileUtils.deleteDirectory(new File(PlotManager.getWorldGuardConfigPath(plot.getWorld().getName()))); @@ -74,14 +76,14 @@ public boolean delete() { return false; } return true; - } + } else Bukkit.getLogger().log(Level.WARNING, "Could not delete world of plot #" + plot.getID()); return false; } @Override public boolean load() { if (isGenerated()) - return PlotSystem.DependencyManager.getMultiverseCore().getMVWorldManager().loadWorld(getName()) || isLoaded(); + return mvCore.getMVWorldManager().loadWorld(getName()) || isLoaded(); return false; } @@ -173,6 +175,6 @@ public boolean isLoaded() { @Override public boolean isGenerated() { - return PlotSystem.DependencyManager.getMultiverseCore().getMVWorldManager().getMVWorld(getName()) != null || PlotSystem.DependencyManager.getMultiverseCore().getMVWorldManager().getUnloadedWorlds().contains(getName()); + return mvCore.getMVWorldManager().getMVWorld(getName()) != null || mvCore.getMVWorldManager().getUnloadedWorlds().contains(getName()); } } From 2ef3bd04ce10f75442c5d5cb6109db9bad65d6a6 Mon Sep 17 00:00:00 2001 From: LordTuxn Date: Fri, 7 Jan 2022 14:21:48 +0100 Subject: [PATCH 09/21] rename methods and functions in IPlotWorld --- .../plotsystem/core/EventListener.java | 6 +-- .../core/system/plot/PlotHandler.java | 10 ++-- .../core/system/plot/PlotManager.java | 2 +- .../core/system/plot/PlotPermissions.java | 2 +- .../plot/generator/AbstractPlotGenerator.java | 10 ++-- .../core/system/plot/world/IPlotWorld.java | 16 +++--- .../core/system/plot/world/PlotWorld.java | 53 ++++++++++--------- 7 files changed, 50 insertions(+), 49 deletions(-) diff --git a/src/main/java/com/alpsbte/plotsystem/core/EventListener.java b/src/main/java/com/alpsbte/plotsystem/core/EventListener.java index 2dc41172..1eb2eb5a 100644 --- a/src/main/java/com/alpsbte/plotsystem/core/EventListener.java +++ b/src/main/java/com/alpsbte/plotsystem/core/EventListener.java @@ -206,7 +206,7 @@ public void onPlayerInteractAtEntity(PlayerInteractAtEntityEvent event) throws S @EventHandler public void onPlayerQuitEvent(PlayerQuitEvent event) throws SQLException { if(PlotManager.isPlotWorld(event.getPlayer().getWorld())) { - PlotManager.getPlotByWorld(event.getPlayer().getWorld()).getWorld().unload(false); + PlotManager.getPlotByWorld(event.getPlayer().getWorld()).getWorld().unloadWorld(false); } DefaultPlotGenerator.playerPlotGenerationHistory.remove(event.getPlayer().getUniqueId()); } @@ -214,14 +214,14 @@ public void onPlayerQuitEvent(PlayerQuitEvent event) throws SQLException { @EventHandler public void onPlayerTeleportEvent(PlayerTeleportEvent event) throws SQLException { if(PlotManager.isPlotWorld(event.getPlayer().getWorld()) && !event.getFrom().getWorld().equals(event.getTo().getWorld())) { - PlotManager.getPlotByWorld(event.getFrom().getWorld()).getWorld().unload(false); + PlotManager.getPlotByWorld(event.getFrom().getWorld()).getWorld().unloadWorld(false); } } @EventHandler public void onPlayerChangedWorldEvent(PlayerChangedWorldEvent event) throws SQLException { if (PlotManager.isPlotWorld(event.getFrom())) { - PlotManager.getPlotByWorld(event.getFrom()).getWorld().unload(false); + PlotManager.getPlotByWorld(event.getFrom()).getWorld().unloadWorld(false); } if (PlotManager.isPlotWorld(event.getPlayer().getWorld())) { diff --git a/src/main/java/com/alpsbte/plotsystem/core/system/plot/PlotHandler.java b/src/main/java/com/alpsbte/plotsystem/core/system/plot/PlotHandler.java index 5b36c92d..b74f2887 100644 --- a/src/main/java/com/alpsbte/plotsystem/core/system/plot/PlotHandler.java +++ b/src/main/java/com/alpsbte/plotsystem/core/system/plot/PlotHandler.java @@ -48,7 +48,7 @@ public class PlotHandler { public static void submitPlot(Plot plot) throws SQLException { plot.setStatus(Status.unreviewed); - if(plot.getWorld().isLoaded()) { + if(plot.getWorld().isWorldLoaded()) { for(Player player : plot.getWorld().getBukkitWorld().getPlayers()) { player.teleport(Utils.getSpawnLocation()); } @@ -70,14 +70,14 @@ public static void undoSubmit(Plot plot) throws SQLException { public static void abandonPlot(Plot plot) { try { - if (plot.getWorld().isGenerated()) { - plot.getWorld().load(); // Load Plot to be listed by Multiverse + if (plot.getWorld().isWorldGenerated()) { + plot.getWorld().loadWorld(); // Load Plot to be listed by Multiverse for (Player player : plot.getWorld().getBukkitWorld().getPlayers()) { player.teleport(Utils.getSpawnLocation()); } - plot.getWorld().delete(); + plot.getWorld().deleteWorld(); } for (Builder builder : plot.getPlotMembers()) { @@ -112,7 +112,7 @@ public static void abandonPlot(Plot plot) { } public static void deletePlot(Plot plot) throws SQLException { - if (plot.getWorld().isGenerated()) abandonPlot(plot); + if (plot.getWorld().isWorldGenerated()) abandonPlot(plot); if (CompletableFuture.supplyAsync(() -> { try { diff --git a/src/main/java/com/alpsbte/plotsystem/core/system/plot/PlotManager.java b/src/main/java/com/alpsbte/plotsystem/core/system/plot/PlotManager.java index 505222d7..71f3ed02 100644 --- a/src/main/java/com/alpsbte/plotsystem/core/system/plot/PlotManager.java +++ b/src/main/java/com/alpsbte/plotsystem/core/system/plot/PlotManager.java @@ -193,7 +193,7 @@ public static CompletableFuture savePlotAsSchematic(Plot plot) throws IOEx // Load finished plot region as cuboid region - plot.getWorld().load(); + plot.getWorld().loadWorld(); CuboidRegion region = new CuboidRegion(new BukkitWorld(plot.getWorld().getBukkitWorld()), schematicMinPoint, schematicMaxPoint); diff --git a/src/main/java/com/alpsbte/plotsystem/core/system/plot/PlotPermissions.java b/src/main/java/com/alpsbte/plotsystem/core/system/plot/PlotPermissions.java index 82822f14..418e9a3d 100644 --- a/src/main/java/com/alpsbte/plotsystem/core/system/plot/PlotPermissions.java +++ b/src/main/java/com/alpsbte/plotsystem/core/system/plot/PlotPermissions.java @@ -66,6 +66,6 @@ public boolean hasReviewerPerms() { } public void save() { - plotWorld.unload(true); + plotWorld.unloadWorld(true); } } diff --git a/src/main/java/com/alpsbte/plotsystem/core/system/plot/generator/AbstractPlotGenerator.java b/src/main/java/com/alpsbte/plotsystem/core/system/plot/generator/AbstractPlotGenerator.java index 81acb82c..f5197457 100644 --- a/src/main/java/com/alpsbte/plotsystem/core/system/plot/generator/AbstractPlotGenerator.java +++ b/src/main/java/com/alpsbte/plotsystem/core/system/plot/generator/AbstractPlotGenerator.java @@ -104,7 +104,7 @@ public AbstractPlotGenerator(@NotNull Plot plot, @NotNull Builder builder) { } if (exception != null) { - if (worldManager.isMVWorld(plot.getWorld().getName())) PlotHandler.abandonPlot(plot); + if (worldManager.isMVWorld(plot.getWorld().getWorldName())) PlotHandler.abandonPlot(plot); onException(exception); } }); @@ -121,9 +121,9 @@ public AbstractPlotGenerator(@NotNull Plot plot, @NotNull Builder builder) { * Generates plot world */ protected void generateWorld() { - if (PlotManager.plotExists(plot.getID())) PlotHandler.abandonPlot(plot); + if (getPlot().getWorld().isWorldGenerated()) plot.getWorld().deleteWorld(); - worldCreator = new WorldCreator(plot.getWorld().getName()); + worldCreator = new WorldCreator(plot.getWorld().getWorldName()); worldCreator.environment(org.bukkit.World.Environment.NORMAL); worldCreator.type(WorldType.FLAT); worldCreator.generatorSettings("2;0;1;"); @@ -136,7 +136,7 @@ protected void generateWorld() { protected void createMultiverseWorld() { // Check if world creator is configured and add new world to multiverse world manager if (worldCreator != null) { - worldManager.addWorld(plot.getWorld().getName(), worldCreator.environment(), null, worldCreator.type(), false, + worldManager.addWorld(plot.getWorld().getWorldName(), worldCreator.environment(), null, worldCreator.type(), false, "VoidGen:{\"caves\":false,\"decoration\":false,\"mobs\":false,\"structures\":false}"); } else { throw new RuntimeException("World Creator is not configured"); @@ -218,7 +218,7 @@ protected void createProtection() { globalRegion.setFlag(DefaultFlag.ENTRY.getRegionGroupFlag(), RegionGroup.ALL); // Create protected region for plot - ProtectedRegion protectedPlotRegion = new ProtectedCuboidRegion(plot.getWorld().getName(), min, max); + ProtectedRegion protectedPlotRegion = new ProtectedCuboidRegion(plot.getWorld().getWorldName(), min, max); protectedPlotRegion.setPriority(100); // Add and save regions diff --git a/src/main/java/com/alpsbte/plotsystem/core/system/plot/world/IPlotWorld.java b/src/main/java/com/alpsbte/plotsystem/core/system/plot/world/IPlotWorld.java index ffb6d455..a9c2b92a 100644 --- a/src/main/java/com/alpsbte/plotsystem/core/system/plot/world/IPlotWorld.java +++ b/src/main/java/com/alpsbte/plotsystem/core/system/plot/world/IPlotWorld.java @@ -15,33 +15,33 @@ public interface IPlotWorld { * @param generator generator type as class * @return true if world was generated successfully */ - boolean generate(@NotNull Builder plotOwner, @NotNull Class generator); + boolean generateWorld(@NotNull Builder plotOwner, @NotNull Class generator); /** * Regenerates the current plot with an optional new generator type * @param generator generator type as class * @return true if world was regenerated successfully */ - boolean regenerate(@NotNull Class generator); + boolean regenWorld(@NotNull Class generator); /** * Deletes the world file and entry in the config file * @return true if world was deleted successfully */ - boolean delete(); + boolean deleteWorld(); /** * Loads the plot world to memory to be used. Plot has to be generated. * @return true if world was loaded successfully */ - boolean load(); + boolean loadWorld(); /** * Unloads the plot world from memory. Plot cannot be used anymore. Plot has to be generated. * @param movePlayers - if true, players will get teleported to the spawn location. Otherwise, plot will not get unloaded. * @return true if world was loaded successfully */ - boolean unload(boolean movePlayers); + boolean unloadWorld(boolean movePlayers); /** * Teleports a player to the spawn point of the plot @@ -65,7 +65,7 @@ public interface IPlotWorld { * Returns plot world name in the format (for example: P-23) * @return world name of the plot */ - String getName(); + String getWorldName(); /** * Loads the protected plot world region from WorldGuard config @@ -77,11 +77,11 @@ public interface IPlotWorld { * Checks if the plot world is loaded to memory. If the plot world has not yet been generated, it will return false. * @return true if world is loaded */ - boolean isLoaded(); + boolean isWorldLoaded(); /** * Checks if the plot world is generated. * @return true if world is generated */ - boolean isGenerated(); + boolean isWorldGenerated(); } diff --git a/src/main/java/com/alpsbte/plotsystem/core/system/plot/world/PlotWorld.java b/src/main/java/com/alpsbte/plotsystem/core/system/plot/world/PlotWorld.java index 5450ef2b..fdc60a83 100644 --- a/src/main/java/com/alpsbte/plotsystem/core/system/plot/world/PlotWorld.java +++ b/src/main/java/com/alpsbte/plotsystem/core/system/plot/world/PlotWorld.java @@ -39,8 +39,8 @@ public PlotWorld(Plot plot) { } @Override - public boolean generate(@NotNull Builder plotOwner, @NotNull Class generator) { - if (!isGenerated()) { + public boolean generateWorld(@NotNull Builder plotOwner, @NotNull Class generator) { + if (!isWorldGenerated()) { if (generator.isInstance(DefaultPlotGenerator.class)) { new DefaultPlotGenerator(plot, plotOwner); } else if (generator.isInstance(RawPlotGenerator.class)) { @@ -52,10 +52,10 @@ public boolean generate(@NotNull Builder plotO } @Override - public boolean regenerate(@NotNull Class generator) { - if (isGenerated() && unload(true)) { + public boolean regenWorld(@NotNull Class generator) { + if (isWorldGenerated() && unloadWorld(true)) { try { - if (delete() && generate(plot.getPlotOwner(), generator)) + if (deleteWorld() && generateWorld(plot.getPlotOwner(), generator)) return true; } catch (SQLException ex) { Bukkit.getLogger().log(Level.SEVERE, "A SQL error occurred!", ex); @@ -65,47 +65,48 @@ public boolean regenerate(@NotNull Class ge } @Override - public boolean delete() { - if (isGenerated() && unload(true)) - if (mvCore.getMVWorldManager().deleteWorld(getName(), true, true) && mvCore.saveWorldConfig()) { + public boolean deleteWorld() { + if (isWorldGenerated() && unloadWorld(true)) { + if (mvCore.getMVWorldManager().deleteWorld(getWorldName(), true, true) && mvCore.saveWorldConfig()) { try { - FileUtils.deleteDirectory(new File(PlotManager.getMultiverseInventoriesConfigPath(plot.getWorld().getName()))); - FileUtils.deleteDirectory(new File(PlotManager.getWorldGuardConfigPath(plot.getWorld().getName()))); + FileUtils.deleteDirectory(new File(PlotManager.getMultiverseInventoriesConfigPath(plot.getWorld().getWorldName()))); + FileUtils.deleteDirectory(new File(PlotManager.getWorldGuardConfigPath(plot.getWorld().getWorldName()))); } catch (IOException ex) { Bukkit.getLogger().log(Level.WARNING, "An error occurred while deleting world configs of plot #" + plot.getID()); return false; } return true; } else Bukkit.getLogger().log(Level.WARNING, "Could not delete world of plot #" + plot.getID()); + } else Bukkit.getLogger().log(Level.WARNING, "Could not delete world of plot #" + plot.getID() + ", because it is not generated or unloaded!"); return false; } @Override - public boolean load() { - if (isGenerated()) - return mvCore.getMVWorldManager().loadWorld(getName()) || isLoaded(); + public boolean loadWorld() { + if (isWorldGenerated()) + return mvCore.getMVWorldManager().loadWorld(getWorldName()) || isWorldLoaded(); return false; } @Override - public boolean unload(boolean movePlayers) { - if(isLoaded()) { + public boolean unloadWorld(boolean movePlayers) { + if(isWorldLoaded()) { if (movePlayers && !getBukkitWorld().getPlayers().isEmpty()) { for (Player player : getBukkitWorld().getPlayers()) { player.teleport(Utils.getSpawnLocation()); } - } else if (!movePlayers && !getBukkitWorld().getPlayers().isEmpty()) return false; + } else return false; Bukkit.getScheduler().scheduleSyncDelayedTask(PlotSystem.getPlugin(), (() -> Bukkit.unloadWorld(getBukkitWorld(), true)), 60L); - return !isLoaded(); + return !isWorldLoaded(); } - return false; + return true; } @Override public boolean teleportPlayer(Player player) { - if (load()) { + if (loadWorld()) { try { player.sendMessage(Utils.getInfoMessageFormat("Teleporting to plot §6#" + plot.getID())); @@ -148,33 +149,33 @@ public Location getSpawnPoint() { @Override public World getBukkitWorld() { - return Bukkit.getWorld(getName()); + return Bukkit.getWorld(getWorldName()); } @Override - public String getName() { + public String getWorldName() { return "P-" + plot.getID(); } @Override public ProtectedRegion getProtectedRegion() { RegionContainer container = PlotSystem.DependencyManager.getWorldGuard().getRegionContainer(); - if (load()) { + if (loadWorld()) { RegionManager regionManager = container.get(getBukkitWorld()); if (regionManager != null) { - return regionManager.getRegion(getName().toLowerCase(Locale.ROOT)); + return regionManager.getRegion(getWorldName().toLowerCase(Locale.ROOT)); } else Bukkit.getLogger().log(Level.WARNING, "Region manager is null"); } return null; } @Override - public boolean isLoaded() { + public boolean isWorldLoaded() { return getBukkitWorld() != null; } @Override - public boolean isGenerated() { - return mvCore.getMVWorldManager().getMVWorld(getName()) != null || mvCore.getMVWorldManager().getUnloadedWorlds().contains(getName()); + public boolean isWorldGenerated() { + return mvCore.getMVWorldManager().getMVWorld(getWorldName()) != null || mvCore.getMVWorldManager().getUnloadedWorlds().contains(getWorldName()); } } From b1b60d6b08384449752d594548fd3bd0f93b64b4 Mon Sep 17 00:00:00 2001 From: LordTuxn Date: Fri, 7 Jan 2022 17:00:12 +0100 Subject: [PATCH 10/21] fix plot worlds not unloading --- .../alpsbte/plotsystem/core/system/plot/world/PlotWorld.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/alpsbte/plotsystem/core/system/plot/world/PlotWorld.java b/src/main/java/com/alpsbte/plotsystem/core/system/plot/world/PlotWorld.java index fdc60a83..a73ccee3 100644 --- a/src/main/java/com/alpsbte/plotsystem/core/system/plot/world/PlotWorld.java +++ b/src/main/java/com/alpsbte/plotsystem/core/system/plot/world/PlotWorld.java @@ -95,7 +95,7 @@ public boolean unloadWorld(boolean movePlayers) { for (Player player : getBukkitWorld().getPlayers()) { player.teleport(Utils.getSpawnLocation()); } - } else return false; + } Bukkit.getScheduler().scheduleSyncDelayedTask(PlotSystem.getPlugin(), (() -> Bukkit.unloadWorld(getBukkitWorld(), true)), 60L); From 8268a57b0888fbe63ed54a9a44eb2b30cfd6c96b Mon Sep 17 00:00:00 2001 From: LordTuxn Date: Fri, 7 Jan 2022 17:00:28 +0100 Subject: [PATCH 11/21] some optimisations --- .../core/system/plot/world/PlotWorld.java | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/src/main/java/com/alpsbte/plotsystem/core/system/plot/world/PlotWorld.java b/src/main/java/com/alpsbte/plotsystem/core/system/plot/world/PlotWorld.java index a73ccee3..9f6d9854 100644 --- a/src/main/java/com/alpsbte/plotsystem/core/system/plot/world/PlotWorld.java +++ b/src/main/java/com/alpsbte/plotsystem/core/system/plot/world/PlotWorld.java @@ -53,20 +53,18 @@ public boolean generateWorld(@NotNull Builder @Override public boolean regenWorld(@NotNull Class generator) { - if (isWorldGenerated() && unloadWorld(true)) { - try { - if (deleteWorld() && generateWorld(plot.getPlotOwner(), generator)) - return true; - } catch (SQLException ex) { - Bukkit.getLogger().log(Level.SEVERE, "A SQL error occurred!", ex); - } + try { + if (deleteWorld() && generateWorld(plot.getPlotOwner(), generator)) + return true; + } catch (SQLException ex) { + Bukkit.getLogger().log(Level.SEVERE, "A SQL error occurred!", ex); } return false; } @Override public boolean deleteWorld() { - if (isWorldGenerated() && unloadWorld(true)) { + if (isWorldGenerated() && loadWorld()) { if (mvCore.getMVWorldManager().deleteWorld(getWorldName(), true, true) && mvCore.saveWorldConfig()) { try { FileUtils.deleteDirectory(new File(PlotManager.getMultiverseInventoriesConfigPath(plot.getWorld().getWorldName()))); From f7c3446b6b4f831805b4e4b7bdff7974aab50ce8 Mon Sep 17 00:00:00 2001 From: LordTuxn Date: Sat, 8 Jan 2022 01:35:36 +0100 Subject: [PATCH 12/21] Fix bug in getReview() --- .../com/alpsbte/plotsystem/core/system/plot/Plot.java | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/alpsbte/plotsystem/core/system/plot/Plot.java b/src/main/java/com/alpsbte/plotsystem/core/system/plot/Plot.java index 895fdd19..189ebf36 100644 --- a/src/main/java/com/alpsbte/plotsystem/core/system/plot/Plot.java +++ b/src/main/java/com/alpsbte/plotsystem/core/system/plot/Plot.java @@ -147,12 +147,13 @@ public void setPlotMembers(@NotNull List plotMembers) throws SQLExcepti @Override public Review getReview() throws SQLException { - if (getStatus() == Status.completed) - try (ResultSet rs = DatabaseConnection.createStatement("SELECT review_id FROM plotsystem_plots WHERE id = ?") - .setValue(this.ID).executeQuery()) { + if(getStatus() == Status.completed || isRejected()) { + try (ResultSet rs = DatabaseConnection.createStatement("SELECT review_id FROM plotsystem_plots WHERE id = ?") + .setValue(this.ID).executeQuery()) { - if (rs.next()) { - return new Review(rs.getInt(1)); + if (rs.next()) { + return new Review(rs.getInt(1)); + } } } return null; From 532481099f6f74655d2cf62813b270f0fc78734d Mon Sep 17 00:00:00 2001 From: LordTuxn Date: Sat, 8 Jan 2022 01:36:47 +0100 Subject: [PATCH 13/21] Fix bug in ReviewPlotMenu and add few optimisations --- .../plotsystem/core/menus/ReviewPlotMenu.java | 18 +++++++++--------- .../core/system/plot/PlotManager.java | 6 +++--- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/src/main/java/com/alpsbte/plotsystem/core/menus/ReviewPlotMenu.java b/src/main/java/com/alpsbte/plotsystem/core/menus/ReviewPlotMenu.java index dfb7726b..2889464d 100644 --- a/src/main/java/com/alpsbte/plotsystem/core/menus/ReviewPlotMenu.java +++ b/src/main/java/com/alpsbte/plotsystem/core/menus/ReviewPlotMenu.java @@ -298,19 +298,19 @@ protected void setItemClickEventsAsync() { clickPlayer.closeInventory(); if (!isRejected) { clickPlayer.sendMessage(Utils.getInfoMessageFormat("Saving plot...")); - if (CompletableFuture.supplyAsync(() -> { - try { - return PlotManager.savePlotAsSchematic(plot); - } catch (IOException | SQLException | WorldEditException ex) { + try { + if (!PlotManager.savePlotAsSchematic(plot)) { + clickPlayer.sendMessage(Utils.getErrorMessageFormat("Could not save plot. Please try again!")); Bukkit.getLogger().log(Level.WARNING, "Could not save finished plot schematic (ID: " + plot.getID() + ")!"); - clickPlayer.sendMessage(Utils.getErrorMessageFormat("An error occurred while saving plot schematic!")); + return; } - return null; - }).join() == null) return; + } catch (IOException | SQLException | WorldEditException ex) { + Bukkit.getLogger().log(Level.WARNING, "Could not save finished plot schematic (ID: " + plot.getID() + ")!", ex); + } + plot.setStatus(Status.completed); plot.getReview().setFeedbackSent(false); plot.getReview().setFeedback("No Feedback"); - plot.setStatus(Status.completed); plot.getPlotOwner().addCompletedBuild(1); // Remove Plot from Owner @@ -364,7 +364,7 @@ protected void setItemClickEventsAsync() { } Bukkit.getScheduler().runTask(PlotSystem.getPlugin(), () -> { - for(Player player : clickPlayer.getWorld().getPlayers()) { + for(Player player : plot.getWorld().getBukkitWorld().getPlayers()) { player.teleport(Utils.getSpawnLocation()); } diff --git a/src/main/java/com/alpsbte/plotsystem/core/system/plot/PlotManager.java b/src/main/java/com/alpsbte/plotsystem/core/system/plot/PlotManager.java index 71f3ed02..ab334d69 100644 --- a/src/main/java/com/alpsbte/plotsystem/core/system/plot/PlotManager.java +++ b/src/main/java/com/alpsbte/plotsystem/core/system/plot/PlotManager.java @@ -141,7 +141,7 @@ private static List listPlots(ResultSet rs) throws SQLException { return plots; } - public static CompletableFuture savePlotAsSchematic(Plot plot) throws IOException, SQLException, WorldEditException { + public static boolean savePlotAsSchematic(Plot plot) throws IOException, SQLException, WorldEditException { // TODO: MOVE CONVERSION TO SEPERATE METHODS Vector terraOrigin, schematicOrigin, plotOrigin; @@ -211,7 +211,7 @@ public static CompletableFuture savePlotAsSchematic(Plot plot) throws IOEx boolean createdDirs = finishedSchematicFile.getParentFile().mkdirs(); boolean createdFile = finishedSchematicFile.createNewFile(); if ((!finishedSchematicFile.getParentFile().exists() && !createdDirs) || (!finishedSchematicFile.exists() && !createdFile)) { - return CompletableFuture.completedFuture(null); + return false; } } @@ -231,7 +231,7 @@ public static CompletableFuture savePlotAsSchematic(Plot plot) throws IOEx }); } - return CompletableFuture.completedFuture(null); + return true; } public static CompletableFuture convertTerraToPlotXZ(Plot plot, double[] terraCoords) throws IOException { From e1a911219f9d807ce102444bea51ade68c2e900b Mon Sep 17 00:00:00 2001 From: LordTuxn Date: Sat, 8 Jan 2022 01:37:19 +0100 Subject: [PATCH 14/21] Add first implementations of the new plot handling system --- .../plotsystem/core/menus/ReviewPlotMenu.java | 3 ++ .../plot/generator/DefaultPlotGenerator.java | 27 +++++++++++++++-- .../core/system/plot/world/PlotWorld.java | 30 +++++++++++++------ 3 files changed, 49 insertions(+), 11 deletions(-) diff --git a/src/main/java/com/alpsbte/plotsystem/core/menus/ReviewPlotMenu.java b/src/main/java/com/alpsbte/plotsystem/core/menus/ReviewPlotMenu.java index 2889464d..33368eaf 100644 --- a/src/main/java/com/alpsbte/plotsystem/core/menus/ReviewPlotMenu.java +++ b/src/main/java/com/alpsbte/plotsystem/core/menus/ReviewPlotMenu.java @@ -368,6 +368,9 @@ protected void setItemClickEventsAsync() { player.teleport(Utils.getSpawnLocation()); } + // Delete plot world after reviewing + plot.getWorld().deleteWorld(); + clickPlayer.sendMessage(reviewerConfirmationMessage); clickPlayer.playSound(clickPlayer.getLocation(), Utils.FinishPlotSound, 1f, 1f); }); diff --git a/src/main/java/com/alpsbte/plotsystem/core/system/plot/generator/DefaultPlotGenerator.java b/src/main/java/com/alpsbte/plotsystem/core/system/plot/generator/DefaultPlotGenerator.java index 1b8c57c6..afef6002 100644 --- a/src/main/java/com/alpsbte/plotsystem/core/system/plot/generator/DefaultPlotGenerator.java +++ b/src/main/java/com/alpsbte/plotsystem/core/system/plot/generator/DefaultPlotGenerator.java @@ -27,13 +27,14 @@ import com.alpsbte.plotsystem.PlotSystem; import com.alpsbte.plotsystem.core.system.Builder; import com.alpsbte.plotsystem.core.system.plot.Plot; -import com.alpsbte.plotsystem.core.system.plot.PlotHandler; import com.alpsbte.plotsystem.core.system.plot.PlotManager; import com.alpsbte.plotsystem.utils.Utils; import com.alpsbte.plotsystem.utils.enums.PlotDifficulty; import com.alpsbte.plotsystem.utils.enums.Status; import org.bukkit.Bukkit; +import org.jetbrains.annotations.NotNull; +import java.io.File; import java.sql.SQLException; import java.time.LocalDateTime; import java.util.HashMap; @@ -50,7 +51,7 @@ public DefaultPlotGenerator(int cityID, PlotDifficulty plotDifficulty, Builder b this(PlotManager.getPlots(cityID, plotDifficulty, Status.unclaimed).get(new Random().nextInt(PlotManager.getPlots(cityID, plotDifficulty, Status.unclaimed).size())), builder); } - public DefaultPlotGenerator(Plot plot, Builder builder) { + public DefaultPlotGenerator(@NotNull Plot plot, @NotNull Builder builder) { super(plot, builder); } @@ -97,4 +98,26 @@ protected void onComplete(boolean failed) throws SQLException { }); } } + + public static final class RawDefaultPlotGenerator extends DefaultPlotGenerator { + + public RawDefaultPlotGenerator(@NotNull Plot plot, @NotNull Builder builder) { + super(plot, builder); + } + + @Override + protected void generateOutlines(File plotSchematic) { + super.generateOutlines(getPlot().getFinishedSchematic()); + } + + @Override + protected boolean init() { + return true; + } + + @Override + protected void onComplete(boolean failed) throws SQLException { + super.onComplete(true); // This code sucks, you know it, and I know it. + } + } } diff --git a/src/main/java/com/alpsbte/plotsystem/core/system/plot/world/PlotWorld.java b/src/main/java/com/alpsbte/plotsystem/core/system/plot/world/PlotWorld.java index 9f6d9854..b25f8e19 100644 --- a/src/main/java/com/alpsbte/plotsystem/core/system/plot/world/PlotWorld.java +++ b/src/main/java/com/alpsbte/plotsystem/core/system/plot/world/PlotWorld.java @@ -41,9 +41,11 @@ public PlotWorld(Plot plot) { @Override public boolean generateWorld(@NotNull Builder plotOwner, @NotNull Class generator) { if (!isWorldGenerated()) { - if (generator.isInstance(DefaultPlotGenerator.class)) { - new DefaultPlotGenerator(plot, plotOwner); - } else if (generator.isInstance(RawPlotGenerator.class)) { + if (generator.isAssignableFrom(DefaultPlotGenerator.class)) { + new DefaultPlotGenerator(plot, plotOwner); + } else if (generator.isAssignableFrom(DefaultPlotGenerator.RawDefaultPlotGenerator.RawDefaultPlotGenerator.class)) { + new DefaultPlotGenerator.RawDefaultPlotGenerator(plot, plotOwner); + } else if (generator.isAssignableFrom(RawPlotGenerator.class)) { new RawPlotGenerator(plot, plotOwner); } else return false; return true; @@ -70,19 +72,29 @@ public boolean deleteWorld() { FileUtils.deleteDirectory(new File(PlotManager.getMultiverseInventoriesConfigPath(plot.getWorld().getWorldName()))); FileUtils.deleteDirectory(new File(PlotManager.getWorldGuardConfigPath(plot.getWorld().getWorldName()))); } catch (IOException ex) { - Bukkit.getLogger().log(Level.WARNING, "An error occurred while deleting world configs of plot #" + plot.getID()); + Bukkit.getLogger().log(Level.WARNING, "An error occurred while deleting world configs of plot #" + plot.getID() + "!"); return false; } return true; - } else Bukkit.getLogger().log(Level.WARNING, "Could not delete world of plot #" + plot.getID()); - } else Bukkit.getLogger().log(Level.WARNING, "Could not delete world of plot #" + plot.getID() + ", because it is not generated or unloaded!"); + } else Bukkit.getLogger().log(Level.WARNING, "Could not delete world of plot #" + plot.getID() + "!"); + } return false; } @Override public boolean loadWorld() { - if (isWorldGenerated()) - return mvCore.getMVWorldManager().loadWorld(getWorldName()) || isWorldLoaded(); + try { + // Generate plot if it doesn't exist + if (!isWorldGenerated() && !generateWorld(plot.getPlotOwner(), DefaultPlotGenerator.RawDefaultPlotGenerator.class)) { + Bukkit.getLogger().log(Level.WARNING, "Could not regenerate world of plot #" + plot.getID() + "!"); + return false; + } + + if (isWorldGenerated()) + return mvCore.getMVWorldManager().loadWorld(getWorldName()) || isWorldLoaded(); + } catch (SQLException ex) { + Bukkit.getLogger().log(Level.SEVERE, "A SQL error occurred!", ex); + } return false; } @@ -128,7 +140,7 @@ public boolean teleportPlayer(Player player) { } catch (SQLException ex) { Bukkit.getLogger().log(Level.SEVERE, "A SQL error occurred!", ex); } - } + } else player.sendMessage(Utils.getErrorMessageFormat("Could not load plot world. Please try again!")); return false; } From 3cd4f7ca8827a7df3d35fab7f2979f2c2d3b9488 Mon Sep 17 00:00:00 2001 From: LordTuxn Date: Sun, 9 Jan 2022 01:41:51 +0100 Subject: [PATCH 15/21] few optimisations --- .../commands/admin/CMD_DeletePlot.java | 9 +- .../commands/plot/CMD_Plot_Abandon.java | 8 +- .../commands/plot/CMD_Plot_Teleport.java | 9 +- .../core/system/plot/PlotHandler.java | 110 ++++++++++-------- 4 files changed, 73 insertions(+), 63 deletions(-) diff --git a/src/main/java/com/alpsbte/plotsystem/commands/admin/CMD_DeletePlot.java b/src/main/java/com/alpsbte/plotsystem/commands/admin/CMD_DeletePlot.java index 2bee926e..6054a67b 100644 --- a/src/main/java/com/alpsbte/plotsystem/commands/admin/CMD_DeletePlot.java +++ b/src/main/java/com/alpsbte/plotsystem/commands/admin/CMD_DeletePlot.java @@ -42,12 +42,13 @@ public boolean onCommand(CommandSender sender, Command cmd, String s, String[] a if(sender.hasPermission(getPermission())) { if(args.length > 0 && Utils.TryParseInt(args[0]) != null) { int plotID = Integer.parseInt(args[0]); - if(PlotManager.plotExists(plotID, true)) { + if(PlotManager.plotExists(plotID)) { try { sender.sendMessage(Utils.getInfoMessageFormat("Deleting plot...")); - PlotHandler.deletePlot(new Plot(plotID)); - sender.sendMessage(Utils.getInfoMessageFormat("Successfully deleted plot with the ID §6#" + plotID + "§a!")); - if (getPlayer(sender) != null) getPlayer(sender).playSound(getPlayer(sender).getLocation(), Utils.Done, 1f, 1f); + if (PlotHandler.deletePlot(new Plot(plotID))) { + sender.sendMessage(Utils.getInfoMessageFormat("Successfully deleted plot with the ID §6#" + plotID + "§a!")); + if (getPlayer(sender) != null) getPlayer(sender).playSound(getPlayer(sender).getLocation(), Utils.Done, 1f, 1f); + } else sender.sendMessage(Utils.getErrorMessageFormat("An unexpected error has occurred!")); } catch (SQLException ex) { sender.sendMessage(Utils.getErrorMessageFormat("An error occurred while executing command!")); Bukkit.getLogger().log(Level.SEVERE, "A SQL error occurred!", ex); diff --git a/src/main/java/com/alpsbte/plotsystem/commands/plot/CMD_Plot_Abandon.java b/src/main/java/com/alpsbte/plotsystem/commands/plot/CMD_Plot_Abandon.java index 64d52f42..22d2da47 100644 --- a/src/main/java/com/alpsbte/plotsystem/commands/plot/CMD_Plot_Abandon.java +++ b/src/main/java/com/alpsbte/plotsystem/commands/plot/CMD_Plot_Abandon.java @@ -64,10 +64,10 @@ public void onCommand(CommandSender sender, String[] args) { if (plot.getStatus() == Status.unfinished) { if (sender.hasPermission("plotsystem.review") || plot.getPlotOwner().getUUID().equals(getPlayer(sender).getUniqueId())) { - PlotHandler.abandonPlot(plot); - - sender.sendMessage(Utils.getInfoMessageFormat("Abandoned plot with the ID §6#" + plot.getID())); - if (getPlayer(sender) != null) getPlayer(sender).playSound(getPlayer(sender).getLocation(), Utils.AbandonPlotSound, 1, 1); + if (PlotHandler.abandonPlot(plot)) { + sender.sendMessage(Utils.getInfoMessageFormat("Abandoned plot with the ID §6#" + plot.getID())); + if (getPlayer(sender) != null) getPlayer(sender).playSound(getPlayer(sender).getLocation(), Utils.AbandonPlotSound, 1, 1); + } else sender.sendMessage(Utils.getErrorMessageFormat("An unexpected error has occurred!")); } else { sender.sendMessage(Utils.getErrorMessageFormat("You are not allowed to abandon this plot!")); } diff --git a/src/main/java/com/alpsbte/plotsystem/commands/plot/CMD_Plot_Teleport.java b/src/main/java/com/alpsbte/plotsystem/commands/plot/CMD_Plot_Teleport.java index 88eac000..03bacdb3 100644 --- a/src/main/java/com/alpsbte/plotsystem/commands/plot/CMD_Plot_Teleport.java +++ b/src/main/java/com/alpsbte/plotsystem/commands/plot/CMD_Plot_Teleport.java @@ -29,10 +29,10 @@ import com.alpsbte.plotsystem.commands.SubCommand; import com.alpsbte.plotsystem.core.system.Builder; import com.alpsbte.plotsystem.core.system.plot.Plot; -import com.alpsbte.plotsystem.core.system.plot.PlotHandler; import com.alpsbte.plotsystem.core.system.plot.PlotManager; import com.alpsbte.plotsystem.core.system.plot.generator.DefaultPlotGenerator; import com.alpsbte.plotsystem.utils.Utils; +import com.alpsbte.plotsystem.utils.enums.Status; import org.bukkit.Bukkit; import org.bukkit.ChatColor; import org.bukkit.command.CommandSender; @@ -52,10 +52,11 @@ public void onCommand(CommandSender sender, String[] args) { if (getPlayer(sender) != null) { if (args.length > 0 && Utils.TryParseInt(args[0]) != null) { int plotID = Integer.parseInt(args[0]); - if (PlotManager.plotExists(plotID)) { - new Plot(plotID).getWorld().teleportPlayer(getPlayer(sender)); + Plot plot; + if (PlotManager.plotExists(plotID) && (plot = new Plot(plotID)).getStatus() != Status.unclaimed) { + plot.getWorld().teleportPlayer(getPlayer(sender)); } else { - if (sender.hasPermission("plotsystem.admin") && PlotManager.plotExists(plotID, true)) { + if (sender.hasPermission("plotsystem.admin") && PlotManager.plotExists(plotID)) { new DefaultPlotGenerator(new Plot(plotID), new Builder(getPlayer(sender).getUniqueId())); } else sender.sendMessage(Utils.getErrorMessageFormat("This plot does not exist!")); diff --git a/src/main/java/com/alpsbte/plotsystem/core/system/plot/PlotHandler.java b/src/main/java/com/alpsbte/plotsystem/core/system/plot/PlotHandler.java index b74f2887..3c10ed17 100644 --- a/src/main/java/com/alpsbte/plotsystem/core/system/plot/PlotHandler.java +++ b/src/main/java/com/alpsbte/plotsystem/core/system/plot/PlotHandler.java @@ -41,6 +41,7 @@ import java.sql.SQLException; import java.util.List; import java.util.concurrent.CompletableFuture; +import java.util.concurrent.CompletionException; import java.util.logging.Level; public class PlotHandler { @@ -68,73 +69,80 @@ public static void undoSubmit(Plot plot) throws SQLException { plot.getPermissions().addBuilderPerms(plot.getPlotOwner().getUUID()).save(); } - public static void abandonPlot(Plot plot) { + public static boolean abandonPlot(Plot plot) { try { - if (plot.getWorld().isWorldGenerated()) { - plot.getWorld().loadWorld(); // Load Plot to be listed by Multiverse - + if (plot.getWorld().isWorldGenerated() && plot.getWorld().loadWorld()) { for (Player player : plot.getWorld().getBukkitWorld().getPlayers()) { player.teleport(Utils.getSpawnLocation()); } - plot.getWorld().deleteWorld(); - } - - for (Builder builder : plot.getPlotMembers()) { - plot.removePlotMember(builder); - } - - CompletableFuture.runAsync(() -> { - try { - if(plot.isReviewed()) { - DatabaseConnection.createStatement("UPDATE plotsystem_plots SET review_id = DEFAULT(review_id) WHERE id = ?") - .setValue(plot.getID()).executeUpdate(); - - DatabaseConnection.createStatement("DELETE FROM plotsystem_reviews WHERE id = ?") - .setValue(plot.getReview().getReviewID()).executeUpdate(); + if (plot.getWorld().deleteWorld()) { + for (Builder builder : plot.getPlotMembers()) { + plot.removePlotMember(builder); } - plot.getPlotOwner().removePlot(plot.getSlot()); - plot.setPlotOwner(null); - plot.setLastActivity(true); - plot.setTotalScore(-1); - plot.setStatus(Status.unclaimed); - } catch (SQLException ex) { - Bukkit.getLogger().log(Level.SEVERE, "A SQL error occurred!", ex); + try { + CompletableFuture.runAsync(() -> { + try { + if (plot.isReviewed()) { + DatabaseConnection.createStatement("UPDATE plotsystem_plots SET review_id = DEFAULT(review_id) WHERE id = ?") + .setValue(plot.getID()).executeUpdate(); + + DatabaseConnection.createStatement("DELETE FROM plotsystem_reviews WHERE id = ?") + .setValue(plot.getReview().getReviewID()).executeUpdate(); + } + + plot.getPlotOwner().removePlot(plot.getSlot()); + plot.setPlotOwner(null); + plot.setLastActivity(true); + plot.setTotalScore(-1); + plot.setStatus(Status.unclaimed); + } catch (SQLException ex) { + Bukkit.getLogger().log(Level.SEVERE, "A SQL error occurred!", ex); + throw new CompletionException(ex); + } + }).join(); + } catch (CompletionException ex) { + return false; + } + return true; } - }).exceptionally(ex -> { - Bukkit.getLogger().log(Level.SEVERE, "Failed to abandon plot!", ex); - return null; - }); + } } catch (SQLException ex) { Bukkit.getLogger().log(Level.SEVERE, "A SQL error occurred!", ex); } + Bukkit.getLogger().log(Level.WARNING, "Failed to abandon plot with the ID " + plot.getID() + "!"); + return false; } - public static void deletePlot(Plot plot) throws SQLException { - if (plot.getWorld().isWorldGenerated()) abandonPlot(plot); - - if (CompletableFuture.supplyAsync(() -> { + public static boolean deletePlot(Plot plot) throws SQLException { + if (plot.getWorld().isWorldGenerated() && abandonPlot(plot)) { try { - Server plotServer = plot.getCity().getCountry().getServer(); + CompletableFuture.runAsync(() -> { + try { + Server plotServer = plot.getCity().getCountry().getServer(); - Files.deleteIfExists(Paths.get(PlotManager.getDefaultSchematicPath(), String.valueOf(plotServer.getID()), "finishedSchematics", String.valueOf(plot.getCity().getID()), plot.getID() + ".schematic")); - Files.deleteIfExists(Paths.get(PlotManager.getDefaultSchematicPath(), String.valueOf(plotServer.getID()), String.valueOf(plot.getCity().getID()), plot.getID() + ".schematic")); - if (plotServer.getFTPConfiguration() != null) { - return FTPManager.deleteSchematics(FTPManager.getFTPUrl(plotServer, plot.getCity().getID()), plot.getID() + ".schematic", false); - } - } catch (IOException | SQLException | URISyntaxException ex) { - Bukkit.getLogger().log(Level.SEVERE, "A SQL error occurred!", ex); - } - return null; - }).whenComplete((result, failed) -> { - try { - DatabaseConnection.createStatement("DELETE FROM plotsystem_plots WHERE id = ?") - .setValue(plot.getID()).executeUpdate(); - } catch (SQLException ex) { - Bukkit.getLogger().log(Level.SEVERE, "A SQL error occurred!", ex); + Files.deleteIfExists(Paths.get(PlotManager.getDefaultSchematicPath(), String.valueOf(plotServer.getID()), "finishedSchematics", String.valueOf(plot.getCity().getID()), plot.getID() + ".schematic")); + Files.deleteIfExists(Paths.get(PlotManager.getDefaultSchematicPath(), String.valueOf(plotServer.getID()), String.valueOf(plot.getCity().getID()), plot.getID() + ".schematic")); + + if (plotServer.getFTPConfiguration() != null) { + FTPManager.deleteSchematics(FTPManager.getFTPUrl(plotServer, plot.getCity().getID()), plot.getID() + ".schematic", false); + } + + DatabaseConnection.createStatement("DELETE FROM plotsystem_plots WHERE id = ?") + .setValue(plot.getID()).executeUpdate(); + } catch (IOException | SQLException | URISyntaxException ex) { + Bukkit.getLogger().log(Level.SEVERE, ex.getMessage(), ex); + throw new CompletionException(ex); + } + }); + } catch (CompletionException ex) { + return false; } - }).join() == null) throw new SQLException(); + return true; + } + Bukkit.getLogger().log(Level.WARNING, "Failed to delete plot with the ID " + plot.getID() + "!"); + return false; } public static void sendLinkMessages(Plot plot, Player player){ From 1fac9945619d46985d7495f7fdef8f4220240dca Mon Sep 17 00:00:00 2001 From: LordTuxn Date: Sun, 9 Jan 2022 01:42:17 +0100 Subject: [PATCH 16/21] rework plotExists() function in PlotManager --- .../core/system/plot/PlotManager.java | 20 ++++++------------- 1 file changed, 6 insertions(+), 14 deletions(-) diff --git a/src/main/java/com/alpsbte/plotsystem/core/system/plot/PlotManager.java b/src/main/java/com/alpsbte/plotsystem/core/system/plot/PlotManager.java index ab334d69..351cb479 100644 --- a/src/main/java/com/alpsbte/plotsystem/core/system/plot/PlotManager.java +++ b/src/main/java/com/alpsbte/plotsystem/core/system/plot/PlotManager.java @@ -324,21 +324,13 @@ public static Plot getPlotByWorld(World plotWorld) throws SQLException { return new Plot(Integer.parseInt(plotWorld.getName().substring(2))); } - @Deprecated public static boolean plotExists(int ID) { - String worldName = "P-" + ID; - return (PlotSystem.DependencyManager.getMultiverseCore().getMVWorldManager().getMVWorld(worldName) != null) || PlotSystem.DependencyManager.getMultiverseCore().getMVWorldManager().getUnloadedWorlds().contains(worldName); - } - - public static boolean plotExists(int ID, boolean system) { - if (system) { - try (ResultSet rs = DatabaseConnection.createStatement("SELECT COUNT(id) FROM plotsystem_plots WHERE id = ?") - .setValue(ID).executeQuery()) { - if (rs.next() && rs.getInt(1) > 0) return true; - } catch (SQLException ex) { - Bukkit.getLogger().log(Level.SEVERE, "A SQL error occurred!", ex); - } - } else return plotExists(ID); + try (ResultSet rs = DatabaseConnection.createStatement("SELECT COUNT(id) FROM plotsystem_plots WHERE id = ?") + .setValue(ID).executeQuery()) { + if (rs.next() && rs.getInt(1) > 0) return true; + } catch (SQLException ex) { + Bukkit.getLogger().log(Level.SEVERE, "A SQL error occurred!", ex); + } return false; } From d71b09e415de3397d694e62dbc3b7d687524c51b Mon Sep 17 00:00:00 2001 From: LordTuxn Date: Sun, 9 Jan 2022 01:43:26 +0100 Subject: [PATCH 17/21] delete completed plots when unloading --- .../plotsystem/core/system/plot/world/PlotWorld.java | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/main/java/com/alpsbte/plotsystem/core/system/plot/world/PlotWorld.java b/src/main/java/com/alpsbte/plotsystem/core/system/plot/world/PlotWorld.java index b25f8e19..d428c472 100644 --- a/src/main/java/com/alpsbte/plotsystem/core/system/plot/world/PlotWorld.java +++ b/src/main/java/com/alpsbte/plotsystem/core/system/plot/world/PlotWorld.java @@ -11,6 +11,7 @@ import com.alpsbte.plotsystem.core.system.plot.generator.DefaultPlotGenerator; import com.alpsbte.plotsystem.core.system.plot.generator.RawPlotGenerator; import com.alpsbte.plotsystem.utils.Utils; +import com.alpsbte.plotsystem.utils.enums.Status; import com.onarandombox.MultiverseCore.MultiverseCore; import com.sk89q.worldguard.bukkit.RegionContainer; import com.sk89q.worldguard.protection.managers.RegionManager; @@ -107,6 +108,15 @@ public boolean unloadWorld(boolean movePlayers) { } } + try { + if (plot.getStatus() == Status.completed && getBukkitWorld().getPlayers().isEmpty()) { + deleteWorld(); + return true; + } + } catch (SQLException ex) { + Bukkit.getLogger().log(Level.SEVERE, "A SQL error occurred!", ex); + } + Bukkit.getScheduler().scheduleSyncDelayedTask(PlotSystem.getPlugin(), (() -> Bukkit.unloadWorld(getBukkitWorld(), true)), 60L); return !isWorldLoaded(); From 507bdeaa5299c66df2153ea1a90d4dd590d79a26 Mon Sep 17 00:00:00 2001 From: LordTuxn Date: Sun, 9 Jan 2022 01:44:58 +0100 Subject: [PATCH 18/21] fix few issues when teleporting to not generated plot --- .../core/system/plot/world/PlotWorld.java | 56 +++++++++++-------- 1 file changed, 33 insertions(+), 23 deletions(-) diff --git a/src/main/java/com/alpsbte/plotsystem/core/system/plot/world/PlotWorld.java b/src/main/java/com/alpsbte/plotsystem/core/system/plot/world/PlotWorld.java index d428c472..cbb5bd96 100644 --- a/src/main/java/com/alpsbte/plotsystem/core/system/plot/world/PlotWorld.java +++ b/src/main/java/com/alpsbte/plotsystem/core/system/plot/world/PlotWorld.java @@ -21,6 +21,7 @@ import org.bukkit.Location; import org.bukkit.World; import org.bukkit.entity.Player; +import org.bukkit.scheduler.BukkitRunnable; import org.jetbrains.annotations.NotNull; import java.io.File; @@ -89,10 +90,9 @@ public boolean loadWorld() { if (!isWorldGenerated() && !generateWorld(plot.getPlotOwner(), DefaultPlotGenerator.RawDefaultPlotGenerator.class)) { Bukkit.getLogger().log(Level.WARNING, "Could not regenerate world of plot #" + plot.getID() + "!"); return false; - } - - if (isWorldGenerated()) + } else if (isWorldGenerated()) return mvCore.getMVWorldManager().loadWorld(getWorldName()) || isWorldLoaded(); + return true; } catch (SQLException ex) { Bukkit.getLogger().log(Level.SEVERE, "A SQL error occurred!", ex); } @@ -127,29 +127,39 @@ public boolean unloadWorld(boolean movePlayers) { @Override public boolean teleportPlayer(Player player) { if (loadWorld()) { - try { player.sendMessage(Utils.getInfoMessageFormat("Teleporting to plot §6#" + plot.getID())); - player.teleport(getSpawnPoint()); - player.playSound(player.getLocation(), Utils.TeleportSound, 1, 1); - player.setAllowFlight(true); - player.setFlying(true); - - player.getInventory().setItem(8, CompanionMenu.getMenuItem()); - if(player.hasPermission("plotsystem.review")) { - player.getInventory().setItem(7, ReviewMenu.getMenuItem()); + long teleportDelay = isWorldGenerated() ? 0 : 10; + new BukkitRunnable() { + int counter = 0; + public void run(){ + counter++; + if (isWorldGenerated() || counter == teleportDelay) { + this.cancel(); + try { + player.teleport(getSpawnPoint()); + player.playSound(player.getLocation(), Utils.TeleportSound, 1, 1); + player.setAllowFlight(true); + player.setFlying(true); + + player.getInventory().setItem(8, CompanionMenu.getMenuItem()); + if(player.hasPermission("plotsystem.review")) { + player.getInventory().setItem(7, ReviewMenu.getMenuItem()); + } + + PlotHandler.sendLinkMessages(plot, player); + PlotHandler.sendGroupTipMessage(plot, player); + + if(plot.getPlotOwner().getUUID().equals(player.getUniqueId())) { + plot.setLastActivity(false); + } + } catch (SQLException ex) { + Bukkit.getLogger().log(Level.SEVERE, "A SQL error occurred!", ex); + } + } } - - PlotHandler.sendLinkMessages(plot, player); - PlotHandler.sendGroupTipMessage(plot, player); - - if(plot.getPlotOwner().getUUID().equals(player.getUniqueId())) { - plot.setLastActivity(false); - } - return true; - } catch (SQLException ex) { - Bukkit.getLogger().log(Level.SEVERE, "A SQL error occurred!", ex); - } + }.runTaskTimer(PlotSystem.getPlugin(), 0, teleportDelay); + return true; } else player.sendMessage(Utils.getErrorMessageFormat("Could not load plot world. Please try again!")); return false; } From 8fe9c2e1deea97e99fa0697a7579bc14cafa151b Mon Sep 17 00:00:00 2001 From: LordTuxn Date: Sun, 9 Jan 2022 14:59:12 +0100 Subject: [PATCH 19/21] fix plot world not unloading when quiting the server --- .../plotsystem/core/EventListener.java | 20 ++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/alpsbte/plotsystem/core/EventListener.java b/src/main/java/com/alpsbte/plotsystem/core/EventListener.java index 1eb2eb5a..2805f438 100644 --- a/src/main/java/com/alpsbte/plotsystem/core/EventListener.java +++ b/src/main/java/com/alpsbte/plotsystem/core/EventListener.java @@ -37,6 +37,7 @@ import com.alpsbte.plotsystem.utils.items.SpecialBlocks; import com.alpsbte.plotsystem.utils.Utils; import com.alpsbte.plotsystem.utils.enums.Status; +import com.onarandombox.MultiverseCore.MVWorld; import com.sk89q.worldguard.bukkit.RegionContainer; import com.sk89q.worldguard.bukkit.RegionQuery; import com.sk89q.worldguard.protection.flags.DefaultFlag; @@ -44,6 +45,7 @@ import me.arcaniax.hdb.api.HeadDatabaseAPI; import org.bukkit.Bukkit; import org.bukkit.Material; +import org.bukkit.World; import org.bukkit.block.BlockState; import org.bukkit.entity.EntityType; import org.bukkit.event.EventHandler; @@ -52,6 +54,7 @@ import org.bukkit.event.block.BlockPlaceEvent; import org.bukkit.event.inventory.InventoryClickEvent; import org.bukkit.event.player.*; +import org.bukkit.event.server.PluginDisableEvent; import org.bukkit.inventory.EquipmentSlot; import org.bukkit.inventory.ItemStack; import org.bukkit.material.TrapDoor; @@ -204,11 +207,18 @@ public void onPlayerInteractAtEntity(PlayerInteractAtEntityEvent event) throws S } @EventHandler - public void onPlayerQuitEvent(PlayerQuitEvent event) throws SQLException { - if(PlotManager.isPlotWorld(event.getPlayer().getWorld())) { - PlotManager.getPlotByWorld(event.getPlayer().getWorld()).getWorld().unloadWorld(false); - } - DefaultPlotGenerator.playerPlotGenerationHistory.remove(event.getPlayer().getUniqueId()); + public void onPlayerQuitEvent(PlayerQuitEvent event) { + final World w = event.getPlayer().getWorld(); + Bukkit.getScheduler().scheduleSyncDelayedTask(PlotSystem.getPlugin(), () -> { + if(PlotManager.isPlotWorld(w)) { + try { + PlotManager.getPlotByWorld(w).getWorld().unloadWorld(false); + } catch (SQLException ex) { + Bukkit.getLogger().log(Level.SEVERE, ex.getMessage(), ex); + } + DefaultPlotGenerator.playerPlotGenerationHistory.remove(event.getPlayer().getUniqueId()); + } + }, 60L); } @EventHandler From b3cd056e21497dcfb7728b09c856ec50c89525aa Mon Sep 17 00:00:00 2001 From: LordTuxn Date: Sun, 9 Jan 2022 15:41:44 +0100 Subject: [PATCH 20/21] add missing file headers --- .../plotsystem/core/system/plot/IPlot.java | 24 +++++++++++++++++++ .../core/system/plot/world/IPlotWorld.java | 24 +++++++++++++++++++ .../core/system/plot/world/PlotWorld.java | 24 +++++++++++++++++++ 3 files changed, 72 insertions(+) diff --git a/src/main/java/com/alpsbte/plotsystem/core/system/plot/IPlot.java b/src/main/java/com/alpsbte/plotsystem/core/system/plot/IPlot.java index 05aab5ab..af8629f6 100644 --- a/src/main/java/com/alpsbte/plotsystem/core/system/plot/IPlot.java +++ b/src/main/java/com/alpsbte/plotsystem/core/system/plot/IPlot.java @@ -1,3 +1,27 @@ +/* + * The MIT License (MIT) + * + * Copyright © 2021-2022, Alps BTE + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + package com.alpsbte.plotsystem.core.system.plot; import com.alpsbte.plotsystem.core.system.Builder; diff --git a/src/main/java/com/alpsbte/plotsystem/core/system/plot/world/IPlotWorld.java b/src/main/java/com/alpsbte/plotsystem/core/system/plot/world/IPlotWorld.java index a9c2b92a..03d0845d 100644 --- a/src/main/java/com/alpsbte/plotsystem/core/system/plot/world/IPlotWorld.java +++ b/src/main/java/com/alpsbte/plotsystem/core/system/plot/world/IPlotWorld.java @@ -1,3 +1,27 @@ +/* + * The MIT License (MIT) + * + * Copyright © 2021-2022, Alps BTE + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + package com.alpsbte.plotsystem.core.system.plot.world; import com.alpsbte.plotsystem.core.system.Builder; diff --git a/src/main/java/com/alpsbte/plotsystem/core/system/plot/world/PlotWorld.java b/src/main/java/com/alpsbte/plotsystem/core/system/plot/world/PlotWorld.java index cbb5bd96..2150b9f0 100644 --- a/src/main/java/com/alpsbte/plotsystem/core/system/plot/world/PlotWorld.java +++ b/src/main/java/com/alpsbte/plotsystem/core/system/plot/world/PlotWorld.java @@ -1,3 +1,27 @@ +/* + * The MIT License (MIT) + * + * Copyright © 2021-2022, Alps BTE + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + package com.alpsbte.plotsystem.core.system.plot.world; import com.alpsbte.plotsystem.PlotSystem; From 220d1fe5ae60c886fea2c2638304c3b31b156e59 Mon Sep 17 00:00:00 2001 From: LordTuxn Date: Sun, 9 Jan 2022 15:42:25 +0100 Subject: [PATCH 21/21] improve bukkit logger outputs and remove unused imports --- .../com/alpsbte/plotsystem/core/EventListener.java | 2 -- .../plotsystem/core/system/plot/world/IPlotWorld.java | 7 +++---- .../plotsystem/core/system/plot/world/PlotWorld.java | 10 +++++----- 3 files changed, 8 insertions(+), 11 deletions(-) diff --git a/src/main/java/com/alpsbte/plotsystem/core/EventListener.java b/src/main/java/com/alpsbte/plotsystem/core/EventListener.java index 2805f438..61d1989d 100644 --- a/src/main/java/com/alpsbte/plotsystem/core/EventListener.java +++ b/src/main/java/com/alpsbte/plotsystem/core/EventListener.java @@ -37,7 +37,6 @@ import com.alpsbte.plotsystem.utils.items.SpecialBlocks; import com.alpsbte.plotsystem.utils.Utils; import com.alpsbte.plotsystem.utils.enums.Status; -import com.onarandombox.MultiverseCore.MVWorld; import com.sk89q.worldguard.bukkit.RegionContainer; import com.sk89q.worldguard.bukkit.RegionQuery; import com.sk89q.worldguard.protection.flags.DefaultFlag; @@ -54,7 +53,6 @@ import org.bukkit.event.block.BlockPlaceEvent; import org.bukkit.event.inventory.InventoryClickEvent; import org.bukkit.event.player.*; -import org.bukkit.event.server.PluginDisableEvent; import org.bukkit.inventory.EquipmentSlot; import org.bukkit.inventory.ItemStack; import org.bukkit.material.TrapDoor; diff --git a/src/main/java/com/alpsbte/plotsystem/core/system/plot/world/IPlotWorld.java b/src/main/java/com/alpsbte/plotsystem/core/system/plot/world/IPlotWorld.java index 03d0845d..6b1dcc9a 100644 --- a/src/main/java/com/alpsbte/plotsystem/core/system/plot/world/IPlotWorld.java +++ b/src/main/java/com/alpsbte/plotsystem/core/system/plot/world/IPlotWorld.java @@ -34,7 +34,7 @@ public interface IPlotWorld { /** - * Generates a new plot world with the required settings and plot schematic + * Generates the plot world with the required configurations and schematic * @param plotOwner plow owner of the plot * @param generator generator type as class * @return true if world was generated successfully @@ -86,7 +86,6 @@ public interface IPlotWorld { World getBukkitWorld(); /** - * Returns plot world name in the format (for example: P-23) * @return world name of the plot */ String getWorldName(); @@ -98,13 +97,13 @@ public interface IPlotWorld { ProtectedRegion getProtectedRegion(); /** - * Checks if the plot world is loaded to memory. If the plot world has not yet been generated, it will return false. + * Checks if the plot world is loaded to memory * @return true if world is loaded */ boolean isWorldLoaded(); /** - * Checks if the plot world is generated. + * Checks if the plot world is generated * @return true if world is generated */ boolean isWorldGenerated(); diff --git a/src/main/java/com/alpsbte/plotsystem/core/system/plot/world/PlotWorld.java b/src/main/java/com/alpsbte/plotsystem/core/system/plot/world/PlotWorld.java index 2150b9f0..351193de 100644 --- a/src/main/java/com/alpsbte/plotsystem/core/system/plot/world/PlotWorld.java +++ b/src/main/java/com/alpsbte/plotsystem/core/system/plot/world/PlotWorld.java @@ -85,7 +85,7 @@ public boolean regenWorld(@NotNull Class ge if (deleteWorld() && generateWorld(plot.getPlotOwner(), generator)) return true; } catch (SQLException ex) { - Bukkit.getLogger().log(Level.SEVERE, "A SQL error occurred!", ex); + Bukkit.getLogger().log(Level.SEVERE, ex.getMessage(), ex); } return false; } @@ -98,7 +98,7 @@ public boolean deleteWorld() { FileUtils.deleteDirectory(new File(PlotManager.getMultiverseInventoriesConfigPath(plot.getWorld().getWorldName()))); FileUtils.deleteDirectory(new File(PlotManager.getWorldGuardConfigPath(plot.getWorld().getWorldName()))); } catch (IOException ex) { - Bukkit.getLogger().log(Level.WARNING, "An error occurred while deleting world configs of plot #" + plot.getID() + "!"); + Bukkit.getLogger().log(Level.WARNING, "Could not delete world configs of plot #" + plot.getID() + "!"); return false; } return true; @@ -118,7 +118,7 @@ public boolean loadWorld() { return mvCore.getMVWorldManager().loadWorld(getWorldName()) || isWorldLoaded(); return true; } catch (SQLException ex) { - Bukkit.getLogger().log(Level.SEVERE, "A SQL error occurred!", ex); + Bukkit.getLogger().log(Level.SEVERE, ex.getMessage(), ex); } return false; } @@ -138,7 +138,7 @@ public boolean unloadWorld(boolean movePlayers) { return true; } } catch (SQLException ex) { - Bukkit.getLogger().log(Level.SEVERE, "A SQL error occurred!", ex); + Bukkit.getLogger().log(Level.SEVERE, ex.getMessage(), ex); } Bukkit.getScheduler().scheduleSyncDelayedTask(PlotSystem.getPlugin(), (() -> @@ -178,7 +178,7 @@ public void run(){ plot.setLastActivity(false); } } catch (SQLException ex) { - Bukkit.getLogger().log(Level.SEVERE, "A SQL error occurred!", ex); + Bukkit.getLogger().log(Level.SEVERE, ex.getMessage(), ex); } } }