|
1 | 1 | package org.mvplugins.multiverse.inventories.handleshare |
2 | 2 |
|
| 3 | +import org.bukkit.Bukkit |
| 4 | +import org.bukkit.Location |
3 | 5 | import org.bukkit.Material |
4 | 6 | import org.bukkit.inventory.ItemStack |
5 | | -import org.junit.jupiter.api.Assertions.assertEquals |
6 | 7 | import org.mvplugins.multiverse.core.world.WorldManager |
7 | 8 | import org.mvplugins.multiverse.core.world.options.CreateWorldOptions |
8 | 9 | import org.mvplugins.multiverse.inventories.TestWithMockBukkit |
9 | 10 | import org.mvplugins.multiverse.inventories.config.InventoriesConfig |
| 11 | +import org.mvplugins.multiverse.inventories.share.Sharables |
| 12 | +import org.mvplugins.multiverse.inventories.util.PlayerStats |
| 13 | +import kotlin.arrayOfNulls |
10 | 14 | import kotlin.test.BeforeTest |
11 | 15 | import kotlin.test.Test |
12 | | -import kotlin.test.assertNotEquals |
| 16 | +import kotlin.test.assertEquals |
13 | 17 | import kotlin.test.assertTrue |
14 | 18 |
|
15 | 19 | class GameModeChangeTest : TestWithMockBukkit() { |
16 | 20 |
|
| 21 | + private lateinit var inventoriesConfig: InventoriesConfig |
| 22 | + private var survivalItems = arrayOfNulls<ItemStack>(PlayerStats.INVENTORY_SIZE) |
| 23 | + private var creativeItems = arrayOfNulls<ItemStack>(PlayerStats.INVENTORY_SIZE) |
| 24 | + |
17 | 25 | @BeforeTest |
18 | 26 | fun setUp() { |
| 27 | + inventoriesConfig = serviceLocator.getActiveService(InventoriesConfig::class.java).takeIf { it != null } ?: run { |
| 28 | + throw IllegalStateException("InventoriesConfig is not available as a service") |
| 29 | + } |
19 | 30 | val worldManager = serviceLocator.getActiveService(WorldManager::class.java).takeIf { it != null } ?: run { |
20 | 31 | throw IllegalStateException("WorldManager is not available as a service") |
21 | 32 | } |
22 | | - assertTrue(worldManager.createWorld(CreateWorldOptions.worldName("world")).isSuccess) |
23 | | - val inventoriesConfig = serviceLocator.getActiveService(InventoriesConfig::class.java).takeIf { it != null } ?: run { |
24 | | - throw IllegalStateException("InventoriesConfig is not available as a service") |
25 | | - } |
| 33 | + |
26 | 34 | writeResourceToConfigFile("/gameplay/gamemode_change_groups.yml", "groups.yml") |
27 | 35 | multiverseInventories.reloadConfig() |
28 | | - inventoriesConfig.setEnableGamemodeShareHandling(true) |
| 36 | + inventoriesConfig.enableGamemodeShareHandling = true |
| 37 | + |
| 38 | + survivalItems[0] = ItemStack.of(Material.STONE_BRICKS, 64) |
| 39 | + survivalItems[1] = ItemStack.of(Material.SAND, 64) |
| 40 | + |
| 41 | + creativeItems[0] = ItemStack.of(Material.HOPPER, 64) |
| 42 | + creativeItems[1] = ItemStack.of(Material.CAMPFIRE, 64) |
| 43 | + |
| 44 | + assertTrue(worldManager.createWorld(CreateWorldOptions.worldName("world")).isSuccess) |
| 45 | + assertTrue(worldManager.createWorld(CreateWorldOptions.worldName("ungrouped")).isSuccess) |
| 46 | + } |
| 47 | + |
| 48 | + @Test |
| 49 | + fun `Test change game mode for grouped worlds with total_xp disabled share`() { |
| 50 | + val player = server.addPlayer("Benji_0224") |
| 51 | + assertTrue(player.teleport(Bukkit.getWorld("world")!!.spawnLocation)) |
| 52 | + |
| 53 | + player.inventory.contents = survivalItems.clone() |
| 54 | + player.totalExperience = 123 |
| 55 | + |
| 56 | + player.gameMode = org.bukkit.GameMode.CREATIVE |
| 57 | + Thread.sleep(5) |
| 58 | + assertInventoryEquals(arrayOfNulls(PlayerStats.INVENTORY_SIZE), player.inventory.contents) |
| 59 | + assertEquals(123, player.totalExperience) |
| 60 | + |
| 61 | + player.inventory.contents = creativeItems.clone() |
| 62 | + player.totalExperience = 321 |
| 63 | + |
| 64 | + player.gameMode = org.bukkit.GameMode.SURVIVAL |
| 65 | + Thread.sleep(5) |
| 66 | + assertInventoryEquals(survivalItems, player.inventory.contents) |
| 67 | + assertEquals(321, player.totalExperience) |
| 68 | + |
| 69 | + player.gameMode = org.bukkit.GameMode.CREATIVE |
| 70 | + Thread.sleep(5) |
| 71 | + assertInventoryEquals(creativeItems, player.inventory.contents) |
| 72 | + assertEquals(321, player.totalExperience) |
29 | 73 | } |
30 | 74 |
|
31 | 75 | @Test |
32 | | - fun `Test change game mode`() { |
| 76 | + fun `Test change game mode for ungrouped worlds`() { |
33 | 77 | val player = server.addPlayer("Benji_0224") |
34 | | - val survivalItems = arrayOf( |
35 | | - ItemStack.of(Material.STONE_BRICKS, 64), |
36 | | - ItemStack.of(Material.SAND, 64), |
37 | | - ) |
38 | | - player.inventory.contents = survivalItems |
| 78 | + assertTrue(player.teleport(Bukkit.getWorld("ungrouped")!!.spawnLocation)) |
| 79 | + |
| 80 | + player.inventory.contents = survivalItems.clone() |
| 81 | + player.totalExperience = 123 |
39 | 82 |
|
40 | 83 | player.gameMode = org.bukkit.GameMode.CREATIVE |
41 | | - Thread.sleep(10) |
42 | | - assertNotEquals(survivalItems[0], player.inventory.getItem(0)) |
43 | | - assertNotEquals(survivalItems[1], player.inventory.getItem(1)) |
44 | | - val creativeItems = arrayOf( |
45 | | - ItemStack.of(Material.OBSIDIAN, 64), |
46 | | - ItemStack.of(Material.HOPPER, 64), |
47 | | - ) |
48 | | - player.inventory.contents = creativeItems |
| 84 | + Thread.sleep(5) |
| 85 | + assertInventoryEquals(arrayOfNulls(PlayerStats.INVENTORY_SIZE), player.inventory.contents) |
| 86 | + assertEquals(0, player.totalExperience) |
| 87 | + |
| 88 | + player.inventory.contents = creativeItems.clone() |
| 89 | + player.totalExperience = 321 |
| 90 | + |
| 91 | + player.gameMode = org.bukkit.GameMode.SURVIVAL |
| 92 | + Thread.sleep(5) |
| 93 | + assertInventoryEquals(survivalItems, player.inventory.contents) |
| 94 | + assertEquals(123, player.totalExperience) |
| 95 | + |
| 96 | + player.gameMode = org.bukkit.GameMode.CREATIVE |
| 97 | + Thread.sleep(5) |
| 98 | + assertInventoryEquals(creativeItems, player.inventory.contents) |
| 99 | + assertEquals(321, player.totalExperience) |
| 100 | + } |
| 101 | + |
| 102 | + @Test |
| 103 | + fun `Test change game mode for ungrouped worlds with last_location`() { |
| 104 | + inventoriesConfig.defaultUngroupedWorlds = false |
| 105 | + inventoriesConfig.useOptionalsForUngroupedWorlds = true |
| 106 | + inventoriesConfig.activeOptionalShares = Sharables.fromSharables(Sharables.LAST_LOCATION) |
| 107 | + |
| 108 | + val survivalLocation = Location(Bukkit.getWorld("ungrouped")!!, 1.0, 2.0, 3.0) |
| 109 | + val creativeLocation = Location(Bukkit.getWorld("ungrouped")!!, 4.0, 5.0, 6.0) |
| 110 | + |
| 111 | + val player = server.addPlayer("Benji_0224") |
| 112 | + assertTrue(player.teleport(survivalLocation.clone())) |
| 113 | + player.inventory.contents = survivalItems.clone() |
| 114 | + |
| 115 | + player.gameMode = org.bukkit.GameMode.CREATIVE |
| 116 | + assertInventoryEquals(arrayOfNulls(PlayerStats.INVENTORY_SIZE), player.inventory.contents) |
| 117 | + assertLocationEquals(survivalLocation, player.location) |
| 118 | + |
| 119 | + assertTrue(player.teleport(creativeLocation.clone())) |
| 120 | + player.inventory.contents = creativeItems.clone() |
| 121 | + |
| 122 | + player.gameMode = org.bukkit.GameMode.SURVIVAL |
| 123 | + assertInventoryEquals(survivalItems, player.inventory.contents) |
| 124 | + assertLocationEquals(survivalLocation, player.location) |
| 125 | + |
| 126 | + player.gameMode = org.bukkit.GameMode.CREATIVE |
| 127 | + assertInventoryEquals(creativeItems, player.inventory.contents) |
| 128 | + assertLocationEquals(creativeLocation, player.location) |
| 129 | + } |
| 130 | + |
| 131 | + @Test |
| 132 | + fun `Test change game mode for ungrouped worlds with useOptionalsForUngroupedWorlds disabled`() { |
| 133 | + inventoriesConfig.defaultUngroupedWorlds = false |
| 134 | + inventoriesConfig.useOptionalsForUngroupedWorlds = false |
| 135 | + inventoriesConfig.activeOptionalShares = Sharables.fromSharables(Sharables.LAST_LOCATION) |
| 136 | + |
| 137 | + val survivalLocation = Location(Bukkit.getWorld("ungrouped")!!, 1.0, 2.0, 3.0) |
| 138 | + val creativeLocation = Location(Bukkit.getWorld("ungrouped")!!, 4.0, 5.0, 6.0) |
| 139 | + |
| 140 | + val player = server.addPlayer("Benji_0224") |
| 141 | + assertTrue(player.teleport(survivalLocation.clone())) |
| 142 | + player.inventory.contents = survivalItems.clone() |
| 143 | + |
| 144 | + player.gameMode = org.bukkit.GameMode.CREATIVE |
| 145 | + Thread.sleep(5) |
| 146 | + assertInventoryEquals(arrayOfNulls(PlayerStats.INVENTORY_SIZE), player.inventory.contents) |
| 147 | + assertLocationEquals(survivalLocation, player.location) |
| 148 | + |
| 149 | + assertTrue(player.teleport(creativeLocation.clone())) |
| 150 | + player.inventory.contents = creativeItems.clone() |
49 | 151 |
|
50 | 152 | player.gameMode = org.bukkit.GameMode.SURVIVAL |
51 | | - Thread.sleep(10) |
52 | | - assertEquals(survivalItems[0], player.inventory.getItem(0)) |
53 | | - assertEquals(survivalItems[1], player.inventory.getItem(1)) |
54 | | - assertNotEquals(creativeItems[0], player.inventory.getItem(0)) |
55 | | - assertNotEquals(creativeItems[1], player.inventory.getItem(1)) |
| 153 | + Thread.sleep(5) |
| 154 | + assertInventoryEquals(survivalItems, player.inventory.contents) |
| 155 | + assertLocationEquals(creativeLocation, player.location) |
56 | 156 |
|
57 | 157 | player.gameMode = org.bukkit.GameMode.CREATIVE |
58 | | - Thread.sleep(10) |
59 | | - assertEquals(creativeItems[0], player.inventory.getItem(0)) |
60 | | - assertEquals(creativeItems[1], player.inventory.getItem(1)) |
61 | | - assertNotEquals(survivalItems[0], player.inventory.getItem(0)) |
62 | | - assertNotEquals(survivalItems[1], player.inventory.getItem(1)) |
| 158 | + Thread.sleep(5) |
| 159 | + assertInventoryEquals(creativeItems, player.inventory.contents) |
| 160 | + assertLocationEquals(creativeLocation, player.location) |
63 | 161 | } |
64 | 162 | } |
0 commit comments