Use PaperLib to load chunks async whenever possible

This commit is contained in:
DoNotSpamPls 2019-01-11 13:10:46 +02:00
parent 03d521d335
commit db4df6f3cf
8 changed files with 88 additions and 40 deletions

View File

@ -36,6 +36,12 @@
</executions> </executions>
<configuration> <configuration>
<createDependencyReducedPom>false</createDependencyReducedPom> <createDependencyReducedPom>false</createDependencyReducedPom>
<relocations>
<relocation>
<pattern>io.papermc.lib</pattern>
<shadedPattern>com.earth2me.essentials.paperlib</shadedPattern>
</relocation>
</relocations>
</configuration> </configuration>
</plugin> </plugin>
</plugins> </plugins>
@ -48,6 +54,12 @@
<version>1.5.6</version> <version>1.5.6</version>
<scope>provided</scope> <scope>provided</scope>
</dependency> </dependency>
<dependency>
<groupId>io.papermc</groupId>
<artifactId>paperlib</artifactId>
<version>1.0.1</version>
<scope>compile</scope>
</dependency>
<dependency> <dependency>
<groupId>net.ess3</groupId> <groupId>net.ess3</groupId>
<artifactId>NMSProvider</artifactId> <artifactId>NMSProvider</artifactId>
@ -95,12 +107,6 @@
<artifactId>ReflectionProvider</artifactId> <artifactId>ReflectionProvider</artifactId>
<version>2.16.0</version> <version>2.16.0</version>
<scope>compile</scope> <scope>compile</scope>
<exclusions>
<exclusion>
<groupId>org.bukkit</groupId>
<artifactId>craftbukkit</artifactId>
</exclusion>
</exclusions>
</dependency> </dependency>
<dependency> <dependency>
<groupId>net.ess3</groupId> <groupId>net.ess3</groupId>

View File

@ -7,6 +7,7 @@ import com.earth2me.essentials.textreader.TextPager;
import com.earth2me.essentials.utils.DateUtil; import com.earth2me.essentials.utils.DateUtil;
import com.earth2me.essentials.utils.LocationUtil; import com.earth2me.essentials.utils.LocationUtil;
import com.earth2me.essentials.utils.MaterialUtil; import com.earth2me.essentials.utils.MaterialUtil;
import io.papermc.lib.PaperLib;
import net.ess3.api.IEssentials; import net.ess3.api.IEssentials;
import org.bukkit.BanEntry; 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())) { while (LocationUtil.isBlockDamaging(loc.getWorld(), loc.getBlockX(), loc.getBlockY() - 1, loc.getBlockZ())) {
loc.setY(loc.getY() + 1d); loc.setY(loc.getY() + 1d);
} }
user.getBase().teleport(loc, TeleportCause.PLUGIN); PaperLib.teleportAsync(user.getBase(), loc);
} }
} }
ess.scheduleSyncDelayedTask(new DelayedClickJumpTask()); ess.scheduleSyncDelayedTask(new DelayedClickJumpTask());

View File

