From 022372e9b7683c4758c0af624c1ea4e26d4b16eb Mon Sep 17 00:00:00 2001 From: Jesse Boyd Date: Tue, 4 Apr 2017 07:59:36 +1000 Subject: [PATCH] Fixes #1534 + various Auto world loading/unloading Auto player teleporting on login --- .../com/plotsquared/bukkit/BukkitMain.java | 72 ++++++++++++++++++- .../plot/commands/Remove.java | 2 +- .../plot/object/Plot.java | 6 +- .../plot/object/PlotPlayer.java | 46 ++++++++++++ .../plot/object/worlds/SinglePlotArea.java | 12 ++++ .../com/plotsquared/nukkit/NukkitMain.java | 48 +++++++++++++ .../com/plotsquared/sponge/SpongeMain.java | 41 +++++++++++ build.gradle | 3 + 8 files changed, 224 insertions(+), 6 deletions(-) diff --git a/Bukkit/src/main/java/com/plotsquared/bukkit/BukkitMain.java b/Bukkit/src/main/java/com/plotsquared/bukkit/BukkitMain.java index a699bcf43..ef152f135 100644 --- a/Bukkit/src/main/java/com/plotsquared/bukkit/BukkitMain.java +++ b/Bukkit/src/main/java/com/plotsquared/bukkit/BukkitMain.java @@ -12,12 +12,30 @@ import com.intellectualcrafters.plot.generator.HybridUtils; import com.intellectualcrafters.plot.generator.IndependentPlotGenerator; import com.intellectualcrafters.plot.object.Plot; import com.intellectualcrafters.plot.object.PlotArea; +import com.intellectualcrafters.plot.object.PlotId; import com.intellectualcrafters.plot.object.PlotPlayer; import com.intellectualcrafters.plot.object.RunnableVal; import com.intellectualcrafters.plot.object.SetupObject; import com.intellectualcrafters.plot.object.chat.PlainChatManager; +import com.intellectualcrafters.plot.object.worlds.PlotAreaManager; +import com.intellectualcrafters.plot.object.worlds.SinglePlotArea; +import com.intellectualcrafters.plot.object.worlds.SinglePlotAreaManager; import com.intellectualcrafters.plot.object.worlds.SingleWorldGenerator; -import com.intellectualcrafters.plot.util.*; +import com.intellectualcrafters.plot.util.AbstractTitle; +import com.intellectualcrafters.plot.util.ChatManager; +import com.intellectualcrafters.plot.util.ChunkManager; +import com.intellectualcrafters.plot.util.ConsoleColors; +import com.intellectualcrafters.plot.util.EconHandler; +import com.intellectualcrafters.plot.util.EventUtil; +import com.intellectualcrafters.plot.util.InventoryUtil; +import com.intellectualcrafters.plot.util.MainUtil; +import com.intellectualcrafters.plot.util.SchematicHandler; +import com.intellectualcrafters.plot.util.SetupUtils; +import com.intellectualcrafters.plot.util.StringMan; +import com.intellectualcrafters.plot.util.TaskManager; +import com.intellectualcrafters.plot.util.UUIDHandler; +import com.intellectualcrafters.plot.util.UUIDHandlerImplementation; +import com.intellectualcrafters.plot.util.WorldUtil; import com.intellectualcrafters.plot.util.block.QueueProvider; import com.intellectualcrafters.plot.uuid.UUIDWrapper; import com.plotsquared.bukkit.database.plotme.ClassicPlotMeConnector; @@ -33,7 +51,21 @@ import com.plotsquared.bukkit.listeners.PlayerEvents_1_9; import com.plotsquared.bukkit.listeners.PlotPlusListener; import com.plotsquared.bukkit.listeners.WorldEvents; import com.plotsquared.bukkit.titles.DefaultTitle_111; -import com.plotsquared.bukkit.util.*; +import com.plotsquared.bukkit.util.BukkitChatManager; +import com.plotsquared.bukkit.util.BukkitChunkManager; +import com.plotsquared.bukkit.util.BukkitCommand; +import com.plotsquared.bukkit.util.BukkitEconHandler; +import com.plotsquared.bukkit.util.BukkitEventUtil; +import com.plotsquared.bukkit.util.BukkitHybridUtils; +import com.plotsquared.bukkit.util.BukkitInventoryUtil; +import com.plotsquared.bukkit.util.BukkitSchematicHandler; +import com.plotsquared.bukkit.util.BukkitSetupUtils; +import com.plotsquared.bukkit.util.BukkitTaskManager; +import com.plotsquared.bukkit.util.BukkitUtil; +import com.plotsquared.bukkit.util.BukkitVersion; +import com.plotsquared.bukkit.util.Metrics; +import com.plotsquared.bukkit.util.SendChunk; +import com.plotsquared.bukkit.util.SetGenCB; import com.plotsquared.bukkit.util.block.BukkitLocalQueue; import com.plotsquared.bukkit.util.block.BukkitLocalQueue_1_7; import com.plotsquared.bukkit.util.block.BukkitLocalQueue_1_8; @@ -56,6 +88,7 @@ import java.util.UUID; import java.util.concurrent.ConcurrentHashMap; import org.bukkit.Bukkit; import org.bukkit.ChatColor; +import org.bukkit.Chunk; import org.bukkit.Location; import org.bukkit.OfflinePlayer; import org.bukkit.World; @@ -159,6 +192,41 @@ public final class BukkitMain extends JavaPlugin implements Listener, IPlotMain } else { PS.log(C.CONSOLE_PLEASE_ENABLE_METRICS.f(getPluginName())); } + if (Settings.Enabled_Components.WORLDS) { + TaskManager.IMP.taskRepeat(new Runnable() { + @Override + public void run() { + unload(); + } + }, 20); + } + } + + public void unload() { + PlotAreaManager manager = PS.get().getPlotAreaManager(); + if (manager instanceof SinglePlotAreaManager) { + long start = System.currentTimeMillis(); + SinglePlotArea area = ((SinglePlotAreaManager) manager).getArea(); + for (World world : Bukkit.getWorlds()) { + String name = world.getName(); + PlotId id = PlotId.fromString(name); + if (id != null) { + Plot plot = area.getOwnedPlot(id); + if (plot != null) { + List players = plot.getPlayersInPlot(); + if (players.isEmpty() && PlotPlayer.wrap(plot.owner) == null) { + for (Chunk chunk : world.getLoadedChunks()) { + chunk.unload(true, false); + if (System.currentTimeMillis() - start > 20) { + return; + } + } + Bukkit.unloadWorld(world, false); + } + } + } + } + } } @Override diff --git a/Core/src/main/java/com/intellectualcrafters/plot/commands/Remove.java b/Core/src/main/java/com/intellectualcrafters/plot/commands/Remove.java index 455e29139..207223384 100644 --- a/Core/src/main/java/com/intellectualcrafters/plot/commands/Remove.java +++ b/Core/src/main/java/com/intellectualcrafters/plot/commands/Remove.java @@ -19,7 +19,7 @@ import java.util.UUID; @CommandDeclaration( command = "remove", - aliases = {"r","untrust", "ut", "undeny", "ud"}, + aliases = {"r","untrust", "ut", "undeny", "unban", "ud"}, description = "Remove a player from a plot", usage = "/plot remove ", category = CommandCategory.SETTINGS, diff --git a/Core/src/main/java/com/intellectualcrafters/plot/object/Plot.java b/Core/src/main/java/com/intellectualcrafters/plot/object/Plot.java index db4da7753..5334b94b1 100644 --- a/Core/src/main/java/com/intellectualcrafters/plot/object/Plot.java +++ b/Core/src/main/java/com/intellectualcrafters/plot/object/Plot.java @@ -1678,7 +1678,7 @@ public class Plot { * @param uuid */ public boolean removeDenied(UUID uuid) { - if (uuid == DBFunc.everyone) { + if (uuid == DBFunc.everyone && !denied.contains(uuid)) { boolean result = false; for (UUID other : new HashSet<>(getDenied())) { result = rmvDenied(other) || result; @@ -1705,7 +1705,7 @@ public class Plot { * @param uuid */ public boolean removeTrusted(UUID uuid) { - if (uuid == DBFunc.everyone) { + if (uuid == DBFunc.everyone && !trusted.contains(uuid)) { boolean result = false; for (UUID other : new HashSet<>(getTrusted())) { result = rmvTrusted(other) || result; @@ -1735,7 +1735,7 @@ public class Plot { if (this.members == null) { return false; } - if (uuid == DBFunc.everyone) { + if (uuid == DBFunc.everyone && !members.contains(uuid)) { boolean result = false; for (UUID other : new HashSet<>(this.members)) { result = rmvMember(other) || result; diff --git a/Core/src/main/java/com/intellectualcrafters/plot/object/PlotPlayer.java b/Core/src/main/java/com/intellectualcrafters/plot/object/PlotPlayer.java index cc3241e4c..125b16f48 100644 --- a/Core/src/main/java/com/intellectualcrafters/plot/object/PlotPlayer.java +++ b/Core/src/main/java/com/intellectualcrafters/plot/object/PlotPlayer.java @@ -5,14 +5,19 @@ import com.intellectualcrafters.plot.commands.RequiredType; import com.intellectualcrafters.plot.config.Settings; import com.intellectualcrafters.plot.database.DBFunc; import com.intellectualcrafters.plot.flag.Flags; +import com.intellectualcrafters.plot.object.worlds.PlotAreaManager; +import com.intellectualcrafters.plot.object.worlds.SinglePlotArea; +import com.intellectualcrafters.plot.object.worlds.SinglePlotAreaManager; import com.intellectualcrafters.plot.util.EconHandler; import com.intellectualcrafters.plot.util.EventUtil; import com.intellectualcrafters.plot.util.Permissions; import com.intellectualcrafters.plot.util.PlotGameMode; import com.intellectualcrafters.plot.util.PlotWeather; +import com.intellectualcrafters.plot.util.TaskManager; import com.intellectualcrafters.plot.util.UUIDHandler; import com.intellectualcrafters.plot.util.expiry.ExpireManager; import com.plotsquared.general.commands.CommandCaller; +import java.nio.ByteBuffer; import java.util.HashMap; import java.util.HashSet; import java.util.Map; @@ -401,6 +406,21 @@ public abstract class PlotPlayer implements CommandCaller, OfflinePlotPlayer { public void unregister() { Plot plot = getCurrentPlot(); if (plot != null) { + if (Settings.Enabled_Components.PERSISTENT_META) { + if (plot.getArea() instanceof SinglePlotArea) { + PlotId id = plot.getId(); + int x = id.x; + int z = id.y; + ByteBuffer buffer = ByteBuffer.allocate(13); + buffer.putShort((short) x); + buffer.putShort((short) z); + Location loc = getLocation(); + buffer.putInt(loc.getX()); + buffer.put((byte) loc.getY()); + buffer.putInt(loc.getZ()); + setPersistentMeta("quitLoc", buffer.array()); + } + } EventUtil.manager.callLeave(this, plot); } if (Settings.Enabled_Components.BAN_DELETER && isBanned()) { @@ -470,6 +490,32 @@ public abstract class PlotPlayer implements CommandCaller, OfflinePlotPlayer { @Override public void run(Map value) { PlotPlayer.this.metaMap = value; + if (!value.isEmpty()) { + if (Settings.Enabled_Components.PERSISTENT_META) { + PlotAreaManager manager = PS.get().getPlotAreaManager(); + if (manager instanceof SinglePlotAreaManager) { + PlotArea area = ((SinglePlotAreaManager) manager).getArea(); + byte[] arr = PlotPlayer.this.getPersistentMeta("quitLoc"); + if (arr != null) { + ByteBuffer quitWorld = ByteBuffer.wrap(arr); + PlotId id = new PlotId(quitWorld.getShort(), quitWorld.getShort()); + int x = quitWorld.getInt(); + int y = quitWorld.get() & 0xFF; + int z = quitWorld.getInt(); + Plot plot = area.getOwnedPlot(id); + if (plot != null && plot.isLoaded()) { + final Location loc = new Location(plot.getWorldName(), x, y, z); + TaskManager.IMP.sync(new RunnableVal() { + @Override + public void run(Object o) { + teleport(loc); + } + }); + } + } + } + } + } } }); } diff --git a/Core/src/main/java/com/intellectualcrafters/plot/object/worlds/SinglePlotArea.java b/Core/src/main/java/com/intellectualcrafters/plot/object/worlds/SinglePlotArea.java index 54e8660f7..afefdefa7 100644 --- a/Core/src/main/java/com/intellectualcrafters/plot/object/worlds/SinglePlotArea.java +++ b/Core/src/main/java/com/intellectualcrafters/plot/object/worlds/SinglePlotArea.java @@ -41,8 +41,20 @@ public class SinglePlotArea extends GridPlotWorld { setup.step = new ConfigurationNode[0]; setup.world = worldName; SetupUtils.manager.setupWorld(setup); +// String worldName = plot.getWorldName(); +// World world = Bukkit.getWorld(worldName); +// if (world != null) { +// return world; +// } +// WorldCreator wc = new WorldCreator(worldName); +// wc.generator("PlotSquared:single"); +// wc.environment(World.Environment.NORMAL); +// wc.type(WorldType.FLAT); +// return AsyncWorld.create(wc); } + + @Override public ConfigurationNode[] getSettingNodes() { return new ConfigurationNode[] { diff --git a/Nukkit/src/main/java/com/plotsquared/nukkit/NukkitMain.java b/Nukkit/src/main/java/com/plotsquared/nukkit/NukkitMain.java index 0daaa8ca2..81f68b9a0 100644 --- a/Nukkit/src/main/java/com/plotsquared/nukkit/NukkitMain.java +++ b/Nukkit/src/main/java/com/plotsquared/nukkit/NukkitMain.java @@ -6,6 +6,7 @@ import cn.nukkit.Player; import cn.nukkit.entity.Entity; import cn.nukkit.event.Listener; import cn.nukkit.level.Level; +import cn.nukkit.level.format.generic.BaseFullChunk; import cn.nukkit.level.generator.Generator; import cn.nukkit.metadata.MetadataValue; import cn.nukkit.plugin.Plugin; @@ -23,11 +24,15 @@ import com.intellectualcrafters.plot.generator.HybridUtils; import com.intellectualcrafters.plot.generator.IndependentPlotGenerator; import com.intellectualcrafters.plot.object.Plot; import com.intellectualcrafters.plot.object.PlotArea; +import com.intellectualcrafters.plot.object.PlotId; import com.intellectualcrafters.plot.object.PlotManager; import com.intellectualcrafters.plot.object.PlotPlayer; import com.intellectualcrafters.plot.object.RunnableVal; import com.intellectualcrafters.plot.object.SetupObject; import com.intellectualcrafters.plot.object.chat.PlainChatManager; +import com.intellectualcrafters.plot.object.worlds.PlotAreaManager; +import com.intellectualcrafters.plot.object.worlds.SinglePlotArea; +import com.intellectualcrafters.plot.object.worlds.SinglePlotAreaManager; import com.intellectualcrafters.plot.util.AbstractTitle; import com.intellectualcrafters.plot.util.ChatManager; import com.intellectualcrafters.plot.util.ChunkManager; @@ -106,11 +111,54 @@ public final class NukkitMain extends PluginBase implements Listener, IPlotMain PS.log(C.CONSOLE_PLEASE_ENABLE_METRICS.f(getPluginName())); } Generator.addGenerator(NukkitHybridGen.class, getPluginName(), 1); + if (Settings.Enabled_Components.WORLDS) { + TaskManager.IMP.taskRepeat(new Runnable() { + @Override + public void run() { + unload(); + } + }, 20); + } } catch (Throwable e) { e.printStackTrace(); } } + public void unload() { + PlotAreaManager manager = PS.get().getPlotAreaManager(); + if (manager instanceof SinglePlotAreaManager) { + long start = System.currentTimeMillis(); + SinglePlotArea area = ((SinglePlotAreaManager) manager).getArea(); + Map worlds = getServer().getLevels(); + Level unload = null; + for (Level world : getServer().getLevels().values()) { + String name = world.getName(); + PlotId id = PlotId.fromString(name); + if (id != null) { + Plot plot = area.getOwnedPlot(id); + if (plot != null) { + List players = plot.getPlayersInPlot(); + if (players.isEmpty() && PlotPlayer.wrap(plot.owner) == null) { + unload = world; + break; + } + } + } + } + if (unload != null) { + Map chunks = unload.getChunks(); + BaseFullChunk[] toUnload = chunks.values().toArray(new BaseFullChunk[chunks.size()]); + for (BaseFullChunk chunk : toUnload) { + chunk.unload(true, false); + if (System.currentTimeMillis() - start > 20) { + return; + } + } + getServer().unloadLevel(unload, true); + } + } + } + @Override public void onDisable() { PS.get().disable(); diff --git a/Sponge/src/main/java/com/plotsquared/sponge/SpongeMain.java b/Sponge/src/main/java/com/plotsquared/sponge/SpongeMain.java index 83f3c0713..ddc6cc304 100644 --- a/Sponge/src/main/java/com/plotsquared/sponge/SpongeMain.java +++ b/Sponge/src/main/java/com/plotsquared/sponge/SpongeMain.java @@ -11,8 +11,13 @@ import com.intellectualcrafters.plot.generator.GeneratorWrapper; import com.intellectualcrafters.plot.generator.HybridGen; import com.intellectualcrafters.plot.generator.HybridUtils; import com.intellectualcrafters.plot.generator.IndependentPlotGenerator; +import com.intellectualcrafters.plot.object.Plot; +import com.intellectualcrafters.plot.object.PlotId; import com.intellectualcrafters.plot.object.PlotPlayer; import com.intellectualcrafters.plot.object.SetupObject; +import com.intellectualcrafters.plot.object.worlds.PlotAreaManager; +import com.intellectualcrafters.plot.object.worlds.SinglePlotArea; +import com.intellectualcrafters.plot.object.worlds.SinglePlotAreaManager; import com.intellectualcrafters.plot.util.AbstractTitle; import com.intellectualcrafters.plot.util.ChatManager; import com.intellectualcrafters.plot.util.ChunkManager; @@ -63,6 +68,7 @@ import org.spongepowered.api.event.game.state.GamePreInitializationEvent; import org.spongepowered.api.plugin.Plugin; import org.spongepowered.api.plugin.PluginContainer; import org.spongepowered.api.profile.GameProfileManager; +import org.spongepowered.api.world.Chunk; import org.spongepowered.api.world.World; import org.spongepowered.api.world.gen.GenerationPopulator; import org.spongepowered.api.world.gen.WorldGenerator; @@ -137,6 +143,41 @@ public class SpongeMain implements IPlotMain { new PS(this, "Sponge"); this.server = this.game.getServer(); this.game.getRegistry().register(WorldGeneratorModifier.class, (WorldGeneratorModifier) PS.get().IMP.getDefaultGenerator().specify(null)); + if (Settings.Enabled_Components.WORLDS) { + TaskManager.IMP.taskRepeat(new Runnable() { + @Override + public void run() { + unload(); + } + }, 20); + } + } + + public void unload() { + PlotAreaManager manager = PS.get().getPlotAreaManager(); + if (manager instanceof SinglePlotAreaManager) { + long start = System.currentTimeMillis(); + SinglePlotArea area = ((SinglePlotAreaManager) manager).getArea(); + for (World world : Sponge.getServer().getWorlds()) { + String name = world.getName(); + PlotId id = PlotId.fromString(name); + if (id != null) { + Plot plot = area.getOwnedPlot(id); + if (plot != null) { + List players = plot.getPlayersInPlot(); + if (players.isEmpty() && PlotPlayer.wrap(plot.owner) == null) { + for (Chunk chunk : world.getLoadedChunks()) { + chunk.unloadChunk(); + if (System.currentTimeMillis() - start > 20) { + return; + } + } + Sponge.getServer().unloadWorld(world); + } + } + } + } + } } @Override diff --git a/build.gradle b/build.gradle index c77c8ca14..2f93de0da 100644 --- a/build.gradle +++ b/build.gradle @@ -20,6 +20,9 @@ ext { } version = "3.5.1-SNAPSHOT-${revision}" description = rootProject.name +if ( project.hasProperty("lzNoVersion") ) { // gradle build -PlzNoVersion + version = "unknown"; +} subprojects { apply plugin: 'java'