Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
0e1cb48
Reduce sync teleporting loads
JRoy Mar 28, 2020
6183009
Avoid argument re-assigning
JRoy Mar 31, 2020
af869f6
Remove async teleports when unnecessary
JRoy Mar 31, 2020
709bb83
Make exceptions cleaner
JRoy Mar 31, 2020
a5bc910
Merge branch '2.x' into feature/better-async
JRoy Apr 3, 2020
7b58655
Async all the things
JRoy Apr 4, 2020
bd49c56
Remove old now fallback method
JRoy Apr 4, 2020
80250ff
Migrate everything to the new async teleport API
JRoy Apr 4, 2020
c299234
Update ITeleport javadocs
JRoy Apr 4, 2020
1fd2d57
Merge branch '2.x' into feature/better-async
JRoy Apr 4, 2020
24cd053
Fix invoking via async context
JRoy Apr 4, 2020
f1e3bf7
Fix /jail using deprecated method
JRoy Apr 6, 2020
ca9ebc1
Fix jail join handler using deprecated method
JRoy Apr 6, 2020
6f4d957
Merge branch '2.x' into feature/better-async
JRoy Apr 6, 2020
30ce336
Rename all teleport classes to indicate async
JRoy Apr 10, 2020
b2f6ce9
Remove deprecated methods
JRoy Apr 10, 2020
8e87db8
Add (and deprecate) old teleport api
JRoy Apr 10, 2020
07a3465
Revert TimedTeleport.java
JRoy Apr 10, 2020
5aa5878
Reduce Diff
JRoy Apr 10, 2020
9302e41
Add legacy sendToJail method
JRoy Apr 10, 2020
fd0ce61
Reduce Diff Further
JRoy Apr 10, 2020
332545e
Use getNewExceptionFuture in Commandtpo
JRoy Apr 13, 2020
d4df801
Use getNewExceptionFuture everywhere
JRoy Apr 13, 2020
944f112
Fix even more usages
JRoy Apr 13, 2020
015eeed
Revert LocationUtil.java
JRoy Apr 20, 2020
1013dd8
Fix issue causing unsafe locations to not work properly
JRoy Apr 20, 2020
c9d35b4
Add deprecated notice in IUser implementation
JRoy Apr 20, 2020
606b204
Use CompletableFuture#completeExceptionally for exceptions
JRoy Apr 20, 2020
e95ce2a
Use Essentials' logger in EssentialsCommand#showError
JRoy Apr 20, 2020
5a194fb
Return implementation rather than interface
JRoy Apr 21, 2020
806d607
Merge branch '2.x' into feature/better-async
JRoy Apr 24, 2020
613665a
Merge branch '2.x' into feature/better-async
JRoy Apr 27, 2020
80a7dcb
Avoid possible deadlocks with entity ejections
JRoy May 2, 2020
b92ea83
Merge remote-tracking branch 'origin/feature/better-async' into featu…
JRoy May 2, 2020
e72746e
Nuke some sync loads with homes
JRoy May 11, 2020
318ef7c
Fix ABI and make the codestyle worse
JRoy May 11, 2020
85a4942
Make the codestyle worse because muh diff
JRoy May 11, 2020
c3ee63c
Further ruin the codestyle
JRoy May 11, 2020
5c9f775
Merge branch '2.x' into feature/better-async
JRoy May 22, 2020
dec46ac
Fix error messages not showing in TimedTeleports
JRoy May 25, 2020
e22a03c
Improve messages around beds for /home
JRoy Jun 11, 2020
9efa114
Fix #3274
JRoy Jun 11, 2020
7ad5bb6
Fix fly safety operators
JRoy Jun 16, 2020
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Essentials/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
<dependency>
<groupId>io.papermc</groupId>
<artifactId>paperlib</artifactId>
<version>1.0.2</version>
<version>1.0.3</version>
<scope>compile</scope>
</dependency>
<dependency>
Expand Down
428 changes: 428 additions & 0 deletions Essentials/src/com/earth2me/essentials/AsyncTeleport.java

Large diffs are not rendered by default.

151 changes: 151 additions & 0 deletions Essentials/src/com/earth2me/essentials/AsyncTimedTeleport.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
package com.earth2me.essentials;

