From d28f4b9880e1d45b80101707e873d08bfcd20122 Mon Sep 17 00:00:00 2001 From: TomTom <93038247+AverageGithub@users.noreply.github.com> Date: Sat, 29 Jun 2024 10:25:15 +0200 Subject: [PATCH] Island recalculate --- .../axminions/api/config/Messages.kt | 2 + .../axminions/api/data/DataHandler.kt | 2 + .../axminions/commands/AxMinionsCommand.kt | 40 +++++++++---------- .../axminions/data/H2DataHandler.kt | 9 +++++ .../listeners/SuperiorSkyBlock2Listener.kt | 12 ++++-- common/src/main/resources/messages.yml | 4 +- 6 files changed, 43 insertions(+), 26 deletions(-) diff --git a/api/src/main/kotlin/com/artillexstudios/axminions/api/config/Messages.kt b/api/src/main/kotlin/com/artillexstudios/axminions/api/config/Messages.kt index 1f20cef..da06537 100644 --- a/api/src/main/kotlin/com/artillexstudios/axminions/api/config/Messages.kt +++ b/api/src/main/kotlin/com/artillexstudios/axminions/api/config/Messages.kt @@ -78,6 +78,8 @@ class Messages(file: File, stream: InputStream) { @JvmStatic fun UPGRADE_FAIL() = AxMinionsAPI.INSTANCE.getMessages().get("upgrades.fail") @JvmStatic + fun RECALC() = AxMinionsAPI.INSTANCE.getMessages().get("recalc") + @JvmStatic fun LOCATION_FORMAT() = AxMinionsAPI.INSTANCE.getMessages().get("location-format") } diff --git a/api/src/main/kotlin/com/artillexstudios/axminions/api/data/DataHandler.kt b/api/src/main/kotlin/com/artillexstudios/axminions/api/data/DataHandler.kt index 11464c4..95bf555 100644 --- a/api/src/main/kotlin/com/artillexstudios/axminions/api/data/DataHandler.kt +++ b/api/src/main/kotlin/com/artillexstudios/axminions/api/data/DataHandler.kt @@ -34,6 +34,8 @@ interface DataHandler { fun islandBreak(island: String) + fun islandReset(island: String) + fun getIsland(island: String): Int fun disable() diff --git a/common/src/main/kotlin/com/artillexstudios/axminions/commands/AxMinionsCommand.kt b/common/src/main/kotlin/com/artillexstudios/axminions/commands/AxMinionsCommand.kt index 36da72a..099052f 100644 --- a/common/src/main/kotlin/com/artillexstudios/axminions/commands/AxMinionsCommand.kt +++ b/common/src/main/kotlin/com/artillexstudios/axminions/commands/AxMinionsCommand.kt @@ -13,19 +13,14 @@ import com.artillexstudios.axminions.converter.LitMinionsConverter import com.artillexstudios.axminions.integrations.island.SuperiorSkyBlock2Integration import com.artillexstudios.axminions.minions.Minions import com.bgsoftware.superiorskyblock.api.SuperiorSkyblockAPI -import net.kyori.adventure.platform.bukkit.BukkitAudiences -import net.kyori.adventure.text.minimessage.MiniMessage import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder +import org.bukkit.Chunk import org.bukkit.World.Environment import org.bukkit.command.CommandSender import org.bukkit.entity.Player -import revxrsal.commands.annotation.AutoComplete -import revxrsal.commands.annotation.Command -import revxrsal.commands.annotation.Default -import revxrsal.commands.annotation.Description -import revxrsal.commands.annotation.Range -import revxrsal.commands.annotation.Subcommand +import revxrsal.commands.annotation.* import revxrsal.commands.bukkit.annotation.CommandPermission +import java.util.concurrent.CompletableFuture @Command("axminions", "minion", "minions") class AxMinionsCommand { @@ -143,32 +138,35 @@ class AxMinionsCommand { fun recalc(player: Player) { val islandId = AxMinionsAPI.INSTANCE.getIntegrations().getIslandIntegration()!!.getIslandAt(player.location) AxMinionsPlugin.dataQueue.submit { + var original = 0 if (islandId.isNotBlank()) { - val islandPlaced = AxMinionsAPI.INSTANCE.getDataHandler().getIsland(islandId) - if (Config.DEBUG()) { - player.sendMessage("Placed: $islandPlaced") - } + original = AxMinionsAPI.INSTANCE.getDataHandler().getIsland(islandId) - for (i in islandPlaced downTo 0) { - AxMinionsPlugin.dataHandler.islandBreak(islandId) - } + AxMinionsPlugin.dataHandler.islandReset(islandId) } val integration = AxMinionsAPI.INSTANCE.getIntegrations().getIslandIntegration() if (integration is SuperiorSkyBlock2Integration) { - val island = SuperiorSkyblockAPI.getIslandAt(player.location) - if (island == null) return@submit + val island = SuperiorSkyblockAPI.getIslandAt(player.location) ?: return@submit val minions = Minions.getMinions() Environment.entries.forEach { entry -> try { - island.getAllChunksAsync(entry, true) { }.forEach { chunk -> - minions.forEach { minion -> - if (minion.getLocation().chunk == chunk) { - AxMinionsPlugin.dataHandler.islandPlace(islandId) + val futures = arrayListOf>() + island.getAllChunksAsync(entry, true) { }.forEach { future -> + futures.add(future) + future.thenAccept { chunk -> + minions.forEach { minion -> + val ch = minion.getLocation().chunk + if (ch.x == chunk.x && ch.z == chunk.z && ch.world == chunk.world) { + AxMinionsPlugin.dataHandler.islandPlace(islandId) + } } } } + CompletableFuture.allOf(*futures.toTypedArray()).thenRun { + player.sendMessage(StringUtils.formatToString(Messages.PREFIX() + Messages.RECALC(), Placeholder.unparsed("from", original.toString()), Placeholder.unparsed("to", futures.size.toString()))) + } } catch (_: NullPointerException) { // SuperiorSkyBlock api does it this way aswell } diff --git a/common/src/main/kotlin/com/artillexstudios/axminions/data/H2DataHandler.kt b/common/src/main/kotlin/com/artillexstudios/axminions/data/H2DataHandler.kt index bdb7ebb..7ca08ac 100644 --- a/common/src/main/kotlin/com/artillexstudios/axminions/data/H2DataHandler.kt +++ b/common/src/main/kotlin/com/artillexstudios/axminions/data/H2DataHandler.kt @@ -385,6 +385,15 @@ class H2DataHandler : DataHandler { } } + override fun islandReset(island: String) { + dataSource.connection.use { connection -> + connection.prepareStatement("UPDATE `axminions_island_counter` SET `placed` = 0 WHERE `island` = ?;").use { statement -> + statement.setString(1, island) + statement.executeUpdate() + } + } + } + override fun islandBreak(island: String) { dataSource.connection.use { connection -> connection.prepareStatement("UPDATE `axminions_island_counter` SET `placed` = `placed` - 1 WHERE `island` = ?;").use { statement -> diff --git a/common/src/main/kotlin/com/artillexstudios/axminions/listeners/SuperiorSkyBlock2Listener.kt b/common/src/main/kotlin/com/artillexstudios/axminions/listeners/SuperiorSkyBlock2Listener.kt index 68215df..0f4f0ec 100644 --- a/common/src/main/kotlin/com/artillexstudios/axminions/listeners/SuperiorSkyBlock2Listener.kt +++ b/common/src/main/kotlin/com/artillexstudios/axminions/listeners/SuperiorSkyBlock2Listener.kt @@ -5,6 +5,7 @@ import com.bgsoftware.superiorskyblock.api.events.IslandDisbandEvent import org.bukkit.World.Environment import org.bukkit.event.EventHandler import org.bukkit.event.Listener +import kotlin.math.min class SuperiorSkyBlock2Listener : Listener { @@ -14,10 +15,13 @@ class SuperiorSkyBlock2Listener : Listener { Environment.entries.forEach { entry -> try { - event.island.getAllChunksAsync(entry, true) { }.forEach { chunk -> - minions.forEach { minion -> - if (minion.getLocation().chunk == chunk) { - minion.remove() + event.island.getAllChunksAsync(entry, true) { }.forEach { future -> + future.thenAccept { chunk -> + minions.forEach { minion -> + val ch = minion.getLocation().chunk + if (ch.x == chunk.x && ch.z == chunk.z && ch.world == chunk.world) { + minion.remove() + } } } } diff --git a/common/src/main/resources/messages.yml b/common/src/main/resources/messages.yml index e3264e8..5405029 100644 --- a/common/src/main/resources/messages.yml +++ b/common/src/main/resources/messages.yml @@ -74,7 +74,9 @@ charge: tools: wrong-tool: "You can't place this item as the tool if this minion!" +recalc: "Recalculated island minion count! -> " + location-format: ";;;" # Do not change! -config-version: 7 \ No newline at end of file +config-version: 8 \ No newline at end of file