2011-03-19 23:39:51 +01:00
|
|
|
package com.earth2me.essentials.spawn;
|
|
|
|
|
2013-10-16 21:59:39 +02:00
|
|
|
import com.earth2me.essentials.CommandSource;
|
2013-03-19 21:24:50 +01:00
|
|
|
import com.earth2me.essentials.Console;
|
2011-06-13 15:05:11 +02:00
|
|
|
import com.earth2me.essentials.Trade;
|
2011-03-19 23:39:51 +01:00
|
|
|
import com.earth2me.essentials.User;
|
|
|
|
import com.earth2me.essentials.commands.EssentialsCommand;
|
2020-08-16 23:40:58 +02:00
|
|
|
import com.earth2me.essentials.commands.NoChargeException;
|
2011-12-04 02:21:05 +01:00
|
|
|
import com.earth2me.essentials.commands.NotEnoughArgumentsException;
|
2021-07-07 20:52:49 +02:00
|
|
|
import net.essentialsx.api.v2.events.UserTeleportSpawnEvent;
|
2011-12-08 03:21:10 +01:00
|
|
|
import org.bukkit.Location;
|
2011-11-21 02:55:26 +01:00
|
|
|
import org.bukkit.Server;
|
2011-12-07 02:12:36 +01:00
|
|
|
import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause;
|
2011-03-19 23:39:51 +01:00
|
|
|
|
2020-08-11 20:09:22 +02:00
|
|
|
import java.util.Collections;
|
|
|
|
import java.util.List;
|
2020-06-24 10:52:25 +02:00
|
|
|
import java.util.concurrent.CompletableFuture;
|
|
|
|
|
2015-04-15 06:06:16 +02:00
|
|
|
import static com.earth2me.essentials.I18n.tl;
|
|
|
|
|
|
|
|
public class Commandspawn extends EssentialsCommand {
|
|
|
|
public Commandspawn() {
|
|
|
|
super("spawn");
|
|
|
|
}
|
2011-03-19 23:39:51 +01:00
|
|
|
|
2015-04-15 06:06:16 +02:00
|
|
|
@Override
|
|
|
|
public void run(final Server server, final User user, final String commandLabel, final String[] args) throws Exception {
|
|
|
|
final Trade charge = new Trade(this.getName(), ess);
|
|
|
|
charge.isAffordableFor(user);
|
|
|
|
if (args.length > 0 && user.isAuthorized("essentials.spawn.others")) {
|
|
|
|
final User otherUser = getPlayer(server, user, args, 0);
|
2020-10-03 19:46:05 +02:00
|
|
|
final CompletableFuture<Boolean> future = new CompletableFuture<>();
|
2020-06-24 10:52:25 +02:00
|
|
|
future.thenAccept(success -> {
|
|
|
|
if (success) {
|
|
|
|
if (!otherUser.equals(user)) {
|
|
|
|
otherUser.sendMessage(tl("teleportAtoB", user.getDisplayName(), "spawn"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
respawn(user.getSource(), user, otherUser, charge, commandLabel, future);
|
2015-04-15 06:06:16 +02:00
|
|
|
} else {
|
2021-07-09 17:55:52 +02:00
|
|
|
respawn(user.getSource(), user, user, charge, commandLabel, new CompletableFuture<>());
|
2015-04-15 06:06:16 +02:00
|
|
|
}
|
2020-10-03 19:46:05 +02:00
|
|
|
|
2020-08-16 23:40:58 +02:00
|
|
|
throw new NoChargeException();
|
2015-04-15 06:06:16 +02:00
|
|
|
}
|
2011-12-04 02:21:05 +01:00
|
|
|
|
2015-04-15 06:06:16 +02:00
|
|
|
@Override
|
|
|
|
protected void run(final Server server, final CommandSource sender, final String commandLabel, final String[] args) throws Exception {
|
2020-08-11 20:09:22 +02:00
|
|
|
if (args.length == 0) {
|
2015-04-15 06:06:16 +02:00
|
|
|
throw new NotEnoughArgumentsException();
|
|
|
|
}
|
|
|
|
final User user = getPlayer(server, args, 0, true, false);
|
2020-10-03 19:46:05 +02:00
|
|
|
final CompletableFuture<Boolean> future = new CompletableFuture<>();
|
2020-06-24 10:52:25 +02:00
|
|
|
respawn(sender, null, user, null, commandLabel, future);
|
|
|
|
future.thenAccept(success -> {
|
|
|
|
if (success) {
|
2020-12-11 17:29:48 +01:00
|
|
|
user.sendMessage(tl("teleportAtoB", Console.DISPLAY_NAME, "spawn"));
|
2020-06-24 10:52:25 +02:00
|
|
|
}
|
|
|
|
});
|
2015-04-15 06:06:16 +02:00
|
|
|
}
|
2013-06-08 20:34:14 +02:00
|
|
|
|
2020-08-11 20:09:22 +02:00
|
|
|
@Override
|
2020-10-03 19:46:05 +02:00
|
|
|
protected List<String> getTabCompleteOptions(final Server server, final CommandSource sender, final String commandLabel, final String[] args) {
|
2020-08-11 20:09:22 +02:00
|
|
|
if (args.length == 1 && sender.isAuthorized("essentials.spawn.others", ess)) {
|
|
|
|
return getPlayers(server, sender);
|
|
|
|
}
|
|
|
|
return Collections.emptyList();
|
|
|
|
}
|
|
|
|
|
2020-10-03 19:46:05 +02:00
|
|
|
private void respawn(final CommandSource sender, final User teleportOwner, final User teleportee, final Trade charge, final String commandLabel, final CompletableFuture<Boolean> future) throws Exception {
|
2020-08-11 20:09:22 +02:00
|
|
|
final Location spawn = ((SpawnStorage) this.module).getSpawn(teleportee.getGroup());
|
2021-06-08 00:23:13 +02:00
|
|
|
if (spawn == null) {
|
|
|
|
return;
|
|
|
|
}
|
2015-04-15 06:06:16 +02:00
|
|
|
sender.sendMessage(tl("teleporting", spawn.getWorld().getName(), spawn.getBlockX(), spawn.getBlockY(), spawn.getBlockZ()));
|
2020-06-24 10:52:25 +02:00
|
|
|
future.exceptionally(e -> {
|
|
|
|
showError(sender.getSender(), e, commandLabel);
|
|
|
|
return false;
|
|
|
|
});
|
2021-07-07 20:52:49 +02:00
|
|
|
final UserTeleportSpawnEvent spawnEvent = new UserTeleportSpawnEvent(teleportee, teleportOwner, teleportee.getGroup(), spawn);
|
|
|
|
ess.getServer().getPluginManager().callEvent(spawnEvent);
|
|
|
|
if (spawnEvent.isCancelled()) {
|
|
|
|
return;
|
|
|
|
}
|
2015-04-15 06:06:16 +02:00
|
|
|
if (teleportOwner == null) {
|
2020-06-24 10:52:25 +02:00
|
|
|
teleportee.getAsyncTeleport().now(spawn, false, TeleportCause.COMMAND, future);
|
2020-08-11 20:09:22 +02:00
|
|
|
return;
|
2015-04-15 06:06:16 +02:00
|
|
|
}
|
2020-08-11 20:09:22 +02:00
|
|
|
teleportOwner.getAsyncTeleport().teleportPlayer(teleportee, spawn, charge, TeleportCause.COMMAND, future);
|
2015-04-15 06:06:16 +02:00
|
|
|
}
|
2011-03-19 23:39:51 +01:00
|
|
|
}
|