TF-EssentialsX/Essentials/src/main/java/com/earth2me/essentials/commands/Commandwarp.java

137 lines
5.7 KiB
Java
Raw Normal View History

package com.earth2me.essentials.commands;
import com.earth2me.essentials.CommandSource;
2011-06-13 13:05:11 +00:00
import com.earth2me.essentials.Trade;
import com.earth2me.essentials.User;
2013-07-14 12:00:03 +00:00
import com.earth2me.essentials.api.IWarps;
2013-06-08 21:31:19 +00:00
import com.earth2me.essentials.utils.NumberUtil;
2013-10-11 02:44:41 +00:00
import com.earth2me.essentials.utils.StringUtil;
2015-04-15 04:06:16 +00:00
import net.ess3.api.IUser;
import org.bukkit.Server;
import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause;
2013-05-05 03:13:17 +00:00
import java.math.BigDecimal;
2011-08-21 19:27:36 +00:00
import java.util.ArrayList;
import java.util.Collections;
2011-08-21 19:27:36 +00:00
import java.util.List;
import java.util.Locale;
import java.util.stream.Collectors;
2015-04-15 04:06:16 +00:00
import static com.earth2me.essentials.I18n.tl;
public class Commandwarp extends EssentialsCommand {
private static final int WARPS_PER_PAGE = 20;
public Commandwarp() {
super("warp");
}
@Override
public void run(final Server server, final User user, final String commandLabel, final String[] args) throws Exception {
if (args.length == 0 || args[0].matches("[0-9]+")) {
if (!user.isAuthorized("essentials.warp.list")) {
throw new Exception(tl("warpListPermission"));
}
warpList(user.getSource(), args, user);
throw new NoChargeException();
}
if (args.length > 0) {
//TODO: Remove 'otherplayers' permission.
User otherUser = null;
if (args.length == 2 && (user.isAuthorized("essentials.warp.otherplayers") || user.isAuthorized("essentials.warp.others"))) {
otherUser = getPlayer(server, user, args, 1);
Reduce sync loads for teleporting (#3102) This PR reduces the number of sync loads occurring on any teleport caused by essentials. Fixes #2861 Fixes #2287 Fixes #3274 Fixes #3201 Fixes #2120 Before this PR, essentials would get a block multiple times causing sync loads to check if it was safe to teleport to. Now, the target block's chunk if fetched async with PaperLib and passed along to `LocationUtil#isBlockUnsafeForUser` (which internally calls other LocationUtil methods what that chunk object) resulting in the chunk only loading once, off the main thread. The only operations remaining on the main thread is `LocationUtil#getSafeDestination`. This is due to the method's recursion which would be a pain to move async. **However:** since the chunk was already loaded async, `LocationUtil#getSafeDestination` most of the time won't cause sync chunk loads. The only time it would cause sync chunk loads is with an unsafe location near a chunk border. ----------------------------------------- * Reduce sync teleporting loads * Avoid argument re-assigning * Remove async teleports when unnecessary * Make exceptions cleaner * Async all the things Made an async version of every method with fallbacks for deprecated methods. * Remove old now fallback method * Migrate everything to the new async teleport API * Update ITeleport javadocs * Fix invoking via async context * Fix /jail using deprecated method * Fix jail join handler using deprecated method * Rename all teleport classes to indicate async * Remove deprecated methods * Add (and deprecate) old teleport api * Revert TimedTeleport.java * Reduce Diff * Add legacy sendToJail method * Reduce Diff Further * Use getNewExceptionFuture in Commandtpo * Use getNewExceptionFuture everywhere * Fix even more usages * Revert LocationUtil.java * Fix issue causing unsafe locations to not work properly * Add deprecated notice in IUser implementation * Use CompletableFuture#completeExceptionally for exceptions * Use Essentials' logger in EssentialsCommand#showError * Return implementation rather than interface * Avoid possible deadlocks with entity ejections * Nuke some sync loads with homes Took 7 hours and 2 PRs to paper but it's here! * Fix ABI and make the codestyle worse * Make the codestyle worse because muh diff * Further ruin the codestyle * Fix error messages not showing in TimedTeleports * Improve messages around beds for /home * Fix #3274 Allow unsafe locations for different worlds + spectator mode * Fix fly safety operators
2020-06-24 08:52:25 +00:00
warpUser(user, otherUser, args[0], commandLabel);
throw new NoChargeException();
2015-04-15 04:06:16 +00:00
}
Reduce sync loads for teleporting (#3102) This PR reduces the number of sync loads occurring on any teleport caused by essentials. Fixes #2861 Fixes #2287 Fixes #3274 Fixes #3201 Fixes #2120 Before this PR, essentials would get a block multiple times causing sync loads to check if it was safe to teleport to. Now, the target block's chunk if fetched async with PaperLib and passed along to `LocationUtil#isBlockUnsafeForUser` (which internally calls other LocationUtil methods what that chunk object) resulting in the chunk only loading once, off the main thread. The only operations remaining on the main thread is `LocationUtil#getSafeDestination`. This is due to the method's recursion which would be a pain to move async. **However:** since the chunk was already loaded async, `LocationUtil#getSafeDestination` most of the time won't cause sync chunk loads. The only time it would cause sync chunk loads is with an unsafe location near a chunk border. ----------------------------------------- * Reduce sync teleporting loads * Avoid argument re-assigning * Remove async teleports when unnecessary * Make exceptions cleaner * Async all the things Made an async version of every method with fallbacks for deprecated methods. * Remove old now fallback method * Migrate everything to the new async teleport API * Update ITeleport javadocs * Fix invoking via async context * Fix /jail using deprecated method * Fix jail join handler using deprecated method * Rename all teleport classes to indicate async * Remove deprecated methods * Add (and deprecate) old teleport api * Revert TimedTeleport.java * Reduce Diff * Add legacy sendToJail method * Reduce Diff Further * Use getNewExceptionFuture in Commandtpo * Use getNewExceptionFuture everywhere * Fix even more usages * Revert LocationUtil.java * Fix issue causing unsafe locations to not work properly * Add deprecated notice in IUser implementation * Use CompletableFuture#completeExceptionally for exceptions * Use Essentials' logger in EssentialsCommand#showError * Return implementation rather than interface * Avoid possible deadlocks with entity ejections * Nuke some sync loads with homes Took 7 hours and 2 PRs to paper but it's here! * Fix ABI and make the codestyle worse * Make the codestyle worse because muh diff * Further ruin the codestyle * Fix error messages not showing in TimedTeleports * Improve messages around beds for /home * Fix #3274 Allow unsafe locations for different worlds + spectator mode * Fix fly safety operators
2020-06-24 08:52:25 +00:00
warpUser(user, user, args[0], commandLabel);
throw new NoChargeException();
2015-04-15 04:06:16 +00:00
}
}
@Override
public void run(final Server server, final CommandSource sender, final String commandLabel, final String[] args) throws Exception {
if (args.length < 2 || NumberUtil.isInt(args[0])) {
warpList(sender, args, null);
throw new NoChargeException();
}
2020-10-03 17:46:05 +00:00
final User otherUser = getPlayer(server, args, 1, true, false);
Reduce sync loads for teleporting (#3102) This PR reduces the number of sync loads occurring on any teleport caused by essentials. Fixes #2861 Fixes #2287 Fixes #3274 Fixes #3201 Fixes #2120 Before this PR, essentials would get a block multiple times causing sync loads to check if it was safe to teleport to. Now, the target block's chunk if fetched async with PaperLib and passed along to `LocationUtil#isBlockUnsafeForUser` (which internally calls other LocationUtil methods what that chunk object) resulting in the chunk only loading once, off the main thread. The only operations remaining on the main thread is `LocationUtil#getSafeDestination`. This is due to the method's recursion which would be a pain to move async. **However:** since the chunk was already loaded async, `LocationUtil#getSafeDestination` most of the time won't cause sync chunk loads. The only time it would cause sync chunk loads is with an unsafe location near a chunk border. ----------------------------------------- * Reduce sync teleporting loads * Avoid argument re-assigning * Remove async teleports when unnecessary * Make exceptions cleaner * Async all the things Made an async version of every method with fallbacks for deprecated methods. * Remove old now fallback method * Migrate everything to the new async teleport API * Update ITeleport javadocs * Fix invoking via async context * Fix /jail using deprecated method * Fix jail join handler using deprecated method * Rename all teleport classes to indicate async * Remove deprecated methods * Add (and deprecate) old teleport api * Revert TimedTeleport.java * Reduce Diff * Add legacy sendToJail method * Reduce Diff Further * Use getNewExceptionFuture in Commandtpo * Use getNewExceptionFuture everywhere * Fix even more usages * Revert LocationUtil.java * Fix issue causing unsafe locations to not work properly * Add deprecated notice in IUser implementation * Use CompletableFuture#completeExceptionally for exceptions * Use Essentials' logger in EssentialsCommand#showError * Return implementation rather than interface * Avoid possible deadlocks with entity ejections * Nuke some sync loads with homes Took 7 hours and 2 PRs to paper but it's here! * Fix ABI and make the codestyle worse * Make the codestyle worse because muh diff * Further ruin the codestyle * Fix error messages not showing in TimedTeleports * Improve messages around beds for /home * Fix #3274 Allow unsafe locations for different worlds + spectator mode * Fix fly safety operators
2020-06-24 08:52:25 +00:00
otherUser.getAsyncTeleport().warp(otherUser, args[0], null, TeleportCause.COMMAND, getNewExceptionFuture(sender, commandLabel));
throw new NoChargeException();
2015-04-15 04:06:16 +00:00
}
//TODO: Use one of the new text classes, like /help ?
private void warpList(final CommandSource sender, final String[] args, final IUser user) throws Exception {
final IWarps warps = ess.getWarps();
final List<String> warpNameList = getAvailableWarpsFor(user);
2015-04-15 04:06:16 +00:00
if (warpNameList.isEmpty()) {
throw new Exception(tl("noWarpsDefined"));
}
int page = 1;
if (args.length > 0 && NumberUtil.isInt(args[0])) {
page = Integer.parseInt(args[0]);
}
final int maxPages = (int) Math.ceil(warpNameList.size() / (double) WARPS_PER_PAGE);
if (page > maxPages) {
page = maxPages;
}
final int warpPage = (page - 1) * WARPS_PER_PAGE;
final String warpList = StringUtil.joinList(warpNameList.subList(warpPage, warpPage + Math.min(warpNameList.size() - warpPage, WARPS_PER_PAGE)));
if (warpNameList.size() > WARPS_PER_PAGE) {
sender.sendMessage(tl("warpsCount", warpNameList.size(), page, maxPages));
sender.sendMessage(tl("warpList", warpList));
} else {
sender.sendMessage(tl("warps", warpList));
}
}
Reduce sync loads for teleporting (#3102) This PR reduces the number of sync loads occurring on any teleport caused by essentials. Fixes #2861 Fixes #2287 Fixes #3274 Fixes #3201 Fixes #2120 Before this PR, essentials would get a block multiple times causing sync loads to check if it was safe to teleport to. Now, the target block's chunk if fetched async with PaperLib and passed along to `LocationUtil#isBlockUnsafeForUser` (which internally calls other LocationUtil methods what that chunk object) resulting in the chunk only loading once, off the main thread. The only operations remaining on the main thread is `LocationUtil#getSafeDestination`. This is due to the method's recursion which would be a pain to move async. **However:** since the chunk was already loaded async, `LocationUtil#getSafeDestination` most of the time won't cause sync chunk loads. The only time it would cause sync chunk loads is with an unsafe location near a chunk border. ----------------------------------------- * Reduce sync teleporting loads * Avoid argument re-assigning * Remove async teleports when unnecessary * Make exceptions cleaner * Async all the things Made an async version of every method with fallbacks for deprecated methods. * Remove old now fallback method * Migrate everything to the new async teleport API * Update ITeleport javadocs * Fix invoking via async context * Fix /jail using deprecated method * Fix jail join handler using deprecated method * Rename all teleport classes to indicate async * Remove deprecated methods * Add (and deprecate) old teleport api * Revert TimedTeleport.java * Reduce Diff * Add legacy sendToJail method * Reduce Diff Further * Use getNewExceptionFuture in Commandtpo * Use getNewExceptionFuture everywhere * Fix even more usages * Revert LocationUtil.java * Fix issue causing unsafe locations to not work properly * Add deprecated notice in IUser implementation * Use CompletableFuture#completeExceptionally for exceptions * Use Essentials' logger in EssentialsCommand#showError * Return implementation rather than interface * Avoid possible deadlocks with entity ejections * Nuke some sync loads with homes Took 7 hours and 2 PRs to paper but it's here! * Fix ABI and make the codestyle worse * Make the codestyle worse because muh diff * Further ruin the codestyle * Fix error messages not showing in TimedTeleports * Improve messages around beds for /home * Fix #3274 Allow unsafe locations for different worlds + spectator mode * Fix fly safety operators
2020-06-24 08:52:25 +00:00
private void warpUser(final User owner, final User user, final String name, final String commandLabel) throws Exception {
2015-04-15 04:06:16 +00:00
final Trade chargeWarp = new Trade("warp-" + name.toLowerCase(Locale.ENGLISH).replace('_', '-'), ess);
final Trade chargeCmd = new Trade(this.getName(), ess);
final BigDecimal fullCharge = chargeWarp.getCommandCost(user).add(chargeCmd.getCommandCost(user));
final Trade charge = new Trade(fullCharge, ess);
charge.isAffordableFor(owner);
if (ess.getSettings().getPerWarpPermission() && !owner.isAuthorized("essentials.warps." + name)) {
throw new Exception(tl("warpUsePermission"));
}
Reduce sync loads for teleporting (#3102) This PR reduces the number of sync loads occurring on any teleport caused by essentials. Fixes #2861 Fixes #2287 Fixes #3274 Fixes #3201 Fixes #2120 Before this PR, essentials would get a block multiple times causing sync loads to check if it was safe to teleport to. Now, the target block's chunk if fetched async with PaperLib and passed along to `LocationUtil#isBlockUnsafeForUser` (which internally calls other LocationUtil methods what that chunk object) resulting in the chunk only loading once, off the main thread. The only operations remaining on the main thread is `LocationUtil#getSafeDestination`. This is due to the method's recursion which would be a pain to move async. **However:** since the chunk was already loaded async, `LocationUtil#getSafeDestination` most of the time won't cause sync chunk loads. The only time it would cause sync chunk loads is with an unsafe location near a chunk border. ----------------------------------------- * Reduce sync teleporting loads * Avoid argument re-assigning * Remove async teleports when unnecessary * Make exceptions cleaner * Async all the things Made an async version of every method with fallbacks for deprecated methods. * Remove old now fallback method * Migrate everything to the new async teleport API * Update ITeleport javadocs * Fix invoking via async context * Fix /jail using deprecated method * Fix jail join handler using deprecated method * Rename all teleport classes to indicate async * Remove deprecated methods * Add (and deprecate) old teleport api * Revert TimedTeleport.java * Reduce Diff * Add legacy sendToJail method * Reduce Diff Further * Use getNewExceptionFuture in Commandtpo * Use getNewExceptionFuture everywhere * Fix even more usages * Revert LocationUtil.java * Fix issue causing unsafe locations to not work properly * Add deprecated notice in IUser implementation * Use CompletableFuture#completeExceptionally for exceptions * Use Essentials' logger in EssentialsCommand#showError * Return implementation rather than interface * Avoid possible deadlocks with entity ejections * Nuke some sync loads with homes Took 7 hours and 2 PRs to paper but it's here! * Fix ABI and make the codestyle worse * Make the codestyle worse because muh diff * Further ruin the codestyle * Fix error messages not showing in TimedTeleports * Improve messages around beds for /home * Fix #3274 Allow unsafe locations for different worlds + spectator mode * Fix fly safety operators
2020-06-24 08:52:25 +00:00
owner.getAsyncTeleport().warp(user, name, charge, TeleportCause.COMMAND, getNewExceptionFuture(user.getSource(), commandLabel));
2015-04-15 04:06:16 +00:00
}
private List<String> getAvailableWarpsFor(final IUser user) {
if (ess.getSettings().getPerWarpPermission() && user != null) {
return ess.getWarps().getList().stream()
.filter(warpName -> user.isAuthorized("essentials.warps." + warpName))
.collect(Collectors.toList());
}
return new ArrayList<>(ess.getWarps().getList());
}
@Override
protected List<String> getTabCompleteOptions(final Server server, final User user, final String commandLabel, final String[] args) {
if (args.length == 1 && user.isAuthorized("essentials.warp.list")) {
return getAvailableWarpsFor(user);
} else if (args.length == 2 && (user.isAuthorized("essentials.warp.otherplayers") || user.isAuthorized("essentials.warp.others"))) {
//TODO: Remove 'otherplayers' permission.
return getPlayers(server, user);
} else {
return Collections.emptyList();
}
}
@Override
protected List<String> getTabCompleteOptions(final Server server, final CommandSource sender, final String commandLabel, final String[] args) {
if (args.length == 1) {
return new ArrayList<>(ess.getWarps().getList());
} else if (args.length == 2) {
return getPlayers(server, sender);
} else {
return Collections.emptyList();
}
}
}