Merge pull request #2304 from BentoBoxWorld/bukkit_paste

Bukkit paste
This commit is contained in:
tastybento 2024-02-23 23:08:27 -08:00 committed by GitHub
commit ad009156e5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 62 additions and 15 deletions

View File

@ -25,7 +25,7 @@ public class AdminBlueprintPasteCommand extends CompositeCommand {
AdminBlueprintCommand parent = (AdminBlueprintCommand) getParent();
BlueprintClipboard clipboard = parent.getClipboards().computeIfAbsent(user.getUniqueId(), v -> new BlueprintClipboard());
if (clipboard.isFull()) {
new BlueprintPaster(getPlugin(), clipboard, user.getLocation()).paste().thenAccept(b -> {
new BlueprintPaster(getPlugin(), clipboard, user.getLocation()).paste(false).thenAccept(b -> {
user.sendMessage("general.success");
parent.showClipboard(user);
});

View File

@ -58,6 +58,7 @@ public class BlueprintPaster {
private final BentoBox plugin;
private final PasteHandler paster = Util.getPasteHandler();
private final PasteHandler fallback = new world.bentobox.bentobox.nms.fallback.PasteHandlerImpl();
private final World world;
// The minimum block position (x,y,z)
private Location pos1;
@ -127,10 +128,20 @@ public class BlueprintPaster {
Iterator<Entry<Vector, BlueprintBlock>> it2,
Iterator<Entry<Vector, List<BlueprintEntity>>> it3,
int pasteSpeed) {}
/**
* The main pasting method
*/
public CompletableFuture<Boolean> paste() {
return this.paste(true);
}
/**
* Paste the clipboard
* @param useNMS if true, NMS pasting will be used, otherwise Bukkit API
* @return Future boolean where true is success
*/
public CompletableFuture<Boolean> paste(boolean useNMS) {
CompletableFuture<Boolean> result = new CompletableFuture<>();
// Iterators for the various maps to paste
final Map<Vector, BlueprintBlock> blocks = blueprint.getBlocks() == null ? Collections.emptyMap() : blueprint.getBlocks();
@ -148,12 +159,12 @@ public class BlueprintPaster {
Bits bits = new Bits(blocks, attached, entities,
blocks.entrySet().iterator(), attached.entrySet().iterator(), entities.entrySet().iterator(),
plugin.getSettings().getPasteSpeed());
pastingTask = Bukkit.getScheduler().runTaskTimer(plugin, () -> pasterTask(result, owner, bits), 0L, 1L);
pastingTask = Bukkit.getScheduler().runTaskTimer(plugin, () -> pasterTask(result, owner, bits, useNMS), 0L, 1L);
return result;
}
private void pasterTask(CompletableFuture<Boolean> result, Optional<User> owner, Bits bits) {
private void pasterTask(CompletableFuture<Boolean> result, Optional<User> owner, Bits bits, boolean useNMS) {
if (!currentTask.isDone()) return;
final int pasteSpeed = plugin.getSettings().getPasteSpeed();
@ -163,10 +174,10 @@ public class BlueprintPaster {
loadChunk();
}
else if (pasteState.equals(PasteState.BLOCKS) || pasteState.equals(PasteState.ATTACHMENTS)) {
pasteBlocks(bits, count, owner, pasteSpeed);
pasteBlocks(bits, count, owner, pasteSpeed, useNMS);
}
else if (pasteState.equals(PasteState.ENTITIES)) {
pasteEntities(bits, count, owner, pasteSpeed);
pasteEntities(bits, count, owner, pasteSpeed, useNMS);
}
else if (pasteState.equals(PasteState.DONE)) {
// All done. Cancel task
@ -188,7 +199,7 @@ public class BlueprintPaster {
result.complete(true);
}
private void pasteEntities(Bits bits, int count, Optional<User> owner, int pasteSpeed) {
private void pasteEntities(Bits bits, int count, Optional<User> owner, int pasteSpeed, boolean useNMS) {
if (bits.it3().hasNext()) {
Map<Location, List<BlueprintEntity>> entityMap = new HashMap<>();
// Paste entities
@ -206,7 +217,8 @@ public class BlueprintPaster {
count++;
}
if (!entityMap.isEmpty()) {
currentTask = paster.pasteEntities(island, world, entityMap);
currentTask = useNMS ? paster.pasteEntities(island, world, entityMap)
: fallback.pasteEntities(island, world, entityMap);
}
} else {
pasteState = PasteState.DONE;
@ -222,7 +234,7 @@ public class BlueprintPaster {
}
private void pasteBlocks(Bits bits, int count, Optional<User> owner, int pasteSpeed) {
private void pasteBlocks(Bits bits, int count, Optional<User> owner, int pasteSpeed, boolean useNMS) {
Iterator<Entry<Vector, BlueprintBlock>> it = pasteState.equals(PasteState.BLOCKS) ? bits.it : bits.it2;
if (it.hasNext()) {
Map<Location, BlueprintBlock> blockMap = new HashMap<>();
@ -241,7 +253,8 @@ public class BlueprintPaster {
count++;
}
if (!blockMap.isEmpty()) {
currentTask = paster.pasteBlocks(island, world, blockMap);
currentTask = useNMS ? paster.pasteBlocks(island, world, blockMap)
: fallback.pasteBlocks(island, world, blockMap);
}
} else {
if (pasteState.equals(PasteState.BLOCKS)) {

View File

@ -445,7 +445,7 @@ public class BlueprintsManager {
* @param name - bundle name
*/
public void paste(GameModeAddon addon, Island island, String name) {
paste(addon, island, name, null);
paste(addon, island, name, null, true);
}
/**
@ -455,9 +455,10 @@ public class BlueprintsManager {
* @param island - the island
* @param name - name of bundle to paste
* @param task - task to run after pasting is completed
* @param useNMS - true to use NMS pasting
* @return true if okay, false is there is a problem
*/
public boolean paste(GameModeAddon addon, Island island, String name, Runnable task) {
public boolean paste(GameModeAddon addon, Island island, String name, Runnable task, boolean useNMS) {
if (validate(addon, name) == null) {
plugin.logError("Tried to paste '" + name + "' but the bundle is not loaded!");
return false;
@ -478,7 +479,9 @@ public class BlueprintsManager {
}
// Paste
if (bp != null) {
new BlueprintPaster(plugin, bp, addon.getOverWorld(), island).paste().thenAccept(b -> pasteNether(addon, bb, island).thenAccept(b2 ->
new BlueprintPaster(plugin, bp, addon.getOverWorld(), island).paste(useNMS)
.thenAccept(b -> pasteNether(addon, bb, island).thenAccept(
b2 ->
pasteEnd(addon, bb, island).thenAccept(message -> sendMessage(island)).thenAccept(b3 -> Bukkit.getScheduler().runTask(plugin, task))));
}
return true;

View File

@ -206,8 +206,11 @@ public class NewIsland {
if (noPaste) {
Bukkit.getScheduler().runTask(plugin, () -> postCreationTask(oldIsland));
} else {
// Find out how far away the player is from the new island
boolean useNMS = !user.getWorld().equals(island.getWorld())
|| (user.getLocation().distance(island.getCenter()) >= Bukkit.getViewDistance() * 16D);
// Create islands, then run task
plugin.getBlueprintsManager().paste(addon, island, name, () -> postCreationTask(oldIsland));
plugin.getBlueprintsManager().paste(addon, island, name, () -> postCreationTask(oldIsland), useNMS);
}
// Set default settings
island.setFlagsDefaults();

View File

@ -20,6 +20,7 @@ import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.block.Block;
import org.bukkit.scheduler.BukkitScheduler;
import org.eclipse.jdt.annotation.NonNull;
import org.junit.After;
import org.junit.Before;
import org.junit.Ignore;
@ -97,6 +98,8 @@ public class NewIslandTest {
private final UUID uuid = UUID.randomUUID();
@Mock
private BlueprintsManager bpm;
@Mock
private @NonNull Location location2;
/**
*/
@ -129,6 +132,8 @@ public class NewIslandTest {
when(user.getPermissionValue(Mockito.anyString(), Mockito.anyInt())).thenReturn(20);
when(user.getUniqueId()).thenReturn(uuid);
when(user.getName()).thenReturn("tastybento");
when(user.getWorld()).thenReturn(world);
when(user.getLocation()).thenReturn(location);
// Events
PowerMockito.mockStatic(IslandEvent.class);
@ -147,9 +152,11 @@ public class NewIslandTest {
// Location and blocks
when(island.getWorld()).thenReturn(world);
when(island.getCenter()).thenReturn(location2);
when(location.getWorld()).thenReturn(world);
when(world.getMaxHeight()).thenReturn(5);
when(location.getBlock()).thenReturn(block);
when(location.distance(any())).thenReturn(320D);
when(block.getType()).thenReturn(Material.AIR);
when(block.isEmpty()).thenReturn(true);
when(world.getBlockAt(anyInt(), anyInt(), anyInt())).thenReturn(block);
@ -163,6 +170,7 @@ public class NewIslandTest {
// Bukkit Scheduler
PowerMockito.mockStatic(Bukkit.class);
when(Bukkit.getScheduler()).thenReturn(scheduler);
when(Bukkit.getViewDistance()).thenReturn(10);
// Addon
when(addon.getOverWorld()).thenReturn(world);
@ -250,12 +258,32 @@ public class NewIslandTest {
* {@link world.bentobox.bentobox.managers.island.NewIsland#builder()}.
*/
@Test
public void testBuilderNoOldIslandPaste() throws Exception {
public void testBuilderNoOldIslandPasteNoNMS() throws Exception {
when(location.distance(any())).thenReturn(30D);
NewIsland.builder().addon(addon).name(NAME).player(user).reason(Reason.CREATE).build();
// Verifications
verify(im).save(eq(island));
verify(island).setFlagsDefaults();
verify(bpm).paste(eq(addon), eq(island), eq(NAME), any(Runnable.class));
verify(bpm).paste(eq(addon), eq(island), eq(NAME), any(Runnable.class), eq(false));
verify(builder, times(2)).build();
verify(bpb).getUniqueId();
verify(ice).getBlueprintBundle();
verify(pm).setDeaths(eq(world), eq(uuid), eq(0));
verify(im, never()).setHomeLocation(eq(user), any());
}
/**
* Test method for
* {@link world.bentobox.bentobox.managers.island.NewIsland#builder()}.
*/
@Test
public void testBuilderNoOldIslandPasteWithNMS() throws Exception {
NewIsland.builder().addon(addon).name(NAME).player(user).reason(Reason.CREATE).build();
PowerMockito.mockStatic(Bukkit.class);
// Verifications
verify(im).save(eq(island));
verify(island).setFlagsDefaults();
verify(bpm).paste(eq(addon), eq(island), eq(NAME), any(Runnable.class), eq(true));
verify(builder, times(2)).build();
verify(bpb).getUniqueId();
verify(ice).getBlueprintBundle();