245 lines
8.6 KiB
Java
Raw Normal View History

2015-07-31 03:24:01 +10:00
package com.plotsquared.sponge.object;
import com.flowpowered.math.vector.Vector3d;
2016-07-25 18:43:11 -04:00
import com.intellectualcrafters.plot.PS;
import com.intellectualcrafters.plot.commands.RequiredType;
import com.intellectualcrafters.plot.object.Location;
import com.intellectualcrafters.plot.object.PlotPlayer;
2016-03-22 21:41:37 -04:00
import com.intellectualcrafters.plot.util.PlotGameMode;
import com.intellectualcrafters.plot.util.PlotWeather;
2016-06-16 10:31:25 +10:00
import com.intellectualcrafters.plot.util.StringMan;
import com.intellectualcrafters.plot.util.UUIDHandler;
import com.plotsquared.sponge.util.SpongeUtil;
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;
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.chat.ChatTypes;
import org.spongepowered.api.text.serializer.TextSerializers;
2016-04-02 01:30:26 -04:00
import org.spongepowered.api.world.World;
2016-06-04 17:19:37 -04:00
import java.util.Optional;
2016-03-28 13:28:21 -04:00
import java.util.UUID;
2015-09-13 14:04:31 +10:00
public class SpongePlayer extends PlotPlayer {
public final Player player;
private UUID uuid;
private String name;
2016-03-22 21:41:37 -04:00
public SpongePlayer(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;
}
2016-06-04 17:19:37 -04:00
@Override
2015-09-13 14:04:31 +10:00
public Location getLocation() {
2016-03-22 21:41:37 -04:00
Location location = super.getLocation();
if (location == null) {
return SpongeUtil.getLocation(this.player);
} else {
return location;
}
}
2015-09-13 14:04:31 +10:00
@Override
2015-09-13 14:04:31 +10:00
public Location getLocationFull() {
2016-03-22 21:41:37 -04:00
return SpongeUtil.getLocationFull(this.player);
}
2015-09-13 14:04:31 +10:00
@Override
2015-09-13 14:04:31 +10:00
public UUID getUUID() {
2016-03-22 21:41:37 -04:00
if (this.uuid == null) {
this.uuid = UUIDHandler.getUUID(this);
}
2016-03-22 21:41:37 -04:00
return this.uuid;
}
2016-06-04 17:19:37 -04:00
@Override public long getLastPlayed() {
return this.player.lastPlayed().get().toEpochMilli();
}
@Override
2016-03-22 21:41:37 -04:00
public boolean hasPermission(String permission) {
return this.player.hasPermission(permission);
}
2015-09-13 14:04:31 +10:00
@Override
2016-03-22 21:41:37 -04:00
public void sendMessage(String message) {
if (!StringMan.isEqual(this.getMeta("lastMessage"), message) || (System.currentTimeMillis() - this.<Long>getMeta("lastMessageTime") > 5000)) {
2016-06-16 10:31:25 +10:00
setMeta("lastMessage", message);
2016-06-19 16:14:13 +10:00
setMeta("lastMessageTime", System.currentTimeMillis());
2016-06-16 10:31:25 +10:00
this.player.sendMessage(ChatTypes.CHAT, TextSerializers.LEGACY_FORMATTING_CODE.deserialize(message));
}
}
2015-09-13 14:04:31 +10:00
@Override
2016-03-22 21:41:37 -04:00
public void teleport(Location location) {
if ((Math.abs(location.getX()) >= 30000000) || (Math.abs(location.getZ()) >= 30000000)) {
2015-09-13 14:04:31 +10:00
return;
}
2016-03-22 21:41:37 -04:00
String world = this.player.getWorld().getName();
if (!world.equals(location.getWorld())) {
this.player.transferToWorld(location.getWorld(), new Vector3d(location.getX(), location.getY(), location.getZ()));
2015-09-13 14:04:31 +10:00
} else {
2016-04-02 01:30:26 -04:00
org.spongepowered.api.world.Location<World> current = this.player.getLocation();
2016-03-22 21:41:37 -04:00
current = current.setPosition(new Vector3d(location.getX(), location.getY(), location.getZ()));
this.player.setLocation(current);
}
}
2015-09-13 14:04:31 +10:00
@Override
2015-09-13 14:04:31 +10:00
public boolean isOnline() {
2016-03-22 21:41:37 -04:00
return this.player.isOnline();
}
2015-09-13 14:04:31 +10:00
@Override
2015-09-13 14:04:31 +10:00
public String getName() {
2016-03-22 21:41:37 -04:00
if (this.name == null) {
this.name = this.player.getName();
}
2016-03-22 21:41:37 -04:00
return this.name;
}
2015-09-13 14:04:31 +10:00
@Override
2016-03-22 21:41:37 -04:00
public void setCompassTarget(Location location) {
2016-07-25 18:43:11 -04:00
Optional<TargetedLocationData> target = this.player.getOrCreate(TargetedLocationData.class);
if (target.isPresent()) {
target.get().set(Keys.TARGETED_LOCATION, SpongeUtil.getLocation(location).getPosition());
} else {
PS.debug("Failed to set compass target.");
}
}
2016-03-22 21:41:37 -04:00
2015-07-31 00:25:16 +10:00
@Override
2016-03-22 21:41:37 -04:00
public void setWeather(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
2016-03-22 21:41:37 -04:00
public PlotGameMode getGameMode() {
GameMode gamemode = this.player.getGameModeData().type().get();
2015-09-13 14:04:31 +10:00
if (gamemode == GameModes.ADVENTURE) {
2016-03-22 21:41:37 -04:00
return PlotGameMode.ADVENTURE;
2016-03-28 17:31:40 -04:00
} else if (gamemode == GameModes.CREATIVE) {
2016-03-22 21:41:37 -04:00
return PlotGameMode.CREATIVE;
2016-03-28 17:31:40 -04:00
} else if (gamemode == GameModes.SPECTATOR) {
2016-03-22 21:41:37 -04:00
return PlotGameMode.SPECTATOR;
2016-03-28 17:31:40 -04:00
} else if (gamemode == GameModes.SURVIVAL) {
2016-03-22 21:41:37 -04:00
return PlotGameMode.SURVIVAL;
2016-03-28 17:31:40 -04:00
} else {
2016-03-28 13:28:21 -04:00
return PlotGameMode.NOT_SET;
}
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
2016-03-22 21:41:37 -04:00
public void setGameMode(PlotGameMode gameMode) {
switch (gameMode) {
2015-10-07 17:33:33 +11:00
case ADVENTURE:
2016-03-22 21:41:37 -04:00
this.player.offer(Keys.GAME_MODE, GameModes.ADVENTURE);
2015-10-07 17:33:33 +11:00
return;
case CREATIVE:
2016-03-22 21:41:37 -04:00
this.player.offer(Keys.GAME_MODE, GameModes.CREATIVE);
2015-10-07 17:33:33 +11:00
return;
case SPECTATOR:
2016-03-22 21:41:37 -04:00
this.player.offer(Keys.GAME_MODE, GameModes.SPECTATOR);
2015-10-07 17:33:33 +11:00
return;
case SURVIVAL:
2016-03-22 21:41:37 -04:00
this.player.offer(Keys.GAME_MODE, GameModes.SURVIVAL);
2015-10-07 17:33:33 +11:00
return;
2016-03-28 13:28:21 -04:00
case NOT_SET:
this.player.offer(Keys.GAME_MODE, GameModes.NOT_SET);
return;
default:
this.player.offer(Keys.GAME_MODE, GameModes.NOT_SET);
2015-10-07 17:33:33 +11:00
}
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
2016-03-22 21:41:37 -04:00
public void setTime(long time) {
2015-07-31 00:25:16 +10:00
throw new UnsupportedOperationException("NOT IMPLEMENTED YET");
}
2015-09-13 14:04:31 +10:00
2016-06-16 02:08:01 +02:00
@Override
public boolean getFlight() {
Optional<Boolean> flying = player.get(Keys.CAN_FLY);
return flying.isPresent() && flying.get();
}
@Override
public void setFlight(boolean fly) {
this.player.offer(Keys.IS_FLYING, fly);
this.player.offer(Keys.CAN_FLY, fly);
}
2015-07-31 00:25:16 +10:00
@Override
2016-03-22 21:41:37 -04:00
public void playMusic(Location location, int id) {
2015-10-07 17:33:33 +11:00
switch (id) {
case 0:
2016-03-22 21:41:37 -04:00
this.player.playSound(null, SpongeUtil.getLocation(location).getPosition(), 1);
2015-10-07 17:33:33 +11:00
break;
case 2256:
this.player.playSound(SoundTypes.RECORD_11, SpongeUtil.getLocation(location).getPosition(), 1);
2015-10-07 17:33:33 +11:00
break;
case 2257:
this.player.playSound(SoundTypes.RECORD_13, SpongeUtil.getLocation(location).getPosition(), 1);
2015-10-07 17:33:33 +11:00
break;
case 2258:
this.player.playSound(SoundTypes.RECORD_BLOCKS, SpongeUtil.getLocation(location).getPosition(), 1);
2015-10-07 17:33:33 +11:00
break;
case 2259:
this.player.playSound(SoundTypes.RECORD_CAT, SpongeUtil.getLocation(location).getPosition(), 1);
2015-10-07 17:33:33 +11:00
break;
case 2260:
this.player.playSound(SoundTypes.RECORD_CHIRP, SpongeUtil.getLocation(location).getPosition(), 1);
2015-10-07 17:33:33 +11:00
break;
case 2261:
this.player.playSound(SoundTypes.RECORD_FAR, SpongeUtil.getLocation(location).getPosition(), 1);
2015-10-07 17:33:33 +11:00
break;
case 2262:
this.player.playSound(SoundTypes.RECORD_MALL, SpongeUtil.getLocation(location).getPosition(), 1);
2015-10-07 17:33:33 +11:00
break;
case 2263:
this.player.playSound(SoundTypes.RECORD_MELLOHI, SpongeUtil.getLocation(location).getPosition(), 1);
2015-10-07 17:33:33 +11:00
break;
case 2264:
this.player.playSound(SoundTypes.RECORD_STAL, SpongeUtil.getLocation(location).getPosition(), 1);
2015-10-07 17:33:33 +11:00
break;
case 2265:
this.player.playSound(SoundTypes.RECORD_STRAD, SpongeUtil.getLocation(location).getPosition(), 1);
2015-10-07 17:33:33 +11:00
break;
case 2266:
this.player.playSound(SoundTypes.RECORD_WAIT, SpongeUtil.getLocation(location).getPosition(), 1);
2015-10-07 17:33:33 +11:00
break;
case 2267:
this.player.playSound(SoundTypes.RECORD_WARD, SpongeUtil.getLocation(location).getPosition(), 1);
2015-10-07 17:33:33 +11:00
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
2016-03-22 21:41:37 -04:00
public void kick(String message) {
this.player.kick(SpongeUtil.getText(message));
2015-07-31 00:25:16 +10:00
}
@Override public void stopSpectating() {
//Not Implemented
}
2015-10-07 17:33:33 +11:00
@Override
public boolean isBanned() {
2016-06-04 17:19:37 -04:00
Optional<BanService> service = Sponge.getServiceManager().provide(BanService.class);
return service.isPresent() && service.get().isBanned(this.player.getProfile());
2015-10-07 17:33:33 +11:00
}
}