import net.ess3.api.IEssentials;
import net.ess3.api.IUser;
import org.bukkit.Location;
import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause;

import java.util.UUID;
import java.util.concurrent.CompletableFuture;

import static com.earth2me.essentials.I18n.tl;


public class AsyncTimedTeleport implements Runnable {
private static final double MOVE_CONSTANT = 0.3;
private final IUser teleportOwner;
private final IEssentials ess;
private final AsyncTeleport teleport;
private final UUID timer_teleportee;
private int timer_task;
private final long timer_started; // time this task was initiated
private final long timer_delay; // how long to delay the teleportPlayer
private double timer_health;
// note that I initially stored a clone of the location for reference, but...
// when comparing locations, I got incorrect mismatches (rounding errors, looked like)
// so, the X/Y/Z values are stored instead and rounded off
private final long timer_initX;
private final long timer_initY;
private final long timer_initZ;
private final ITarget timer_teleportTarget;
private final boolean timer_respawn;
private final boolean timer_canMove;
private final Trade timer_chargeFor;
private final TeleportCause timer_cause;

AsyncTimedTeleport(IUser user, IEssentials ess, AsyncTeleport teleport, long delay, IUser teleportUser, ITarget target, Trade chargeFor, TeleportCause cause, boolean respawn) {
this.teleportOwner = user;
this.ess = ess;
this.teleport = teleport;
this.timer_started = System.currentTimeMillis();
this.timer_delay = delay;
this.timer_health = teleportUser.getBase().getHealth();
this.timer_initX = Math.round(teleportUser.getBase().getLocation().getX() * MOVE_CONSTANT);
this.timer_initY = Math.round(teleportUser.getBase().getLocation().getY() * MOVE_CONSTANT);
this.timer_initZ = Math.round(teleportUser.getBase().getLocation().getZ() * MOVE_CONSTANT);
this.timer_teleportee = teleportUser.getBase().getUniqueId();
this.timer_teleportTarget = target;
this.timer_chargeFor = chargeFor;
this.timer_cause = cause;
this.timer_respawn = respawn;
this.timer_canMove = user.isAuthorized("essentials.teleport.timer.move");

timer_task = ess.runTaskTimerAsynchronously(this, 20, 20).getTaskId();
}

@Override
public void run() {

if (teleportOwner == null || !teleportOwner.getBase().isOnline() || teleportOwner.getBase().getLocation() == null) {
cancelTimer(false);
return;
}

final IUser teleportUser = ess.getUser(this.timer_teleportee);

if (teleportUser == null || !teleportUser.getBase().isOnline()) {
cancelTimer(false);
return;
}

final Location currLocation = teleportUser.getBase().getLocation();
if (currLocation == null) {
cancelTimer(false);
return;
}

if (!timer_canMove && (Math.round(currLocation.getX() * MOVE_CONSTANT) != timer_initX || Math.round(currLocation.getY() * MOVE_CONSTANT) != timer_initY || Math.round(currLocation.getZ() * MOVE_CONSTANT) != timer_initZ || teleportUser.getBase().getHealth() < timer_health)) {
// user moved, cancelTimer teleportPlayer
cancelTimer(true);
return;
}

class DelayedTeleportTask implements Runnable {
@Override
public void run() {

timer_health = teleportUser.getBase().getHealth(); // in case user healed, then later gets injured
final long now = System.currentTimeMillis();
if (now > timer_started + timer_delay) {
try {
teleport.cooldown(false);
} catch (Throwable ex) {
teleportOwner.sendMessage(tl("cooldownWithMessage", ex.getMessage()));
if (teleportOwner != teleportUser) {
teleportUser.sendMessage(tl("cooldownWithMessage", ex.getMessage()));
}
}
try {
cancelTimer(false);
teleportUser.sendMessage(tl("teleportationCommencing"));

CompletableFuture<Boolean> future = new CompletableFuture<>();
future.exceptionally(e -> {
ess.showError(teleportOwner.getSource(), e, "\\ teleport");
return false;
});
if (timer_chargeFor != null) {
timer_chargeFor.isAffordableFor(teleportOwner);
}
if (timer_respawn) {
teleport.respawnNow(teleportUser, timer_cause, future);
} else {
teleport.nowAsync(teleportUser, timer_teleportTarget, timer_cause, future);
}
future.thenAccept(success -> {
if (timer_chargeFor != null) {
try {
timer_chargeFor.charge(teleportOwner);
} catch (ChargeException ex) {
ess.showError(teleportOwner.getSource(), ex, "\\ teleport");
}
}
});

} catch (Exception ex) {
ess.showError(teleportOwner.getSource(), ex, "\\ teleport");
}
}
}
}
ess.scheduleSyncDelayedTask(new DelayedTeleportTask());
}

//If we need to cancelTimer a pending teleportPlayer call this method
void cancelTimer(boolean notifyUser) {
if (timer_task == -1) {
return;
}
try {
ess.getServer().getScheduler().cancelTask(timer_task);
if (notifyUser) {
teleportOwner.sendMessage(tl("pendingTeleportCancelled"));
if (timer_teleportee != null && !timer_teleportee.equals(teleportOwner.getBase().getUniqueId())) {
ess.getUser(timer_teleportee).sendMessage(tl("pendingTeleportCancelled"));
}
}
} finally {
timer_task = -1;
}
}
}
7 changes: 7 additions & 0 deletions Essentials/src/com/earth2me/essentials/IUser.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.earth2me.essentials;

