PlotSquared/Bukkit/src/main/java/com/plotsquared/bukkit/player/BukkitPlayer.java

326 lines
12 KiB
Java
Raw Normal View History

2020-04-11 02:19:18 +02:00
package com.plotsquared.bukkit.player;
2015-02-20 12:23:48 +01:00
2020-04-11 02:19:18 +02:00
import com.plotsquared.bukkit.util.BukkitUtil;
import com.plotsquared.PlotSquared;
import com.plotsquared.config.Captions;
import com.plotsquared.location.Location;
import com.plotsquared.player.PlotPlayer;
import com.plotsquared.events.TeleportCause;
import com.plotsquared.util.EconHandler;
import com.plotsquared.util.MathMan;
import com.plotsquared.plot.PlotWeather;
import com.plotsquared.util.StringMan;
import com.plotsquared.util.uuid.UUIDHandler;
2019-11-04 20:58:24 +01:00
import com.sk89q.worldedit.bukkit.BukkitAdapter;
2019-11-04 22:55:40 +01:00
import com.sk89q.worldedit.extension.platform.Actor;
2019-11-04 20:55:55 +01:00
import com.sk89q.worldedit.world.item.ItemType;
import com.sk89q.worldedit.world.item.ItemTypes;
import io.papermc.lib.PaperLib;
import org.bukkit.GameMode;
import org.bukkit.Sound;
import org.bukkit.WeatherType;
2015-07-30 16:25:16 +02:00
import org.bukkit.entity.Player;
import org.bukkit.event.Event;
import org.bukkit.event.EventException;
import org.bukkit.event.player.PlayerTeleportEvent;
2018-08-10 17:01:10 +02:00
import org.bukkit.permissions.PermissionAttachmentInfo;
import org.bukkit.plugin.RegisteredListener;
import org.jetbrains.annotations.NotNull;
2015-07-30 16:25:16 +02:00
2018-12-26 19:16:34 +01:00
import java.util.Arrays;
2018-05-18 06:48:17 +02:00
import java.util.Set;
import java.util.UUID;
2018-12-26 19:16:34 +01:00
import java.util.stream.Collectors;
2018-05-17 09:46:54 +02:00
import static com.sk89q.worldedit.world.gamemode.GameModes.ADVENTURE;
import static com.sk89q.worldedit.world.gamemode.GameModes.CREATIVE;
import static com.sk89q.worldedit.world.gamemode.GameModes.SPECTATOR;
import static com.sk89q.worldedit.world.gamemode.GameModes.SURVIVAL;
2015-09-13 06:04:31 +02:00
public class BukkitPlayer extends PlotPlayer {
2018-08-10 17:01:10 +02:00
2019-04-24 00:48:22 +02:00
private static boolean CHECK_EFFECTIVE = true;
2015-02-20 12:23:48 +01:00
public final Player player;
private boolean offline;
private UUID uuid;
private String name;
2016-06-04 23:19:37 +02:00
2015-02-21 12:38:44 +01:00
/**
2016-03-28 19:28:21 +02:00
* <p>Please do not use this method. Instead use
* BukkitUtil.getPlayer(Player), as it caches player objects.</p>
2018-08-10 17:01:10 +02:00
*
* @param player Bukkit player instance
2015-02-21 12:38:44 +01:00
*/
2019-08-06 22:08:56 +02:00
public BukkitPlayer(@NotNull final Player player) {
2015-02-20 12:23:48 +01:00
this.player = player;
super.populatePersistentMetaMap();
2015-02-20 12:23:48 +01:00
}
2016-03-23 02:41:37 +01:00
2019-08-06 22:08:56 +02:00
public BukkitPlayer(@NotNull final Player player, final boolean offline) {
2015-08-01 22:11:28 +02:00
this.player = player;
this.offline = offline;
super.populatePersistentMetaMap();
2015-08-01 22:11:28 +02:00
}
2016-06-04 23:19:37 +02:00
2019-11-04 22:55:40 +01:00
@Override public Actor toActor() {
return BukkitAdapter.adapt(player);
}
2019-09-08 20:02:45 +02:00
@NotNull @Override public Location getLocation() {
final Location location = super.getLocation();
2016-03-23 02:41:37 +01:00
return location == null ? BukkitUtil.getLocation(this.player) : location;
2015-02-20 12:23:48 +01:00
}
2018-08-10 17:01:10 +02:00
@NotNull @Override public UUID getUUID() {
2016-03-23 02:41:37 +01:00
if (this.uuid == null) {
this.uuid = UUIDHandler.getUUID(this);
2015-02-20 12:23:48 +01:00
}
2016-03-23 02:41:37 +01:00
return this.uuid;
2015-02-20 12:23:48 +01:00
}
2016-06-04 23:19:37 +02:00
@Override public long getLastPlayed() {
return this.player.getLastPlayed();
}
2019-09-08 20:02:45 +02:00
@Override public boolean canTeleport(@NotNull final Location location) {
final org.bukkit.Location to = BukkitUtil.getLocation(location);
final org.bukkit.Location from = player.getLocation();
PlayerTeleportEvent event = new PlayerTeleportEvent(player, from, to);
callEvent(event);
if (event.isCancelled() || !event.getTo().equals(to)) {
return false;
}
event = new PlayerTeleportEvent(player, to, from);
callEvent(event);
return true;
}
@Override
public void sendTitle(String title, String subtitle, int fadeIn, int stay, int fadeOut) {
player.sendTitle(title, subtitle, fadeIn, stay, fadeOut);
}
2019-08-06 22:08:56 +02:00
private void callEvent(@NotNull final Event event) {
final RegisteredListener[] listeners = event.getHandlers().getRegisteredListeners();
for (final RegisteredListener listener : listeners) {
2018-12-27 17:29:35 +01:00
if (listener.getPlugin().getName().equals(PlotSquared.imp().getPluginName())) {
continue;
}
try {
listener.callEvent(event);
} catch (final EventException e) {
e.printStackTrace();
}
}
}
@Override public boolean hasPermission(final String permission) {
2016-03-23 02:41:37 +01:00
if (this.offline && EconHandler.manager != null) {
return EconHandler.manager.hasPermission(getName(), permission);
}
2016-03-23 02:41:37 +01:00
return this.player.hasPermission(permission);
2015-02-20 12:23:48 +01:00
}
2016-03-23 02:41:37 +01:00
@Override public int hasPermissionRange(final String stub, final int range) {
if (hasPermission(Captions.PERMISSION_ADMIN.getTranslated())) {
2018-05-17 09:46:54 +02:00
return Integer.MAX_VALUE;
}
final String[] nodes = stub.split("\\.");
final StringBuilder n = new StringBuilder();
2018-05-17 09:46:54 +02:00
for (int i = 0; i < (nodes.length - 1); i++) {
n.append(nodes[i]).append(".");
if (!stub.equals(n + Captions.PERMISSION_STAR.getTranslated())) {
if (hasPermission(n + Captions.PERMISSION_STAR.getTranslated())) {
2018-05-17 09:46:54 +02:00
return Integer.MAX_VALUE;
}
}
}
if (hasPermission(stub + ".*")) {
return Integer.MAX_VALUE;
}
int max = 0;
if (CHECK_EFFECTIVE) {
boolean hasAny = false;
String stubPlus = stub + ".";
final Set<PermissionAttachmentInfo> effective = player.getEffectivePermissions();
if (!effective.isEmpty()) {
for (PermissionAttachmentInfo attach : effective) {
String permStr = attach.getPermission();
if (permStr.startsWith(stubPlus)) {
hasAny = true;
String end = permStr.substring(stubPlus.length());
if (MathMan.isInteger(end)) {
int val = Integer.parseInt(end);
if (val > range) {
return val;
}
if (val > max) {
max = val;
}
}
2018-05-18 06:48:17 +02:00
}
}
if (hasAny) {
return max;
2018-05-17 09:46:54 +02:00
}
// Workaround
for (PermissionAttachmentInfo attach : effective) {
String permStr = attach.getPermission();
2019-04-22 17:53:24 +02:00
if (permStr.startsWith("plots.") && !permStr.equals("plots.use")) {
return max;
}
}
CHECK_EFFECTIVE = false;
}
}
for (int i = range; i > 0; i--) {
if (hasPermission(stub + "." + i)) {
return i;
2018-05-17 09:46:54 +02:00
}
}
return max;
}
@Override public boolean isPermissionSet(final String permission) {
2017-01-12 21:22:28 +01:00
return this.player.isPermissionSet(permission);
}
@Override public void sendMessage(String message) {
message = message.replace('\u2010', '%')
.replace('\u2020', '&').replace('\u2030', '&');
if (!StringMan.isEqual(this.getMeta("lastMessage"), message) || (
2018-08-10 17:01:10 +02:00
System.currentTimeMillis() - this.<Long>getMeta("lastMessageTime") > 5000)) {
2016-06-16 02:31:25 +02:00
setMeta("lastMessage", message);
2016-06-19 08:14:13 +02:00
setMeta("lastMessageTime", System.currentTimeMillis());
2016-06-16 02:31:25 +02:00
this.player.sendMessage(message);
}
2015-02-20 12:23:48 +01:00
}
2018-08-10 17:01:10 +02:00
@Override public void teleport(@NotNull final Location location, @NotNull final TeleportCause cause) {
2016-03-23 02:41:37 +01:00
if (Math.abs(location.getX()) >= 30000000 || Math.abs(location.getZ()) >= 30000000) {
2015-09-13 06:04:31 +02:00
return;
}
final org.bukkit.Location bukkitLocation = new org.bukkit.Location(BukkitUtil.getWorld(location.getWorld()), location.getX() + 0.5,
location.getY(), location.getZ() + 0.5, location.getYaw(), location.getPitch());
PaperLib.teleportAsync(player, bukkitLocation, getTeleportCause(cause));
2015-02-23 02:32:27 +01:00
}
2018-08-10 17:01:10 +02:00
@Override public String getName() {
2016-03-23 02:41:37 +01:00
if (this.name == null) {
this.name = this.player.getName();
2015-02-20 12:23:48 +01:00
}
2016-03-23 02:41:37 +01:00
return this.name;
2015-02-20 12:23:48 +01:00
}
2018-08-10 17:01:10 +02:00
@Override public boolean isOnline() {
2016-03-23 02:41:37 +01:00
return !this.offline && this.player.isOnline();
2015-02-20 12:23:48 +01:00
}
2018-08-10 17:01:10 +02:00
@Override public void setCompassTarget(Location location) {
2016-03-23 02:41:37 +01:00
this.player.setCompassTarget(
2018-08-10 17:01:10 +02:00
new org.bukkit.Location(BukkitUtil.getWorld(location.getWorld()), location.getX(),
location.getY(), location.getZ()));
2015-02-22 13:46:47 +01:00
}
2018-08-10 17:01:10 +02:00
@Override public Location getLocationFull() {
2016-03-23 02:41:37 +01:00
return BukkitUtil.getLocationFull(this.player);
2015-02-23 12:37:36 +01:00
}
2016-03-23 02:41:37 +01:00
2019-08-06 22:08:56 +02:00
@Override public void setWeather(@NotNull final PlotWeather weather) {
2015-09-13 06:04:31 +02:00
switch (weather) {
2015-07-30 16:25:16 +02:00
case CLEAR:
2016-03-23 02:41:37 +01:00
this.player.setPlayerWeather(WeatherType.CLEAR);
2016-03-23 18:09:13 +01:00
break;
case RAIN:
2016-03-23 02:41:37 +01:00
this.player.setPlayerWeather(WeatherType.DOWNFALL);
2016-03-23 18:09:13 +01:00
break;
2015-07-30 16:25:16 +02:00
case RESET:
2016-03-23 18:09:13 +01:00
default:
this.player.resetPlayerWeather();
break;
2015-07-30 16:25:16 +02:00
}
}
2018-08-10 17:01:10 +02:00
@NotNull @Override public com.sk89q.worldedit.world.gamemode.GameMode getGameMode() {
2016-03-23 02:41:37 +01:00
switch (this.player.getGameMode()) {
2015-07-30 16:25:16 +02:00
case ADVENTURE:
return ADVENTURE;
2015-07-30 16:25:16 +02:00
case CREATIVE:
return CREATIVE;
2015-07-30 16:25:16 +02:00
case SPECTATOR:
return SPECTATOR;
2015-07-30 16:25:16 +02:00
case SURVIVAL:
2016-03-23 18:09:13 +01:00
default:
return SURVIVAL;
2015-07-30 16:25:16 +02:00
}
}
2018-08-10 17:01:10 +02:00
@Override public void setGameMode(@NotNull final com.sk89q.worldedit.world.gamemode.GameMode gameMode) {
if (ADVENTURE.equals(gameMode)) {
this.player.setGameMode(GameMode.ADVENTURE);
} else if (CREATIVE.equals(gameMode)) {
this.player.setGameMode(GameMode.CREATIVE);
} else if (SPECTATOR.equals(gameMode)) {
this.player.setGameMode(GameMode.SPECTATOR);
} else {
this.player.setGameMode(GameMode.SURVIVAL);
2015-07-30 16:25:16 +02:00
}
}
2018-08-10 17:01:10 +02:00
@Override public void setTime(final long time) {
if (time != Long.MAX_VALUE) {
2016-03-23 02:41:37 +01:00
this.player.setPlayerTime(time, false);
} else {
2016-03-23 02:41:37 +01:00
this.player.resetPlayerTime();
}
2015-07-30 16:25:16 +02:00
}
2016-06-16 02:08:01 +02:00
2018-08-10 17:01:10 +02:00
@Override public boolean getFlight() {
2016-06-16 02:08:01 +02:00
return player.getAllowFlight();
}
2018-08-10 17:01:10 +02:00
@Override public void setFlight(boolean fly) {
this.player.setAllowFlight(fly);
}
2019-11-04 20:55:55 +01:00
@Override public void playMusic(@NotNull final Location location, @NotNull final ItemType id) {
if (id == ItemTypes.AIR) {
2018-12-26 19:16:34 +01:00
// Let's just stop all the discs because why not?
for (final Sound sound : Arrays.stream(Sound.values())
.filter(sound -> sound.name().contains("DISC")).collect(Collectors.toList())) {
2018-12-26 19:16:34 +01:00
player.stopSound(sound);
}
// this.player.playEffect(BukkitUtil.getLocation(location), Effect.RECORD_PLAY, Material.AIR);
} else {
// this.player.playEffect(BukkitUtil.getLocation(location), Effect.RECORD_PLAY, id.to(Material.class));
this.player.playSound(BukkitUtil.getLocation(location),
2019-11-04 20:55:55 +01:00
Sound.valueOf(BukkitAdapter.adapt(id).name()), Float.MAX_VALUE, 1f);
2018-12-26 19:16:34 +01:00
}
2015-07-30 16:25:16 +02:00
}
2018-08-10 17:01:10 +02:00
@Override public void kick(final String message) {
2016-03-23 02:41:37 +01:00
this.player.kickPlayer(message);
2015-07-30 16:25:16 +02:00
}
@Override public void stopSpectating() {
if (getGameMode() == SPECTATOR) {
2016-03-23 02:41:37 +01:00
this.player.setSpectatorTarget(null);
}
}
2018-08-10 17:01:10 +02:00
@Override public boolean isBanned() {
2016-03-23 02:41:37 +01:00
return this.player.isBanned();
2015-10-07 08:33:33 +02:00
}
public PlayerTeleportEvent.TeleportCause getTeleportCause(@NotNull final TeleportCause cause) {
switch (cause) {
case COMMAND:
return PlayerTeleportEvent.TeleportCause.COMMAND;
case PLUGIN:
return PlayerTeleportEvent.TeleportCause.PLUGIN;
default: return PlayerTeleportEvent.TeleportCause.UNKNOWN;
}
}
2015-02-20 12:23:48 +01:00
}