diff --git a/.gitignore b/.gitignore index a973fe6..3795fb2 100644 --- a/.gitignore +++ b/.gitignore @@ -11,4 +11,4 @@ launch.cmd /paper/ .gradle/ build/ -/run +run/ diff --git a/plugin/build.gradle.kts b/plugin/build.gradle.kts index 72d503b..02aaccf 100644 --- a/plugin/build.gradle.kts +++ b/plugin/build.gradle.kts @@ -41,7 +41,7 @@ dependencies { implementation("co.aikar:acf-paper:0.5.1-SNAPSHOT") implementation("com.jeff_media:MorePersistentDataTypes:2.4.0") - implementation("com.github.Seggan:kfun:0.1.0") + implementation("io.github.seggan:sf4k:0.1.1") implementation(project(":uom")) ksp(project(":uom-processor")) @@ -49,6 +49,7 @@ dependencies { testImplementation(kotlin("test")) testImplementation("io.kotest:kotest-assertions-core:5.8.0") testImplementation("com.github.seeseemelk:MockBukkit-v1.20:3.78.0") + testImplementation("com.github.shynixn.mccoroutine:mccoroutine-bukkit-test:2.14.0") } group = "io.github.addoncommunity.galactifun" diff --git a/plugin/src/main/kotlin/io/github/addoncommunity/galactifun/Galactifun2.kt b/plugin/src/main/kotlin/io/github/addoncommunity/galactifun/Galactifun2.kt index 6f11d1f..a04f3b6 100644 --- a/plugin/src/main/kotlin/io/github/addoncommunity/galactifun/Galactifun2.kt +++ b/plugin/src/main/kotlin/io/github/addoncommunity/galactifun/Galactifun2.kt @@ -17,7 +17,7 @@ import io.github.addoncommunity.galactifun.units.Angle.Companion.degrees import io.github.addoncommunity.galactifun.units.Distance.Companion.au import io.github.addoncommunity.galactifun.units.Distance.Companion.kilometers import io.github.addoncommunity.galactifun.units.Mass.Companion.kilograms -import io.github.seggan.kfun.AbstractAddon +import io.github.seggan.sf4k.AbstractAddon import io.github.thebusybiscuit.slimefun4.api.MinecraftVersion import io.github.thebusybiscuit.slimefun4.implementation.Slimefun import io.github.thebusybiscuit.slimefun4.libraries.paperlib.PaperLib @@ -42,13 +42,13 @@ open class Galactifun2 : AbstractAddon() { var isTest = classLoader.javaClass.packageName.startsWith("be.seeseemelk.mockbukkit") - override fun onLoad() { + override suspend fun onLoadAsync() { if (!isTest) { Bukkit.spigot().config["world-settings.default.verbose"] = false } } - override fun onEnable() { + override suspend fun onEnableAsync() { instance = this var shouldDisable = false @@ -152,7 +152,7 @@ open class Galactifun2 : AbstractAddon() { doTestingStuff() } - override fun onDisable() { + override suspend fun onDisableAsync() { instance = null } diff --git a/plugin/src/main/kotlin/io/github/addoncommunity/galactifun/api/objects/PlanetaryObject.kt b/plugin/src/main/kotlin/io/github/addoncommunity/galactifun/api/objects/PlanetaryObject.kt index c0f28f6..3040952 100644 --- a/plugin/src/main/kotlin/io/github/addoncommunity/galactifun/api/objects/PlanetaryObject.kt +++ b/plugin/src/main/kotlin/io/github/addoncommunity/galactifun/api/objects/PlanetaryObject.kt @@ -12,7 +12,7 @@ import io.github.addoncommunity.galactifun.units.Velocity import io.github.addoncommunity.galactifun.units.Velocity.Companion.metersPerSecond import io.github.addoncommunity.galactifun.units.abs import io.github.addoncommunity.galactifun.units.cos -import io.github.seggan.kfun.location.plus +import io.github.seggan.sf4k.location.plus import kotlinx.datetime.Instant import org.bukkit.Location import org.bukkit.inventory.ItemStack diff --git a/plugin/src/main/kotlin/io/github/addoncommunity/galactifun/impl/Gf2Command.kt b/plugin/src/main/kotlin/io/github/addoncommunity/galactifun/impl/Gf2Command.kt index 59533b8..114ab02 100644 --- a/plugin/src/main/kotlin/io/github/addoncommunity/galactifun/impl/Gf2Command.kt +++ b/plugin/src/main/kotlin/io/github/addoncommunity/galactifun/impl/Gf2Command.kt @@ -6,7 +6,7 @@ import io.github.addoncommunity.galactifun.api.objects.PlanetaryObject import io.github.addoncommunity.galactifun.api.objects.planet.PlanetaryWorld import io.github.addoncommunity.galactifun.impl.managers.PlanetManager import io.github.addoncommunity.galactifun.util.galactifunTeleport -import io.github.seggan.kfun.location.plusAssign +import io.github.seggan.sf4k.location.plusAssign import kotlinx.datetime.Clock import org.bukkit.Location import org.bukkit.entity.Player diff --git a/plugin/src/main/kotlin/io/github/addoncommunity/galactifun/impl/items/CommandComputer.kt b/plugin/src/main/kotlin/io/github/addoncommunity/galactifun/impl/items/CommandComputer.kt index dcd364a..4ad9134 100644 --- a/plugin/src/main/kotlin/io/github/addoncommunity/galactifun/impl/items/CommandComputer.kt +++ b/plugin/src/main/kotlin/io/github/addoncommunity/galactifun/impl/items/CommandComputer.kt @@ -1,14 +1,12 @@ package io.github.addoncommunity.galactifun.impl.items -import io.github.addoncommunity.galactifun.api.objects.properties.atmosphere.Gas import io.github.addoncommunity.galactifun.api.rockets.RocketInfo import io.github.addoncommunity.galactifun.impl.managers.RocketManager import io.github.addoncommunity.galactifun.util.checkBlock import io.github.addoncommunity.galactifun.util.floodSearch -import io.github.addoncommunity.galactifun.util.general.mergeMaps import io.github.addoncommunity.galactifun.util.items.TickingBlock import io.github.addoncommunity.galactifun.util.plus -import io.github.seggan.kfun.location.position +import io.github.seggan.sf4k.location.position import io.github.thebusybiscuit.slimefun4.api.events.PlayerRightClickEvent import io.github.thebusybiscuit.slimefun4.api.items.ItemGroup import io.github.thebusybiscuit.slimefun4.api.items.SlimefunItem @@ -53,13 +51,13 @@ class CommandComputer( } private fun rescanRocket(pos: BlockPosition) { - val rocketBlocks = pos.floodSearch { it.checkBlock() != null } + val rocketBlocks = pos.floodSearch { this.checkBlock() != null } val detected = if (!rocketBlocks.exceededMax) rocketBlocks.found else emptySet() val blocks = detected.map(BlockPosition::getBlock) - val fuel = blocks.processSlimefunBlocks(FuelTank::getFuelLevel) - .fold(emptyMap()) { acc, map -> acc.mergeMaps(map, Double::plus) } - val engines = blocks.processSlimefunBlocks { this } - RocketManager.register(RocketInfo(pos, detected, fuel, engines)) + val engines = blocks.processSlimefunBlocks { + this to it.position.floodSearch { this.checkBlock() != null }.found + } + RocketManager.register(RocketInfo(pos, detected, engines)) } private fun onRightClick(e: PlayerRightClickEvent) { diff --git a/plugin/src/main/kotlin/io/github/addoncommunity/galactifun/impl/items/FuelTank.kt b/plugin/src/main/kotlin/io/github/addoncommunity/galactifun/impl/items/FuelTank.kt index 121c8e0..8de2f19 100644 --- a/plugin/src/main/kotlin/io/github/addoncommunity/galactifun/impl/items/FuelTank.kt +++ b/plugin/src/main/kotlin/io/github/addoncommunity/galactifun/impl/items/FuelTank.kt @@ -1,17 +1,13 @@ package io.github.addoncommunity.galactifun.impl.items import io.github.addoncommunity.galactifun.api.objects.properties.atmosphere.Gas -import io.github.addoncommunity.galactifun.serial.EnumBlockStorageDataType -import io.github.addoncommunity.galactifun.serial.MapBlockStorageDataType import io.github.addoncommunity.galactifun.util.adjacentFaces import io.github.addoncommunity.galactifun.util.checkBlock import io.github.addoncommunity.galactifun.util.general.enumMapOf import io.github.addoncommunity.galactifun.util.general.mergeMaps import io.github.addoncommunity.galactifun.util.items.TickingBlock import io.github.addoncommunity.galactifun.util.items.buildMenu -import io.github.seggan.kfun.serial.BlockStorageDataType -import io.github.seggan.kfun.serial.getBlockStorage -import io.github.seggan.kfun.serial.setBlockStorage +import io.github.seggan.sf4k.serial.* import io.github.thebusybiscuit.slimefun4.api.items.ItemGroup import io.github.thebusybiscuit.slimefun4.api.items.SlimefunItemStack import io.github.thebusybiscuit.slimefun4.api.recipes.RecipeType diff --git a/plugin/src/main/kotlin/io/github/addoncommunity/galactifun/serial/CollectionBlockStorageDataType.kt b/plugin/src/main/kotlin/io/github/addoncommunity/galactifun/serial/CollectionBlockStorageDataType.kt deleted file mode 100644 index 84a47de..0000000 --- a/plugin/src/main/kotlin/io/github/addoncommunity/galactifun/serial/CollectionBlockStorageDataType.kt +++ /dev/null @@ -1,69 +0,0 @@ -package io.github.addoncommunity.galactifun.serial - -import io.github.addoncommunity.galactifun.Constants -import io.github.seggan.kfun.serial.BlockStorageDataType - -abstract class CollectionBlockStorageDataType>( - private val contentSerializer: BlockStorageDataType -) : BlockStorageDataType { - - override fun serialize(value: C): String { - val sb = StringBuilder() - sb.append(value.size.toString(Constants.MAX_RADIX)) - sb.append(' ') - for (element in value) { - val serialized = contentSerializer.serialize(element) - sb.append(serialized.length.toString(Constants.MAX_RADIX)) - sb.append(' ') - sb.append(serialized) - } - return sb.toString() - } - - override fun deserialize(value: String): C? { - val parts = value.split(' ', limit = 2) - if (parts.size != 2) return null - val size = parts[0].toInt(Constants.MAX_RADIX) - val coll = provideCollection(size) - var rest = parts[1] - for (i in 0 until size) { - val nextParts = rest.split(' ', limit = 2) - if (nextParts.size != 2) return null - val length = nextParts[0].toInt(Constants.MAX_RADIX) - val serialized = nextParts[1].substring(0, length) - coll.add(contentSerializer.deserialize(serialized) ?: return null) - rest = nextParts[1].substring(length) - } - if (rest.isNotEmpty()) return null - - // You better not break on me - @Suppress("UNCHECKED_CAST") - return coll as C - } - - protected abstract fun provideCollection(size: Int): MutableCollection -} - -class ListBlockStorageDataType(contentSerializer: BlockStorageDataType) : - CollectionBlockStorageDataType>(contentSerializer) { - override fun provideCollection(size: Int): MutableList = ArrayList(size) -} - -class SetBlockStorageDataType(contentSerializer: BlockStorageDataType) : - CollectionBlockStorageDataType>(contentSerializer) { - override fun provideCollection(size: Int): MutableSet = LinkedHashSet(size) -} - -class MapBlockStorageDataType( - keySerializer: BlockStorageDataType, - valueSerializer: BlockStorageDataType -) : BlockStorageDataType> { - - private val internalSerializer = ListBlockStorageDataType( - PairBlockStorageDataType(keySerializer, valueSerializer) - ) - - override fun serialize(value: Map): String = internalSerializer.serialize(value.toList()) - - override fun deserialize(value: String): Map? = internalSerializer.deserialize(value)?.toMap() -} \ No newline at end of file diff --git a/plugin/src/main/kotlin/io/github/addoncommunity/galactifun/serial/MiscBlockStorageDataType.kt b/plugin/src/main/kotlin/io/github/addoncommunity/galactifun/serial/MiscBlockStorageDataType.kt deleted file mode 100644 index 09972ed..0000000 --- a/plugin/src/main/kotlin/io/github/addoncommunity/galactifun/serial/MiscBlockStorageDataType.kt +++ /dev/null @@ -1,45 +0,0 @@ -package io.github.addoncommunity.galactifun.serial - -import io.github.addoncommunity.galactifun.Constants -import io.github.seggan.kfun.serial.BlockStorageDataType -import kotlin.reflect.KClass - -class PairBlockStorageDataType( - private val serializerA: BlockStorageDataType, - private val serializerB: BlockStorageDataType -) : BlockStorageDataType> { - - override fun serialize(value: Pair): String { - val sb = StringBuilder() - val a = serializerA.serialize(value.first) - sb.append(a.length.toString(Constants.MAX_RADIX)) - sb.append(' ') - sb.append(a) - sb.append(serializerB.serialize(value.second)) - return sb.toString() - } - - override fun deserialize(value: String): Pair? { - val parts = value.split(' ', limit = 2) - if (parts.size != 2) return null - val length = parts[0].toInt(Constants.MAX_RADIX) - val a = parts[1].substring(0, length) - val b = parts[1].substring(length) - return serializerA.deserialize(a)?.let { da -> - serializerB.deserialize(b)?.let { db -> - da to db - } - } - } -} - -class EnumBlockStorageDataType>(enumClass: Class) : BlockStorageDataType { - - constructor(enumClass: KClass) : this(enumClass.java) - - private val constants = enumClass.enumConstants.associateBy(Enum::name) - - override fun serialize(value: E): String = value.name - - override fun deserialize(value: String): E? = constants[value] -} \ No newline at end of file diff --git a/plugin/src/main/kotlin/io/github/addoncommunity/galactifun/util/worldgen/DoubleChunkGrid.kt b/plugin/src/main/kotlin/io/github/addoncommunity/galactifun/util/worldgen/DoubleChunkGrid.kt index 13c6c9c..8e6bbf7 100644 --- a/plugin/src/main/kotlin/io/github/addoncommunity/galactifun/util/worldgen/DoubleChunkGrid.kt +++ b/plugin/src/main/kotlin/io/github/addoncommunity/galactifun/util/worldgen/DoubleChunkGrid.kt @@ -1,9 +1,12 @@ package io.github.addoncommunity.galactifun.util.worldgen +import com.github.shynixn.mccoroutine.bukkit.launch import com.google.common.util.concurrent.AtomicDoubleArray +import io.github.addoncommunity.galactifun.pluginInstance import it.unimi.dsi.fastutil.longs.Long2LongOpenHashMap import it.unimi.dsi.fastutil.longs.Long2ObjectOpenHashMap -import kotlinx.coroutines.* +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.delay import java.lang.ref.WeakReference import java.util.concurrent.locks.ReentrantReadWriteLock @@ -21,7 +24,7 @@ class DoubleChunkGrid { init { val ref = WeakReference(this) - coroScope.launch { + pluginInstance.launch(Dispatchers.IO) { while (true) { val time = System.currentTimeMillis() val grid = ref.get() ?: break @@ -92,11 +95,6 @@ class DoubleChunkGrid { fun getOrSet(x: Int, y: Int, value: () -> Double) = getOrSet(x shr 4, y shr 4, x and 15, y and 15, value) - - companion object { - @OptIn(DelicateCoroutinesApi::class) - private val coroScope = CoroutineScope(newFixedThreadPoolContext(4, "Galactifun2-DoubleChunkGrid")) - } } private infix fun Int.pack(other: Int) = (this.toLong() shl 32) or (other.toLong() and 0xFFFFFFFFL) \ No newline at end of file diff --git a/plugin/src/test/kotlin/io/github/addoncommunity/test/CommonTest.kt b/plugin/src/test/kotlin/io/github/addoncommunity/test/CommonTest.kt index 454af6a..495fa2c 100644 --- a/plugin/src/test/kotlin/io/github/addoncommunity/test/CommonTest.kt +++ b/plugin/src/test/kotlin/io/github/addoncommunity/test/CommonTest.kt @@ -2,6 +2,8 @@ package io.github.addoncommunity.test import be.seeseemelk.mockbukkit.MockBukkit import be.seeseemelk.mockbukkit.UnimplementedOperationException +import com.github.shynixn.mccoroutine.bukkit.MCCoroutine +import com.github.shynixn.mccoroutine.bukkit.test.TestMCCoroutine import io.github.addoncommunity.galactifun.Galactifun2 import io.github.thebusybiscuit.slimefun4.implementation.Slimefun import org.junit.jupiter.api.AfterEach @@ -9,10 +11,14 @@ import org.junit.jupiter.api.BeforeEach abstract class CommonTest { + private lateinit var driver: String + @BeforeEach fun setUp() { MockBukkit.mock() MockBukkit.load(Slimefun::class.java) + driver = MCCoroutine.Driver + MCCoroutine.Driver = TestMCCoroutine.Driver try { MockBukkit.load(Galactifun2::class.java) } catch (e: UnimplementedOperationException) { @@ -23,5 +29,6 @@ abstract class CommonTest { @AfterEach fun tearDown() { MockBukkit.unmock() + MCCoroutine.Driver = driver } } \ No newline at end of file diff --git a/plugin/src/test/kotlin/io/github/addoncommunity/test/serial/SerialTest.kt b/plugin/src/test/kotlin/io/github/addoncommunity/test/serial/SerialTest.kt deleted file mode 100644 index a549410..0000000 --- a/plugin/src/test/kotlin/io/github/addoncommunity/test/serial/SerialTest.kt +++ /dev/null @@ -1,40 +0,0 @@ -package io.github.addoncommunity.test.serial - -import io.github.addoncommunity.galactifun.serial.ListBlockStorageDataType -import io.github.addoncommunity.galactifun.serial.MapBlockStorageDataType -import io.github.addoncommunity.galactifun.serial.SetBlockStorageDataType -import io.github.addoncommunity.test.CommonTest -import io.github.seggan.kfun.serial.BlockStorageDataType -import io.kotest.matchers.equals.shouldBeEqual -import io.kotest.matchers.nulls.shouldNotBeNull -import kotlin.test.Test - -class SerialTest : CommonTest() { - - @Test - fun testList() { - val serializer = ListBlockStorageDataType(BlockStorageDataType.INT) - val list = listOf(1, 2, 3, 4, 5) - val serialized = serializer.serialize(list) - val deserialized = serializer.deserialize(serialized) - deserialized.shouldNotBeNull() shouldBeEqual list - } - - @Test - fun testMap() { - val serializer = MapBlockStorageDataType(BlockStorageDataType.INT, BlockStorageDataType.STRING) - val map = mapOf(1 to "one", 2 to "two", 3 to "three") - val serialized = serializer.serialize(map) - val deserialized = serializer.deserialize(serialized) - deserialized.shouldNotBeNull() shouldBeEqual map - } - - @Test - fun testSet() { - val serializer = SetBlockStorageDataType(BlockStorageDataType.INT) - val set = setOf(1, 2, 3, 4, 5) - val serialized = serializer.serialize(set) - val deserialized = serializer.deserialize(serialized) - deserialized.shouldNotBeNull() shouldBeEqual set - } -} \ No newline at end of file