2011-03-30 06:03:21 +02:00
|
|
|
package com.earth2me.essentials.commands;
|
|
|
|
|
2020-06-24 10:52:25 +02:00
|
|
|
import com.earth2me.essentials.AsyncTeleport;
|
2021-12-04 15:40:06 +01:00
|
|
|
import com.earth2me.essentials.IUser;
|
2011-06-13 15:05:11 +02:00
|
|
|
import com.earth2me.essentials.Trade;
|
2011-03-30 06:03:21 +02:00
|
|
|
import com.earth2me.essentials.User;
|
2024-02-03 21:38:14 +01:00
|
|
|
import net.ess3.api.TranslatableException;
|
2021-12-04 15:40:06 +01:00
|
|
|
import net.essentialsx.api.v2.events.TeleportRequestResponseEvent;
|
|
|
|
import org.bukkit.Bukkit;
|
2014-01-12 02:23:12 +01:00
|
|
|
import org.bukkit.Location;
|
2011-11-18 18:42:26 +01:00
|
|
|
import org.bukkit.Server;
|
2011-12-07 02:12:36 +01:00
|
|
|
import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause;
|
2011-03-30 06:03:21 +02:00
|
|
|
|
2021-12-04 15:40:06 +01:00
|
|
|
import java.util.ArrayList;
|
|
|
|
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
|
|
|
public class Commandtpaccept extends EssentialsCommand {
|
|
|
|
public Commandtpaccept() {
|
|
|
|
super("tpaccept");
|
|
|
|
}
|
2011-03-30 06:03:21 +02: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 {
|
2021-12-04 15:40:06 +01:00
|
|
|
final boolean acceptAll;
|
|
|
|
if (args.length > 0) {
|
|
|
|
acceptAll = args[0].equals("*") || args[0].equalsIgnoreCase("all");
|
|
|
|
} else {
|
|
|
|
acceptAll = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!user.hasPendingTpaRequests(true, acceptAll)) {
|
2024-02-03 21:38:14 +01:00
|
|
|
throw new TranslatableException("noPendingRequest");
|
2015-04-15 06:06:16 +02:00
|
|
|
}
|
This is a big refactoring of the user class and more.
Many commands have been cleaned.
File changes:
- all user data has been moved from users.yml to userdata folder
- all files in userdata folder are lower case
Both changes should be done automatically.
Class changes:
- Moved all user data functions to UserData class
- Moved all user teleport functions to Teleport class
- Moved the user list to Essentials class
- Less static functions for better testing
- EssentialsCommand now has ess Property (Essentials class)
- New NotEnoughArgumentsException, that will show command description and syntax
New commands:
- /seen, shows the last login or logout
- /tempban, temporarily ban someone
- /tjail and mute, temporarily option added
Other changes:
- ban reason is saved
- don't show "You have xxx mail" on login, if user doesn't have essentials.mail permission
- time will be parsed: years, months (mo), weeks, days, hours, minutes (m), seconds, these can be shortened and combined, example: 2 days 5h 30m
git-svn-id: https://svn.java.net/svn/essentials~svn/trunk@1300 e251c2fe-e539-e718-e476-b85c1f46cddb
2011-05-01 23:07:30 +02:00
|
|
|
|
2021-12-04 15:40:06 +01:00
|
|
|
if (args.length > 0) {
|
|
|
|
if (acceptAll) {
|
|
|
|
acceptAllRequests(user, commandLabel);
|
|
|
|
throw new NoChargeException();
|
|
|
|
}
|
2024-02-03 21:38:14 +01:00
|
|
|
user.sendTl("requestAccepted");
|
2021-12-04 15:40:06 +01:00
|
|
|
handleTeleport(user, user.getOutstandingTpaRequest(getPlayer(server, user, args, 0).getName(), true), commandLabel);
|
|
|
|
} else {
|
2024-02-03 21:38:14 +01:00
|
|
|
user.sendTl("requestAccepted");
|
2021-12-04 15:40:06 +01:00
|
|
|
handleTeleport(user, user.getNextTpaRequest(true, false, false), commandLabel);
|
|
|
|
}
|
|
|
|
throw new NoChargeException();
|
|
|
|
}
|
|
|
|
|
|
|
|
private void acceptAllRequests(final User user, final String commandLabel) throws Exception {
|
|
|
|
IUser.TpaRequest request;
|
|
|
|
int count = 0;
|
|
|
|
while ((request = user.getNextTpaRequest(true, true, true)) != null) {
|
|
|
|
try {
|
|
|
|
handleTeleport(user, request, commandLabel);
|
|
|
|
count++;
|
|
|
|
} catch (Exception e) {
|
|
|
|
ess.showError(user.getSource(), e, commandLabel);
|
|
|
|
} finally {
|
|
|
|
user.removeTpaRequest(request.getName());
|
|
|
|
}
|
|
|
|
}
|
2024-02-03 21:38:14 +01:00
|
|
|
user.sendTl("requestAcceptedAll", count);
|
2021-12-04 15:40:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected List<String> getTabCompleteOptions(Server server, User user, String commandLabel, String[] args) {
|
|
|
|
if (args.length == 1) {
|
|
|
|
final List<String> options = new ArrayList<>(user.getPendingTpaKeys());
|
|
|
|
options.add("*");
|
|
|
|
return options;
|
|
|
|
} else {
|
|
|
|
return Collections.emptyList();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void handleTeleport(final User user, final IUser.TpaRequest request, String commandLabel) throws Exception {
|
|
|
|
if (request == null) {
|
2024-02-03 21:38:14 +01:00
|
|
|
throw new TranslatableException("noPendingRequest");
|
2015-04-15 06:06:16 +02:00
|
|
|
}
|
2021-12-04 15:40:06 +01:00
|
|
|
final User requester = ess.getUser(request.getRequesterUuid());
|
2012-03-20 15:32:11 +01:00
|
|
|
|
2021-12-04 15:40:06 +01:00
|
|
|
if (!requester.getBase().isOnline()) {
|
|
|
|
user.removeTpaRequest(request.getName());
|
2024-02-03 21:38:14 +01:00
|
|
|
throw new TranslatableException("noPendingRequest");
|
2015-04-15 06:06:16 +02:00
|
|
|
}
|
2012-03-20 15:32:11 +01:00
|
|
|
|
2021-12-04 15:40:06 +01:00
|
|
|
if (request.isHere() && ((!requester.isAuthorized("essentials.tpahere") && !requester.isAuthorized("essentials.tpaall")) || (user.getWorld() != requester.getWorld() && ess.getSettings().isWorldTeleportPermissions() && !user.isAuthorized("essentials.worlds." + user.getWorld().getName())))) {
|
2024-02-03 21:38:14 +01:00
|
|
|
throw new TranslatableException("noPendingRequest");
|
2015-04-15 06:06:16 +02:00
|
|
|
}
|
2012-03-20 15:32:11 +01:00
|
|
|
|
2021-12-04 15:40:06 +01:00
|
|
|
if (!request.isHere() && (!requester.isAuthorized("essentials.tpa") || (user.getWorld() != requester.getWorld() && ess.getSettings().isWorldTeleportPermissions() && !user.isAuthorized("essentials.worlds." + requester.getWorld().getName())))) {
|
2024-02-03 21:38:14 +01:00
|
|
|
throw new TranslatableException("noPendingRequest");
|
2015-04-15 06:06:16 +02:00
|
|
|
}
|
This is a big refactoring of the user class and more.
Many commands have been cleaned.
File changes:
- all user data has been moved from users.yml to userdata folder
- all files in userdata folder are lower case
Both changes should be done automatically.
Class changes:
- Moved all user data functions to UserData class
- Moved all user teleport functions to Teleport class
- Moved the user list to Essentials class
- Less static functions for better testing
- EssentialsCommand now has ess Property (Essentials class)
- New NotEnoughArgumentsException, that will show command description and syntax
New commands:
- /seen, shows the last login or logout
- /tempban, temporarily ban someone
- /tjail and mute, temporarily option added
Other changes:
- ban reason is saved
- don't show "You have xxx mail" on login, if user doesn't have essentials.mail permission
- time will be parsed: years, months (mo), weeks, days, hours, minutes (m), seconds, these can be shortened and combined, example: 2 days 5h 30m
git-svn-id: https://svn.java.net/svn/essentials~svn/trunk@1300 e251c2fe-e539-e718-e476-b85c1f46cddb
2011-05-01 23:07:30 +02:00
|
|
|
|
2021-12-04 15:40:06 +01:00
|
|
|
final TeleportRequestResponseEvent event = new TeleportRequestResponseEvent(user, requester, request, true);
|
|
|
|
Bukkit.getPluginManager().callEvent(event);
|
|
|
|
if (event.isCancelled()) {
|
|
|
|
if (ess.getSettings().isDebug()) {
|
2022-06-27 20:54:10 +02:00
|
|
|
ess.getLogger().info("TPA accept cancelled by API for " + user.getName() + " (requested by " + requester.getName() + ")");
|
2021-12-04 15:40:06 +01:00
|
|
|
}
|
|
|
|
return;
|
2015-04-15 06:06:16 +02:00
|
|
|
}
|
2011-12-09 00:43:09 +01:00
|
|
|
|
2015-04-15 06:06:16 +02:00
|
|
|
final Trade charge = new Trade(this.getName(), ess);
|
2024-02-10 05:01:20 +01:00
|
|
|
requester.sendTl("requestAcceptedFrom", user.getDisplayName());
|
2011-11-15 22:47:02 +01:00
|
|
|
|
2020-10-03 19:46:05 +02:00
|
|
|
final CompletableFuture<Boolean> future = getNewExceptionFuture(requester.getSource(), commandLabel);
|
2020-06-24 10:52:25 +02:00
|
|
|
future.exceptionally(e -> {
|
2024-02-03 21:38:14 +01:00
|
|
|
user.sendTl("pendingTeleportCancelled");
|
2020-06-24 10:52:25 +02:00
|
|
|
return false;
|
|
|
|
});
|
2021-12-04 15:40:06 +01:00
|
|
|
if (request.isHere()) {
|
|
|
|
final Location loc = request.getLocation();
|
2020-10-03 19:46:05 +02:00
|
|
|
final AsyncTeleport teleport = requester.getAsyncTeleport();
|
2020-06-24 10:52:25 +02:00
|
|
|
teleport.setTpType(AsyncTeleport.TeleportType.TPA);
|
|
|
|
future.thenAccept(success -> {
|
|
|
|
if (success) {
|
2024-02-03 21:38:14 +01:00
|
|
|
requester.sendTl("teleporting", loc.getWorld().getName(), loc.getBlockX(), loc.getBlockY(), loc.getBlockZ());
|
2020-06-24 10:52:25 +02:00
|
|
|
}
|
|
|
|
});
|
2021-12-04 15:40:06 +01:00
|
|
|
teleport.teleportPlayer(user, loc, charge, TeleportCause.COMMAND, future);
|
2020-06-24 10:52:25 +02:00
|
|
|
} else {
|
2020-10-03 19:46:05 +02:00
|
|
|
final AsyncTeleport teleport = requester.getAsyncTeleport();
|
2020-06-24 10:52:25 +02:00
|
|
|
teleport.setTpType(AsyncTeleport.TeleportType.TPA);
|
|
|
|
teleport.teleport(user.getBase(), charge, TeleportCause.COMMAND, future);
|
2015-04-15 06:06:16 +02:00
|
|
|
}
|
2021-12-04 15:40:06 +01:00
|
|
|
user.removeTpaRequest(request.getName());
|
2015-04-15 06:06:16 +02:00
|
|
|
}
|
2011-03-30 06:03:21 +02:00
|
|
|
}
|