From 8efc78e1c95c1114b4f56fb1ed4e5619b72028e4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexander=20S=C3=B6derberg?= Date: Tue, 19 May 2020 23:05:36 +0200 Subject: [PATCH] IT COMPILES! --- .../bukkit/player/BukkitPlayerManager.java | 17 +- .../bukkit/util/uuid/DefaultUUIDWrapper.java | 66 ------- .../java/com/plotsquared/core/IPlotMain.java | 2 +- .../core/backup/PlayerBackupProfile.java | 2 +- .../com/plotsquared/core/command/Buy.java | 9 +- .../plotsquared/core/command/DebugExec.java | 27 --- .../plotsquared/core/command/DebugPaste.java | 3 +- .../com/plotsquared/core/command/Grant.java | 76 ++++---- .../com/plotsquared/core/command/Kick.java | 99 +++++----- .../plotsquared/core/command/MainCommand.java | 1 - .../com/plotsquared/core/command/Merge.java | 5 +- .../com/plotsquared/core/command/Owner.java | 175 ++++++++++-------- .../com/plotsquared/core/command/Purge.java | 17 +- .../java/com/plotsquared/core/plot/Plot.java | 5 +- .../core/plot/expiration/ExpireManager.java | 52 ++---- .../core/queue/LocalBlockQueue.java | 8 +- .../plotsquared/core/util/PlayerManager.java | 35 +++- .../core/util/SchematicHandler.java | 15 +- .../util/UUIDHandlerImplementationTest.java | 49 ----- 19 files changed, 267 insertions(+), 396 deletions(-) delete mode 100644 Bukkit/src/main/java/com/plotsquared/bukkit/util/uuid/DefaultUUIDWrapper.java delete mode 100644 Core/src/test/java/com/plotsquared/core/plot/util/UUIDHandlerImplementationTest.java diff --git a/Bukkit/src/main/java/com/plotsquared/bukkit/player/BukkitPlayerManager.java b/Bukkit/src/main/java/com/plotsquared/bukkit/player/BukkitPlayerManager.java index 6e628a8a7..cdb770e2f 100644 --- a/Bukkit/src/main/java/com/plotsquared/bukkit/player/BukkitPlayerManager.java +++ b/Bukkit/src/main/java/com/plotsquared/bukkit/player/BukkitPlayerManager.java @@ -25,20 +25,20 @@ */ package com.plotsquared.bukkit.player; -import com.plotsquared.core.player.PlotPlayer; import com.plotsquared.core.util.PlayerManager; import org.bukkit.Bukkit; import org.bukkit.entity.Player; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import java.util.UUID; /** * Player manager providing {@link BukkitPlayer Bukkit players} */ -public class BukkitPlayerManager extends PlayerManager { +public class BukkitPlayerManager extends PlayerManager { - @Override @NotNull public PlotPlayer createPlayer(@NotNull final UUID uuid) { + @Override @NotNull public BukkitPlayer createPlayer(@NotNull final UUID uuid) { final Player player = Bukkit.getPlayer(uuid); if (player == null || !player.isOnline()) { throw new NoSuchPlayerException(uuid); @@ -46,4 +46,15 @@ public class BukkitPlayerManager extends PlayerManager { return new BukkitPlayer(player); } + @Nullable @Override public BukkitOfflinePlayer getOfflinePlayer(@Nullable final UUID uuid) { + if (uuid == null) { + return null; + } + return new BukkitOfflinePlayer(Bukkit.getOfflinePlayer(uuid)); + } + + @NotNull @Override public BukkitOfflinePlayer getOfflinePlayer(@NotNull final String username) { + return new BukkitOfflinePlayer(Bukkit.getOfflinePlayer(username)); + } + } diff --git a/Bukkit/src/main/java/com/plotsquared/bukkit/util/uuid/DefaultUUIDWrapper.java b/Bukkit/src/main/java/com/plotsquared/bukkit/util/uuid/DefaultUUIDWrapper.java deleted file mode 100644 index a2898ee91..000000000 --- a/Bukkit/src/main/java/com/plotsquared/bukkit/util/uuid/DefaultUUIDWrapper.java +++ /dev/null @@ -1,66 +0,0 @@ -/* - * _____ _ _ _____ _ - * | __ \| | | | / ____| | | - * | |__) | | ___ | |_| (___ __ _ _ _ __ _ _ __ ___ __| | - * | ___/| |/ _ \| __|\___ \ / _` | | | |/ _` | '__/ _ \/ _` | - * | | | | (_) | |_ ____) | (_| | |_| | (_| | | | __/ (_| | - * |_| |_|\___/ \__|_____/ \__, |\__,_|\__,_|_| \___|\__,_| - * | | - * |_| - * PlotSquared plot management system for Minecraft - * Copyright (C) 2020 IntellectualSites - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -package com.plotsquared.bukkit.util.uuid; - -import com.plotsquared.bukkit.player.BukkitOfflinePlayer; -import com.plotsquared.bukkit.player.BukkitPlayer; -import com.plotsquared.core.player.OfflinePlotPlayer; -import com.plotsquared.core.player.PlotPlayer; -import com.plotsquared.core.util.uuid.UUIDWrapper; -import org.bukkit.Bukkit; -import org.bukkit.OfflinePlayer; -import org.jetbrains.annotations.NotNull; - -import java.util.Arrays; -import java.util.UUID; - -public class DefaultUUIDWrapper extends UUIDWrapper { - - @NotNull @Override public UUID getUUID(PlotPlayer player) { - return ((BukkitPlayer) player).player.getUniqueId(); - } - - @Override public UUID getUUID(OfflinePlotPlayer player) { - return player.getUUID(); - } - - @Override public OfflinePlotPlayer getOfflinePlayer(UUID uuid) { - return new BukkitOfflinePlayer(Bukkit.getOfflinePlayer(uuid)); - } - - @Override public UUID getUUID(String name) { - return Bukkit.getOfflinePlayer(name).getUniqueId(); - } - - @Override public OfflinePlotPlayer[] getOfflinePlayers() { - OfflinePlayer[] ops = Bukkit.getOfflinePlayers(); - return Arrays.stream(ops).map(BukkitOfflinePlayer::new).toArray(BukkitOfflinePlayer[]::new); - } - - @Override public OfflinePlotPlayer getOfflinePlayer(String name) { - return new BukkitOfflinePlayer(Bukkit.getOfflinePlayer(name)); - } -} diff --git a/Core/src/main/java/com/plotsquared/core/IPlotMain.java b/Core/src/main/java/com/plotsquared/core/IPlotMain.java index 2705d54c2..9e04d85ec 100644 --- a/Core/src/main/java/com/plotsquared/core/IPlotMain.java +++ b/Core/src/main/java/com/plotsquared/core/IPlotMain.java @@ -286,6 +286,6 @@ public interface IPlotMain extends ILogger { * * @return Player manager */ - @NotNull PlayerManager getPlayerManager(); + @NotNull PlayerManager getPlayerManager(); } diff --git a/Core/src/main/java/com/plotsquared/core/backup/PlayerBackupProfile.java b/Core/src/main/java/com/plotsquared/core/backup/PlayerBackupProfile.java index 8853f3dff..c71057ab5 100644 --- a/Core/src/main/java/com/plotsquared/core/backup/PlayerBackupProfile.java +++ b/Core/src/main/java/com/plotsquared/core/backup/PlayerBackupProfile.java @@ -142,7 +142,7 @@ public class PlayerBackupProfile implements BackupProfile { } final List plots = Collections.singletonList(plot); final boolean result = SchematicHandler.manager.exportAll(plots, getBackupDirectory().toFile(), - "%world%-%id%-%owner%-" + System.currentTimeMillis(), () -> + "%world%-%id%-" + System.currentTimeMillis(), () -> future.complete(new Backup(this, System.currentTimeMillis(), null))); if (!result) { future.completeExceptionally(new RuntimeException("Failed to complete the backup")); diff --git a/Core/src/main/java/com/plotsquared/core/command/Buy.java b/Core/src/main/java/com/plotsquared/core/command/Buy.java index a660a39a1..b986a7983 100644 --- a/Core/src/main/java/com/plotsquared/core/command/Buy.java +++ b/Core/src/main/java/com/plotsquared/core/command/Buy.java @@ -37,7 +37,6 @@ import com.plotsquared.core.util.EconHandler; import com.plotsquared.core.util.MainUtil; import com.plotsquared.core.util.task.RunnableVal2; import com.plotsquared.core.util.task.RunnableVal3; -import com.plotsquared.core.util.uuid.UUIDHandler; import java.util.Set; import java.util.concurrent.CompletableFuture; @@ -82,10 +81,10 @@ public class Buy extends Command { // Success confirm.run(this, () -> { Captions.REMOVED_BALANCE.send(player, price); - EconHandler.manager - .depositMoney(UUIDHandler.getUUIDWrapper().getOfflinePlayer(plot.getOwnerAbs()), - price); - PlotPlayer owner = UUIDHandler.getPlayer(plot.getOwnerAbs()); + + EconHandler.manager.depositMoney(PlotSquared.imp().getPlayerManager().getOfflinePlayer(plot.getOwnerAbs()), price); + + PlotPlayer owner = PlotSquared.imp().getPlayerManager().getPlayerIfExists(plot.getOwnerAbs()); if (owner != null) { Captions.PLOT_SOLD.send(owner, plot.getId(), player.getName(), price); } diff --git a/Core/src/main/java/com/plotsquared/core/command/DebugExec.java b/Core/src/main/java/com/plotsquared/core/command/DebugExec.java index 8976c3348..9f179803f 100644 --- a/Core/src/main/java/com/plotsquared/core/command/DebugExec.java +++ b/Core/src/main/java/com/plotsquared/core/command/DebugExec.java @@ -35,7 +35,6 @@ import com.plotsquared.core.events.Result; import com.plotsquared.core.generator.HybridUtils; import com.plotsquared.core.location.Location; import com.plotsquared.core.player.ConsolePlayer; -import com.plotsquared.core.player.OfflinePlotPlayer; import com.plotsquared.core.player.PlotPlayer; import com.plotsquared.core.plot.Plot; import com.plotsquared.core.plot.PlotArea; @@ -59,7 +58,6 @@ import com.plotsquared.core.util.task.RunnableVal; import com.plotsquared.core.util.task.RunnableVal2; import com.plotsquared.core.util.task.RunnableVal3; import com.plotsquared.core.util.task.TaskManager; -import com.plotsquared.core.util.uuid.UUIDHandler; import com.sk89q.worldedit.world.block.BlockState; import javax.script.Bindings; @@ -71,13 +69,10 @@ import javax.script.SimpleScriptContext; import java.io.File; import java.io.IOException; import java.nio.charset.StandardCharsets; -import java.sql.Timestamp; import java.util.Arrays; -import java.util.Date; import java.util.HashMap; import java.util.HashSet; import java.util.List; -import java.util.UUID; import java.util.concurrent.CompletableFuture; @CommandDeclaration(command = "debugexec", @@ -172,7 +167,6 @@ public class DebugExec extends SubCommand { this.scope.put("SetupUtils", SetupUtils.manager); this.scope.put("EventUtil", PlotSquared.get().getEventDispatcher()); this.scope.put("EconHandler", EconHandler.manager); - this.scope.put("UUIDHandler", UUIDHandler.implementation); this.scope.put("DBFunc", DBFunc.dbManager); this.scope.put("HybridUtils", HybridUtils.manager); this.scope.put("IMP", PlotSquared.get().IMP); @@ -301,27 +295,6 @@ public class DebugExec extends SubCommand { } else { return MainUtil.sendMessage(player, "Plot expiry task already started"); } - case "seen": - if (args.length != 2) { - return MainUtil.sendMessage(player, "Use /plot debugexec seen "); - } - UUID uuid = UUIDHandler.getUUID(args[1], null); - if (uuid == null) { - return MainUtil.sendMessage(player, "Player not found: " + args[1]); - } - OfflinePlotPlayer op = UUIDHandler.getUUIDWrapper().getOfflinePlayer(uuid); - if (op == null || op.getLastPlayed() == 0) { - return MainUtil - .sendMessage(player, "Player hasn't connected before: " + args[1]); - } - Timestamp stamp = new Timestamp(op.getLastPlayed()); - Date date = new Date(stamp.getTime()); - MainUtil.sendMessage(player, "PLAYER: " + args[1]); - MainUtil.sendMessage(player, "UUID: " + uuid); - MainUtil.sendMessage(player, "Object: " + date.toGMTString()); - MainUtil.sendMessage(player, "GMT: " + date.toGMTString()); - MainUtil.sendMessage(player, "Local: " + date.toLocaleString()); - return true; case "h": case "he": case "?": diff --git a/Core/src/main/java/com/plotsquared/core/command/DebugPaste.java b/Core/src/main/java/com/plotsquared/core/command/DebugPaste.java index c5c42a4c2..412d2b066 100644 --- a/Core/src/main/java/com/plotsquared/core/command/DebugPaste.java +++ b/Core/src/main/java/com/plotsquared/core/command/DebugPaste.java @@ -35,7 +35,6 @@ import com.plotsquared.core.util.MainUtil; import com.plotsquared.core.util.PremiumVerification; import com.plotsquared.core.util.net.IncendoPaster; import com.plotsquared.core.util.task.TaskManager; -import com.plotsquared.core.util.uuid.UUIDHandler; import lombok.NonNull; import java.io.BufferedReader; @@ -90,7 +89,7 @@ public class DebugPaste extends SubCommand { b.append("# Server Information\n"); b.append("Server Version: ").append(PlotSquared.get().IMP.getServerImplementation()) .append("\n"); - b.append("online_mode: ").append(UUIDHandler.getUUIDWrapper()).append(';') + b.append("online_mode: ").append(!Settings.UUID.OFFLINE).append(';') .append(!Settings.UUID.OFFLINE).append('\n'); b.append("Plugins:"); for (Map.Entry, Boolean> pluginInfo : PlotSquared diff --git a/Core/src/main/java/com/plotsquared/core/command/Grant.java b/Core/src/main/java/com/plotsquared/core/command/Grant.java index acec1b084..39351c27f 100644 --- a/Core/src/main/java/com/plotsquared/core/command/Grant.java +++ b/Core/src/main/java/com/plotsquared/core/command/Grant.java @@ -26,6 +26,7 @@ package com.plotsquared.core.command; import com.google.common.primitives.Ints; +import com.plotsquared.core.PlotSquared; import com.plotsquared.core.configuration.CaptionUtility; import com.plotsquared.core.configuration.Captions; import com.plotsquared.core.database.DBFunc; @@ -35,10 +36,10 @@ import com.plotsquared.core.util.Permissions; import com.plotsquared.core.util.task.RunnableVal; import com.plotsquared.core.util.task.RunnableVal2; import com.plotsquared.core.util.task.RunnableVal3; -import com.plotsquared.core.util.uuid.UUIDHandler; import java.util.UUID; import java.util.concurrent.CompletableFuture; +import java.util.concurrent.TimeoutException; @CommandDeclaration(command = "grant", category = CommandCategory.CLAIMING, @@ -69,43 +70,44 @@ public class Grant extends Command { if (args.length > 2) { break; } - final UUID uuid; - if (args.length == 2) { - uuid = UUIDHandler.getUUIDFromString(args[1]); - } else { - uuid = player.getUUID(); - } - if (uuid == null) { - Captions.INVALID_PLAYER.send(player, args[1]); - return CompletableFuture.completedFuture(false); - } - MainUtil.getPersistentMeta(uuid, "grantedPlots", new RunnableVal() { - @Override public void run(byte[] array) { - if (arg0.equals("check")) { // check - int granted; - if (array == null) { - granted = 0; - } else { - granted = Ints.fromByteArray(array); + MainUtil.getUUIDsFromString(args[1], (uuids, throwable) -> { + if (throwable instanceof TimeoutException) { + MainUtil.sendMessage(player, Captions.FETCHING_PLAYERS_TIMEOUT); + } else if (throwable != null || uuids.size() != 1) { + MainUtil.sendMessage(player, Captions.INVALID_PLAYER); + } else { + final UUID uuid = uuids.toArray(new UUID[0])[0]; + MainUtil.getPersistentMeta(uuid, + "grantedPlots", new RunnableVal() { + @Override public void run(byte[] array) { + if (arg0.equals("check")) { // check + int granted; + if (array == null) { + granted = 0; + } else { + granted = Ints.fromByteArray(array); + } + Captions.GRANTED_PLOTS.send(player, granted); + } else { // add + int amount; + if (array == null) { + amount = 1; + } else { + amount = 1 + Ints.fromByteArray(array); + } + boolean replace = array != null; + String key = "grantedPlots"; + byte[] rawData = Ints.toByteArray(amount); + + PlotPlayer online = PlotSquared.imp().getPlayerManager().getPlayerIfExists(uuid); + if (online != null) { + online.setPersistentMeta(key, rawData); + } else { + DBFunc.addPersistentMeta(uuid, key, rawData, replace); + } + } } - Captions.GRANTED_PLOTS.send(player, granted); - } else { // add - int amount; - if (array == null) { - amount = 1; - } else { - amount = 1 + Ints.fromByteArray(array); - } - boolean replace = array != null; - String key = "grantedPlots"; - byte[] rawData = Ints.toByteArray(amount); - PlotPlayer online = UUIDHandler.getPlayer(uuid); - if (online != null) { - online.setPersistentMeta(key, rawData); - } else { - DBFunc.addPersistentMeta(uuid, key, rawData, replace); - } - } + }); } }); return CompletableFuture.completedFuture(true); diff --git a/Core/src/main/java/com/plotsquared/core/command/Kick.java b/Core/src/main/java/com/plotsquared/core/command/Kick.java index 1d113a874..69ace1482 100644 --- a/Core/src/main/java/com/plotsquared/core/command/Kick.java +++ b/Core/src/main/java/com/plotsquared/core/command/Kick.java @@ -34,11 +34,11 @@ import com.plotsquared.core.plot.Plot; import com.plotsquared.core.util.MainUtil; import com.plotsquared.core.util.Permissions; import com.plotsquared.core.util.WorldUtil; -import com.plotsquared.core.util.uuid.UUIDHandler; import java.util.HashSet; import java.util.Set; import java.util.UUID; +import java.util.concurrent.TimeoutException; @CommandDeclaration(command = "kick", aliases = "k", @@ -64,57 +64,62 @@ public class Kick extends SubCommand { MainUtil.sendMessage(player, Captions.NO_PLOT_PERMS); return false; } - Set uuids = MainUtil.getUUIDsFromString(args[0]); - if (uuids.isEmpty()) { - MainUtil.sendMessage(player, Captions.INVALID_PLAYER, args[0]); - return false; - } - Set players = new HashSet<>(); - for (UUID uuid : uuids) { - if (uuid == DBFunc.EVERYONE) { - for (PlotPlayer pp : plot.getPlayersInPlot()) { - if (pp == player || Permissions - .hasPermission(pp, Captions.PERMISSION_ADMIN_ENTRY_DENIED)) { + + MainUtil.getUUIDsFromString(args[0], (uuids, throwable) -> { + if (throwable instanceof TimeoutException) { + MainUtil.sendMessage(player, Captions.FETCHING_PLAYERS_TIMEOUT); + } else if (throwable != null || uuids.isEmpty()) { + MainUtil.sendMessage(player, Captions.INVALID_PLAYER); + } else { + Set players = new HashSet<>(); + for (UUID uuid : uuids) { + if (uuid == DBFunc.EVERYONE) { + for (PlotPlayer pp : plot.getPlayersInPlot()) { + if (pp == player || Permissions + .hasPermission(pp, Captions.PERMISSION_ADMIN_ENTRY_DENIED)) { + continue; + } + players.add(pp); + } continue; } - players.add(pp); + PlotPlayer pp = PlotSquared.imp().getPlayerManager().getPlayerIfExists(uuid); + if (pp != null) { + players.add(pp); + } } - continue; - } - PlotPlayer pp = UUIDHandler.getPlayer(uuid); - if (pp != null) { - players.add(pp); - } - } - players.remove(player); // Don't ever kick the calling player - if (players.isEmpty()) { - MainUtil.sendMessage(player, Captions.INVALID_PLAYER, args[0]); - return false; - } - for (PlotPlayer player2 : players) { - if (!plot.equals(player2.getCurrentPlot())) { - MainUtil.sendMessage(player, Captions.INVALID_PLAYER, args[0]); - return false; - } - if (Permissions.hasPermission(player2, Captions.PERMISSION_ADMIN_ENTRY_DENIED)) { - Captions.CANNOT_KICK_PLAYER.send(player, player2.getName()); - return false; - } - Location spawn = WorldUtil.IMP.getSpawn(location.getWorld()); - Captions.YOU_GOT_KICKED.send(player2); - if (plot.equals(spawn.getPlot())) { - Location newSpawn = WorldUtil.IMP - .getSpawn(PlotSquared.get().getPlotAreaManager().getAllWorlds()[0]); - if (plot.equals(newSpawn.getPlot())) { - // Kick from server if you can't be teleported to spawn - player2.kick(Captions.YOU_GOT_KICKED.getTranslated()); - } else { - player2.plotkick(newSpawn); + players.remove(player); // Don't ever kick the calling player + if (players.isEmpty()) { + MainUtil.sendMessage(player, Captions.INVALID_PLAYER, args[0]); + return; + } + for (PlotPlayer player2 : players) { + if (!plot.equals(player2.getCurrentPlot())) { + MainUtil.sendMessage(player, Captions.INVALID_PLAYER, args[0]); + return; + } + if (Permissions.hasPermission(player2, Captions.PERMISSION_ADMIN_ENTRY_DENIED)) { + Captions.CANNOT_KICK_PLAYER.send(player, player2.getName()); + return; + } + Location spawn = WorldUtil.IMP.getSpawn(location.getWorld()); + Captions.YOU_GOT_KICKED.send(player2); + if (plot.equals(spawn.getPlot())) { + Location newSpawn = WorldUtil.IMP + .getSpawn(PlotSquared.get().getPlotAreaManager().getAllWorlds()[0]); + if (plot.equals(newSpawn.getPlot())) { + // Kick from server if you can't be teleported to spawn + player2.kick(Captions.YOU_GOT_KICKED.getTranslated()); + } else { + player2.plotkick(newSpawn); + } + } else { + player2.plotkick(spawn); + } } - } else { - player2.plotkick(spawn); } - } + }); + return true; } } diff --git a/Core/src/main/java/com/plotsquared/core/command/MainCommand.java b/Core/src/main/java/com/plotsquared/core/command/MainCommand.java index 03be061ca..133e27ab2 100644 --- a/Core/src/main/java/com/plotsquared/core/command/MainCommand.java +++ b/Core/src/main/java/com/plotsquared/core/command/MainCommand.java @@ -99,7 +99,6 @@ public class MainCommand extends Command { new DebugPaste(); new Unlink(); new Kick(); - new DebugClaimTest(); new Inbox(); new Comment(); new DatabaseCommand(); diff --git a/Core/src/main/java/com/plotsquared/core/command/Merge.java b/Core/src/main/java/com/plotsquared/core/command/Merge.java index 7998e3a84..53f88e2e7 100644 --- a/Core/src/main/java/com/plotsquared/core/command/Merge.java +++ b/Core/src/main/java/com/plotsquared/core/command/Merge.java @@ -40,7 +40,6 @@ import com.plotsquared.core.util.Expression; import com.plotsquared.core.util.MainUtil; import com.plotsquared.core.util.Permissions; import com.plotsquared.core.util.StringMan; -import com.plotsquared.core.util.uuid.UUIDHandler; import java.util.UUID; @@ -213,7 +212,7 @@ public class Merge extends SubCommand { java.util.Set uuids = adjacent.getOwners(); boolean isOnline = false; for (final UUID owner : uuids) { - final PlotPlayer accepter = UUIDHandler.getPlayer(owner); + final PlotPlayer accepter = PlotSquared.imp().getPlayerManager().getPlayerIfExists(owner); if (!force && accepter == null) { continue; } @@ -222,7 +221,7 @@ public class Merge extends SubCommand { Runnable run = () -> { MainUtil.sendMessage(accepter, Captions.MERGE_ACCEPTED); plot.autoMerge(dir, maxSize - size, owner, terrain); - PlotPlayer plotPlayer = UUIDHandler.getPlayer(player.getUUID()); + PlotPlayer plotPlayer = PlotSquared.imp().getPlayerManager().getPlayerIfExists(player.getUUID()); if (plotPlayer == null) { sendMessage(accepter, Captions.MERGE_NOT_VALID); return; diff --git a/Core/src/main/java/com/plotsquared/core/command/Owner.java b/Core/src/main/java/com/plotsquared/core/command/Owner.java index 2c3d8e965..cb436fad2 100644 --- a/Core/src/main/java/com/plotsquared/core/command/Owner.java +++ b/Core/src/main/java/com/plotsquared/core/command/Owner.java @@ -36,10 +36,11 @@ import com.plotsquared.core.plot.Plot; import com.plotsquared.core.util.MainUtil; import com.plotsquared.core.util.Permissions; import com.plotsquared.core.util.task.TaskManager; -import com.plotsquared.core.util.uuid.UUIDHandler; import java.util.Set; import java.util.UUID; +import java.util.concurrent.TimeoutException; +import java.util.function.Consumer; @CommandDeclaration(command = "setowner", permission = "plots.set.owner", @@ -57,91 +58,103 @@ public class Owner extends SetCommand { return false; } Set plots = plot.getConnectedPlots(); - UUID uuid = null; + + final Consumer uuidConsumer = uuid -> { + if (uuid == null && !value.equalsIgnoreCase("none") && !value.equalsIgnoreCase("null") + && !value.equalsIgnoreCase("-")) { + Captions.INVALID_PLAYER.send(player, value); + return; + } + PlotChangeOwnerEvent event = PlotSquared.get().getEventDispatcher() + .callOwnerChange(player, plot, plot.hasOwner() ? plot.getOwnerAbs() : null, uuid, + plot.hasOwner()); + if (event.getEventResult() == Result.DENY) { + sendMessage(player, Captions.EVENT_DENIED, "Owner change"); + return; + } + uuid = event.getNewOwner(); + boolean force = event.getEventResult() == Result.FORCE; + if (uuid == null) { + if (!force && !Permissions + .hasPermission(player, Captions.PERMISSION_ADMIN_COMMAND_SET_OWNER.getTranslated(), + true)) { + return; + } + PlotUnlinkEvent unlinkEvent = PlotSquared.get().getEventDispatcher() + .callUnlink(plot.getArea(), plot, false, false, PlotUnlinkEvent.REASON.NEW_OWNER); + if (unlinkEvent.getEventResult() == Result.DENY) { + sendMessage(player, Captions.EVENT_DENIED, "Unlink on owner change"); + return; + } + plot.unlinkPlot(unlinkEvent.isCreateRoad(), unlinkEvent.isCreateRoad()); + Set connected = plot.getConnectedPlots(); + for (Plot current : connected) { + current.unclaim(); + current.removeSign(); + } + MainUtil.sendMessage(player, Captions.SET_OWNER); + return; + } + final PlotPlayer other = PlotSquared.imp().getPlayerManager().getPlayerIfExists(uuid); + if (plot.isOwner(uuid)) { + Captions.ALREADY_OWNER.send(player, MainUtil.getName(uuid)); + return; + } + if (!force && !Permissions + .hasPermission(player, Captions.PERMISSION_ADMIN_COMMAND_SET_OWNER)) { + if (other == null) { + Captions.INVALID_PLAYER_OFFLINE.send(player, value); + return; + } + int size = plots.size(); + int currentPlots = (Settings.Limit.GLOBAL ? + other.getPlotCount() : + other.getPlotCount(plot.getWorldName())) + size; + if (currentPlots > other.getAllowedPlots()) { + sendMessage(player, Captions.CANT_TRANSFER_MORE_PLOTS); + return; + } + } + final UUID finalUUID = uuid; + PlotSquared.get().getImpromptuUUIDPipeline().getSingle(uuid, (finalName, throwable) -> { + final boolean removeDenied = plot.isDenied(finalUUID); + Runnable run = () -> { + if (plot.setOwner(finalUUID, player)) { + if (removeDenied) + plot.removeDenied(finalUUID); + plot.setSign(finalName); + MainUtil.sendMessage(player, Captions.SET_OWNER); + if (other != null) { + MainUtil.sendMessage(other, Captions.NOW_OWNER, + plot.getArea() + ";" + plot.getId()); + } + } else { + MainUtil.sendMessage(player, Captions.SET_OWNER_CANCELLED); + } + }; + if (hasConfirmation(player)) { + CmdConfirm.addPending(player, "/plot set owner " + value, run); + } else { + TaskManager.runTask(run); + } + }); + }; + if (value.length() == 36) { try { - uuid = UUID.fromString(value); + uuidConsumer.accept(UUID.fromString(value)); } catch (Exception ignored) { } } else { - uuid = UUIDHandler.getUUID(value, null); - } - if (uuid == null && !value.equalsIgnoreCase("none") && !value.equalsIgnoreCase("null") - && !value.equalsIgnoreCase("-")) { - Captions.INVALID_PLAYER.send(player, value); - return false; - } - PlotChangeOwnerEvent event = PlotSquared.get().getEventDispatcher() - .callOwnerChange(player, plot, plot.hasOwner() ? plot.getOwnerAbs() : null, uuid, - plot.hasOwner()); - if (event.getEventResult() == Result.DENY) { - sendMessage(player, Captions.EVENT_DENIED, "Owner change"); - return false; - } - uuid = event.getNewOwner(); - boolean force = event.getEventResult() == Result.FORCE; - if (uuid == null) { - if (!force && !Permissions - .hasPermission(player, Captions.PERMISSION_ADMIN_COMMAND_SET_OWNER.getTranslated(), - true)) { - return false; - } - PlotUnlinkEvent unlinkEvent = PlotSquared.get().getEventDispatcher() - .callUnlink(plot.getArea(), plot, false, false, PlotUnlinkEvent.REASON.NEW_OWNER); - if (unlinkEvent.getEventResult() == Result.DENY) { - sendMessage(player, Captions.EVENT_DENIED, "Unlink on owner change"); - return true; - } - plot.unlinkPlot(unlinkEvent.isCreateRoad(), unlinkEvent.isCreateRoad()); - Set connected = plot.getConnectedPlots(); - for (Plot current : connected) { - current.unclaim(); - current.removeSign(); - } - MainUtil.sendMessage(player, Captions.SET_OWNER); - return true; - } - final PlotPlayer other = UUIDHandler.getPlayer(uuid); - if (plot.isOwner(uuid)) { - Captions.ALREADY_OWNER.send(player, MainUtil.getName(uuid)); - return false; - } - if (!force && !Permissions - .hasPermission(player, Captions.PERMISSION_ADMIN_COMMAND_SET_OWNER)) { - if (other == null) { - Captions.INVALID_PLAYER_OFFLINE.send(player, value); - return false; - } - int size = plots.size(); - int currentPlots = (Settings.Limit.GLOBAL ? - other.getPlotCount() : - other.getPlotCount(plot.getWorldName())) + size; - if (currentPlots > other.getAllowedPlots()) { - sendMessage(player, Captions.CANT_TRANSFER_MORE_PLOTS); - return false; - } - } - final String finalName = UUIDHandler.getName(uuid); - final UUID finalUUID = uuid; - final boolean removeDenied = plot.isDenied(finalUUID); - Runnable run = () -> { - if (plot.setOwner(finalUUID, player)) { - if (removeDenied) - plot.removeDenied(finalUUID); - plot.setSign(finalName); - MainUtil.sendMessage(player, Captions.SET_OWNER); - if (other != null) { - MainUtil.sendMessage(other, Captions.NOW_OWNER, - plot.getArea() + ";" + plot.getId()); - } - } else { - MainUtil.sendMessage(player, Captions.SET_OWNER_CANCELLED); - } - }; - if (hasConfirmation(player)) { - CmdConfirm.addPending(player, "/plot set owner " + value, run); - } else { - TaskManager.runTask(run); + PlotSquared.get().getImpromptuUUIDPipeline().getSingle(value, (uuid, throwable) -> { + if (throwable instanceof TimeoutException) { + MainUtil.sendMessage(player, Captions.FETCHING_PLAYERS_TIMEOUT); + } else if (throwable != null) { + MainUtil.sendMessage(player, Captions.INVALID_PLAYER); + } else { + uuidConsumer.accept(uuid); + } + }); } return true; } diff --git a/Core/src/main/java/com/plotsquared/core/command/Purge.java b/Core/src/main/java/com/plotsquared/core/command/Purge.java index 3691f44c9..50b323300 100644 --- a/Core/src/main/java/com/plotsquared/core/command/Purge.java +++ b/Core/src/main/java/com/plotsquared/core/command/Purge.java @@ -36,7 +36,6 @@ import com.plotsquared.core.plot.PlotArea; import com.plotsquared.core.plot.PlotId; import com.plotsquared.core.util.StringMan; import com.plotsquared.core.util.task.TaskManager; -import com.plotsquared.core.util.uuid.UUIDHandler; import java.util.HashMap; import java.util.HashSet; @@ -65,7 +64,6 @@ public class Purge extends SubCommand { PlotId id = null; UUID owner = null; UUID added = null; - boolean unknown = false; boolean clear = false; for (String arg : args) { String[] split = arg.split(":"); @@ -97,7 +95,7 @@ public class Purge extends SubCommand { break; case "owner": case "o": - owner = UUIDHandler.getUUID(split[1], null); + owner = PlotSquared.get().getImpromptuUUIDPipeline().getSingle(split[1], Settings.UUID.BLOCKING_TIMEOUT); if (owner == null) { Captions.INVALID_PLAYER.send(player, split[1]); return false; @@ -105,17 +103,12 @@ public class Purge extends SubCommand { break; case "shared": case "s": - added = UUIDHandler.getUUID(split[1], null); + added = PlotSquared.get().getImpromptuUUIDPipeline().getSingle(split[1], Settings.UUID.BLOCKING_TIMEOUT); if (added == null) { Captions.INVALID_PLAYER.send(player, split[1]); return false; } break; - case "unknown": - case "?": - case "u": - unknown = Boolean.parseBoolean(split[1]); - break; case "clear": case "c": case "delete": @@ -145,9 +138,6 @@ public class Purge extends SubCommand { if (added != null && !plot.isAdded(added)) { continue; } - if (unknown && UUIDHandler.getName(plot.getOwnerAbs()) != null) { - continue; - } toDelete.addAll(plot.getConnectedPlots()); } if (PlotSquared.get().plots_tmp != null) { @@ -168,9 +158,6 @@ public class Purge extends SubCommand { if (added != null && !plot.isAdded(added)) { continue; } - if (unknown && UUIDHandler.getName(plot.getOwnerAbs()) != null) { - continue; - } toDelete.add(plot); } } diff --git a/Core/src/main/java/com/plotsquared/core/plot/Plot.java b/Core/src/main/java/com/plotsquared/core/plot/Plot.java index e7cfbcafd..9ac482be6 100644 --- a/Core/src/main/java/com/plotsquared/core/plot/Plot.java +++ b/Core/src/main/java/com/plotsquared/core/plot/Plot.java @@ -496,7 +496,7 @@ public class Plot { } if (isMerged()) { Set plots = getConnectedPlots(); - Plot[] array = plots.toArray(new Plot[plots.size()]); + Plot[] array = plots.toArray(new Plot[0]); ImmutableSet.Builder owners = ImmutableSet.builder(); UUID last = this.getOwner(); owners.add(this.getOwner()); @@ -1711,7 +1711,8 @@ public class Plot { this.setSign("unknown"); return; } - PlotSquared.get().getImpromptuUUIDPipeline().getSingle(this.getOwnerAbs(), this::setSign); + PlotSquared.get().getImpromptuUUIDPipeline().getSingle(this.getOwnerAbs(), (username, sign) -> + this.setSign(username)); } /** diff --git a/Core/src/main/java/com/plotsquared/core/plot/expiration/ExpireManager.java b/Core/src/main/java/com/plotsquared/core/plot/expiration/ExpireManager.java index 6b5f2bbae..16d4ee41f 100644 --- a/Core/src/main/java/com/plotsquared/core/plot/expiration/ExpireManager.java +++ b/Core/src/main/java/com/plotsquared/core/plot/expiration/ExpireManager.java @@ -27,7 +27,6 @@ package com.plotsquared.core.plot.expiration; import com.plotsquared.core.PlotSquared; import com.plotsquared.core.configuration.Captions; -import com.plotsquared.core.configuration.Settings; import com.plotsquared.core.database.DBFunc; import com.plotsquared.core.events.PlotFlagAddEvent; import com.plotsquared.core.events.PlotUnlinkEvent; @@ -48,7 +47,6 @@ import com.plotsquared.core.util.StringMan; import com.plotsquared.core.util.task.RunnableVal; import com.plotsquared.core.util.task.RunnableVal3; import com.plotsquared.core.util.task.TaskManager; -import com.plotsquared.core.util.uuid.UUIDHandler; import java.util.ArrayDeque; import java.util.ArrayList; @@ -411,13 +409,13 @@ public class ExpireManager { } } for (UUID helper : plot.getTrusted()) { - PlotPlayer player = UUIDHandler.getPlayer(helper); + PlotPlayer player = PlotSquared.imp().getPlayerManager().getPlayerIfExists(helper); if (player != null) { MainUtil.sendMessage(player, Captions.PLOT_REMOVED_USER, plot.toString()); } } for (UUID helper : plot.getMembers()) { - PlotPlayer player = UUIDHandler.getPlayer(helper); + PlotPlayer player = PlotSquared.imp().getPlayerManager().getPlayerIfExists(helper); if (player != null) { MainUtil.sendMessage(player, Captions.PLOT_REMOVED_USER, plot.toString()); } @@ -433,56 +431,34 @@ public class ExpireManager { .getString(plots)); PlotSquared.debug("$4 - Area: " + plot.getArea()); if (plot.hasOwner()) { - PlotSquared.debug("$4 - Owner: " + UUIDHandler.getName(plot.getOwner())); + PlotSquared.debug("$4 - Owner: " + plot.getOwner()); } else { PlotSquared.debug("$4 - Owner: Unowned"); } } public long getAge(UUID uuid) { - if (UUIDHandler.getPlayer(uuid) != null) { + if (PlotSquared.imp().getPlayerManager().getPlayerIfExists(uuid) != null) { return 0; } - String name = UUIDHandler.getName(uuid); - if (name != null) { - Long last = this.dates_cache.get(uuid); - if (last == null) { - OfflinePlotPlayer opp; - if (Settings.UUID.NATIVE_UUID_PROVIDER) { - opp = UUIDHandler.getUUIDWrapper().getOfflinePlayer(uuid); - } else { - opp = UUIDHandler.getUUIDWrapper().getOfflinePlayer(name); - } - if (opp != null && (last = opp.getLastPlayed()) != 0) { - this.dates_cache.put(uuid, last); - } else { - return 0; - } - } - if (last == 0) { + Long last = this.dates_cache.get(uuid); + if (last == null) { + OfflinePlotPlayer opp = PlotSquared.imp().getPlayerManager().getOfflinePlayer(uuid); + if (opp != null && (last = opp.getLastPlayed()) != 0) { + this.dates_cache.put(uuid, last); + } else { return 0; } - return System.currentTimeMillis() - last; } - return 0; - } - - public long getAccountAge(Plot plot) { - if (!plot.hasOwner() || Objects.equals(DBFunc.EVERYONE, plot.getOwner()) - || UUIDHandler.getPlayer(plot.getOwner()) != null || plot.getRunning() > 0) { - return Long.MAX_VALUE; + if (last == 0) { + return 0; } - long max = 0; - for (UUID owner : plot.getOwners()) { - long age = getAccountAge(owner); - max = Math.max(age, max); - } - return max; + return System.currentTimeMillis() - last; } public long getAge(Plot plot) { if (!plot.hasOwner() || Objects.equals(DBFunc.EVERYONE, plot.getOwner()) - || UUIDHandler.getPlayer(plot.getOwner()) != null || plot.getRunning() > 0) { + || PlotSquared.imp().getPlayerManager().getPlayerIfExists(plot.getOwner()) != null || plot.getRunning() > 0) { return 0; } diff --git a/Core/src/main/java/com/plotsquared/core/queue/LocalBlockQueue.java b/Core/src/main/java/com/plotsquared/core/queue/LocalBlockQueue.java index f979b4e9f..76fa5e28a 100644 --- a/Core/src/main/java/com/plotsquared/core/queue/LocalBlockQueue.java +++ b/Core/src/main/java/com/plotsquared/core/queue/LocalBlockQueue.java @@ -25,13 +25,13 @@ */ package com.plotsquared.core.queue; +import com.plotsquared.core.PlotSquared; import com.plotsquared.core.location.Location; import com.plotsquared.core.player.PlotPlayer; import com.plotsquared.core.util.PatternUtil; import com.plotsquared.core.util.SchematicHandler; import com.plotsquared.core.util.StringMan; import com.plotsquared.core.util.WorldUtil; -import com.plotsquared.core.util.uuid.UUIDHandler; import com.sk89q.jnbt.CompoundTag; import com.sk89q.worldedit.function.pattern.Pattern; import com.sk89q.worldedit.math.BlockVector2; @@ -43,8 +43,6 @@ import lombok.Setter; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; -import java.util.Map; - public abstract class LocalBlockQueue { @Getter @Setter private boolean forceSync = false; @@ -124,8 +122,8 @@ public abstract class LocalBlockQueue { regenChunk(x, z); fixChunkLighting(x, z); BlockVector2 loc = BlockVector2.at(x, z); - for (Map.Entry entry : UUIDHandler.getPlayers().entrySet()) { - PlotPlayer pp = entry.getValue(); + + for (final PlotPlayer pp : PlotSquared.imp().getPlayerManager().getPlayers()) { Location pLoc = pp.getLocation(); if (!StringMan.isEqual(getWorld(), pLoc.getWorld()) || !pLoc.getBlockVector2() .equals(loc)) { diff --git a/Core/src/main/java/com/plotsquared/core/util/PlayerManager.java b/Core/src/main/java/com/plotsquared/core/util/PlayerManager.java index fd1eebe4e..473826b2d 100644 --- a/Core/src/main/java/com/plotsquared/core/util/PlayerManager.java +++ b/Core/src/main/java/com/plotsquared/core/util/PlayerManager.java @@ -25,6 +25,7 @@ */ package com.plotsquared.core.util; +import com.plotsquared.core.player.OfflinePlotPlayer; import com.plotsquared.core.player.PlotPlayer; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -38,9 +39,9 @@ import java.util.UUID; /** * Manages player instances */ -public abstract class PlayerManager { +public abstract class PlayerManager

{ - private final Map playerMap = new HashMap<>(); + private final Map playerMap = new HashMap<>(); private final Object playerLock = new Object(); /** @@ -60,15 +61,15 @@ public abstract class PlayerManager { * @param uuid Player UUID * @return Player, or null */ - @Nullable public PlotPlayer getPlayerIfExists(@Nullable final UUID uuid) { + @Nullable public P getPlayerIfExists(@Nullable final UUID uuid) { if (uuid == null) { return null; } return this.playerMap.get(uuid); } - @Nullable public PlotPlayer getPlayerIfExists(@Nullable final String name) { - for (final PlotPlayer plotPlayer : this.playerMap.values()) { + @Nullable public P getPlayerIfExists(@Nullable final String name) { + for (final P plotPlayer : this.playerMap.values()) { if (plotPlayer.getName().equalsIgnoreCase(name)) { return plotPlayer; } @@ -86,9 +87,9 @@ public abstract class PlayerManager { * @param uuid Player UUID * @return Player object */ - @NotNull public PlotPlayer getPlayer(@NotNull final UUID uuid) { + @NotNull public P getPlayer(@NotNull final UUID uuid) { synchronized (playerLock) { - PlotPlayer player = this.playerMap.get(uuid); + P player = this.playerMap.get(uuid); if (player == null) { player = createPlayer(uuid); this.playerMap.put(uuid, player); @@ -97,14 +98,30 @@ public abstract class PlayerManager { } } - @NotNull protected abstract PlotPlayer createPlayer(@NotNull final UUID uuid); + @NotNull protected abstract P createPlayer(@NotNull final UUID uuid); + + /** + * Get an an offline player object from the player's UUID + * + * @param uuid Player UUID + * @return Offline player object + */ + @Nullable public abstract O getOfflinePlayer(@Nullable final UUID uuid); + + /** + * Get an offline player object from the player's username + * + * @param username Player name + * @return Offline player object + */ + @Nullable public abstract O getOfflinePlayer(@NotNull final String username); /** * Get all online players * * @return Unmodifiable collection of players */ - public Collection getPlayers() { + public Collection

getPlayers() { return Collections.unmodifiableCollection(this.playerMap.values()); } diff --git a/Core/src/main/java/com/plotsquared/core/util/SchematicHandler.java b/Core/src/main/java/com/plotsquared/core/util/SchematicHandler.java index aa9e7a04c..78cc7192c 100644 --- a/Core/src/main/java/com/plotsquared/core/util/SchematicHandler.java +++ b/Core/src/main/java/com/plotsquared/core/util/SchematicHandler.java @@ -35,7 +35,6 @@ import com.plotsquared.core.plot.schematic.Schematic; import com.plotsquared.core.queue.LocalBlockQueue; import com.plotsquared.core.util.task.RunnableVal; import com.plotsquared.core.util.task.TaskManager; -import com.plotsquared.core.util.uuid.UUIDHandler; import com.sk89q.jnbt.CompoundTag; import com.sk89q.jnbt.NBTInputStream; import com.sk89q.jnbt.NBTOutputStream; @@ -104,16 +103,24 @@ public abstract class SchematicHandler { Iterator i = plots.iterator(); final Plot plot = i.next(); i.remove(); - String owner = UUIDHandler.getName(plot.getOwnerAbs()); - if (owner == null) { + + PlotSquared.get().getImpromptuUUIDPipeline().getSingle(plot.getOwnerAbs(), (username, throwable) -> { + + }); + + final String owner; + if (plot.hasOwner()) { + owner = plot.getOwnerAbs().toString(); + } else { owner = "unknown"; } + final String name; if (namingScheme == null) { name = plot.getId().x + ";" + plot.getId().y + ',' + plot.getArea() + ',' + owner; } else { - name = namingScheme.replaceAll("%owner%", owner) + name = namingScheme .replaceAll("%id%", plot.getId().toString()) .replaceAll("%idx%", plot.getId().x + "") .replaceAll("%idy%", plot.getId().y + "") diff --git a/Core/src/test/java/com/plotsquared/core/plot/util/UUIDHandlerImplementationTest.java b/Core/src/test/java/com/plotsquared/core/plot/util/UUIDHandlerImplementationTest.java deleted file mode 100644 index 9bcad05bd..000000000 --- a/Core/src/test/java/com/plotsquared/core/plot/util/UUIDHandlerImplementationTest.java +++ /dev/null @@ -1,49 +0,0 @@ -/* - * _____ _ _ _____ _ - * | __ \| | | | / ____| | | - * | |__) | | ___ | |_| (___ __ _ _ _ __ _ _ __ ___ __| | - * | ___/| |/ _ \| __|\___ \ / _` | | | |/ _` | '__/ _ \/ _` | - * | | | | (_) | |_ ____) | (_| | |_| | (_| | | | __/ (_| | - * |_| |_|\___/ \__|_____/ \__, |\__,_|\__,_|_| \___|\__,_| - * | | - * |_| - * PlotSquared plot management system for Minecraft - * Copyright (C) 2020 IntellectualSites - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -package com.plotsquared.core.plot.util; - -import com.plotsquared.core.database.AbstractDBTest; -import com.plotsquared.core.database.DBFunc; -import com.plotsquared.core.util.task.RunnableVal; -import com.plotsquared.core.util.uuid.OfflinePlayerService; -import org.junit.Before; - -import java.util.UUID; - -public class UUIDHandlerImplementationTest extends UUIDHandlerImplementation { - - public UUIDHandlerImplementationTest(OfflinePlayerService wrapper) { - super(wrapper); - } - - @Before public void setUp() throws Exception { - DBFunc.dbManager = new AbstractDBTest(); - } - - @Override public void fetchUUID(String name, RunnableVal ifFetch) { - - } -}