import com.earth2me.essentials.api.IAsyncTeleport;
import com.earth2me.essentials.commands.IEssentialsCommand;
import net.ess3.api.ITeleport;
import net.ess3.api.MaxMoneyException;
Expand Down Expand Up @@ -54,8 +55,14 @@ public interface IUser {
*/
boolean hasOutstandingTeleportRequest();

/**
* @deprecated This API is not asynchronous. Use {@link com.earth2me.essentials.api.IAsyncTeleport IAsyncTeleport} with {@link IUser#getAsyncTeleport()}
*/
@Deprecated
ITeleport getTeleport();

IAsyncTeleport getAsyncTeleport();

BigDecimal getMoney();

void setMoney(final BigDecimal value) throws MaxMoneyException;
Expand Down
37 changes: 33 additions & 4 deletions Essentials/src/com/earth2me/essentials/Jails.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

import java.io.File;
import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.util.logging.Level;
import java.util.logging.Logger;

Expand Down Expand Up @@ -116,7 +117,11 @@ public void removeJail(final String jail) throws Exception {
}
}

/**
* @deprecated This method does not use asynchronous teleportation. Use {@link Jails#sendToJail(IUser, String, CompletableFuture)}
*/
@Override
@Deprecated
public void sendToJail(final IUser user, final String jail) throws Exception {
acquireReadLock();
try {
Expand All @@ -130,6 +135,24 @@ public void sendToJail(final IUser user, final String jail) throws Exception {
}
}

@Override
public void sendToJail(IUser user, String jail, CompletableFuture<Boolean> future) throws Exception {
acquireReadLock();
try {
if (user.getBase().isOnline()) {
Location loc = getJail(jail);
user.getAsyncTeleport().now(loc, false, TeleportCause.COMMAND, future);
future.thenAccept(success -> {
user.setJail(jail);
});
return;
}
user.setJail(jail);
} finally {
unlock();
}
}

@Override
public void setJail(final String jailName, final Location loc) throws Exception {
acquireWriteLock();
Expand Down Expand Up @@ -254,16 +277,22 @@ public void onJailPlayerJoin(final PlayerJoinEvent event) {
return;
}

try {
sendToJail(user, user.getJail());
} catch (Exception ex) {
CompletableFuture<Boolean> future = new CompletableFuture<>();
future.exceptionally(ex -> {
if (ess.getSettings().isDebug()) {
LOGGER.log(Level.INFO, tl("returnPlayerToJailError", user.getName(), ex.getLocalizedMessage()), ex);
} else {
LOGGER.log(Level.INFO, tl("returnPlayerToJailError", user.getName(), ex.getLocalizedMessage()));
}
return false;
});
future.thenAccept(success -> user.sendMessage(tl("jailMessage")));

try {
sendToJail(user, user.getJail(), future);
} catch (Exception ex) {
future.completeExceptionally(ex);
}
user.sendMessage(tl("jailMessage"));
}
}
}
Loading