mirror of
https://github.com/BentoBoxWorld/BentoBox.git
synced 2025-01-25 09:31:46 +01:00
commit
ad009156e5
@ -25,7 +25,7 @@ public class AdminBlueprintPasteCommand extends CompositeCommand {
|
|||||||
AdminBlueprintCommand parent = (AdminBlueprintCommand) getParent();
|
AdminBlueprintCommand parent = (AdminBlueprintCommand) getParent();
|
||||||
BlueprintClipboard clipboard = parent.getClipboards().computeIfAbsent(user.getUniqueId(), v -> new BlueprintClipboard());
|
BlueprintClipboard clipboard = parent.getClipboards().computeIfAbsent(user.getUniqueId(), v -> new BlueprintClipboard());
|
||||||
if (clipboard.isFull()) {
|
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");
|
user.sendMessage("general.success");
|
||||||
parent.showClipboard(user);
|
parent.showClipboard(user);
|
||||||
});
|
});
|
||||||
|
@ -58,6 +58,7 @@ public class BlueprintPaster {
|
|||||||
|
|
||||||
private final BentoBox plugin;
|
private final BentoBox plugin;
|
||||||
private final PasteHandler paster = Util.getPasteHandler();
|
private final PasteHandler paster = Util.getPasteHandler();
|
||||||
|
private final PasteHandler fallback = new world.bentobox.bentobox.nms.fallback.PasteHandlerImpl();
|
||||||
private final World world;
|
private final World world;
|
||||||
// The minimum block position (x,y,z)
|
// The minimum block position (x,y,z)
|
||||||
private Location pos1;
|
private Location pos1;
|
||||||
@ -127,10 +128,20 @@ public class BlueprintPaster {
|
|||||||
Iterator<Entry<Vector, BlueprintBlock>> it2,
|
Iterator<Entry<Vector, BlueprintBlock>> it2,
|
||||||
Iterator<Entry<Vector, List<BlueprintEntity>>> it3,
|
Iterator<Entry<Vector, List<BlueprintEntity>>> it3,
|
||||||
int pasteSpeed) {}
|
int pasteSpeed) {}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The main pasting method
|
* The main pasting method
|
||||||
*/
|
*/
|
||||||
public CompletableFuture<Boolean> paste() {
|
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<>();
|
CompletableFuture<Boolean> result = new CompletableFuture<>();
|
||||||
// Iterators for the various maps to paste
|
// Iterators for the various maps to paste
|
||||||
final Map<Vector, BlueprintBlock> blocks = blueprint.getBlocks() == null ? Collections.emptyMap() : blueprint.getBlocks();
|
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,
|
Bits bits = new Bits(blocks, attached, entities,
|
||||||
blocks.entrySet().iterator(), attached.entrySet().iterator(), entities.entrySet().iterator(),
|
blocks.entrySet().iterator(), attached.entrySet().iterator(), entities.entrySet().iterator(),
|
||||||
plugin.getSettings().getPasteSpeed());
|
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;
|
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;
|
if (!currentTask.isDone()) return;
|
||||||
|
|
||||||
final int pasteSpeed = plugin.getSettings().getPasteSpeed();
|
final int pasteSpeed = plugin.getSettings().getPasteSpeed();
|
||||||
@ -163,10 +174,10 @@ public class BlueprintPaster {
|
|||||||
loadChunk();
|
loadChunk();
|
||||||
}
|
}
|
||||||
else if (pasteState.equals(PasteState.BLOCKS) || pasteState.equals(PasteState.ATTACHMENTS)) {
|
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)) {
|
else if (pasteState.equals(PasteState.ENTITIES)) {
|
||||||
pasteEntities(bits, count, owner, pasteSpeed);
|
pasteEntities(bits, count, owner, pasteSpeed, useNMS);
|
||||||
}
|
}
|
||||||
else if (pasteState.equals(PasteState.DONE)) {
|
else if (pasteState.equals(PasteState.DONE)) {
|
||||||
// All done. Cancel task
|
// All done. Cancel task
|
||||||
@ -188,7 +199,7 @@ public class BlueprintPaster {
|
|||||||
result.complete(true);
|
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()) {
|
if (bits.it3().hasNext()) {
|
||||||
Map<Location, List<BlueprintEntity>> entityMap = new HashMap<>();
|
Map<Location, List<BlueprintEntity>> entityMap = new HashMap<>();
|
||||||
// Paste entities
|
// Paste entities
|
||||||
@ -206,7 +217,8 @@ public class BlueprintPaster {
|
|||||||
count++;
|
count++;
|
||||||
}
|
}
|
||||||
if (!entityMap.isEmpty()) {
|
if (!entityMap.isEmpty()) {
|
||||||
currentTask = paster.pasteEntities(island, world, entityMap);
|
currentTask = useNMS ? paster.pasteEntities(island, world, entityMap)
|
||||||
|
: fallback.pasteEntities(island, world, entityMap);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
pasteState = PasteState.DONE;
|
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;
|
Iterator<Entry<Vector, BlueprintBlock>> it = pasteState.equals(PasteState.BLOCKS) ? bits.it : bits.it2;
|
||||||
if (it.hasNext()) {
|
if (it.hasNext()) {
|
||||||
Map<Location, BlueprintBlock> blockMap = new HashMap<>();
|
Map<Location, BlueprintBlock> blockMap = new HashMap<>();
|
||||||
@ -241,7 +253,8 @@ public class BlueprintPaster {
|
|||||||
count++;
|
count++;
|
||||||
}
|
}
|
||||||
if (!blockMap.isEmpty()) {
|
if (!blockMap.isEmpty()) {
|
||||||
currentTask = paster.pasteBlocks(island, world, blockMap);
|
currentTask = useNMS ? paster.pasteBlocks(island, world, blockMap)
|
||||||
|
: fallback.pasteBlocks(island, world, blockMap);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if (pasteState.equals(PasteState.BLOCKS)) {
|
if (pasteState.equals(PasteState.BLOCKS)) {
|
||||||
|
@ -445,7 +445,7 @@ public class BlueprintsManager {
|
|||||||
* @param name - bundle name
|
* @param name - bundle name
|
||||||
*/
|
*/
|
||||||
public void paste(GameModeAddon addon, Island island, String 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 island - the island
|
||||||
* @param name - name of bundle to paste
|
* @param name - name of bundle to paste
|
||||||
* @param task - task to run after pasting is completed
|
* @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
|
* @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) {
|
if (validate(addon, name) == null) {
|
||||||
plugin.logError("Tried to paste '" + name + "' but the bundle is not loaded!");
|
plugin.logError("Tried to paste '" + name + "' but the bundle is not loaded!");
|
||||||
return false;
|
return false;
|
||||||
@ -478,7 +479,9 @@ public class BlueprintsManager {
|
|||||||
}
|
}
|
||||||
// Paste
|
// Paste
|
||||||
if (bp != null) {
|
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))));
|
pasteEnd(addon, bb, island).thenAccept(message -> sendMessage(island)).thenAccept(b3 -> Bukkit.getScheduler().runTask(plugin, task))));
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
|
@ -206,8 +206,11 @@ public class NewIsland {
|
|||||||
if (noPaste) {
|
if (noPaste) {
|
||||||
Bukkit.getScheduler().runTask(plugin, () -> postCreationTask(oldIsland));
|
Bukkit.getScheduler().runTask(plugin, () -> postCreationTask(oldIsland));
|
||||||
} else {
|
} 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
|
// 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
|
// Set default settings
|
||||||
island.setFlagsDefaults();
|
island.setFlagsDefaults();
|
||||||
|
@ -20,6 +20,7 @@ import org.bukkit.Material;
|
|||||||
import org.bukkit.World;
|
import org.bukkit.World;
|
||||||
import org.bukkit.block.Block;
|
import org.bukkit.block.Block;
|
||||||
import org.bukkit.scheduler.BukkitScheduler;
|
import org.bukkit.scheduler.BukkitScheduler;
|
||||||
|
import org.eclipse.jdt.annotation.NonNull;
|
||||||
import org.junit.After;
|
import org.junit.After;
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Ignore;
|
import org.junit.Ignore;
|
||||||
@ -97,6 +98,8 @@ public class NewIslandTest {
|
|||||||
private final UUID uuid = UUID.randomUUID();
|
private final UUID uuid = UUID.randomUUID();
|
||||||
@Mock
|
@Mock
|
||||||
private BlueprintsManager bpm;
|
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.getPermissionValue(Mockito.anyString(), Mockito.anyInt())).thenReturn(20);
|
||||||
when(user.getUniqueId()).thenReturn(uuid);
|
when(user.getUniqueId()).thenReturn(uuid);
|
||||||
when(user.getName()).thenReturn("tastybento");
|
when(user.getName()).thenReturn("tastybento");
|
||||||
|
when(user.getWorld()).thenReturn(world);
|
||||||
|
when(user.getLocation()).thenReturn(location);
|
||||||
|
|
||||||
// Events
|
// Events
|
||||||
PowerMockito.mockStatic(IslandEvent.class);
|
PowerMockito.mockStatic(IslandEvent.class);
|
||||||
@ -147,9 +152,11 @@ public class NewIslandTest {
|
|||||||
|
|
||||||
// Location and blocks
|
// Location and blocks
|
||||||
when(island.getWorld()).thenReturn(world);
|
when(island.getWorld()).thenReturn(world);
|
||||||
|
when(island.getCenter()).thenReturn(location2);
|
||||||
when(location.getWorld()).thenReturn(world);
|
when(location.getWorld()).thenReturn(world);
|
||||||
when(world.getMaxHeight()).thenReturn(5);
|
when(world.getMaxHeight()).thenReturn(5);
|
||||||
when(location.getBlock()).thenReturn(block);
|
when(location.getBlock()).thenReturn(block);
|
||||||
|
when(location.distance(any())).thenReturn(320D);
|
||||||
when(block.getType()).thenReturn(Material.AIR);
|
when(block.getType()).thenReturn(Material.AIR);
|
||||||
when(block.isEmpty()).thenReturn(true);
|
when(block.isEmpty()).thenReturn(true);
|
||||||
when(world.getBlockAt(anyInt(), anyInt(), anyInt())).thenReturn(block);
|
when(world.getBlockAt(anyInt(), anyInt(), anyInt())).thenReturn(block);
|
||||||
@ -163,6 +170,7 @@ public class NewIslandTest {
|
|||||||
// Bukkit Scheduler
|
// Bukkit Scheduler
|
||||||
PowerMockito.mockStatic(Bukkit.class);
|
PowerMockito.mockStatic(Bukkit.class);
|
||||||
when(Bukkit.getScheduler()).thenReturn(scheduler);
|
when(Bukkit.getScheduler()).thenReturn(scheduler);
|
||||||
|
when(Bukkit.getViewDistance()).thenReturn(10);
|
||||||
|
|
||||||
// Addon
|
// Addon
|
||||||
when(addon.getOverWorld()).thenReturn(world);
|
when(addon.getOverWorld()).thenReturn(world);
|
||||||
@ -250,12 +258,32 @@ public class NewIslandTest {
|
|||||||
* {@link world.bentobox.bentobox.managers.island.NewIsland#builder()}.
|
* {@link world.bentobox.bentobox.managers.island.NewIsland#builder()}.
|
||||||
*/
|
*/
|
||||||
@Test
|
@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();
|
NewIsland.builder().addon(addon).name(NAME).player(user).reason(Reason.CREATE).build();
|
||||||
// Verifications
|
// Verifications
|
||||||
verify(im).save(eq(island));
|
verify(im).save(eq(island));
|
||||||
verify(island).setFlagsDefaults();
|
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(builder, times(2)).build();
|
||||||
verify(bpb).getUniqueId();
|
verify(bpb).getUniqueId();
|
||||||
verify(ice).getBlueprintBundle();
|
verify(ice).getBlueprintBundle();
|
||||||
|
Loading…
Reference in New Issue
Block a user