@ -2,7 +2,9 @@ package com.earth2me.essentials;
import com.earth2me.essentials.utils.DateUtil; import com.earth2me.essentials.utils.DateUtil;
import com.earth2me.essentials.utils.LocationUtil; import com.earth2me.essentials.utils.LocationUtil;
import io.papermc.lib.PaperLib;
import net.ess3.api.IEssentials; import net.ess3.api.IEssentials;
import net.ess3.api.ITeleport;
import net.ess3.api.IUser; import net.ess3.api.IUser;
import net.ess3.api.events.UserWarpEvent; import net.ess3.api.events.UserWarpEvent;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
@ -18,7 +20,7 @@ import java.util.GregorianCalendar;
import static com.earth2me.essentials.I18n.tl; 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 IUser teleportOwner;
private final IEssentials ess; private final IEssentials ess;
private TimedTeleport timedTeleport; private TimedTeleport timedTeleport;
@ -49,7 +51,7 @@ public class Teleport implements net.ess3.api.ITeleport {
final long earliestLong = earliestTime.getTimeInMillis(); final long earliestLong = earliestTime.getTimeInMillis();
// When was the last teleportPlayer used? // When was the last teleportPlayer used?
final Long lastTime = teleportOwner.getLastTeleportTimestamp(); final long lastTime = teleportOwner.getLastTeleportTimestamp();
if (lastTime > time.getTimeInMillis()) { if (lastTime > time.getTimeInMillis()) {
// This is to make sure time didn't get messed up on last teleportPlayer use. // 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); cooldown(false);
} }
final ITarget target = new LocationTarget(loc); final ITarget target = new LocationTarget(loc);
now(teleportOwner, target, cause); now(teleportOwner, target);
} }
@Override @Override
@ -112,11 +114,11 @@ public class Teleport implements net.ess3.api.ITeleport {
cooldown(false); cooldown(false);
} }
final ITarget target = new PlayerTarget(entity); 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())); 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); cancel(false);
teleportee.setLastLocation(); teleportee.setLastLocation();
Location loc = target.getLocation(); 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 (LocationUtil.isBlockUnsafeForUser(teleportee, loc.getWorld(), loc.getBlockX(), loc.getBlockY(), loc.getBlockZ())) {
if (ess.getSettings().isTeleportSafetyEnabled()) { if (ess.getSettings().isTeleportSafetyEnabled()) {
if (ess.getSettings().isForceDisableTeleportSafety()) { if (ess.getSettings().isForceDisableTeleportSafety()) {
teleportee.getBase().teleport(loc, cause); PaperLib.teleportAsync(teleportee.getBase(), loc);
} else { } else {
teleportee.getBase().teleport(LocationUtil.getSafeDestination(ess, teleportee, loc), cause); PaperLib.teleportAsync(teleportee.getBase(), LocationUtil.getSafeDestination(ess, teleportee, loc));
} }
} else { } else {
throw new Exception(tl("unsafeTeleportDestination", loc.getWorld().getName(), loc.getBlockX(), loc.getBlockY(), loc.getBlockZ())); throw new Exception(tl("unsafeTeleportDestination", loc.getWorld().getName(), loc.getBlockX(), loc.getBlockY(), loc.getBlockZ()));
} }
} else { } else {
if (ess.getSettings().isForceDisableTeleportSafety()) { if (ess.getSettings().isForceDisableTeleportSafety()) {
teleportee.getBase().teleport(loc, cause); PaperLib.teleportAsync(teleportee.getBase(), loc);
} else { } else {
if (ess.getSettings().isTeleportToCenterLocation()) { if (ess.getSettings().isTeleportToCenterLocation()) {
loc = LocationUtil.getRoundedDestination(loc); 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); cooldown(true);
if (delay <= 0 || teleportOwner.isAuthorized("essentials.teleport.timer.bypass") || teleportee.isAuthorized("essentials.teleport.timer.bypass")) { if (delay <= 0 || teleportOwner.isAuthorized("essentials.teleport.timer.bypass") || teleportee.isAuthorized("essentials.teleport.timer.bypass")) {
cooldown(false); cooldown(false);
now(teleportee, target, cause); now(teleportee, target);
if (cashCharge != null) { if (cashCharge != null) {
cashCharge.charge(teleportOwner); cashCharge.charge(teleportOwner);
} }
@ -219,7 +221,7 @@ public class Teleport implements net.ess3.api.ITeleport {
cooldown(true); cooldown(true);
if (delay <= 0 || teleportOwner.isAuthorized("essentials.teleport.timer.bypass")) { if (delay <= 0 || teleportOwner.isAuthorized("essentials.teleport.timer.bypass")) {
cooldown(false); cooldown(false);
respawnNow(teleportOwner, cause); respawnNow(teleportOwner);
if (chargeFor != null) { if (chargeFor != null) {
chargeFor.charge(teleportOwner); 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); 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(); final Player player = teleportee.getBase();
Location bed = player.getBedSpawnLocation(); Location bed = player.getBedSpawnLocation();
if (bed != null) { if (bed != null) {
now(teleportee, new LocationTarget(bed), cause); now(teleportee, new LocationTarget(bed));
} else { } else {
if (ess.getSettings().isDebug()) { if (ess.getSettings().isDebug()) {
ess.getLogger().info("Could not find bed spawn, forcing respawn event."); ess.getLogger().info("Could not find bed spawn, forcing respawn event.");
} }
final PlayerRespawnEvent pre = new PlayerRespawnEvent(player, player.getWorld().getSpawnLocation(), false); final PlayerRespawnEvent pre = new PlayerRespawnEvent(player, player.getWorld().getSpawnLocation(), false);
ess.getServer().getPluginManager().callEvent(pre); 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 //This function is used to throw a user back after a jail sentence
@Override @Override
public void back() throws Exception { public void back() throws Exception {
now(teleportOwner, new LocationTarget(teleportOwner.getLastLocation()), TeleportCause.COMMAND); now(teleportOwner, new LocationTarget(teleportOwner.getLastLocation()));
} }
public void setTpType(TeleportType tpType) { public void setTpType(TeleportType tpType) {

View File

@ -104,9 +104,9 @@ public class TimedTeleport implements Runnable {
timer_chargeFor.isAffordableFor(teleportOwner); timer_chargeFor.isAffordableFor(teleportOwner);
} }
if (timer_respawn) { if (timer_respawn) {
teleport.respawnNow(teleportUser, timer_cause); teleport.respawnNow(teleportUser);
} else { } else {
teleport.now(teleportUser, timer_teleportTarget, timer_cause); teleport.now(teleportUser, timer_teleportTarget);
} }
if (timer_chargeFor != null) { if (timer_chargeFor != null) {
timer_chargeFor.charge(teleportOwner); timer_chargeFor.charge(teleportOwner);

View File

@ -1,6 +1,7 @@
package com.earth2me.essentials.utils; package com.earth2me.essentials.utils;
import com.earth2me.essentials.IEssentials; import com.earth2me.essentials.IEssentials;
import io.papermc.lib.PaperLib;
import net.ess3.api.IUser; import net.ess3.api.IUser;
import org.bukkit.GameMode; import org.bukkit.GameMode;
import org.bukkit.Location; import org.bukkit.Location;
@ -17,7 +18,7 @@ import static com.earth2me.essentials.I18n.tl;
public class LocationUtil { public class LocationUtil {
// The player can stand inside these materials // The player can stand inside these materials
public static final Set<Material> HOLLOW_MATERIALS = new HashSet<>(); private static final Set<Material> HOLLOW_MATERIALS = new HashSet<>();
private static final Set<Material> TRANSPARENT_MATERIALS = new HashSet<>(); private static final Set<Material> TRANSPARENT_MATERIALS = new HashSet<>();
static { static {
@ -32,8 +33,7 @@ public class LocationUtil {
TRANSPARENT_MATERIALS.add(Material.WATER); TRANSPARENT_MATERIALS.add(Material.WATER);
try { try {
TRANSPARENT_MATERIALS.add(Material.valueOf("FLOWING_WATER")); 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; public static final int RADIUS = 3;
@ -49,7 +49,7 @@ public class LocationUtil {
public int y; public int y;
public int z; public int z;
public Vector3D(int x, int y, int z) { Vector3D(int x, int y, int z) {
this.x = x; this.x = x;
this.y = y; this.y = y;
this.z = z; this.z = z;
@ -57,7 +57,7 @@ public class LocationUtil {
} }
static { static {
List<Vector3D> pos = new ArrayList<Vector3D>(); List<Vector3D> pos = new ArrayList<>();
for (int x = -RADIUS; x <= RADIUS; x++) { for (int x = -RADIUS; x <= RADIUS; x++) {
for (int y = -RADIUS; y <= RADIUS; y++) { for (int y = -RADIUS; y <= RADIUS; y++) {
for (int z = -RADIUS; z <= RADIUS; z++) { for (int z = -RADIUS; z <= RADIUS; z++) {
@ -65,16 +65,10 @@ public class LocationUtil {
} }
} }
} }
Collections.sort(pos, new Comparator<Vector3D>() { pos.sort(Comparator.comparingInt(a -> (a.x * a.x + a.y * a.y + a.z * a.z)));
@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);
}
});
VOLUME = pos.toArray(new Vector3D[0]); VOLUME = pos.toArray(new Vector3D[0]);
} }
@SuppressWarnings("deprecation")
public static Location getTarget(final LivingEntity entity) throws Exception { public static Location getTarget(final LivingEntity entity) throws Exception {
Block block = null; Block block = null;
try { try {
@ -88,7 +82,8 @@ public class LocationUtil {
return block.getLocation(); 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()); 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); 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); 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) { 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); final Block below = world.getBlockAt(x, y - 1, z);
switch (below.getType()) { switch (below.getType()) {
@ -159,7 +155,6 @@ public class LocationUtil {
user.getBase().setFlying(true); user.getBase().setFlying(true);
} }
// ess can be null if old deprecated method is calling it. // 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()) { if (ess == null || ess.getSettings().isTeleportToCenterLocation()) {
return getRoundedDestination(loc); return getRoundedDestination(loc);
} else { } else {

View File

@ -13,6 +13,38 @@
<build> <build>
<finalName>EssentialsXSpawn-${full.version}</finalName> <finalName>EssentialsXSpawn-${full.version}</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<showDeprecation>false</showDeprecation>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.1</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
<configuration>
<createDependencyReducedPom>false</createDependencyReducedPom>
<relocations>
<relocation>
<pattern>io.papermc.lib</pattern>
<shadedPattern>com.earth2me.essentials.spawn.paperlib</shadedPattern>
</relocation>
</relocations>
</configuration>
</plugin>
</plugins>
</build> </build>
<dependencies> <dependencies>
@ -20,6 +52,13 @@
<groupId>net.ess3</groupId> <groupId>net.ess3</groupId>
<artifactId>EssentialsX</artifactId> <artifactId>EssentialsX</artifactId>
<version>${project.version}</version> <version>${project.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>io.papermc</groupId>
<artifactId>paperlib</artifactId>
<version>1.0.1</version>
<scope>compile</scope>
</dependency> </dependency>
</dependencies> </dependencies>
</project> </project>

View File

@ -6,6 +6,7 @@ import com.earth2me.essentials.User;
import com.earth2me.essentials.textreader.IText; import com.earth2me.essentials.textreader.IText;
import com.earth2me.essentials.textreader.KeywordReplacer; import com.earth2me.essentials.textreader.KeywordReplacer;
import com.earth2me.essentials.textreader.SimpleTextPager; import com.earth2me.essentials.textreader.SimpleTextPager;
import io.papermc.lib.PaperLib;
import net.ess3.api.IEssentials; import net.ess3.api.IEssentials;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
@ -75,7 +76,7 @@ class EssentialsSpawnPlayerListener implements Listener {
Location spawn = spawns.getSpawn(user.getGroup()); Location spawn = spawns.getSpawn(user.getGroup());
try { try {
// We don't use user.getTeleport() because it stores last location, which is unwanted in this case. // 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) { } catch (Exception e) {
ess.showError(user.getSource(), e, "spawn-on-join"); ess.showError(user.getSource(), e, "spawn-on-join");
} }

View File

@ -31,6 +31,10 @@
<id>bukkit-repo</id> <id>bukkit-repo</id>
<url>https://hub.spigotmc.org/nexus/content/groups/public/</url> <url>https://hub.spigotmc.org/nexus/content/groups/public/</url>
</repository> </repository>
<repository>
<id>paper-repo</id>
<url>https://papermc.io/repo/repository/maven-public/</url>
</repository>
</repositories> </repositories>
<modules> <modules>