Island recalculate

This commit is contained in:
TomTom 2024-06-29 10:25:15 +02:00
parent ac503fe71a
commit d28f4b9880
6 changed files with 43 additions and 26 deletions

View File

@ -78,6 +78,8 @@ class Messages(file: File, stream: InputStream) {
@JvmStatic
fun UPGRADE_FAIL() = AxMinionsAPI.INSTANCE.getMessages().get<String>("upgrades.fail")
@JvmStatic
fun RECALC() = AxMinionsAPI.INSTANCE.getMessages().get<String>("recalc")
@JvmStatic
fun LOCATION_FORMAT() = AxMinionsAPI.INSTANCE.getMessages().get<String>("location-format")
}

View File

@ -34,6 +34,8 @@ interface DataHandler {
fun islandBreak(island: String)
fun islandReset(island: String)
fun getIsland(island: String): Int
fun disable()

View File

@ -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<CompletableFuture<Chunk>>()
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
}

View File

@ -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 ->

View File

@ -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()
}
}
}
}

View File

@ -74,7 +74,9 @@ charge:
tools:
wrong-tool: "<red>You can't place this item as the tool if this minion!"
recalc: "<green>Recalculated island minion count! <from> -> <to>"
location-format: "<world>;<x>;<y>;<z>"
# Do not change!
config-version: 7
config-version: 8