2015-07-26 16:51:12 +02:00
|
|
|
package com.plotsquared.bukkit.object;
|
2015-02-20 12:23:48 +01:00
|
|
|
|
2016-09-26 10:31:08 +02:00
|
|
|
import com.intellectualcrafters.plot.PS;
|
2016-02-25 15:17:07 +01:00
|
|
|
import com.intellectualcrafters.plot.object.Location;
|
|
|
|
import com.intellectualcrafters.plot.object.PlotPlayer;
|
|
|
|
import com.intellectualcrafters.plot.util.EconHandler;
|
2016-03-23 02:41:37 +01:00
|
|
|
import com.intellectualcrafters.plot.util.PlotGameMode;
|
2016-02-25 15:17:07 +01:00
|
|
|
import com.intellectualcrafters.plot.util.PlotWeather;
|
2016-06-16 02:31:25 +02:00
|
|
|
import com.intellectualcrafters.plot.util.StringMan;
|
2016-02-25 15:17:07 +01:00
|
|
|
import com.intellectualcrafters.plot.util.UUIDHandler;
|
|
|
|
import com.plotsquared.bukkit.util.BukkitUtil;
|
2015-07-30 16:25:16 +02:00
|
|
|
import org.bukkit.Effect;
|
|
|
|
import org.bukkit.GameMode;
|
|
|
|
import org.bukkit.WeatherType;
|
|
|
|
import org.bukkit.entity.Player;
|
2016-06-20 19:02:41 +02:00
|
|
|
import org.bukkit.event.EventException;
|
|
|
|
import org.bukkit.event.player.PlayerTeleportEvent;
|
2015-09-05 03:35:43 +02:00
|
|
|
import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause;
|
2015-07-30 16:25:16 +02:00
|
|
|
|
2016-02-25 15:17:07 +01:00
|
|
|
import java.util.UUID;
|
2016-06-20 19:02:41 +02:00
|
|
|
import org.bukkit.plugin.RegisteredListener;
|
2015-02-20 12:23:48 +01:00
|
|
|
|
2015-09-13 06:04:31 +02:00
|
|
|
public class BukkitPlayer extends PlotPlayer {
|
|
|
|
|
2015-02-20 12:23:48 +01:00
|
|
|
public final Player player;
|
2016-02-14 02:01:18 +01:00
|
|
|
public boolean offline;
|
2015-07-28 13:38:49 +02:00
|
|
|
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>
|
2015-02-21 12:38:44 +01:00
|
|
|
* @param player
|
|
|
|
*/
|
2016-03-23 02:41:37 +01:00
|
|
|
public BukkitPlayer(Player player) {
|
2015-02-20 12:23:48 +01:00
|
|
|
this.player = player;
|
2016-01-16 17:07:42 +01:00
|
|
|
super.populatePersistentMetaMap();
|
2015-02-20 12:23:48 +01:00
|
|
|
}
|
2016-03-23 02:41:37 +01:00
|
|
|
|
|
|
|
public BukkitPlayer(Player player, boolean offline) {
|
2015-08-01 22:11:28 +02:00
|
|
|
this.player = player;
|
|
|
|
this.offline = offline;
|
2016-01-16 17:07:42 +01:00
|
|
|
super.populatePersistentMetaMap();
|
2015-08-01 22:11:28 +02:00
|
|
|
}
|
2016-06-04 23:19:37 +02:00
|
|
|
|
2015-02-20 12:23:48 +01:00
|
|
|
@Override
|
2015-09-13 06:04:31 +02:00
|
|
|
public Location getLocation() {
|
2016-03-23 02:41:37 +01:00
|
|
|
Location location = super.getLocation();
|
|
|
|
return location == null ? BukkitUtil.getLocation(this.player) : location;
|
2015-02-20 12:23:48 +01:00
|
|
|
}
|
2015-09-13 06:04:31 +02:00
|
|
|
|
2015-02-20 12:23:48 +01:00
|
|
|
@Override
|
2015-09-13 06:04:31 +02:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2016-06-20 19:02:41 +02:00
|
|
|
@Override
|
|
|
|
public boolean canTeleport(Location loc) {
|
|
|
|
org.bukkit.Location to = BukkitUtil.getLocation(loc);
|
|
|
|
org.bukkit.Location from = player.getLocation();
|
|
|
|
PlayerTeleportEvent event = new PlayerTeleportEvent(player, from, to);
|
|
|
|
RegisteredListener[] listeners = event.getHandlers().getRegisteredListeners();
|
|
|
|
for (RegisteredListener listener : listeners) {
|
2016-09-26 10:31:08 +02:00
|
|
|
if (listener.getPlugin().getName().equals(PS.imp().getPluginName())) {
|
2016-06-20 19:02:41 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
listener.callEvent(event);
|
|
|
|
} catch (EventException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (event.isCancelled() || !event.getTo().equals(to)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
event = new PlayerTeleportEvent(player, to, from);
|
|
|
|
for (RegisteredListener listener : listeners) {
|
2016-09-26 10:31:08 +02:00
|
|
|
if (listener.getPlugin().getName().equals(PS.imp().getPluginName())) {
|
2016-06-20 19:02:41 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
listener.callEvent(event);
|
|
|
|
} catch (EventException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-02-20 12:23:48 +01:00
|
|
|
@Override
|
2016-03-23 02:41:37 +01:00
|
|
|
public boolean hasPermission(String permission) {
|
|
|
|
if (this.offline && EconHandler.manager != null) {
|
|
|
|
return EconHandler.manager.hasPermission(getName(), permission);
|
2015-08-20 06:56:25 +02:00
|
|
|
}
|
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
|
|
|
|
2015-02-20 12:23:48 +01:00
|
|
|
@Override
|
2016-03-23 02:41:37 +01:00
|
|
|
public void sendMessage(String message) {
|
2016-06-19 08:14:13 +02:00
|
|
|
if (!StringMan.isEqual(this.<String>getMeta("lastMessage"), message) || (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
|
|
|
}
|
2015-09-13 06:04:31 +02:00
|
|
|
|
2015-02-20 12:23:48 +01:00
|
|
|
@Override
|
2016-03-23 02:41:37 +01:00
|
|
|
public void teleport(Location location) {
|
|
|
|
if (Math.abs(location.getX()) >= 30000000 || Math.abs(location.getZ()) >= 30000000) {
|
2015-09-13 06:04:31 +02:00
|
|
|
return;
|
|
|
|
}
|
2016-03-23 02:41:37 +01:00
|
|
|
this.player.teleport(
|
|
|
|
new org.bukkit.Location(BukkitUtil.getWorld(location.getWorld()), location.getX() + 0.5, location.getY(), location.getZ() + 0.5,
|
|
|
|
location.getYaw(), location.getPitch()), TeleportCause.COMMAND);
|
2015-02-23 02:32:27 +01:00
|
|
|
}
|
2015-09-13 06:04:31 +02:00
|
|
|
|
2015-02-20 12:23:48 +01:00
|
|
|
@Override
|
2015-09-13 06:04:31 +02:00
|
|
|
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
|
|
|
}
|
2015-09-13 06:04:31 +02:00
|
|
|
|
2015-02-20 12:23:48 +01:00
|
|
|
@Override
|
2015-09-13 06:04:31 +02:00
|
|
|
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
|
|
|
}
|
2015-09-13 06:04:31 +02:00
|
|
|
|
2015-02-22 13:46:47 +01:00
|
|
|
@Override
|
2016-03-23 02:41:37 +01:00
|
|
|
public void setCompassTarget(Location location) {
|
|
|
|
this.player.setCompassTarget(
|
|
|
|
new org.bukkit.Location(BukkitUtil.getWorld(location.getWorld()), location.getX(), location.getY(), location.getZ()));
|
2015-09-13 06:04:31 +02:00
|
|
|
|
2015-02-22 13:46:47 +01:00
|
|
|
}
|
2015-09-13 06:04:31 +02:00
|
|
|
|
2015-02-23 12:37:36 +01:00
|
|
|
@Override
|
2015-09-13 06:04:31 +02:00
|
|
|
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
|
|
|
|
2015-07-30 16:25:16 +02:00
|
|
|
@Override
|
2016-03-23 02:41:37 +01:00
|
|
|
public void setWeather(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;
|
2016-02-14 02:01:18 +01:00
|
|
|
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 02:41:37 +01:00
|
|
|
this.player.resetPlayerWeather();
|
2016-03-23 18:09:13 +01:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
this.player.resetPlayerWeather();
|
|
|
|
break;
|
2015-07-30 16:25:16 +02:00
|
|
|
}
|
|
|
|
}
|
2015-09-13 06:04:31 +02:00
|
|
|
|
2015-07-30 16:25:16 +02:00
|
|
|
@Override
|
2016-03-23 02:41:37 +01:00
|
|
|
public PlotGameMode getGameMode() {
|
|
|
|
switch (this.player.getGameMode()) {
|
2015-07-30 16:25:16 +02:00
|
|
|
case ADVENTURE:
|
2016-03-23 02:41:37 +01:00
|
|
|
return PlotGameMode.ADVENTURE;
|
2015-07-30 16:25:16 +02:00
|
|
|
case CREATIVE:
|
2016-03-23 02:41:37 +01:00
|
|
|
return PlotGameMode.CREATIVE;
|
2015-07-30 16:25:16 +02:00
|
|
|
case SPECTATOR:
|
2016-03-23 02:41:37 +01:00
|
|
|
return PlotGameMode.SPECTATOR;
|
2015-07-30 16:25:16 +02:00
|
|
|
case SURVIVAL:
|
2016-03-23 02:41:37 +01:00
|
|
|
return PlotGameMode.SURVIVAL;
|
2016-03-23 18:09:13 +01:00
|
|
|
default:
|
2016-03-28 19:28:21 +02:00
|
|
|
return PlotGameMode.NOT_SET;
|
2015-07-30 16:25:16 +02:00
|
|
|
}
|
|
|
|
}
|
2015-09-13 06:04:31 +02:00
|
|
|
|
2015-07-30 16:25:16 +02:00
|
|
|
@Override
|
2016-03-23 02:41:37 +01:00
|
|
|
public void setGameMode(PlotGameMode gameMode) {
|
|
|
|
switch (gameMode) {
|
2015-07-30 16:25:16 +02:00
|
|
|
case ADVENTURE:
|
2016-03-23 02:41:37 +01:00
|
|
|
this.player.setGameMode(GameMode.ADVENTURE);
|
2016-03-23 18:09:13 +01:00
|
|
|
break;
|
2015-07-30 16:25:16 +02:00
|
|
|
case CREATIVE:
|
2016-03-23 02:41:37 +01:00
|
|
|
this.player.setGameMode(GameMode.CREATIVE);
|
2016-03-23 18:09:13 +01:00
|
|
|
break;
|
2015-07-30 16:25:16 +02:00
|
|
|
case SPECTATOR:
|
2016-03-23 02:41:37 +01:00
|
|
|
this.player.setGameMode(GameMode.SPECTATOR);
|
2016-03-23 18:09:13 +01:00
|
|
|
break;
|
2015-07-30 16:25:16 +02:00
|
|
|
case SURVIVAL:
|
2016-03-23 02:41:37 +01:00
|
|
|
this.player.setGameMode(GameMode.SURVIVAL);
|
2016-03-23 18:09:13 +01:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
this.player.setGameMode(GameMode.SURVIVAL);
|
|
|
|
break;
|
2015-07-30 16:25:16 +02:00
|
|
|
}
|
|
|
|
}
|
2015-09-13 06:04:31 +02:00
|
|
|
|
2015-07-30 16:25:16 +02:00
|
|
|
@Override
|
2016-03-23 02:41:37 +01:00
|
|
|
public void setTime(long time) {
|
2015-11-15 03:30:52 +01:00
|
|
|
if (time != Long.MAX_VALUE) {
|
2016-03-23 02:41:37 +01:00
|
|
|
this.player.setPlayerTime(time, false);
|
2015-11-15 03:30:52 +01:00
|
|
|
} else {
|
2016-03-23 02:41:37 +01:00
|
|
|
this.player.resetPlayerTime();
|
2015-11-15 03:30:52 +01:00
|
|
|
}
|
2015-07-30 16:25:16 +02:00
|
|
|
}
|
2015-09-13 06:04:31 +02:00
|
|
|
|
2015-07-30 16:25:16 +02:00
|
|
|
@Override
|
2016-03-23 02:41:37 +01:00
|
|
|
public void setFlight(boolean fly) {
|
|
|
|
this.player.setAllowFlight(fly);
|
2015-07-30 16:25:16 +02:00
|
|
|
}
|
2016-06-16 02:08:01 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean getFlight() {
|
|
|
|
return player.getAllowFlight();
|
|
|
|
}
|
|
|
|
|
2015-07-30 16:25:16 +02:00
|
|
|
@Override
|
2016-03-23 02:41:37 +01:00
|
|
|
public void playMusic(Location location, int id) {
|
2016-06-13 21:05:38 +02:00
|
|
|
//noinspection deprecation
|
|
|
|
this.player.playEffect(BukkitUtil.getLocation(location), Effect.RECORD_PLAY, id);
|
2015-07-30 16:25:16 +02:00
|
|
|
}
|
2015-09-13 06:04:31 +02:00
|
|
|
|
2015-07-30 16:25:16 +02:00
|
|
|
@Override
|
2016-03-23 02:41:37 +01:00
|
|
|
public void kick(String message) {
|
|
|
|
this.player.kickPlayer(message);
|
2015-07-30 16:25:16 +02:00
|
|
|
}
|
2016-02-28 02:11:18 +01:00
|
|
|
|
|
|
|
@Override public void stopSpectating() {
|
2016-03-23 02:41:37 +01:00
|
|
|
if (getGameMode() == PlotGameMode.SPECTATOR) {
|
|
|
|
this.player.setSpectatorTarget(null);
|
2016-02-28 02:11:18 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-07 08:33:33 +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
|
|
|
}
|
2015-02-20 12:23:48 +01:00
|
|
|
}
|