Essentials/EssentialsSpawn/src/main/java/com/earth2me/essentials/spawn/EssentialsSpawnPlayerListen...

164 lines
5.9 KiB
Java
Raw Normal View History

package com.earth2me.essentials.spawn;
import com.earth2me.essentials.Kit;
import com.earth2me.essentials.OfflinePlayer;
import com.earth2me.essentials.User;
import com.earth2me.essentials.textreader.IText;
import com.earth2me.essentials.textreader.KeywordReplacer;
import com.earth2me.essentials.textreader.SimpleTextPager;
import com.earth2me.essentials.utils.VersionUtil;
import net.ess3.api.IEssentials;
import org.bukkit.Location;
import org.bukkit.entity.Player;
2012-01-20 05:34:28 +01:00
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerRespawnEvent;
import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause;
import java.util.List;
2015-04-15 06:06:16 +02:00
import java.util.Locale;
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 10:52:25 +02:00
import java.util.concurrent.CompletableFuture;
2015-04-15 06:06:16 +02:00
import java.util.logging.Level;
import java.util.logging.Logger;
import static com.earth2me.essentials.I18n.tl;
class EssentialsSpawnPlayerListener implements Listener {
private static final Logger logger = EssentialsSpawn.getWrappedLogger();
2015-04-15 06:06:16 +02:00
private final transient IEssentials ess;
private final transient SpawnStorage spawns;
EssentialsSpawnPlayerListener(final IEssentials ess, final SpawnStorage spawns) {
2015-04-15 06:06:16 +02:00
super();
this.ess = ess;
this.spawns = spawns;
}
void onPlayerRespawn(final PlayerRespawnEvent event) {
2015-04-15 06:06:16 +02:00
final User user = ess.getUser(event.getPlayer());
if (user.isJailed() && user.getJail() != null && !user.getJail().isEmpty()) {
return;
}
if (VersionUtil.getServerBukkitVersion().isHigherThanOrEqualTo(VersionUtil.v1_16_1_R01) && event.isAnchorSpawn() && ess.getSettings().isRespawnAtAnchor()) {
return;
}
2015-04-15 06:06:16 +02:00
if (ess.getSettings().getRespawnAtHome()) {
2020-10-03 19:46:05 +02:00
final Location home;
Location bed = null;
if (ess.getSettings().isRespawnAtBed()) {
// cannot nuke this sync load due to the event being sync so it would hand either way
bed = user.getBase().getBedSpawnLocation();
}
2015-04-15 06:06:16 +02:00
if (bed != null) {
home = bed;
} else {
home = user.getHome(user.getLocation());
}
2015-04-15 06:06:16 +02:00
if (home != null) {
event.setRespawnLocation(home);
return;
}
}
final Location spawn = spawns.getSpawn(user.getGroup());
if (spawn != null) {
event.setRespawnLocation(spawn);
}
}
void onPlayerJoin(final PlayerJoinEvent event) {
ess.runTaskAsynchronously(() -> delayedJoin(event.getPlayer()));
2015-04-15 06:06:16 +02:00
}
private void delayedJoin(final Player player) {
2015-04-15 06:06:16 +02:00
if (player.hasPlayedBefore()) {
logger.log(Level.FINE, "Old player join");
2020-10-03 19:46:05 +02:00
final List<String> spawnOnJoinGroups = ess.getSettings().getSpawnOnJoinGroups();
if (!spawnOnJoinGroups.isEmpty()) {
final User user = ess.getUser(player);
2020-10-03 19:46:05 +02:00
if (ess.getSettings().isUserInSpawnOnJoinGroup(user) && !user.isAuthorized("essentials.spawn-on-join.exempt")) {
ess.scheduleSyncDelayedTask(() -> {
2020-10-03 19:46:05 +02:00
final Location spawn = spawns.getSpawn(user.getGroup());
if (spawn == null) {
return;
}
final CompletableFuture<Boolean> future = new CompletableFuture<>();
future.exceptionally(e -> {
ess.showError(user.getSource(), e, "spawn-on-join");
return false;
});
user.getAsyncTeleport().nowUnsafe(spawn, TeleportCause.PLUGIN, future);
});
}
}
2015-04-15 06:06:16 +02:00
return;
}
final User user = ess.getUser(player);
if (!"none".equalsIgnoreCase(ess.getSettings().getNewbieSpawn())) {
ess.scheduleSyncDelayedTask(new NewPlayerTeleport(user), 1L);
}
ess.scheduleSyncDelayedTask(() -> {
if (!user.getBase().isOnline()) {
return;
}
2015-04-15 06:06:16 +02:00
//This method allows for multiple line player announce messages using multiline yaml syntax #EasterEgg
if (ess.getSettings().getAnnounceNewPlayers()) {
final IText output = new KeywordReplacer(ess.getSettings().getAnnounceNewPlayerFormat(), user.getSource(), ess);
final SimpleTextPager pager = new SimpleTextPager(output);
2015-04-15 06:06:16 +02:00
2020-10-03 19:46:05 +02:00
for (final String line : pager.getLines()) {
ess.broadcastMessage(user, line);
2015-04-15 06:06:16 +02:00
}
}
2015-04-15 06:06:16 +02:00
final String kitName = ess.getSettings().getNewPlayerKit();
if (!kitName.isEmpty()) {
try {
final Kit kit = new Kit(kitName.toLowerCase(Locale.ENGLISH), ess);
kit.expandItems(user);
2020-10-03 19:46:05 +02:00
} catch (final Exception ex) {
logger.log(Level.WARNING, ex.getMessage());
2015-04-15 06:06:16 +02:00
}
}
logger.log(Level.FINE, "New player join");
2015-04-15 06:06:16 +02:00
}, 2L);
}
private class NewPlayerTeleport implements Runnable {
private final transient User user;
NewPlayerTeleport(final User user) {
2015-04-15 06:06:16 +02:00
this.user = user;
}
@Override
public void run() {
if (user.getBase() instanceof OfflinePlayer || !user.getBase().isOnline()) {
return;
}
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 10:52:25 +02:00
final Location spawn = spawns.getSpawn(ess.getSettings().getNewbieSpawn());
if (spawn != null) {
2020-10-03 19:46:05 +02:00
final CompletableFuture<Boolean> future = new CompletableFuture<>();
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 10:52:25 +02:00
future.exceptionally(e -> {
logger.log(Level.WARNING, tl("teleportNewPlayerError"), e);
return false;
});
user.getAsyncTeleport().now(spawn, false, TeleportCause.PLUGIN, future);
2015-04-15 06:06:16 +02:00
}
}
}
}