Support nms pasting (#2406)

This commit is contained in:
tastybento 2024-06-14 13:10:01 -07:00 committed by GitHub
parent 86e5a02516
commit 96564275a8
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -8,6 +8,7 @@ import java.util.Arrays;
import java.util.Date; import java.util.Date;
import java.util.Enumeration; import java.util.Enumeration;
import java.util.List; import java.util.List;
import java.util.Objects;
import java.util.UUID; import java.util.UUID;
import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletableFuture;
import java.util.jar.JarEntry; import java.util.jar.JarEntry;
@ -15,6 +16,8 @@ import java.util.jar.JarFile;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import javax.annotation.Nonnull;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
import org.bukkit.Chunk; import org.bukkit.Chunk;
@ -73,6 +76,11 @@ public class Util {
private static PasteHandler pasteHandler = null; private static PasteHandler pasteHandler = null;
private static WorldRegenerator regenerator = null; private static WorldRegenerator regenerator = null;
// Bukkit method that was added in 2011
// Example value: 1.20.4-R0.1-SNAPSHOT
private static final String bukkitVersion = "v" + Bukkit.getServer().getBukkitVersion().replace('.', '_').replace('-', '_');
private static final String pluginPackageName = plugin.getClass().getPackage().getName();
private Util() {} private Util() {}
/** /**
@ -373,7 +381,7 @@ public class Util {
* @return Future that completes with the result of the teleport * @return Future that completes with the result of the teleport
*/ */
@NonNull @NonNull
public static CompletableFuture<Boolean> teleportAsync(@NonNull Entity entity, @NonNull Location location) { public static CompletableFuture<Boolean> teleportAsync(@Nonnull Entity entity, @Nonnull Location location) {
return PaperLib.teleportAsync(entity, location); return PaperLib.teleportAsync(entity, location);
} }
@ -385,7 +393,8 @@ public class Util {
* @return Future that completes with the result of the teleport * @return Future that completes with the result of the teleport
*/ */
@NonNull @NonNull
public static CompletableFuture<Boolean> teleportAsync(@NonNull Entity entity, @NonNull Location location, TeleportCause cause) { public static CompletableFuture<Boolean> teleportAsync(@Nonnull Entity entity, @Nonnull Location location,
TeleportCause cause) {
return PaperLib.teleportAsync(entity, location, cause); return PaperLib.teleportAsync(entity, location, cause);
} }
@ -396,7 +405,8 @@ public class Util {
*/ */
@NonNull @NonNull
public static CompletableFuture<Chunk> getChunkAtAsync(@NonNull Location loc) { public static CompletableFuture<Chunk> getChunkAtAsync(@NonNull Location loc) {
return getChunkAtAsync(loc.getWorld(), loc.getBlockX() >> 4, loc.getBlockZ() >> 4, true); return getChunkAtAsync(Objects.requireNonNull(loc.getWorld()), loc.getBlockX() >> 4, loc.getBlockZ() >> 4,
true);
} }
/** /**
@ -407,7 +417,7 @@ public class Util {
*/ */
@NonNull @NonNull
public static CompletableFuture<Chunk> getChunkAtAsync(@NonNull Location loc, boolean gen) { public static CompletableFuture<Chunk> getChunkAtAsync(@NonNull Location loc, boolean gen) {
return getChunkAtAsync(loc.getWorld(), loc.getBlockX() >> 4, loc.getBlockZ() >> 4, gen); return getChunkAtAsync(Objects.requireNonNull(loc.getWorld()), loc.getBlockX() >> 4, loc.getBlockZ() >> 4, gen);
} }
/** /**
@ -418,7 +428,7 @@ public class Util {
* @return Future that completes with the chunk * @return Future that completes with the chunk
*/ */
@NonNull @NonNull
public static CompletableFuture<Chunk> getChunkAtAsync(@NonNull World world, int x, int z) { public static CompletableFuture<Chunk> getChunkAtAsync(@Nonnull World world, int x, int z) {
return getChunkAtAsync(world, x, z, true); return getChunkAtAsync(world, x, z, true);
} }
@ -431,7 +441,7 @@ public class Util {
* @return Future that completes with the chunk, or null if the chunk did not exists and generation was not requested. * @return Future that completes with the chunk, or null if the chunk did not exists and generation was not requested.
*/ */
@NonNull @NonNull
public static CompletableFuture<Chunk> getChunkAtAsync(@NonNull World world, int x, int z, boolean gen) { public static CompletableFuture<Chunk> getChunkAtAsync(@Nonnull World world, int x, int z, boolean gen) {
return PaperLib.getChunkAtAsync(world, x, z, gen); return PaperLib.getChunkAtAsync(world, x, z, gen);
} }
@ -441,7 +451,7 @@ public class Util {
* @return If the chunk is generated or not * @return If the chunk is generated or not
*/ */
public static boolean isChunkGenerated(@NonNull Location loc) { public static boolean isChunkGenerated(@NonNull Location loc) {
return isChunkGenerated(loc.getWorld(), loc.getBlockX() >> 4, loc.getBlockZ() >> 4); return isChunkGenerated(Objects.requireNonNull(loc.getWorld()), loc.getBlockX() >> 4, loc.getBlockZ() >> 4);
} }
/** /**
@ -451,7 +461,7 @@ public class Util {
* @param z Z coordinate of the chunk to checl * @param z Z coordinate of the chunk to checl
* @return If the chunk is generated or not * @return If the chunk is generated or not
*/ */
public static boolean isChunkGenerated(@NonNull World world, int x, int z) { public static boolean isChunkGenerated(@Nonnull World world, int x, int z) {
return PaperLib.isChunkGenerated(world, x, z); return PaperLib.isChunkGenerated(world, x, z);
} }
@ -462,7 +472,7 @@ public class Util {
* @return The BlockState * @return The BlockState
*/ */
@NonNull @NonNull
public static BlockStateSnapshotResult getBlockState(@NonNull Block block, boolean useSnapshot) { public static BlockStateSnapshotResult getBlockState(@Nonnull Block block, boolean useSnapshot) {
return PaperLib.getBlockState(block, useSnapshot); return PaperLib.getBlockState(block, useSnapshot);
} }
@ -720,11 +730,6 @@ public class Util {
*/ */
public static WorldRegenerator getRegenerator() { public static WorldRegenerator getRegenerator() {
if (regenerator == null) { if (regenerator == null) {
// Bukkit method that was added in 2011
// Example value: 1.20.4-R0.1-SNAPSHOT
String bukkitVersion = "v" + Bukkit.getServer().getBukkitVersion().replace('.', '_').replace('-', '_');
String pluginPackageName = plugin.getClass().getPackage().getName();
WorldRegenerator handler; WorldRegenerator handler;
try { try {
Class<?> clazz = Class.forName(pluginPackageName + ".nms." + bukkitVersion + ".WorldRegeneratorImpl"); Class<?> clazz = Class.forName(pluginPackageName + ".nms." + bukkitVersion + ".WorldRegeneratorImpl");
@ -748,20 +753,22 @@ public class Util {
*/ */
public static PasteHandler getPasteHandler() { public static PasteHandler getPasteHandler() {
if (pasteHandler == null) { if (pasteHandler == null) {
String serverPackageName = Bukkit.getServer().getClass().getPackage().getName();
// Bukkit method that was added in 2011
// Example value: 1.20.4-R0.1-SNAPSHOT
String bukkitVersion = "v" + Bukkit.getServer().getBukkitVersion().replace('.', '_').replace('-', '_');
String pluginPackageName = plugin.getClass().getPackage().getName(); String pluginPackageName = plugin.getClass().getPackage().getName();
String version = serverPackageName.substring(serverPackageName.lastIndexOf('.') + 1); BentoBox.getInstance().log("Optimizing for " + bukkitVersion);
BentoBox.getInstance().log("Optimizing for " + version);
PasteHandler handler; PasteHandler handler;
try { try {
Class<?> clazz = Class.forName(pluginPackageName + ".nms." + version + ".PasteHandlerImpl"); Class<?> clazz = Class.forName(pluginPackageName + ".nms." + bukkitVersion + ".PasteHandlerImpl");
if (PasteHandler.class.isAssignableFrom(clazz)) { if (PasteHandler.class.isAssignableFrom(clazz)) {
handler = (PasteHandler) clazz.getConstructor().newInstance(); handler = (PasteHandler) clazz.getConstructor().newInstance();
} else { } else {
throw new IllegalStateException("Class " + clazz.getName() + " does not implement PasteHandler"); throw new IllegalStateException("Class " + clazz.getName() + " does not implement PasteHandler");
} }
} catch (Exception e) { } catch (Exception e) {
plugin.logWarning("No PasteHandler found for " + version + ", falling back to Bukkit API."); plugin.logWarning("No PasteHandler found for " + bukkitVersion + ", falling back to Bukkit API.");
handler = new world.bentobox.bentobox.nms.fallback.PasteHandlerImpl(); handler = new world.bentobox.bentobox.nms.fallback.PasteHandlerImpl();
} }
setPasteHandler(handler); setPasteHandler(handler);