2015-07-26 16:51:12 +02:00
|
|
|
package com.plotsquared.bukkit.object;
|
2015-02-20 12:23:48 +01:00
|
|
|
|
2015-07-30 16:25:16 +02:00
|
|
|
import java.util.HashSet;
|
|
|
|
import java.util.UUID;
|
|
|
|
|
|
|
|
import org.bukkit.Bukkit;
|
|
|
|
import org.bukkit.Effect;
|
|
|
|
import org.bukkit.GameMode;
|
|
|
|
import org.bukkit.WeatherType;
|
|
|
|
import org.bukkit.entity.Player;
|
|
|
|
import org.bukkit.permissions.Permission;
|
|
|
|
import org.bukkit.permissions.PermissionDefault;
|
2015-09-03 07:45:25 +02:00
|
|
|
import org.bukkit.plugin.PluginManager;
|
2015-07-30 16:25:16 +02:00
|
|
|
|
2015-07-27 17:14:38 +02:00
|
|
|
import com.intellectualcrafters.plot.config.C;
|
2015-03-12 10:28:08 +01:00
|
|
|
import com.intellectualcrafters.plot.config.Settings;
|
2015-07-27 19:50:04 +02:00
|
|
|
import com.intellectualcrafters.plot.object.Location;
|
|
|
|
import com.intellectualcrafters.plot.object.PlotPlayer;
|
2015-06-05 14:39:48 +02:00
|
|
|
import com.intellectualcrafters.plot.util.EconHandler;
|
2015-07-27 17:14:38 +02:00
|
|
|
import com.intellectualcrafters.plot.util.MainUtil;
|
2015-07-30 16:25:16 +02:00
|
|
|
import com.intellectualcrafters.plot.util.PlotGamemode;
|
|
|
|
import com.intellectualcrafters.plot.util.PlotWeather;
|
2015-07-27 09:26:50 +02:00
|
|
|
import com.intellectualcrafters.plot.util.UUIDHandler;
|
2015-07-30 19:24:01 +02:00
|
|
|
import com.plotsquared.bukkit.util.BukkitUtil;
|
2015-02-20 12:23:48 +01:00
|
|
|
|
2015-07-30 19:24:01 +02:00
|
|
|
public class BukkitPlayer extends PlotPlayer {
|
2015-02-23 02:32:27 +01:00
|
|
|
|
2015-02-20 12:23:48 +01:00
|
|
|
public final Player player;
|
2015-07-28 13:38:49 +02:00
|
|
|
private UUID uuid;
|
|
|
|
private String name;
|
2015-04-06 14:16:24 +02:00
|
|
|
private long last = 0;
|
2015-07-28 15:25:03 +02:00
|
|
|
public HashSet<String> hasPerm = new HashSet<>();
|
|
|
|
public HashSet<String> noPerm = new HashSet<>();
|
2015-08-01 22:11:28 +02:00
|
|
|
public boolean offline;
|
2015-02-23 02:32:27 +01:00
|
|
|
|
2015-02-21 12:38:44 +01:00
|
|
|
/**
|
|
|
|
* Please do not use this method. Instead use BukkitUtil.getPlayer(Player), as it caches player objects.
|
|
|
|
* @param player
|
|
|
|
*/
|
2015-02-23 02:32:27 +01:00
|
|
|
public BukkitPlayer(final Player player) {
|
2015-02-20 12:23:48 +01:00
|
|
|
this.player = player;
|
|
|
|
}
|
2015-04-06 14:16:24 +02:00
|
|
|
|
2015-08-01 22:11:28 +02:00
|
|
|
public BukkitPlayer(final Player player, boolean offline) {
|
|
|
|
this.player = player;
|
|
|
|
this.offline = offline;
|
|
|
|
}
|
|
|
|
|
2015-04-06 14:16:24 +02:00
|
|
|
public long getPreviousLogin() {
|
|
|
|
if (last == 0) {
|
|
|
|
last = player.getLastPlayed();
|
|
|
|
}
|
|
|
|
return last;
|
|
|
|
}
|
2015-02-23 02:32:27 +01:00
|
|
|
|
2015-02-20 12:23:48 +01:00
|
|
|
@Override
|
|
|
|
public Location getLocation() {
|
2015-07-30 19:24:01 +02:00
|
|
|
Location loc = super.getLocation();
|
|
|
|
return loc == null ? BukkitUtil.getLocation(this.player) : loc;
|
2015-02-20 12:23:48 +01:00
|
|
|
}
|
2015-02-23 02:32:27 +01:00
|
|
|
|
2015-02-20 12:23:48 +01:00
|
|
|
@Override
|
|
|
|
public UUID getUUID() {
|
|
|
|
if (this.uuid == null) {
|
2015-02-21 13:52:50 +01:00
|
|
|
this.uuid = UUIDHandler.getUUID(this);
|
2015-02-20 12:23:48 +01:00
|
|
|
}
|
|
|
|
return this.uuid;
|
|
|
|
}
|
2015-02-23 02:32:27 +01:00
|
|
|
|
2015-02-20 12:23:48 +01:00
|
|
|
@Override
|
2015-08-20 06:56:25 +02:00
|
|
|
public boolean hasPermission(final String node) {
|
2015-03-12 10:28:08 +01:00
|
|
|
if (Settings.PERMISSION_CACHING) {
|
2015-08-20 06:56:25 +02:00
|
|
|
if (this.noPerm.contains(node)) {
|
2015-03-12 10:28:08 +01:00
|
|
|
return false;
|
|
|
|
}
|
2015-08-20 06:56:25 +02:00
|
|
|
if (this.hasPerm.contains(node)) {
|
2015-03-12 10:28:08 +01:00
|
|
|
return true;
|
|
|
|
}
|
2015-02-21 07:42:30 +01:00
|
|
|
}
|
2015-08-01 22:11:28 +02:00
|
|
|
if (offline && EconHandler.manager != null) {
|
2015-08-20 06:56:25 +02:00
|
|
|
return EconHandler.manager.hasPermission(getName(), node);
|
|
|
|
}
|
|
|
|
boolean value = this.player.hasPermission(node);
|
|
|
|
if (Settings.PERMISSION_CACHING) {
|
|
|
|
if (value) {
|
|
|
|
this.hasPerm.add(node);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
this.noPerm.add(node);
|
|
|
|
}
|
2015-08-01 22:11:28 +02:00
|
|
|
}
|
2015-08-20 06:56:25 +02:00
|
|
|
return value;
|
2015-02-20 12:23:48 +01:00
|
|
|
}
|
2015-02-23 02:32:27 +01:00
|
|
|
|
2015-09-03 07:45:25 +02:00
|
|
|
public Permission getPermission(String node) {
|
|
|
|
PluginManager manager = Bukkit.getPluginManager();
|
|
|
|
Permission perm = manager.getPermission(node);
|
|
|
|
if (perm == null) {
|
|
|
|
String[] nodes = node.split("\\.");
|
|
|
|
perm = new Permission(node);
|
|
|
|
final StringBuilder n = new StringBuilder();
|
|
|
|
for (int i = 0; i < (nodes.length - 1); i++) {
|
|
|
|
n.append(nodes[i] + ("."));
|
|
|
|
if (!node.equals(n + C.PERMISSION_STAR.s())) {
|
|
|
|
Permission parent = getPermission(n + C.PERMISSION_STAR.s());
|
|
|
|
if (parent != null) {
|
|
|
|
perm.addParent(parent, true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
manager.addPermission(perm);
|
|
|
|
}
|
|
|
|
manager.recalculatePermissionDefaults(perm);
|
|
|
|
perm.recalculatePermissibles();
|
|
|
|
return perm;
|
|
|
|
}
|
|
|
|
|
2015-02-20 12:23:48 +01:00
|
|
|
@Override
|
2015-02-23 02:32:27 +01:00
|
|
|
public void sendMessage(final String message) {
|
2015-02-20 12:23:48 +01:00
|
|
|
this.player.sendMessage(message);
|
|
|
|
}
|
2015-02-23 02:32:27 +01:00
|
|
|
|
2015-07-27 17:14:38 +02:00
|
|
|
@Override
|
|
|
|
public void sendMessage(C c, String... args) {
|
|
|
|
MainUtil.sendMessage(this, c, args);
|
|
|
|
}
|
|
|
|
|
2015-02-20 12:23:48 +01:00
|
|
|
@Override
|
2015-02-23 02:32:27 +01:00
|
|
|
public void teleport(final Location loc) {
|
2015-08-28 08:36:26 +02:00
|
|
|
if (Math.abs(loc.getX()) >= 30000000 || Math.abs(loc.getZ()) >= 30000000) {
|
|
|
|
return;
|
|
|
|
}
|
2015-07-20 07:14:46 +02:00
|
|
|
this.player.teleport(new org.bukkit.Location(BukkitUtil.getWorld(loc.getWorld()), loc.getX() + 0.5, loc.getY(), loc.getZ() + 0.5, loc.getYaw(), loc.getPitch()));
|
2015-02-23 02:32:27 +01:00
|
|
|
}
|
|
|
|
|
2015-02-20 12:23:48 +01:00
|
|
|
@Override
|
|
|
|
public String getName() {
|
|
|
|
if (this.name == null) {
|
2015-02-23 02:32:27 +01:00
|
|
|
this.name = this.player.getName();
|
2015-02-20 12:23:48 +01:00
|
|
|
}
|
|
|
|
return this.name;
|
|
|
|
}
|
2015-02-23 02:32:27 +01:00
|
|
|
|
2015-02-20 12:23:48 +01:00
|
|
|
@Override
|
|
|
|
public boolean isOnline() {
|
2015-08-01 22:11:28 +02:00
|
|
|
return !offline && this.player.isOnline();
|
2015-02-20 12:23:48 +01:00
|
|
|
}
|
2015-02-23 02:32:27 +01:00
|
|
|
|
2015-02-22 13:46:47 +01:00
|
|
|
@Override
|
2015-02-23 02:32:27 +01:00
|
|
|
public void setCompassTarget(final Location loc) {
|
|
|
|
this.player.setCompassTarget(new org.bukkit.Location(BukkitUtil.getWorld(loc.getWorld()), loc.getX(), loc.getY(), loc.getZ()));
|
|
|
|
|
2015-02-22 13:46:47 +01:00
|
|
|
}
|
2015-02-23 02:32:27 +01:00
|
|
|
|
2015-02-23 12:37:36 +01:00
|
|
|
@Override
|
|
|
|
public Location getLocationFull() {
|
|
|
|
return BukkitUtil.getLocationFull(this.player);
|
|
|
|
}
|
2015-05-04 11:52:37 +02:00
|
|
|
|
2015-06-05 14:39:48 +02:00
|
|
|
@Override
|
|
|
|
public void setAttribute(String key) {
|
2015-06-05 14:42:45 +02:00
|
|
|
key = "plotsquared_user_attributes." + key;
|
2015-08-24 18:28:32 +02:00
|
|
|
if (EconHandler.manager == null || player.hasPermission("plotsquared_user_attributes.*")) {
|
2015-08-18 15:20:11 +02:00
|
|
|
setMeta(key, true);
|
|
|
|
return;
|
|
|
|
}
|
2015-08-01 22:11:28 +02:00
|
|
|
EconHandler.manager.setPermission(getName(), key, true);
|
2015-06-05 14:39:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean getAttribute(String key) {
|
2015-06-05 14:42:45 +02:00
|
|
|
key = "plotsquared_user_attributes." + key;
|
2015-08-24 16:35:37 +02:00
|
|
|
if (EconHandler.manager == null || player.hasPermission("plotsquared_user_attributes.*")) {
|
2015-08-18 15:20:11 +02:00
|
|
|
Object v = getMeta(key);
|
|
|
|
return v == null ? false : (Boolean) v;
|
|
|
|
}
|
2015-06-05 15:05:17 +02:00
|
|
|
Permission perm = Bukkit.getServer().getPluginManager().getPermission(key);
|
|
|
|
if (perm == null) {
|
|
|
|
perm = new Permission(key, PermissionDefault.FALSE);
|
|
|
|
Bukkit.getServer().getPluginManager().addPermission(perm);
|
|
|
|
Bukkit.getServer().getPluginManager().recalculatePermissionDefaults(perm);
|
|
|
|
}
|
2015-06-05 14:39:48 +02:00
|
|
|
return player.hasPermission(key);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void removeAttribute(String key) {
|
2015-06-05 14:42:45 +02:00
|
|
|
key = "plotsquared_user_attributes." + key;
|
2015-08-26 08:44:50 +02:00
|
|
|
if (EconHandler.manager == null || player.hasPermission("plotsquared_user_attributes.*")) {
|
2015-08-18 15:20:11 +02:00
|
|
|
deleteMeta(key);
|
|
|
|
return;
|
|
|
|
}
|
2015-08-01 22:11:28 +02:00
|
|
|
EconHandler.manager.setPermission(getName(), key, false);
|
2015-06-05 14:39:48 +02:00
|
|
|
}
|
2015-06-23 13:13:48 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void loadData() {
|
|
|
|
if (!player.isOnline()) {
|
|
|
|
player.loadData();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void saveData() {
|
|
|
|
player.saveData();
|
|
|
|
}
|
2015-07-27 17:14:38 +02:00
|
|
|
|
2015-07-30 16:25:16 +02:00
|
|
|
@Override
|
|
|
|
public void setWeather(PlotWeather weather) {
|
|
|
|
switch (weather) {
|
|
|
|
case CLEAR:
|
|
|
|
player.setPlayerWeather(WeatherType.CLEAR);
|
|
|
|
return;
|
|
|
|
case RAIN: {
|
|
|
|
player.setPlayerWeather(WeatherType.DOWNFALL);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
case RESET:
|
|
|
|
player.resetPlayerWeather();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public PlotGamemode getGamemode() {
|
|
|
|
switch (player.getGameMode()) {
|
|
|
|
case ADVENTURE:
|
|
|
|
return PlotGamemode.ADVENTURE;
|
|
|
|
case CREATIVE:
|
|
|
|
return PlotGamemode.CREATIVE;
|
|
|
|
case SPECTATOR:
|
|
|
|
return PlotGamemode.SPECTATOR;
|
|
|
|
case SURVIVAL:
|
|
|
|
return PlotGamemode.SURVIVAL;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void setGamemode(PlotGamemode gamemode) {
|
|
|
|
switch (gamemode) {
|
|
|
|
case ADVENTURE:
|
|
|
|
player.setGameMode(GameMode.ADVENTURE);
|
2015-08-07 01:30:08 +02:00
|
|
|
return;
|
2015-07-30 16:25:16 +02:00
|
|
|
case CREATIVE:
|
|
|
|
player.setGameMode(GameMode.CREATIVE);
|
2015-08-07 01:30:08 +02:00
|
|
|
return;
|
2015-07-30 16:25:16 +02:00
|
|
|
case SPECTATOR:
|
|
|
|
player.setGameMode(GameMode.SPECTATOR);
|
2015-08-07 01:30:08 +02:00
|
|
|
return;
|
2015-07-30 16:25:16 +02:00
|
|
|
case SURVIVAL:
|
|
|
|
player.setGameMode(GameMode.SURVIVAL);
|
2015-08-07 01:30:08 +02:00
|
|
|
return;
|
2015-07-30 16:25:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void setTime(long time) {
|
|
|
|
player.setPlayerTime(time, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void setFlight(boolean fly) {
|
|
|
|
player.setAllowFlight(fly);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void playMusic(Location loc, int id) {
|
2015-08-06 18:05:15 +02:00
|
|
|
player.playEffect(BukkitUtil.getLocation(loc), Effect.RECORD_PLAY, id);
|
2015-07-30 16:25:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void kick(String message) {
|
|
|
|
player.kickPlayer(message);
|
|
|
|
}
|
2015-02-20 12:23:48 +01:00
|
|
|
}
|