From c638876aa29d2c2bedd5ffbd4bb20f5acfb0edd7 Mon Sep 17 00:00:00 2001 From: TomTom <93038247+AverageGithub@users.noreply.github.com> Date: Thu, 13 Jun 2024 20:33:52 +0200 Subject: [PATCH] Try fix --- .../axminions/api/data/DataHandler.kt | 6 +++ .../integrations/types/IslandIntegration.kt | 4 +- .../axminions/data/H2DataHandler.kt | 42 ++++++++++++++++++ .../island/IridiumSkyBlockIntegration.kt | 22 ++++------ .../island/SuperiorSkyBlock2Integration.kt | 19 ++------ .../listeners/MinionPlaceListener.kt | 43 +++++++++++-------- .../axminions/minions/Minion.kt | 7 +++ 7 files changed, 94 insertions(+), 49 deletions(-) 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 517e3e1..11464c4 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 @@ -30,5 +30,11 @@ interface DataHandler { fun isMinion(location: Location): Boolean + fun islandPlace(island: String) + + fun islandBreak(island: String) + + fun getIsland(island: String): Int + fun disable() } \ No newline at end of file diff --git a/api/src/main/kotlin/com/artillexstudios/axminions/api/integrations/types/IslandIntegration.kt b/api/src/main/kotlin/com/artillexstudios/axminions/api/integrations/types/IslandIntegration.kt index 9900227..45ecd8a 100644 --- a/api/src/main/kotlin/com/artillexstudios/axminions/api/integrations/types/IslandIntegration.kt +++ b/api/src/main/kotlin/com/artillexstudios/axminions/api/integrations/types/IslandIntegration.kt @@ -1,12 +1,12 @@ package com.artillexstudios.axminions.api.integrations.types import com.artillexstudios.axminions.api.integrations.Integration +import org.bukkit.Location import org.bukkit.block.Block -import org.bukkit.entity.Player interface IslandIntegration : Integration { - fun getIslandPlaced(player: Player): Int + fun getIslandAt(location: Location): String fun handleBlockBreak(block: Block) } \ No newline at end of file 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 828ad8f..749b3b5 100644 --- a/common/src/main/kotlin/com/artillexstudios/axminions/data/H2DataHandler.kt +++ b/common/src/main/kotlin/com/artillexstudios/axminions/data/H2DataHandler.kt @@ -2,6 +2,7 @@ package com.artillexstudios.axminions.data import com.artillexstudios.axapi.serializers.Serializers import com.artillexstudios.axminions.AxMinionsPlugin +import com.artillexstudios.axminions.api.config.Config import com.artillexstudios.axminions.api.data.DataHandler import com.artillexstudios.axminions.api.minions.Direction import com.artillexstudios.axminions.api.minions.miniontype.MinionType @@ -73,6 +74,15 @@ class H2DataHandler : DataHandler { it.executeUpdate() } } + + if (Config.ISLAND_LIMIT() > 0) { + dataSource.connection.use { connection -> + connection.prepareStatement("CREATE TABLE IF NOT EXISTS `axminions_island_counter`(`island` VARCHAR(256) PRIMARY KEY, `placed` INT);") + .use { + it.executeUpdate() + } + } + } } override fun insertType(minionType: MinionType) { @@ -364,6 +374,38 @@ class H2DataHandler : DataHandler { return false } + override fun islandPlace(island: String) { + dataSource.connection.use { connection -> + connection.prepareStatement("UPDATE `axminions_island_counter` SET `placed` = `placed` + 1 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 -> + statement.setString(1, island) + statement.executeUpdate() + } + } + } + + override fun getIsland(island: String): Int { + dataSource.connection.use { connection -> + connection.prepareStatement("SELECT `placed` FROM `axminions_island_counter` WHERE `island` = ?").use { statement -> + statement.executeQuery().use { resultSet -> + if (resultSet.next()) { + return resultSet.getInt("placed") + } + } + } + } + + return 0 + } + override fun disable() { dataSource.connection.prepareStatement("SHUTDOWN DEFRAG;").executeUpdate() } diff --git a/common/src/main/kotlin/com/artillexstudios/axminions/integrations/island/IridiumSkyBlockIntegration.kt b/common/src/main/kotlin/com/artillexstudios/axminions/integrations/island/IridiumSkyBlockIntegration.kt index 332052d..c75fc75 100644 --- a/common/src/main/kotlin/com/artillexstudios/axminions/integrations/island/IridiumSkyBlockIntegration.kt +++ b/common/src/main/kotlin/com/artillexstudios/axminions/integrations/island/IridiumSkyBlockIntegration.kt @@ -1,29 +1,23 @@ package com.artillexstudios.axminions.integrations.island -import com.artillexstudios.axminions.api.AxMinionsAPI import com.artillexstudios.axminions.api.integrations.types.IslandIntegration -import com.artillexstudios.axminions.api.utils.fastFor -import com.iridium.iridiumskyblock.IridiumSkyblock import com.iridium.iridiumskyblock.api.IridiumSkyblockAPI +import org.bukkit.Location import org.bukkit.block.Block -import org.bukkit.entity.Player class IridiumSkyBlockIntegration : IslandIntegration { - override fun getIslandPlaced(player: Player): Int { - val island = IridiumSkyblockAPI.getInstance().getUser(player).island - if (island.isEmpty) { - return 0 + override fun getIslandAt(location: Location): String { + val island = IridiumSkyblockAPI.getInstance().getIslandViaLocation(location) + + if (island.isPresent) { + return island.get().id.toString() } - var amount = 0 - IridiumSkyblock.getInstance().teamManager.getTeamMembers(island.get()).fastFor { - amount += AxMinionsAPI.INSTANCE.getDataHandler().getMinionAmount(it.uuid) - } - - return amount + return "" } + override fun handleBlockBreak(block: Block) { } diff --git a/common/src/main/kotlin/com/artillexstudios/axminions/integrations/island/SuperiorSkyBlock2Integration.kt b/common/src/main/kotlin/com/artillexstudios/axminions/integrations/island/SuperiorSkyBlock2Integration.kt index d88b86e..d75f154 100644 --- a/common/src/main/kotlin/com/artillexstudios/axminions/integrations/island/SuperiorSkyBlock2Integration.kt +++ b/common/src/main/kotlin/com/artillexstudios/axminions/integrations/island/SuperiorSkyBlock2Integration.kt @@ -1,28 +1,15 @@ package com.artillexstudios.axminions.integrations.island -import com.artillexstudios.axminions.api.AxMinionsAPI import com.artillexstudios.axminions.api.config.Config import com.artillexstudios.axminions.api.integrations.types.IslandIntegration import com.bgsoftware.superiorskyblock.api.SuperiorSkyblockAPI +import org.bukkit.Location import org.bukkit.block.Block -import org.bukkit.entity.Player class SuperiorSkyBlock2Integration : IslandIntegration { - override fun getIslandPlaced(player: Player): Int { - var placed = 0 - SuperiorSkyblockAPI.getPlayer(player.uniqueId).island?.getIslandMembers(true)?.forEach { - val a = AxMinionsAPI.INSTANCE.getDataHandler().getMinionAmount(it.uniqueId) - placed += a - if (Config.DEBUG()) { - println("Member: ${it.name} - $a") - } - } - - if (Config.DEBUG()) { - println("Placed total: $placed") - } - return placed + override fun getIslandAt(location: Location): String { + return SuperiorSkyblockAPI.getIslandAt(location)?.uniqueId?.toString() ?: "" } override fun handleBlockBreak(block: Block) { diff --git a/common/src/main/kotlin/com/artillexstudios/axminions/listeners/MinionPlaceListener.kt b/common/src/main/kotlin/com/artillexstudios/axminions/listeners/MinionPlaceListener.kt index 8cfcb03..239c9b0 100644 --- a/common/src/main/kotlin/com/artillexstudios/axminions/listeners/MinionPlaceListener.kt +++ b/common/src/main/kotlin/com/artillexstudios/axminions/listeners/MinionPlaceListener.kt @@ -6,22 +6,22 @@ import com.artillexstudios.axminions.AxMinionsPlugin import com.artillexstudios.axminions.api.AxMinionsAPI import com.artillexstudios.axminions.api.config.Config import com.artillexstudios.axminions.api.config.Messages +import com.artillexstudios.axminions.api.events.PreMinionPlaceEvent import com.artillexstudios.axminions.api.minions.Direction import com.artillexstudios.axminions.api.minions.miniontype.MinionTypes import com.artillexstudios.axminions.api.utils.Keys import com.artillexstudios.axminions.minions.Minion import com.artillexstudios.axminions.minions.Minions import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder +import org.bukkit.Bukkit import org.bukkit.Material import org.bukkit.event.EventHandler import org.bukkit.event.Listener import org.bukkit.event.block.Action +import org.bukkit.event.block.BlockPlaceEvent import org.bukkit.event.player.PlayerInteractEvent import org.bukkit.inventory.ItemStack import org.bukkit.persistence.PersistentDataType -import org.bukkit.event.block.BlockPlaceEvent -import com.artillexstudios.axminions.api.events.PreMinionPlaceEvent -import org.bukkit.Bukkit class MinionPlaceListener : Listener { @@ -77,24 +77,28 @@ class MinionPlaceListener : Listener { val islandLimit = Config.ISLAND_LIMIT() var islandPlaced = 0 + var islandId = "" if (islandLimit > 0 && AxMinionsAPI.INSTANCE.getIntegrations().getIslandIntegration() != null) { - islandPlaced = AxMinionsAPI.INSTANCE.getIntegrations().getIslandIntegration()!!.getIslandPlaced(event.player) + islandId = AxMinionsAPI.INSTANCE.getIntegrations().getIslandIntegration()!!.getIslandAt(location) + if (islandId.isNotBlank()) { + islandPlaced = AxMinionsAPI.INSTANCE.getDataHandler().getIsland(islandId) - if (islandPlaced >= islandLimit && !event.player.hasPermission("axminions.limit.*")) { - event.player.sendMessage( - StringUtils.formatToString( - Messages.PREFIX() + Messages.ISLAND_LIMIT_REACHED(), - Placeholder.unparsed("placed", islandPlaced.toString()), - Placeholder.unparsed("max", islandLimit.toString()) + if (islandPlaced >= islandLimit && !event.player.hasPermission("axminions.limit.*")) { + event.player.sendMessage( + StringUtils.formatToString( + Messages.PREFIX() + Messages.ISLAND_LIMIT_REACHED(), + Placeholder.unparsed("placed", islandPlaced.toString()), + Placeholder.unparsed("max", islandLimit.toString()) + ) ) - ) - Scheduler.get().run { _ -> - meta = item.itemMeta!! - meta.persistentDataContainer.remove(Keys.PLACED) - item.itemMeta = meta + Scheduler.get().run { _ -> + meta = item.itemMeta!! + meta.persistentDataContainer.remove(Keys.PLACED) + item.itemMeta = meta + } + return@submit } - return@submit } } @@ -125,7 +129,7 @@ class MinionPlaceListener : Listener { } return@submit } - + val locationId = AxMinionsPlugin.dataHandler.getLocationID(location) val minion = Minion( location, @@ -144,6 +148,7 @@ class MinionPlaceListener : Listener { ) Minions.startTicking(chunk) + Scheduler.get().run { _ -> meta = item.itemMeta!! meta.persistentDataContainer.remove(Keys.PLACED) @@ -164,6 +169,10 @@ class MinionPlaceListener : Listener { minion.setOwnerOnline(true) AxMinionsPlugin.dataHandler.saveMinion(minion) + if (islandId.isNotBlank()) { + AxMinionsPlugin.dataHandler.islandPlace(islandId) + } + event.player.sendMessage( StringUtils.formatToString( Messages.PREFIX() + Messages.PLACE_SUCCESS(), diff --git a/common/src/main/kotlin/com/artillexstudios/axminions/minions/Minion.kt b/common/src/main/kotlin/com/artillexstudios/axminions/minions/Minion.kt index eeaa3d2..703a2fe 100644 --- a/common/src/main/kotlin/com/artillexstudios/axminions/minions/Minion.kt +++ b/common/src/main/kotlin/com/artillexstudios/axminions/minions/Minion.kt @@ -550,6 +550,13 @@ class Minion( AxMinionsPlugin.dataQueue.submit { AxMinionsPlugin.dataHandler.deleteMinion(this) + + if (AxMinionsAPI.INSTANCE.getIntegrations().getIslandIntegration() != null) { + val islandId = AxMinionsAPI.INSTANCE.getIntegrations().getIslandIntegration()!!.getIslandAt(location) + if (islandId.isNotBlank()) { + AxMinionsPlugin.dataHandler.islandBreak(islandId) + } + } } }