From c4a5eb2c887a29459e48f84bf2bdfdec1962bf97 Mon Sep 17 00:00:00 2001 From: tastybento Date: Sun, 30 Dec 2018 18:09:56 -0800 Subject: [PATCH] WIP pastes but attachments fall when split across paste boundaries --- .../world/bentobox/bentobox/Settings.java | 21 +++++ .../commands/island/IslandResetCommand.java | 1 + .../bentobox/managers/IslandsManager.java | 35 ++++----- .../bentobox/managers/island/NewIsland.java | 15 ++-- .../bentobox/bentobox/schems/Clipboard.java | 5 +- src/main/resources/config.yml | 3 + src/main/resources/locales/en-US.yml | 2 +- .../admin/AdminClearresetsallCommandTest.java | 1 + .../bentobox/schems/ClipboardTest.java | 77 +++++++++---------- 9 files changed, 92 insertions(+), 68 deletions(-) diff --git a/src/main/java/world/bentobox/bentobox/Settings.java b/src/main/java/world/bentobox/bentobox/Settings.java index ffacb0330..6eaa346da 100644 --- a/src/main/java/world/bentobox/bentobox/Settings.java +++ b/src/main/java/world/bentobox/bentobox/Settings.java @@ -146,6 +146,11 @@ public class Settings implements DataObject { @ConfigEntry(path = "island.name.max-length") private int nameMaxLength = 20; + @ConfigComment("Number of blocks to paste per tick when pasting a schem") + @ConfigComment("Smaller values will help reduce noticeable lag but will make pasting take longer") + @ConfigEntry(path = "island.paste-speed") + private int pasteSpeed = 1000; + // Ranks @ConfigEntry(path = "island.custom-ranks", experimental = true) private Map customRanks = new HashMap<>(); @@ -373,6 +378,20 @@ public class Settings implements DataObject { this.nameMaxLength = nameMaxLength; } + /** + * @param pasteSpeed the pasteSpeed to set + */ + public void setPasteSpeed(int pasteSpeed) { + this.pasteSpeed = pasteSpeed; + } + + /** + * @return paste speed in blocks per tick + */ + public int getPasteSpeed() { + return this.pasteSpeed; + } + public Map getCustomRanks() { return customRanks; } @@ -397,4 +416,6 @@ public class Settings implements DataObject { this.uniqueId = uniqueId; } + + } \ No newline at end of file diff --git a/src/main/java/world/bentobox/bentobox/api/commands/island/IslandResetCommand.java b/src/main/java/world/bentobox/bentobox/api/commands/island/IslandResetCommand.java index 0baf1bb6d..cfa819d85 100644 --- a/src/main/java/world/bentobox/bentobox/api/commands/island/IslandResetCommand.java +++ b/src/main/java/world/bentobox/bentobox/api/commands/island/IslandResetCommand.java @@ -90,6 +90,7 @@ public class IslandResetCommand extends ConfirmableCommand { // Reset the island Player player = user.getPlayer(); player.setGameMode(GameMode.SPECTATOR); + user.sendMessage("commands.island.create.creating-island"); // Get the player's old island Island oldIsland = getIslands().getIsland(getWorld(), player.getUniqueId()); // Remove them from this island (it still exists and will be deleted later) diff --git a/src/main/java/world/bentobox/bentobox/managers/IslandsManager.java b/src/main/java/world/bentobox/bentobox/managers/IslandsManager.java index ff7729d52..96923c628 100644 --- a/src/main/java/world/bentobox/bentobox/managers/IslandsManager.java +++ b/src/main/java/world/bentobox/bentobox/managers/IslandsManager.java @@ -564,6 +564,7 @@ public class IslandsManager { user.sendMessage("commands.island.go.teleported", TextVariables.NUMBER, String.valueOf(number)); } // Exit spectator mode if in it + if (player.getGameMode().equals(GameMode.SPECTATOR)) { player.setGameMode(plugin.getIWM().getDefaultGameMode(world)); } @@ -679,25 +680,23 @@ public class IslandsManager { * @param island to remove players from */ public void removePlayersFromIsland(Island island) { - // Teleport players away - for (Player player : Bukkit.getOnlinePlayers()) { - if (island.inIslandSpace(player.getLocation().getBlockX(), player.getLocation().getBlockZ())) { - // Teleport island players to their island home - if (hasIsland(island.getWorld(), player.getUniqueId()) || plugin.getIslands().inTeam(island.getWorld(), player.getUniqueId())) { - homeTeleport(island.getWorld(), player); + World w = island.getWorld(); + Bukkit.getOnlinePlayers().stream().filter(p -> island.onIsland(p.getLocation())).forEach(p -> { + // Teleport island players to their island home + if (!island.getMemberSet().contains(p.getUniqueId()) && (hasIsland(w, p.getUniqueId()) || inTeam(w, p.getUniqueId()))) { + homeTeleport(w, p); + } else { + // Move player to spawn + if (spawn.containsKey(w)) { + // go to island spawn + p.teleport(spawn.get(w).getSpawnPoint(w.getEnvironment())); } else { - // Move player to spawn - if (spawn.containsKey(island.getWorld())) { - // go to island spawn - player.teleport(spawn.get(island.getWorld()).getSpawnPoint(island.getWorld().getEnvironment())); - } else { - plugin.logWarning("During island deletion player " + player.getName() + " could not be sent home so was placed into spectator mode."); - player.setGameMode(GameMode.SPECTATOR); - player.getPlayer().setFlying(true); - } + plugin.logWarning("During island deletion player " + p.getName() + " could not be sent home so was placed into spectator mode."); + p.setGameMode(GameMode.SPECTATOR); + p.setFlying(true); } } - } + }); } /** @@ -797,13 +796,13 @@ public class IslandsManager { */ public void setOwner(User user, UUID targetUUID, Island island) { islandCache.setOwner(island, targetUUID); - user.sendMessage("commands.island.team.setowner.name-is-the-owner", "[name]", plugin.getPlayers().getName(targetUUID)); + user.sendMessage("commands.island.team.setowner.name-is-the-owner", "[name]", plugin.getPlayers().getName(targetUUID)); plugin.getIWM().getAddon(island.getWorld()).ifPresent(addon -> { User target = User.getInstance(targetUUID); // Tell target. If they are offline, then they may receive a message when they login target.sendMessage("commands.island.team.setowner.you-are-the-owner"); // Permission checks for range changes only work when the target is online - if (target.isOnline()) { + if (target.isOnline()) { // Check if new owner has a different range permission than the island size int range = target.getPermissionValue( addon.getPermissionPrefix() + "island.range", diff --git a/src/main/java/world/bentobox/bentobox/managers/island/NewIsland.java b/src/main/java/world/bentobox/bentobox/managers/island/NewIsland.java index c8fcb07bc..9c7ab9c86 100644 --- a/src/main/java/world/bentobox/bentobox/managers/island/NewIsland.java +++ b/src/main/java/world/bentobox/bentobox/managers/island/NewIsland.java @@ -46,11 +46,7 @@ public class NewIsland { this.reason = reason; this.world = world; this.name = name; - newIsland(); - if (oldIsland != null) { - // Delete the old island - plugin.getIslands().deleteIsland(oldIsland, true); - } + newIsland(oldIsland); } /** @@ -121,8 +117,9 @@ public class NewIsland { /** * Makes an island. + * @param oldIsland */ - public void newIsland() { + public void newIsland(Island oldIsland) { Location next = getNextIsland(); if (next == null) { plugin.logError("Failed to make island - no unoccupied spot found"); @@ -165,6 +162,12 @@ public class NewIsland { // Teleport player after this island is built plugin.getIslands().homeTeleport(world, user.getPlayer(), true); + + // Delete old island + if (oldIsland != null) { + // Delete the old island + plugin.getIslands().deleteIsland(oldIsland, true); + } }); // Make nether island if (plugin.getIWM().isNetherGenerate(world) && plugin.getIWM().isNetherIslands(world) && plugin.getIWM().getNetherWorld(world) != null) { diff --git a/src/main/java/world/bentobox/bentobox/schems/Clipboard.java b/src/main/java/world/bentobox/bentobox/schems/Clipboard.java index 6264571cd..a0db08086 100644 --- a/src/main/java/world/bentobox/bentobox/schems/Clipboard.java +++ b/src/main/java/world/bentobox/bentobox/schems/Clipboard.java @@ -58,7 +58,7 @@ import world.bentobox.bentobox.util.Util; public class Clipboard { // Speed of pasting - private static final int BLOCKS_PER_TICK = Integer.MAX_VALUE; + private int pasteSpeed = 200; // Commonly used texts along this class. private static final String ATTACHED = "attached"; @@ -87,6 +87,7 @@ public class Clipboard { schemFolder.mkdirs(); } this.schemFolder = schemFolder; + pasteSpeed = plugin.getSettings().getPasteSpeed(); } /** @@ -198,7 +199,7 @@ public class Clipboard { Iterator it = blockConfig.getConfigurationSection(BLOCK).getKeys(false).iterator(); pastingTask = Bukkit.getScheduler().runTaskTimer(plugin, () -> { int count = 0; - while (count < BLOCKS_PER_TICK && it.hasNext()) { + while (count < pasteSpeed && it.hasNext()) { pasteBlock(world, island, loc, blockConfig.getConfigurationSection(BLOCK + "." + it.next())); count++; } diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index 65179082b..f392ccfba 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -75,6 +75,9 @@ island: # These set the minimum and maximum size of a name. min-length: 4 max-length: 20 + # Number of blocks to paste per tick when pasting a schem + # Smaller values will help reduce noticeable lag but will make pasting take longer + paste-speed: 1000 # /!\ This feature is experimental and might not work as expected or might not work at all. custom-ranks: {} # These settings should not be edited diff --git a/src/main/resources/locales/en-US.yml b/src/main/resources/locales/en-US.yml index 2d4e8866e..b8f5f1909 100644 --- a/src/main/resources/locales/en-US.yml +++ b/src/main/resources/locales/en-US.yml @@ -246,7 +246,7 @@ commands: parameters: "" too-many-islands: "&cThere are too many islands in this world: there isn't enough room for yours to be created." unable-create-island: "&cYour island could not be generated, please contact an administrator." - creating-island: "&aCreating your island..." + creating-island: "&aCreating your island, please wait a moment..." pick-world: "&cPick a world from [worlds]." unknown-schem: "&cThat schem has not been loaded yet." info: diff --git a/src/test/java/world/bentobox/bentobox/api/commands/admin/AdminClearresetsallCommandTest.java b/src/test/java/world/bentobox/bentobox/api/commands/admin/AdminClearresetsallCommandTest.java index c7e5e9417..c1c24cad1 100644 --- a/src/test/java/world/bentobox/bentobox/api/commands/admin/AdminClearresetsallCommandTest.java +++ b/src/test/java/world/bentobox/bentobox/api/commands/admin/AdminClearresetsallCommandTest.java @@ -108,6 +108,7 @@ public class AdminClearresetsallCommandTest { /** * Test method for . */ + @SuppressWarnings("deprecation") @Test public void testExecuteCheckConfirm() { AdminClearresetsallCommand itl = new AdminClearresetsallCommand(ac); diff --git a/src/test/java/world/bentobox/bentobox/schems/ClipboardTest.java b/src/test/java/world/bentobox/bentobox/schems/ClipboardTest.java index 056809897..781333f3d 100644 --- a/src/test/java/world/bentobox/bentobox/schems/ClipboardTest.java +++ b/src/test/java/world/bentobox/bentobox/schems/ClipboardTest.java @@ -47,6 +47,7 @@ import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.modules.junit4.PowerMockRunner; import world.bentobox.bentobox.BentoBox; +import world.bentobox.bentobox.Settings; import world.bentobox.bentobox.api.localization.TextVariables; import world.bentobox.bentobox.api.user.User; import world.bentobox.bentobox.database.objects.Island; @@ -76,7 +77,7 @@ public class ClipboardTest { block = mock(Block.class); when(block.getType()).thenReturn(Material.GRASS); when(block.getLocation()).thenReturn(loc); - + BlockData bd = mock(BlockData.class); when(bd.getAsString()).thenReturn("Block_data"); when(block.getBlockData()).thenReturn(bd); @@ -87,7 +88,7 @@ public class ClipboardTest { when(loc.getBlockZ()).thenReturn(3); when(loc.getBlock()).thenReturn(block); when(loc.toVector()).thenReturn(new Vector(1,2,3)); - + loc2 = mock(Location.class); when(loc2.getWorld()).thenReturn(world); when(loc2.getBlockX()).thenReturn(2); @@ -95,7 +96,7 @@ public class ClipboardTest { when(loc2.getBlockZ()).thenReturn(4); when(loc2.getBlock()).thenReturn(block); // Living entities - + List ents = new ArrayList<>(); Pig pig = mock(Pig.class); Player player = mock(Player.class); @@ -109,7 +110,7 @@ public class ClipboardTest { when(player.getLocation()).thenReturn(loc); when(sheep.getLocation()).thenReturn(loc); when(horse.getLocation()).thenReturn(loc); - + when(pig.getType()).thenReturn(EntityType.PIG); when(player.getType()).thenReturn(EntityType.PLAYER); when(cow.getType()).thenReturn(EntityType.COW); @@ -122,7 +123,7 @@ public class ClipboardTest { HorseInventory inv = mock(HorseInventory.class); when(horse.getInventory()).thenReturn(inv); - + // UUIDs (I'm going to assume these will all be unique (prays to god of randomness) when(creeper.getUniqueId()).thenReturn(UUID.randomUUID()); when(player.getUniqueId()).thenReturn(UUID.randomUUID()); @@ -138,29 +139,34 @@ public class ClipboardTest { ents.add(sheep); ents.add(horse); when(world.getLivingEntities()).thenReturn(ents); - + user = mock(User.class); User.setPlugin(plugin); when(user.getLocation()).thenReturn(loc); - + // Scheduler PowerMockito.mockStatic(Bukkit.class); sched = mock(BukkitScheduler.class); when(Bukkit.getScheduler()).thenReturn(sched); - + + // Settings + Settings settings = mock(Settings.class); + when(settings.getPasteSpeed()).thenReturn(200); + when(plugin.getSettings()).thenReturn(settings); + } @Test public void testClipboard() { when(schemFolder.exists()).thenReturn(false); - new Clipboard(plugin, schemFolder); + new Clipboard(plugin, schemFolder); Mockito.verify(schemFolder).mkdirs(); } @Test public void testSetGetPos1() { - Clipboard cb = new Clipboard(plugin, schemFolder); + Clipboard cb = new Clipboard(plugin, schemFolder); assertNull(cb.getPos1()); cb.setPos1(loc); assertEquals(loc, cb.getPos1()); @@ -169,7 +175,7 @@ public class ClipboardTest { @Test public void testSetGetPos2() { - Clipboard cb = new Clipboard(plugin, schemFolder); + Clipboard cb = new Clipboard(plugin, schemFolder); assertNull(cb.getPos2()); cb.setPos2(loc); assertEquals(loc, cb.getPos2()); @@ -177,7 +183,7 @@ public class ClipboardTest { @Test public void testSetGetOrigin() { - Clipboard cb = new Clipboard(plugin, schemFolder); + Clipboard cb = new Clipboard(plugin, schemFolder); assertNull(cb.getOrigin()); cb.setOrigin(loc); assertEquals(loc, cb.getOrigin()); @@ -185,28 +191,28 @@ public class ClipboardTest { @Test public void testCopyNoPos1Pos2() { - Clipboard cb = new Clipboard(plugin, schemFolder); + Clipboard cb = new Clipboard(plugin, schemFolder); cb.copy(user, false); Mockito.verify(user).sendMessage(Mockito.eq("commands.admin.schem.need-pos1-pos2")); } - + @Test public void testCopyNoPos2() { - Clipboard cb = new Clipboard(plugin, schemFolder); + Clipboard cb = new Clipboard(plugin, schemFolder); cb.setPos1(loc); cb.copy(user, false); Mockito.verify(user).sendMessage(Mockito.eq("commands.admin.schem.need-pos1-pos2")); } - + @Test public void testCopy() { - Clipboard cb = new Clipboard(plugin, schemFolder); + Clipboard cb = new Clipboard(plugin, schemFolder); cb.setPos1(loc); cb.setPos2(loc2); cb.copy(user, false); Mockito.verify(user).sendMessage("commands.admin.schem.copied-blocks", TextVariables.NUMBER, "8"); } - + @Test public void testCopySigns() { when(block.getType()).thenReturn(Material.SIGN); @@ -214,7 +220,7 @@ public class ClipboardTest { String[] lines = {"line1", "line2", "line3", "line4"}; when(bs.getLines()).thenReturn(lines); when(block.getState()).thenReturn(bs); - Clipboard cb = new Clipboard(plugin, schemFolder); + Clipboard cb = new Clipboard(plugin, schemFolder); cb.setPos1(loc); cb.setPos2(loc2); cb.copy(user, false); @@ -232,7 +238,7 @@ public class ClipboardTest { when(inv.getContents()).thenReturn(contents); when(bs.getInventory()).thenReturn(inv); when(block.getState()).thenReturn(bs); - Clipboard cb = new Clipboard(plugin, schemFolder); + Clipboard cb = new Clipboard(plugin, schemFolder); cb.setPos1(loc); cb.setPos2(loc2); cb.copy(user, false); @@ -240,14 +246,14 @@ public class ClipboardTest { // Every block is a sign, so this should be called 8 times Mockito.verify(bs, Mockito.times(8)).getInventory(); } - + @Test public void testCopyCreatureSpawners() { when(block.getType()).thenReturn(Material.SPAWNER); CreatureSpawner bs = mock(CreatureSpawner.class); when(bs.getSpawnedType()).thenReturn(EntityType.CAVE_SPIDER); when(block.getState()).thenReturn(bs); - Clipboard cb = new Clipboard(plugin, schemFolder); + Clipboard cb = new Clipboard(plugin, schemFolder); cb.setPos1(loc); cb.setPos2(loc2); cb.copy(user, false); @@ -255,7 +261,7 @@ public class ClipboardTest { // Every block is a sign, so this should be called 8 times Mockito.verify(bs, Mockito.times(8)).getMaxNearbyEntities(); } - + @Test public void testCopyAir() { // No entities @@ -263,7 +269,7 @@ public class ClipboardTest { when(block.getType()).thenReturn(Material.AIR); BlockState bs = mock(BlockState.class); when(block.getState()).thenReturn(bs); - Clipboard cb = new Clipboard(plugin, schemFolder); + Clipboard cb = new Clipboard(plugin, schemFolder); cb.setPos1(loc); cb.setPos2(loc2); // Do not copy air @@ -273,7 +279,7 @@ public class ClipboardTest { Mockito.verify(user).sendMessage("commands.admin.schem.copied-blocks", TextVariables.NUMBER, "8"); } - + @Test public void testPasteIslandNoData() { Clipboard cb = new Clipboard(plugin, schemFolder); @@ -281,10 +287,10 @@ public class ClipboardTest { when(island.getCenter()).thenReturn(loc); cb.pasteIsland(world, island, () -> {}); Mockito.verify(plugin).logError(Mockito.eq("Clipboard has no block data in it to paste!")); - // Verify the task is run - Mockito.verify(sched).runTaskLater(Mockito.eq(plugin), Mockito.any(Runnable.class), Mockito.eq(2L)); + // Verify the task is run + Mockito.verify(sched, Mockito.never()).runTaskTimer(Mockito.any(), Mockito.any(Runnable.class), Mockito.eq(0L), Mockito.eq(1L)); } - + @Test public void testPasteIslandWithData() { Clipboard cb = new Clipboard(plugin, schemFolder); @@ -294,19 +300,8 @@ public class ClipboardTest { cb.setPos2(loc2); cb.copy(user, false); cb.pasteIsland(world, island, () -> {}); - // This is set just once because the coords of the block are always the same - Mockito.verify(block).setBlockData(Mockito.any()); - // Verify the entities are spawned - Mockito.verify(world).spawnEntity(Mockito.eq(null), Mockito.eq(EntityType.PIG)); - Mockito.verify(world).spawnEntity(Mockito.eq(null), Mockito.eq(EntityType.CREEPER)); - Mockito.verify(world).spawnEntity(Mockito.eq(null), Mockito.eq(EntityType.HORSE)); - Mockito.verify(world).spawnEntity(Mockito.eq(null), Mockito.eq(EntityType.SHEEP)); - Mockito.verify(world).spawnEntity(Mockito.eq(null), Mockito.eq(EntityType.COW)); - // Player should NOT spawn!! - Mockito.verify(world, Mockito.never()).spawnEntity(Mockito.eq(null), Mockito.eq(EntityType.PLAYER)); - // Verify the task is run - Mockito.verify(sched).runTaskLater(Mockito.eq(plugin), Mockito.any(Runnable.class), Mockito.eq(2L)); + Mockito.verify(sched).runTaskTimer(Mockito.any(), Mockito.any(Runnable.class), Mockito.eq(0L), Mockito.eq(1L)); } @Test @@ -315,7 +310,7 @@ public class ClipboardTest { cb.pasteClipboard(loc); Mockito.verify(plugin).logError(Mockito.eq("Clipboard has no block data in it to paste!")); } - + @Test public void testPasteClipboard() { Clipboard cb = new Clipboard(plugin, schemFolder);