Implements NMS for pasting and supports older versions of server (#2110)

* Implements NMS for pasting and supports older versions of server

* Remove trial code that shouldn't have been committed.

* Remove trial code that shouldn't have been committed.

* Remove 1.19.3 support.

* Use 1.19.4 in POM
This commit is contained in:
tastybento 2023-03-26 10:05:54 -07:00 committed by GitHub
parent 85a7375004
commit 1a293a6936
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 111 additions and 13 deletions

View File

@ -91,6 +91,7 @@
<build.version>1.22.1</build.version>
<sonar.organization>bentobox-world</sonar.organization>
<sonar.host.url>https://sonarcloud.io</sonar.host.url>
<server.jars>${project.basedir}/lib</server.jars>
</properties>
<!-- Profiles will allow to automatically change build version. -->
@ -312,7 +313,7 @@
<version>3.2.2</version>
<scope>provided</scope>
</dependency>
<!-- Spigot NMS. Used for chunk deletion and pasting. -->
<!-- Spigot NMS. Used for chunk deletion and pasting.-->
<dependency>
<groupId>org.spigotmc</groupId>
<artifactId>spigot</artifactId>

View File

@ -13,7 +13,6 @@ import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.bukkit.generator.BlockPopulator;
import org.bukkit.generator.ChunkGenerator;
import org.bukkit.generator.LimitedRegion;
import org.bukkit.inventory.InventoryHolder;
import org.bukkit.scheduler.BukkitRunnable;
import org.bukkit.util.BoundingBox;

View File

@ -0,0 +1,82 @@
package world.bentobox.bentobox.nms.v1_19_R3;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.stream.Collectors;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.block.Block;
import org.bukkit.block.data.BlockData;
import org.bukkit.craftbukkit.v1_19_R3.CraftWorld;
import org.bukkit.craftbukkit.v1_19_R3.block.data.CraftBlockData;
import net.minecraft.core.BlockPosition;
import net.minecraft.world.level.block.state.IBlockData;
import net.minecraft.world.level.chunk.Chunk;
import world.bentobox.bentobox.blueprints.dataobjects.BlueprintBlock;
import world.bentobox.bentobox.blueprints.dataobjects.BlueprintEntity;
import world.bentobox.bentobox.database.objects.Island;
import world.bentobox.bentobox.nms.PasteHandler;
import world.bentobox.bentobox.util.DefaultPasteUtil;
import world.bentobox.bentobox.util.Util;
public class PasteHandlerImpl implements PasteHandler {
protected static final IBlockData AIR = ((CraftBlockData) Bukkit.createBlockData(Material.AIR)).getState();
@Override
public CompletableFuture<Void> pasteBlocks(Island island, World world, Map<Location, BlueprintBlock> blockMap) {
return blockMap.entrySet().stream()
.map(entry -> setBlock(island, entry.getKey(), entry.getValue()))
.collect(
Collectors.collectingAndThen(
Collectors.toList(),
list -> CompletableFuture.allOf(list.toArray(new CompletableFuture[0]))
)
);
}
@Override
public CompletableFuture<Void> pasteEntities(Island island, World world, Map<Location, List<BlueprintEntity>> entityMap) {
return entityMap.entrySet().stream()
.map(entry -> DefaultPasteUtil.setEntity(island, entry.getKey(), entry.getValue()))
.collect(
Collectors.collectingAndThen(
Collectors.toList(),
list -> CompletableFuture.allOf(list.toArray(new CompletableFuture[0]))
)
);
}
/**
* Set the block to the location
*
* @param island - island
* @param location - location
* @param bpBlock - blueprint block
*/
public static CompletableFuture<Void> setBlock(Island island, Location location, BlueprintBlock bpBlock) {
return Util.getChunkAtAsync(location).thenRun(() -> {
Block block = location.getBlock();
// Set the block data - default is AIR
BlockData bd = DefaultPasteUtil.createBlockData(bpBlock);
CraftBlockData craft = (CraftBlockData) bd;
net.minecraft.world.level.World nmsWorld = ((CraftWorld) location.getWorld()).getHandle();
Chunk nmsChunk = nmsWorld.d(location.getBlockX() >> 4, location.getBlockZ() >> 4);
BlockPosition bp = new BlockPosition(location.getBlockX(), location.getBlockY(), location.getBlockZ());
// Setting the block to air before setting to another state prevents some console errors
nmsChunk.a(bp, AIR, false);
nmsChunk.a(bp, craft.getState(), false);
block.setBlockData(bd, false);
DefaultPasteUtil.setBlockState(island, block, bpBlock);
// Set biome
if (bpBlock.getBiome() != null) {
block.setBiome(bpBlock.getBiome());
}
});
}
}

View File

@ -738,6 +738,32 @@ public class Util {
}
return regenerator;
}
/**
* Checks what version the server is running and picks the appropriate NMS handler, or fallback
* @return PasteHandler
*/
public static PasteHandler getPasteHandler() {
if (pasteHandler == null) {
String serverPackageName = Bukkit.getServer().getClass().getPackage().getName();
String pluginPackageName = plugin.getClass().getPackage().getName();
String version = serverPackageName.substring(serverPackageName.lastIndexOf('.') + 1);
PasteHandler handler;
try {
Class<?> clazz = Class.forName(pluginPackageName + ".nms." + version + ".PasteHandlerImpl");
if (PasteHandler.class.isAssignableFrom(clazz)) {
handler = (PasteHandler) clazz.getConstructor().newInstance();
} else {
throw new IllegalStateException("Class " + clazz.getName() + " does not implement PasteHandler");
}
} catch (Exception e) {
plugin.logWarning("No PasteHandler found for " + version + ", falling back to Bukkit API.");
handler = new world.bentobox.bentobox.nms.fallback.PasteHandlerImpl();
}
setPasteHandler(handler);
}
return pasteHandler;
}
/**
* Set the paste handler the plugin will use
@ -747,17 +773,6 @@ public class Util {
Util.pasteHandler = pasteHandler;
}
/**
* Get the paste handler the plugin will use
* @return an NMS accelerated class for this server
*/
public static PasteHandler getPasteHandler() {
if (pasteHandler == null) {
setPasteHandler(new world.bentobox.bentobox.nms.fallback.PasteHandlerImpl());
}
return pasteHandler;
}
/**
* Broadcast a localized message to all players with the permission {@link Server#BROADCAST_CHANNEL_USERS}
*

View File

@ -60,3 +60,4 @@ permissions:
bentobox.version:
description: Allows to use /bentobox version
default: op