289 lines
9.6 KiB
Java
Raw Normal View History

2015-07-31 03:24:01 +10:00
package com.plotsquared.sponge.object;
import java.time.Instant;
import java.util.HashSet;
import java.util.UUID;
2016-02-19 12:03:22 -05:00
import org.spongepowered.api.Sponge;
2015-08-23 11:36:41 +01:00
import org.spongepowered.api.data.key.Keys;
import org.spongepowered.api.data.manipulator.mutable.TargetedLocationData;
import org.spongepowered.api.data.value.mutable.Value;
2015-10-07 17:33:33 +11:00
import org.spongepowered.api.effect.sound.SoundTypes;
import org.spongepowered.api.entity.living.player.Player;
import org.spongepowered.api.entity.living.player.gamemode.GameMode;
import org.spongepowered.api.entity.living.player.gamemode.GameModes;
import org.spongepowered.api.service.ban.BanService;
import org.spongepowered.api.text.Text;
import org.spongepowered.api.text.chat.ChatTypes;
import com.flowpowered.math.vector.Vector3d;
import com.intellectualcrafters.plot.commands.RequiredType;
import com.intellectualcrafters.plot.config.Settings;
import com.intellectualcrafters.plot.object.Location;
import com.intellectualcrafters.plot.object.PlotPlayer;
2015-10-07 17:33:33 +11:00
import com.intellectualcrafters.plot.util.EconHandler;
2015-07-31 00:25:16 +10:00
import com.intellectualcrafters.plot.util.PlotGamemode;
import com.intellectualcrafters.plot.util.PlotWeather;
import com.intellectualcrafters.plot.util.UUIDHandler;
2015-07-31 00:25:16 +10:00
import com.plotsquared.sponge.util.SpongeUtil;
2015-09-13 14:04:31 +10:00
public class SpongePlayer extends PlotPlayer {
public final Player player;
private UUID uuid;
private String name;
private long last = 0;
2015-07-31 06:13:54 +10:00
public HashSet<String> hasPerm = new HashSet<>();
public HashSet<String> noPerm = new HashSet<>();
2015-09-13 14:04:31 +10:00
public SpongePlayer(final Player player) {
this.player = player;
super.populatePersistentMetaMap();
}
2015-09-13 14:04:31 +10:00
@Override
2015-09-13 14:04:31 +10:00
public RequiredType getSuperCaller() {
return RequiredType.PLAYER;
}
2015-09-13 14:04:31 +10:00
@Override
2015-09-13 14:04:31 +10:00
public long getPreviousLogin() {
if (last != 0) {
return last;
}
final Value<Instant> data = player.getJoinData().lastPlayed();
2015-09-13 14:04:31 +10:00
if (data.exists()) {
return last = data.get().getEpochSecond() * 1000;
2015-09-13 14:04:31 +10:00
}
return 0;
}
2015-09-13 14:04:31 +10:00
@Override
2015-09-13 14:04:31 +10:00
public Location getLocation() {
2015-09-11 20:09:22 +10:00
final Location loc = super.getLocation();
return loc == null ? SpongeUtil.getLocation(player) : loc;
}
2015-09-13 14:04:31 +10:00
@Override
2015-09-13 14:04:31 +10:00
public Location getLocationFull() {
return SpongeUtil.getLocationFull(player);
}
2015-09-13 14:04:31 +10:00
@Override
2015-09-13 14:04:31 +10:00
public UUID getUUID() {
if (uuid == null) {
2015-09-11 20:09:22 +10:00
uuid = UUIDHandler.getUUID(this);
}
return uuid;
}
2015-09-13 14:04:31 +10:00
@Override
2015-09-13 14:04:31 +10:00
public boolean hasPermission(final String perm) {
if (Settings.PERMISSION_CACHING) {
if (noPerm.contains(perm)) {
return false;
}
if (hasPerm.contains(perm)) {
return true;
}
2015-09-11 20:09:22 +10:00
final boolean result = player.hasPermission(perm);
2015-09-13 14:04:31 +10:00
if (!result) {
2015-09-11 20:09:22 +10:00
noPerm.add(perm);
return false;
}
2015-09-11 20:09:22 +10:00
hasPerm.add(perm);
return true;
}
2016-02-19 12:03:22 -05:00
return player.hasPermission(perm);
}
2015-09-13 14:04:31 +10:00
@Override
2015-09-13 14:04:31 +10:00
public void sendMessage(final String message) {
player.sendMessage(ChatTypes.CHAT, Text.of(message));
}
2015-09-13 14:04:31 +10:00
@Override
2015-09-13 14:04:31 +10:00
public void teleport(final Location loc) {
if ((Math.abs(loc.getX()) >= 30000000) || (Math.abs(loc.getZ()) >= 30000000)) {
return;
}
2015-09-11 20:09:22 +10:00
final String world = player.getWorld().getName();
2015-09-13 14:04:31 +10:00
if (!world.equals(loc.getWorld())) {
2015-07-31 00:25:16 +10:00
player.transferToWorld(loc.getWorld(), new Vector3d(loc.getX(), loc.getY(), loc.getZ()));
2015-09-13 14:04:31 +10:00
} else {
org.spongepowered.api.world.Location current = player.getLocation();
2015-07-31 00:25:16 +10:00
current = current.setPosition(new Vector3d(loc.getX(), loc.getY(), loc.getZ()));
player.setLocation(current);
}
}
2015-09-13 14:04:31 +10:00
@Override
2015-09-13 14:04:31 +10:00
public boolean isOnline() {
return player.isOnline();
}
2015-09-13 14:04:31 +10:00
@Override
2015-09-13 14:04:31 +10:00
public String getName() {
if (name == null) {
2015-09-11 20:09:22 +10:00
name = player.getName();
}
2015-09-11 20:09:22 +10:00
return name;
}
2015-09-13 14:04:31 +10:00
@Override
2015-09-13 14:04:31 +10:00
public void setCompassTarget(final Location loc) {
2015-09-11 20:09:22 +10:00
final TargetedLocationData target = player.getOrCreate(TargetedLocationData.class).get();
2015-08-14 08:52:31 +10:00
target.set(Keys.TARGETED_LOCATION, SpongeUtil.getLocation(loc));
}
2015-09-13 14:04:31 +10:00
@Override
2015-09-13 14:04:31 +10:00
public void loadData() {
// TODO Auto-generated method stub
throw new UnsupportedOperationException("NOT IMPLEMENTED YET");
}
2015-09-13 14:04:31 +10:00
@Override
2015-09-13 14:04:31 +10:00
public void saveData() {
// TODO Auto-generated method stub
throw new UnsupportedOperationException("NOT IMPLEMENTED YET");
}
2015-09-13 14:04:31 +10:00
@Override
2015-09-13 14:04:31 +10:00
public void setAttribute(String key) {
key = "plotsquared_user_attributes." + key;
2015-10-07 17:33:33 +11:00
if ((EconHandler.manager == null) || player.hasPermission("plotsquared_user_attributes.*")) {
setMeta(key, true);
return;
}
EconHandler.manager.setPermission(getName(), key, true);
}
2015-09-13 14:04:31 +10:00
@Override
2015-09-13 14:04:31 +10:00
public boolean getAttribute(String key) {
key = "plotsquared_user_attributes." + key;
2015-10-07 17:33:33 +11:00
if ((EconHandler.manager == null) || player.hasPermission("plotsquared_user_attributes.*")) {
final Object v = getMeta(key);
return v == null ? false : (Boolean) v;
}
return player.hasPermission(key);
}
2015-09-13 14:04:31 +10:00
@Override
2015-09-13 14:04:31 +10:00
public void removeAttribute(String key) {
key = "plotsquared_user_attributes." + key;
2015-10-07 17:33:33 +11:00
EconHandler.manager.setPermission(getName(), key, false);
2015-09-07 22:31:48 +10:00
deleteMeta(key);
}
2015-09-13 14:04:31 +10:00
2015-07-31 00:25:16 +10:00
@Override
2015-09-13 14:04:31 +10:00
public void setWeather(final PlotWeather weather) {
2015-07-31 00:25:16 +10:00
// TODO Auto-generated method stub
throw new UnsupportedOperationException("NOT IMPLEMENTED YET");
}
2015-09-13 14:04:31 +10:00
2015-07-31 00:25:16 +10:00
@Override
2015-09-13 14:04:31 +10:00
public PlotGamemode getGamemode() {
2015-09-11 20:09:22 +10:00
final GameMode gamemode = player.getGameModeData().type().get();
2015-09-13 14:04:31 +10:00
if (gamemode == GameModes.ADVENTURE) {
return PlotGamemode.ADVENTURE;
}
if (gamemode == GameModes.CREATIVE) {
return PlotGamemode.CREATIVE;
}
if (gamemode == GameModes.SPECTATOR) {
return PlotGamemode.SPECTATOR;
}
if (gamemode == GameModes.SURVIVAL) {
return PlotGamemode.SURVIVAL;
}
2015-07-31 00:25:16 +10:00
throw new UnsupportedOperationException("INVALID GAMEMODE");
}
2015-09-13 14:04:31 +10:00
2015-07-31 00:25:16 +10:00
@Override
2015-09-13 14:04:31 +10:00
public void setGamemode(final PlotGamemode gamemode) {
2015-10-07 17:33:33 +11:00
switch (gamemode) {
case ADVENTURE:
player.offer(Keys.GAME_MODE, GameModes.ADVENTURE);
return;
case CREATIVE:
player.offer(Keys.GAME_MODE, GameModes.CREATIVE);
return;
case SPECTATOR:
player.offer(Keys.GAME_MODE, GameModes.SPECTATOR);
return;
case SURVIVAL:
player.offer(Keys.GAME_MODE, GameModes.SURVIVAL);
return;
}
2015-09-11 20:09:22 +10:00
}
2015-09-13 14:04:31 +10:00
2015-09-11 20:09:22 +10:00
@Override
2015-09-13 14:04:31 +10:00
public void setTime(final long time) {
2015-07-31 00:25:16 +10:00
// TODO Auto-generated method stub
if (time != Long.MAX_VALUE) {} else {}
2015-07-31 00:25:16 +10:00
throw new UnsupportedOperationException("NOT IMPLEMENTED YET");
}
2015-09-13 14:04:31 +10:00
2015-07-31 00:25:16 +10:00
@Override
2015-09-13 14:04:31 +10:00
public void setFlight(final boolean fly) {
2015-10-07 17:33:33 +11:00
player.offer(Keys.IS_FLYING, fly);
player.offer(Keys.CAN_FLY, fly);
2015-07-31 00:25:16 +10:00
}
2015-09-13 14:04:31 +10:00
2015-07-31 00:25:16 +10:00
@Override
2015-09-13 14:04:31 +10:00
public void playMusic(final Location loc, final int id) {
2015-10-07 17:33:33 +11:00
switch (id) {
case 0:
player.playSound(null, SpongeUtil.getLocation(loc).getPosition(), 1);
break;
case 2256:
player.playSound(SoundTypes.RECORDS_11, SpongeUtil.getLocation(loc).getPosition(), 1);
break;
case 2257:
player.playSound(SoundTypes.RECORDS_13, SpongeUtil.getLocation(loc).getPosition(), 1);
break;
case 2258:
player.playSound(SoundTypes.RECORDS_BLOCKS, SpongeUtil.getLocation(loc).getPosition(), 1);
break;
case 2259:
player.playSound(SoundTypes.RECORDS_CAT, SpongeUtil.getLocation(loc).getPosition(), 1);
break;
case 2260:
player.playSound(SoundTypes.RECORDS_CHIRP, SpongeUtil.getLocation(loc).getPosition(), 1);
break;
case 2261:
player.playSound(SoundTypes.RECORDS_FAR, SpongeUtil.getLocation(loc).getPosition(), 1);
break;
case 2262:
player.playSound(SoundTypes.RECORDS_MALL, SpongeUtil.getLocation(loc).getPosition(), 1);
break;
case 2263:
player.playSound(SoundTypes.RECORDS_MELLOHI, SpongeUtil.getLocation(loc).getPosition(), 1);
break;
case 2264:
player.playSound(SoundTypes.RECORDS_STAL, SpongeUtil.getLocation(loc).getPosition(), 1);
break;
case 2265:
player.playSound(SoundTypes.RECORDS_STRAD, SpongeUtil.getLocation(loc).getPosition(), 1);
break;
case 2266:
player.playSound(SoundTypes.RECORDS_WAIT, SpongeUtil.getLocation(loc).getPosition(), 1);
break;
case 2267:
player.playSound(SoundTypes.RECORDS_WARD, SpongeUtil.getLocation(loc).getPosition(), 1);
break;
}
2015-07-31 00:25:16 +10:00
}
2015-09-13 14:04:31 +10:00
2015-07-31 00:25:16 +10:00
@Override
2015-09-13 14:04:31 +10:00
public void kick(final String message) {
2016-02-19 12:03:22 -05:00
player.kick(Text.of(message));
2015-07-31 00:25:16 +10:00
}
2015-10-07 17:33:33 +11:00
@Override
public boolean isBanned() {
2016-02-19 12:03:22 -05:00
BanService service = Sponge.getServiceManager().provide(BanService.class).get();
2016-02-19 12:01:24 -05:00
return service.isBanned(player.getProfile());
2015-10-07 17:33:33 +11:00
}
}