Remove construction of fake player entities for offline players.

This commit is contained in:
Alexander Söderberg 2020-07-23 12:47:00 +02:00
parent 9d6744ec15
commit 7fc8238fb0
17 changed files with 40 additions and 286 deletions

View File

@ -570,7 +570,8 @@ import static com.plotsquared.core.util.ReflectionUtils.getRefClass;
}
final Plot plot = area.getOwnedPlot(id);
if (plot != null) {
if (!plot.getFlag(ServerPlotFlag.class) || PlotPlayer.wrap(plot.getOwner()) == null) {
if (!plot.getFlag(ServerPlotFlag.class) || PlotSquared.platform().getPlayerManager()
.getPlayerIfExists(plot.getOwner()) == null) {
if (world.getKeepSpawnInMemory()) {
world.setKeepSpawnInMemory(false);
return;
@ -1078,39 +1079,6 @@ import static com.plotsquared.core.util.ReflectionUtils.getRefClass;
}
}
/**
* Attempt to retrieve a {@link PlotPlayer} from a player identifier.
* This method accepts:
* - {@link Player} objects,
* - {@link OfflinePlayer} objects,
* - {@link String} usernames for online players, and
* - {@link UUID} UUIDs for online players
* <p>
* In the case of offline players, a fake {@link Player} instance will be created.
* This is a rather expensive operation, and should be avoided if possible.
*
* @param player The player to convert to a PlotPlayer
* @return The plot player instance that corresponds to the identifier, or null
* if no such player object could be created
*/
@Override @Nullable public PlotPlayer<Player> wrapPlayer(final Object player) {
if (player instanceof Player) {
return BukkitUtil.adapt((Player) player);
}
if (player instanceof OfflinePlayer) {
return BukkitUtil.adapt((OfflinePlayer) player);
}
if (player instanceof String) {
return (PlotPlayer<Player>) PlotSquared.platform().getPlayerManager()
.getPlayerIfExists((String) player);
}
if (player instanceof UUID) {
return (PlotPlayer<Player>) PlotSquared.platform().getPlayerManager()
.getPlayerIfExists((UUID) player);
}
return null;
}
@Override public String getNMSPackage() {
final String name = Bukkit.getServer().getClass().getPackage().getName();
return name.substring(name.lastIndexOf('.') + 1);

View File

@ -30,6 +30,8 @@ import com.plotsquared.core.permissions.PermissionHandler;
import com.plotsquared.core.permissions.PermissionProfile;
import com.plotsquared.core.player.OfflinePlotPlayer;
import org.bukkit.OfflinePlayer;
import javax.annotation.Nonnegative;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
@ -43,8 +45,6 @@ public class BukkitOfflinePlayer implements OfflinePlotPlayer {
/**
* Please do not use this method. Instead use BukkitUtil.getPlayer(Player),
* as it caches player objects.
*
* @param player
*/
public BukkitOfflinePlayer(@Nonnull final OfflinePlayer player, @Nonnull final
PermissionHandler permissionHandler) {
@ -57,12 +57,8 @@ public class BukkitOfflinePlayer implements OfflinePlotPlayer {
return this.player.getUniqueId();
}
@Override public long getLastPlayed() {
return this.player.getLastPlayed();
}
@Override public boolean isOnline() {
return this.player.isOnline();
@Override @Nonnegative public long getLastPlayed() {
return this.player.getLastSeen();
}
@Override public String getName() {

View File

@ -75,10 +75,8 @@ public class BukkitPlayer extends PlotPlayer<Player> {
private final EconHandler econHandler;
public final Player player;
private boolean offline;
private String name;
/**
* <p>Please do not use this method. Instead use
* BukkitUtil.getPlayer(Player), as it caches player objects.</p>
@ -90,19 +88,12 @@ public class BukkitPlayer extends PlotPlayer<Player> {
this(plotAreaManager, eventDispatcher, player, false, econHandler, permissionHandler);
}
public BukkitPlayer(@Nonnull final PlotAreaManager plotAreaManager, @Nonnull final EventDispatcher eventDispatcher,
@Nonnull final Player player, final boolean offline, @Nullable final EconHandler econHandler,
@Nonnull final PermissionHandler permissionHandler) {
this(plotAreaManager, eventDispatcher, player, offline, true, econHandler, permissionHandler);
}
public BukkitPlayer(@Nonnull final PlotAreaManager plotAreaManager, @Nonnull final
EventDispatcher eventDispatcher, @Nonnull final Player player, final boolean offline,
EventDispatcher eventDispatcher, @Nonnull final Player player,
final boolean realPlayer, @Nullable final EconHandler econHandler,
@Nonnull final PermissionHandler permissionHandler) {
super(plotAreaManager, eventDispatcher, econHandler, permissionHandler);
this.player = player;
this.offline = offline;
this.econHandler = econHandler;
if (realPlayer) {
super.populatePersistentMetaMap();
@ -130,8 +121,8 @@ public class BukkitPlayer extends PlotPlayer<Player> {
return player.getUniqueId();
}
@Override public long getLastPlayed() {
return this.player.getLastPlayed();
@Override @Nonnegative public long getLastPlayed() {
return this.player.getLastSeen();
}
@Override public boolean canTeleport(@Nonnull final Location location) {
@ -255,10 +246,6 @@ public class BukkitPlayer extends PlotPlayer<Player> {
return this.name;
}
@Override public boolean isOnline() {
return !this.offline && this.player.isOnline();
}
@Override public void setCompassTarget(Location location) {
this.player.setCompassTarget(
new org.bukkit.Location(BukkitUtil.getWorld(location.getWorldName()), location.getX(),

View File

@ -60,11 +60,13 @@ import java.util.UUID;
}
@Nonnull @Override public BukkitPlayer getPlayer(@Nonnull final Player object) {
if (!object.isOnline()) {
throw new NoSuchPlayerException(object.getUniqueId());
}
try {
return getPlayer(object.getUniqueId());
} catch (final NoSuchPlayerException exception) {
return new BukkitPlayer(this.plotAreaManager, this.eventDispatcher, object,
object.isOnline(), false, this.econHandler, this.permissionHandler);
return new BukkitPlayer(this.plotAreaManager, this.eventDispatcher, object, false, this.econHandler, this.permissionHandler);
}
}

View File

@ -53,7 +53,6 @@ import io.papermc.lib.PaperLib;
import org.bukkit.Bukkit;
import org.bukkit.Chunk;
import org.bukkit.Material;
import org.bukkit.OfflinePlayer;
import org.bukkit.World;
import org.bukkit.block.Biome;
import org.bukkit.block.Block;
@ -101,7 +100,6 @@ import java.util.Collection;
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;
import java.util.UUID;
import java.util.function.Consumer;
import java.util.function.IntConsumer;
import java.util.stream.Stream;
@ -116,24 +114,6 @@ import java.util.stream.Stream;
super(regionManager);
}
/**
* Get a {@link PlotPlayer} from an {@link OfflinePlayer}. If the player is
* online, it returns a complete player. If the player is offline, it creates
* a fake player
*
* @param op Offline player
* @return Plot player instance
*/
@Nonnull public static PlotPlayer<Player> adapt(@Nonnull final OfflinePlayer op) {
if (op.isOnline()) {
return adapt(Objects.requireNonNull(op.getPlayer()));
}
final Player player = OfflinePlayerUtil.loadPlayer(op);
player.loadData();
return new BukkitPlayer(PlotSquared.get().getPlotAreaManager(),
PlotSquared.get().getEventDispatcher(), player, true, PlotSquared.platform().getEconHandler(), PlotSquared.platform().getPermissionHandler());
}
/**
* Turn a Bukkit {@link Player} into a PlotSquared {@link PlotPlayer}
*
@ -214,18 +194,6 @@ import java.util.stream.Stream;
}
}
/**
* Gets the PlotPlayer for a UUID. The PlotPlayer is usually cached and
* will provide useful functions relating to players.
*
* @param uuid the uuid to wrap
* @return a {@code PlotPlayer}
* @see PlotPlayer#wrap(Object)
*/
@Override @Nonnull public PlotPlayer<?> getPlayer(@Nonnull final UUID uuid) {
return PlotPlayer.wrap(Bukkit.getOfflinePlayer(uuid));
}
@Override public boolean isBlockSame(@Nonnull final BlockState block1,
@Nonnull final BlockState block2) {
if (block1.equals(block2)) {

View File

@ -1,133 +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 <http://www.gnu.org/licenses/>.
*/
package com.plotsquared.bukkit.util;
import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.util.UUID;
import static com.plotsquared.core.util.ReflectionUtils.callConstructor;
import static com.plotsquared.core.util.ReflectionUtils.callMethod;
import static com.plotsquared.core.util.ReflectionUtils.getCbClass;
import static com.plotsquared.core.util.ReflectionUtils.getField;
import static com.plotsquared.core.util.ReflectionUtils.getNmsClass;
import static com.plotsquared.core.util.ReflectionUtils.getUtilClass;
import static com.plotsquared.core.util.ReflectionUtils.makeConstructor;
import static com.plotsquared.core.util.ReflectionUtils.makeField;
import static com.plotsquared.core.util.ReflectionUtils.makeMethod;
public class OfflinePlayerUtil {
public static Player loadPlayer(OfflinePlayer player) {
if (player == null) {
return null;
}
if (player instanceof Player) {
return (Player) player;
}
return loadPlayer(player.getUniqueId(), player.getName());
}
private static Player loadPlayer(UUID id, String name) {
Object server = getMinecraftServer();
Object interactManager = newPlayerInteractManager();
Object worldServer = getWorldServer();
Object profile = newGameProfile(id, name);
Class<?> entityPlayerClass = getNmsClass("EntityPlayer");
Constructor entityPlayerConstructor =
makeConstructor(entityPlayerClass, getNmsClass("MinecraftServer"),
getNmsClass("WorldServer"), getUtilClass("com.mojang.authlib.GameProfile"),
getNmsClass("PlayerInteractManager"));
Object entityPlayer =
callConstructor(entityPlayerConstructor, server, worldServer, profile, interactManager);
return (Player) getBukkitEntity(entityPlayer);
}
private static Object newGameProfile(UUID id, String name) {
Class<?> gameProfileClass = getUtilClass("com.mojang.authlib.GameProfile");
if (gameProfileClass == null) { //Before uuids
return name;
}
Constructor gameProfileConstructor =
makeConstructor(gameProfileClass, UUID.class, String.class);
if (gameProfileConstructor == null) { //Version has string constructor
gameProfileConstructor = makeConstructor(gameProfileClass, String.class, String.class);
return callConstructor(gameProfileConstructor, id.toString(), name);
} else { //Version has uuid constructor
return callConstructor(gameProfileConstructor, id, name);
}
}
private static Object newPlayerInteractManager() {
Object worldServer = getWorldServer();
Class<?> playerInteractClass = getNmsClass("PlayerInteractManager");
Class<?> worldClass = getNmsClass("World");
Constructor<?> c = makeConstructor(playerInteractClass, worldClass);
if (c == null) {
c = makeConstructor(playerInteractClass, getNmsClass("WorldServer"));
}
return callConstructor(c, worldServer);
}
public static Object getWorldServerNew() {
Object server = getMinecraftServer();
Class<?> minecraftServerClass = getNmsClass("MinecraftServer");
Class<?> dimensionManager = getNmsClass("DimensionManager");
Object overworld = getField(makeField(dimensionManager, "OVERWORLD"), null);
Method getWorldServer =
makeMethod(minecraftServerClass, "getWorldServer", dimensionManager);
return callMethod(getWorldServer, server, overworld);
}
private static Object getWorldServer() {
Object server = getMinecraftServer();
Class<?> minecraftServerClass = getNmsClass("MinecraftServer");
Method getWorldServer = makeMethod(minecraftServerClass, "getWorldServer", int.class);
Object o;
try {
o = callMethod(getWorldServer, server, 0);
} catch (final RuntimeException e) {
o = getWorldServerNew();
}
return o;
}
//NMS Utils
private static Object getMinecraftServer() {
return callMethod(makeMethod(getCbClass("CraftServer"), "getServer"), Bukkit.getServer());
}
private static Entity getBukkitEntity(Object o) {
Method getBukkitEntity = makeMethod(o.getClass(), "getBukkitEntity");
return callMethod(getBukkitEntity, o);
}
}

View File

@ -39,6 +39,8 @@ import com.plotsquared.core.util.SchematicHandler;
import com.plotsquared.core.util.query.PlotQuery;
import lombok.NoArgsConstructor;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.Collections;
import java.util.Set;
import java.util.UUID;
@ -178,10 +180,9 @@ import java.util.UUID;
*
* @param uuid the uuid of the player to wrap
* @return a {@code PlotPlayer}
* @see PlotPlayer#wrap(Object)
*/
public PlotPlayer wrapPlayer(UUID uuid) {
return PlotPlayer.wrap(uuid);
@Nullable public PlotPlayer<?> wrapPlayer(@Nonnull final UUID uuid) {
return PlotSquared.platform().getPlayerManager().getPlayerIfExists(uuid);
}
/**
@ -189,10 +190,9 @@ import java.util.UUID;
*
* @param player the player to wrap
* @return a {@code PlotPlayer}
* @see PlotPlayer#wrap(Object)
*/
public PlotPlayer wrapPlayer(String player) {
return PlotPlayer.wrap(player);
@Nullable public PlotPlayer<?> wrapPlayer(@Nonnull final String player) {
return PlotSquared.platform().getPlayerManager().getPlayerIfExists(player);
}
/**

View File

@ -27,6 +27,7 @@ package com.plotsquared.core;
import com.google.inject.Injector;
import com.google.inject.Key;
import com.google.inject.TypeLiteral;
import com.plotsquared.core.backup.BackupManager;
import com.plotsquared.core.generator.GeneratorWrapper;
import com.plotsquared.core.generator.HybridUtils;
@ -80,14 +81,6 @@ public interface PlotPlatform<P> extends ILogger {
*/
File getWorldContainer();
/**
* Wraps a player into a PlotPlayer object.
*
* @param player The player to convert to a PlotPlayer
* @return A PlotPlayer
*/
@Nullable PlotPlayer<P> wrapPlayer(Object player);
/**
* Completely shuts down the plugin.
*/
@ -186,7 +179,7 @@ public interface PlotPlatform<P> extends ILogger {
* @return Player manager
*/
@Nonnull default PlayerManager<? extends PlotPlayer<P>, ? extends P> getPlayerManager() {
return getInjector().getInstance(PlayerManager.class);
return getInjector().getInstance(Key.get(new TypeLiteral<PlayerManager<? extends PlotPlayer<P>, ? extends P>>() {}));
}
/**

View File

@ -93,7 +93,7 @@ public class Buy extends Command {
this.econHandler.depositMoney(PlotSquared.platform().getPlayerManager().getOfflinePlayer(plot.getOwnerAbs()), price);
PlotPlayer owner = PlotSquared.platform().getPlayerManager().getPlayerIfExists(plot.getOwnerAbs());
PlotPlayer<?> owner = PlotSquared.platform().getPlayerManager().getPlayerIfExists(plot.getOwnerAbs());
if (owner != null) {
Captions.PLOT_SOLD.send(owner, plot.getId(), player.getName(), price);
}

View File

@ -94,7 +94,7 @@ public class PlotListener {
++value.count;
if (value.count == value.interval) {
value.count = 0;
PlotPlayer<?> player = PlotPlayer.wrap(entry.getKey());
final PlotPlayer<?> player = PlotSquared.platform().getPlayerManager().getPlayerIfExists(entry.getKey());
if (player == null) {
iterator.remove();
continue;
@ -114,7 +114,7 @@ public class PlotListener {
++value.count;
if (value.count == value.interval) {
value.count = 0;
PlotPlayer<?> player = PlotSquared.platform().getWorldUtil().getPlayer(entry.getKey());
final PlotPlayer<?> player = PlotSquared.platform().getPlayerManager().getPlayerIfExists(entry.getKey());
if (player == null) {
iterator.remove();
continue;

View File

@ -26,6 +26,7 @@
package com.plotsquared.core.listener;
import com.google.inject.Inject;
import com.plotsquared.core.PlotSquared;
import com.plotsquared.core.configuration.Captions;
import com.plotsquared.core.configuration.Settings;
import com.plotsquared.core.player.PlotPlayer;
@ -72,7 +73,7 @@ public class WESubscriber {
Actor actor = event.getActor();
if (actor != null && actor.isPlayer()) {
String name = actor.getName();
PlotPlayer plotPlayer = PlotPlayer.wrap(name);
final PlotPlayer<?> plotPlayer = PlotSquared.platform().getPlayerManager().getPlayerIfExists(name);
Set<CuboidRegion> mask;
if (plotPlayer == null) {
Player player = (Player) actor;

View File

@ -120,7 +120,7 @@ public class ConsolePlayer extends PlotPlayer<Actor> {
}
@Override public long getLastPlayed() {
return 0;
return System.currentTimeMillis();
}
@Override public void sendMessage(String message) {
@ -132,10 +132,6 @@ public class ConsolePlayer extends PlotPlayer<Actor> {
setMeta(META_LOCATION, location);
}
@Override public boolean isOnline() {
return true;
}
@Override public String getName() {
return "*";
}

View File

@ -27,6 +27,7 @@ package com.plotsquared.core.player;
import com.plotsquared.core.permissions.PermissionHolder;
import javax.annotation.Nonnegative;
import java.util.UUID;
public interface OfflinePlotPlayer extends PermissionHolder {
@ -42,16 +43,8 @@ public interface OfflinePlotPlayer extends PermissionHolder {
* Gets the time in milliseconds when the player was last seen online.
*
* @return the time in milliseconds when last online
* @deprecated This method may be inconsistent across platforms. The javadoc may be wrong depending on which platform is used.
*/
@SuppressWarnings("DeprecatedIsStillUsed") @Deprecated long getLastPlayed();
/**
* Checks if this player is online.
*
* @return {@code true} if this player is online
*/
boolean isOnline();
@Nonnegative long getLastPlayed();
/**
* Gets the name of this player.

View File

@ -140,20 +140,6 @@ public abstract class PlotPlayer<P> implements CommandCaller, OfflinePlotPlayer
return players;
}
/**
* Efficiently wrap a Player, or OfflinePlayer object to get a PlotPlayer (or fetch if it's already cached)<br>
* - Accepts sponge/bukkit Player (online)
* - Accepts player name (online)
* - Accepts UUID
* - Accepts bukkit OfflinePlayer (offline)
*
* @param player Player object to wrap
* @return Wrapped player
*/
public static PlotPlayer<?> wrap(Object player) {
return PlotSquared.platform().wrapPlayer(player);
}
@Override public final boolean hasPermission(@Nullable final String world,
@Nonnull final String permission) {
return this.permissionProfile.hasPermission(world, permission);

View File

@ -1926,11 +1926,16 @@ public class Plot {
DBFunc.createPlotAndSettings(this, () -> {
PlotArea plotworld = Plot.this.area;
if (notify && plotworld.isAutoMerge()) {
PlotPlayer player = this.worldUtil.getPlayer(uuid);
final PlotPlayer<?> player = PlotSquared.platform().getPlayerManager()
.getPlayerIfExists(uuid);
PlotMergeEvent event = this.eventDispatcher
.callMerge(this, Direction.ALL, Integer.MAX_VALUE, player);
if (event.getEventResult() == Result.DENY) {
sendMessage(player, Captions.EVENT_DENIED, "Auto merge on claim");
if (player != null) {
sendMessage(player, Captions.EVENT_DENIED, "Auto merge on claim");
}
return;
}
Plot.this.autoMerge(event.getDir(), event.getMax(), uuid, true);
@ -3083,10 +3088,10 @@ public class Plot {
if (!TaskManager.removeFromTeleportQueue(name)) {
return;
}
if (player.isOnline()) {
try {
MainUtil.sendMessage(player, Captions.TELEPORTED_TO_PLOT);
player.teleport(location, cause);
}
} catch (final Exception ignored) {}
}, TaskTime.seconds(Settings.Teleport.DELAY));
resultConsumer.accept(true);
};

View File

@ -418,13 +418,13 @@ public class ExpireManager {
}
}
for (UUID helper : plot.getTrusted()) {
PlotPlayer player = PlotSquared.platform().getPlayerManager().getPlayerIfExists(helper);
PlotPlayer<?> player = PlotSquared.platform().getPlayerManager().getPlayerIfExists(helper);
if (player != null) {
MainUtil.sendMessage(player, Captions.PLOT_REMOVED_USER, plot.toString());
}
}
for (UUID helper : plot.getMembers()) {
PlotPlayer player = PlotSquared.platform().getPlayerManager().getPlayerIfExists(helper);
PlotPlayer<?> player = PlotSquared.platform().getPlayerManager().getPlayerIfExists(helper);
if (player != null) {
MainUtil.sendMessage(player, Captions.PLOT_REMOVED_USER, plot.toString());
}

View File

@ -328,14 +328,6 @@ public abstract class WorldUtil {
*/
public abstract boolean isBlockSame(@Nonnull BlockState block1, @Nonnull BlockState block2);
/**
* Get a player object for the player with the given UUID
*
* @param uuid Player UUID
* @return Player object
*/
@Nonnull public abstract PlotPlayer<?> getPlayer(@Nonnull UUID uuid);
/**
* Get the player health
*