From db4df6f3cfa82d15d889933711674d02f5d95440 Mon Sep 17 00:00:00 2001
From: DoNotSpamPls <7570108+DoNotSpamPls@users.noreply.github.com>
Date: Fri, 11 Jan 2019 13:10:46 +0200
Subject: [PATCH] Use PaperLib to load chunks async whenever possible
---
Essentials/pom.xml | 18 ++++++---
.../essentials/EssentialsPlayerListener.java | 3 +-
.../src/com/earth2me/essentials/Teleport.java | 32 ++++++++-------
.../earth2me/essentials/TimedTeleport.java | 4 +-
.../essentials/utils/LocationUtil.java | 25 +++++-------
EssentialsSpawn/pom.xml | 39 +++++++++++++++++++
.../spawn/EssentialsSpawnPlayerListener.java | 3 +-
pom.xml | 4 ++
8 files changed, 88 insertions(+), 40 deletions(-)
diff --git a/Essentials/pom.xml b/Essentials/pom.xml
index 055298cd2..7841a3c1d 100644
--- a/Essentials/pom.xml
+++ b/Essentials/pom.xml
@@ -36,6 +36,12 @@
false
+
+
+ io.papermc.lib
+ com.earth2me.essentials.paperlib
+
+
@@ -48,6 +54,12 @@
1.5.6
provided
+
+ io.papermc
+ paperlib
+ 1.0.1
+ compile
+
net.ess3
NMSProvider
@@ -95,12 +107,6 @@
ReflectionProvider
2.16.0
compile
-
-
- org.bukkit
- craftbukkit
-
-
net.ess3
diff --git a/Essentials/src/com/earth2me/essentials/EssentialsPlayerListener.java b/Essentials/src/com/earth2me/essentials/EssentialsPlayerListener.java
index 9192d6c2d..4a16ef23a 100644
--- a/Essentials/src/com/earth2me/essentials/EssentialsPlayerListener.java
+++ b/Essentials/src/com/earth2me/essentials/EssentialsPlayerListener.java
@@ -7,6 +7,7 @@ import com.earth2me.essentials.textreader.TextPager;
import com.earth2me.essentials.utils.DateUtil;
import com.earth2me.essentials.utils.LocationUtil;
import com.earth2me.essentials.utils.MaterialUtil;
+import io.papermc.lib.PaperLib;
import net.ess3.api.IEssentials;
import org.bukkit.BanEntry;
@@ -642,7 +643,7 @@ public class EssentialsPlayerListener implements Listener {
while (LocationUtil.isBlockDamaging(loc.getWorld(), loc.getBlockX(), loc.getBlockY() - 1, loc.getBlockZ())) {
loc.setY(loc.getY() + 1d);
}
- user.getBase().teleport(loc, TeleportCause.PLUGIN);
+ PaperLib.teleportAsync(user.getBase(), loc);
}
}
ess.scheduleSyncDelayedTask(new DelayedClickJumpTask());
diff --git a/Essentials/src/com/earth2me/essentials/Teleport.java b/Essentials/src/com/earth2me/essentials/Teleport.java
index 82d17ffae..4fc44a3e3 100644
--- a/Essentials/src/com/earth2me/essentials/Teleport.java
+++ b/Essentials/src/com/earth2me/essentials/Teleport.java
@@ -2,7 +2,9 @@ package com.earth2me.essentials;
import com.earth2me.essentials.utils.DateUtil;
import com.earth2me.essentials.utils.LocationUtil;
+import io.papermc.lib.PaperLib;
import net.ess3.api.IEssentials;
+import net.ess3.api.ITeleport;
import net.ess3.api.IUser;
import net.ess3.api.events.UserWarpEvent;
import org.bukkit.Bukkit;
@@ -18,7 +20,7 @@ import java.util.GregorianCalendar;
import static com.earth2me.essentials.I18n.tl;
-public class Teleport implements net.ess3.api.ITeleport {
+public class Teleport implements ITeleport {
private final IUser teleportOwner;
private final IEssentials ess;
private TimedTeleport timedTeleport;
@@ -49,7 +51,7 @@ public class Teleport implements net.ess3.api.ITeleport {
final long earliestLong = earliestTime.getTimeInMillis();
// When was the last teleportPlayer used?
- final Long lastTime = teleportOwner.getLastTeleportTimestamp();
+ final long lastTime = teleportOwner.getLastTeleportTimestamp();
if (lastTime > time.getTimeInMillis()) {
// This is to make sure time didn't get messed up on last teleportPlayer use.
@@ -103,7 +105,7 @@ public class Teleport implements net.ess3.api.ITeleport {
cooldown(false);
}
final ITarget target = new LocationTarget(loc);
- now(teleportOwner, target, cause);
+ now(teleportOwner, target);
}
@Override
@@ -112,11 +114,11 @@ public class Teleport implements net.ess3.api.ITeleport {
cooldown(false);
}
final ITarget target = new PlayerTarget(entity);
- now(teleportOwner, target, cause);
+ now(teleportOwner, target);
teleportOwner.sendMessage(tl("teleporting", target.getLocation().getWorld().getName(), target.getLocation().getBlockX(), target.getLocation().getBlockY(), target.getLocation().getBlockZ()));
}
- protected void now(IUser teleportee, ITarget target, TeleportCause cause) throws Exception {
+ protected void now(IUser teleportee, ITarget target) throws Exception {
cancel(false);
teleportee.setLastLocation();
Location loc = target.getLocation();
@@ -124,21 +126,21 @@ public class Teleport implements net.ess3.api.ITeleport {
if (LocationUtil.isBlockUnsafeForUser(teleportee, loc.getWorld(), loc.getBlockX(), loc.getBlockY(), loc.getBlockZ())) {
if (ess.getSettings().isTeleportSafetyEnabled()) {
if (ess.getSettings().isForceDisableTeleportSafety()) {
- teleportee.getBase().teleport(loc, cause);
+ PaperLib.teleportAsync(teleportee.getBase(), loc);
} else {
- teleportee.getBase().teleport(LocationUtil.getSafeDestination(ess, teleportee, loc), cause);
+ PaperLib.teleportAsync(teleportee.getBase(), LocationUtil.getSafeDestination(ess, teleportee, loc));
}
} else {
throw new Exception(tl("unsafeTeleportDestination", loc.getWorld().getName(), loc.getBlockX(), loc.getBlockY(), loc.getBlockZ()));
}
} else {
if (ess.getSettings().isForceDisableTeleportSafety()) {
- teleportee.getBase().teleport(loc, cause);
+ PaperLib.teleportAsync(teleportee.getBase(), loc);
} else {
if (ess.getSettings().isTeleportToCenterLocation()) {
loc = LocationUtil.getRoundedDestination(loc);
}
- teleportee.getBase().teleport(loc, cause);
+ PaperLib.teleportAsync(teleportee.getBase(), loc);
}
}
}
@@ -197,7 +199,7 @@ public class Teleport implements net.ess3.api.ITeleport {
cooldown(true);
if (delay <= 0 || teleportOwner.isAuthorized("essentials.teleport.timer.bypass") || teleportee.isAuthorized("essentials.teleport.timer.bypass")) {
cooldown(false);
- now(teleportee, target, cause);
+ now(teleportee, target);
if (cashCharge != null) {
cashCharge.charge(teleportOwner);
}
@@ -219,7 +221,7 @@ public class Teleport implements net.ess3.api.ITeleport {
cooldown(true);
if (delay <= 0 || teleportOwner.isAuthorized("essentials.teleport.timer.bypass")) {
cooldown(false);
- respawnNow(teleportOwner, cause);
+ respawnNow(teleportOwner);
if (chargeFor != null) {
chargeFor.charge(teleportOwner);
}
@@ -231,18 +233,18 @@ public class Teleport implements net.ess3.api.ITeleport {
initTimer((long) (delay * 1000.0), teleportOwner, null, chargeFor, cause, true);
}
- protected void respawnNow(IUser teleportee, TeleportCause cause) throws Exception {
+ void respawnNow(IUser teleportee) throws Exception {
final Player player = teleportee.getBase();
Location bed = player.getBedSpawnLocation();
if (bed != null) {
- now(teleportee, new LocationTarget(bed), cause);
+ now(teleportee, new LocationTarget(bed));
} else {
if (ess.getSettings().isDebug()) {
ess.getLogger().info("Could not find bed spawn, forcing respawn event.");
}
final PlayerRespawnEvent pre = new PlayerRespawnEvent(player, player.getWorld().getSpawnLocation(), false);
ess.getServer().getPluginManager().callEvent(pre);
- now(teleportee, new LocationTarget(pre.getRespawnLocation()), cause);
+ now(teleportee, new LocationTarget(pre.getRespawnLocation()));
}
}
@@ -276,7 +278,7 @@ public class Teleport implements net.ess3.api.ITeleport {
//This function is used to throw a user back after a jail sentence
@Override
public void back() throws Exception {
- now(teleportOwner, new LocationTarget(teleportOwner.getLastLocation()), TeleportCause.COMMAND);
+ now(teleportOwner, new LocationTarget(teleportOwner.getLastLocation()));
}
public void setTpType(TeleportType tpType) {
diff --git a/Essentials/src/com/earth2me/essentials/TimedTeleport.java b/Essentials/src/com/earth2me/essentials/TimedTeleport.java
index 0544c6c8c..6093e009c 100644
--- a/Essentials/src/com/earth2me/essentials/TimedTeleport.java
+++ b/Essentials/src/com/earth2me/essentials/TimedTeleport.java
@@ -104,9 +104,9 @@ public class TimedTeleport implements Runnable {
timer_chargeFor.isAffordableFor(teleportOwner);
}
if (timer_respawn) {
- teleport.respawnNow(teleportUser, timer_cause);
+ teleport.respawnNow(teleportUser);
} else {
- teleport.now(teleportUser, timer_teleportTarget, timer_cause);
+ teleport.now(teleportUser, timer_teleportTarget);
}
if (timer_chargeFor != null) {
timer_chargeFor.charge(teleportOwner);
diff --git a/Essentials/src/com/earth2me/essentials/utils/LocationUtil.java b/Essentials/src/com/earth2me/essentials/utils/LocationUtil.java
index 571333347..2be35da08 100644
--- a/Essentials/src/com/earth2me/essentials/utils/LocationUtil.java
+++ b/Essentials/src/com/earth2me/essentials/utils/LocationUtil.java
@@ -1,6 +1,7 @@
package com.earth2me.essentials.utils;
import com.earth2me.essentials.IEssentials;
+import io.papermc.lib.PaperLib;
import net.ess3.api.IUser;
import org.bukkit.GameMode;
import org.bukkit.Location;
@@ -17,7 +18,7 @@ import static com.earth2me.essentials.I18n.tl;
public class LocationUtil {
// The player can stand inside these materials
- public static final Set HOLLOW_MATERIALS = new HashSet<>();
+ private static final Set HOLLOW_MATERIALS = new HashSet<>();
private static final Set TRANSPARENT_MATERIALS = new HashSet<>();
static {
@@ -32,8 +33,7 @@ public class LocationUtil {
TRANSPARENT_MATERIALS.add(Material.WATER);
try {
TRANSPARENT_MATERIALS.add(Material.valueOf("FLOWING_WATER"));
- } catch (Exception ignored) { // 1.13 WATER uses Levelled
- }
+ } catch (Exception ignored) {} // 1.13 WATER uses Levelled
}
public static final int RADIUS = 3;
@@ -49,7 +49,7 @@ public class LocationUtil {
public int y;
public int z;
- public Vector3D(int x, int y, int z) {
+ Vector3D(int x, int y, int z) {
this.x = x;
this.y = y;
this.z = z;
@@ -57,7 +57,7 @@ public class LocationUtil {
}
static {
- List pos = new ArrayList();
+ List pos = new ArrayList<>();
for (int x = -RADIUS; x <= RADIUS; x++) {
for (int y = -RADIUS; y <= RADIUS; y++) {
for (int z = -RADIUS; z <= RADIUS; z++) {
@@ -65,16 +65,10 @@ public class LocationUtil {
}
}
}
- Collections.sort(pos, new Comparator() {
- @Override
- public int compare(Vector3D a, Vector3D b) {
- return (a.x * a.x + a.y * a.y + a.z * a.z) - (b.x * b.x + b.y * b.y + b.z * b.z);
- }
- });
+ pos.sort(Comparator.comparingInt(a -> (a.x * a.x + a.y * a.y + a.z * a.z)));
VOLUME = pos.toArray(new Vector3D[0]);
}
- @SuppressWarnings("deprecation")
public static Location getTarget(final LivingEntity entity) throws Exception {
Block block = null;
try {
@@ -88,7 +82,8 @@ public class LocationUtil {
return block.getLocation();
}
- static boolean isBlockAboveAir(final World world, final int x, final int y, final int z) {
+ private static boolean isBlockAboveAir(final World world, final int x, final int y, final int z) {
+ PaperLib.getChunkAtAsync(world, x, z); // Get the chunk first so it can be loaded async
return y > world.getMaxHeight() || HOLLOW_MATERIALS.contains(world.getBlockAt(x, y - 1, z).getType());
}
@@ -103,11 +98,12 @@ public class LocationUtil {
return isBlockAboveAir(world, x, y, z);
}
- public static boolean isBlockUnsafe(final World world, final int x, final int y, final int z) {
+ private static boolean isBlockUnsafe(final World world, final int x, final int y, final int z) {
return isBlockDamaging(world, x, y, z) || isBlockAboveAir(world, x, y, z);
}
public static boolean isBlockDamaging(final World world, final int x, final int y, final int z) {
+ PaperLib.getChunkAtAsync(world, x, z); // Get the chunk first so it can be loaded async
final Block below = world.getBlockAt(x, y - 1, z);
switch (below.getType()) {
@@ -159,7 +155,6 @@ public class LocationUtil {
user.getBase().setFlying(true);
}
// ess can be null if old deprecated method is calling it.
- System.out.println((ess == null) + " " + ess.getSettings().isTeleportToCenterLocation());
if (ess == null || ess.getSettings().isTeleportToCenterLocation()) {
return getRoundedDestination(loc);
} else {
diff --git a/EssentialsSpawn/pom.xml b/EssentialsSpawn/pom.xml
index f32b8eace..eb0df4e8c 100644
--- a/EssentialsSpawn/pom.xml
+++ b/EssentialsSpawn/pom.xml
@@ -13,6 +13,38 @@
EssentialsXSpawn-${full.version}
+
+
+ org.apache.maven.plugins
+ maven-compiler-plugin
+ 3.8.0
+
+ false
+
+
+
+ org.apache.maven.plugins
+ maven-shade-plugin
+ 3.2.1
+
+
+ package
+
+ shade
+
+
+
+
+ false
+
+
+ io.papermc.lib
+ com.earth2me.essentials.spawn.paperlib
+
+
+
+
+
@@ -20,6 +52,13 @@
net.ess3
EssentialsX
${project.version}
+ provided
+
+
+ io.papermc
+ paperlib
+ 1.0.1
+ compile
\ No newline at end of file
diff --git a/EssentialsSpawn/src/com/earth2me/essentials/spawn/EssentialsSpawnPlayerListener.java b/EssentialsSpawn/src/com/earth2me/essentials/spawn/EssentialsSpawnPlayerListener.java
index d4e787f6b..383c11220 100644
--- a/EssentialsSpawn/src/com/earth2me/essentials/spawn/EssentialsSpawnPlayerListener.java
+++ b/EssentialsSpawn/src/com/earth2me/essentials/spawn/EssentialsSpawnPlayerListener.java
@@ -6,6 +6,7 @@ import com.earth2me.essentials.User;
import com.earth2me.essentials.textreader.IText;
import com.earth2me.essentials.textreader.KeywordReplacer;
import com.earth2me.essentials.textreader.SimpleTextPager;
+import io.papermc.lib.PaperLib;
import net.ess3.api.IEssentials;
import org.bukkit.Location;
import org.bukkit.entity.Player;
@@ -75,7 +76,7 @@ class EssentialsSpawnPlayerListener implements Listener {
Location spawn = spawns.getSpawn(user.getGroup());
try {
// We don't use user.getTeleport() because it stores last location, which is unwanted in this case.
- user.getBase().teleport(spawn, TeleportCause.PLUGIN);
+ PaperLib.teleportAsync(user.getBase(), spawn);
} catch (Exception e) {
ess.showError(user.getSource(), e, "spawn-on-join");
}
diff --git a/pom.xml b/pom.xml
index 84bdab522..0c75e67ec 100644
--- a/pom.xml
+++ b/pom.xml
@@ -31,6 +31,10 @@
bukkit-repo
https://hub.spigotmc.org/nexus/content/groups/public/
+
+ paper-repo
+ https://papermc.io/repo/repository/maven-public/
+