Skip to content

Commit

Permalink
Update to sf4k 0.6
Browse files Browse the repository at this point in the history
  • Loading branch information
Seggan committed Sep 23, 2024
1 parent 2c62add commit 04c7d99
Show file tree
Hide file tree
Showing 10 changed files with 112 additions and 181 deletions.
2 changes: 1 addition & 1 deletion plugin/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ dependencies {
implementation("org.bstats:bstats-bukkit:3.0.2")
implementation("co.aikar:acf-paper:0.5.1-SNAPSHOT")

implementation("io.github.seggan:sf4k:0.5.0")
implementation("io.github.seggan:sf4k:0.6.0")

testImplementation(kotlin("test"))
testImplementation("io.strikt:strikt-core:0.34.0")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import io.github.addoncommunity.galactifun.units.Mass.Companion.kilograms
import io.github.addoncommunity.galactifun.util.bukkit.plus
import io.github.addoncommunity.galactifun.util.general.log
import io.github.seggan.sf4k.AbstractAddon
import io.github.seggan.sf4k.serial.serializers.CustomSerializerRegistry
import io.github.seggan.sf4k.serial.serializers.BukkitSerializerRegistry
import io.github.thebusybiscuit.slimefun4.api.MinecraftVersion
import io.github.thebusybiscuit.slimefun4.implementation.Slimefun
import io.github.thebusybiscuit.slimefun4.libraries.paperlib.PaperLib
Expand All @@ -37,6 +37,7 @@ import org.bukkit.Bukkit
import org.bukkit.Material
import org.bukkit.block.Biome
import org.bukkit.plugin.java.JavaPlugin
import java.nio.file.Path
import java.util.logging.Level
import kotlin.coroutines.CoroutineContext
import kotlin.script.experimental.api.ResultValue
Expand All @@ -50,14 +51,15 @@ open class Galactifun2 : AbstractAddon() {

private lateinit var manager: PaperCommandManager
lateinit var launchMessages: List<String> private set
lateinit var structuresFolder: Path private set

var isTest = classLoader.javaClass.packageName.startsWith("be.seeseemelk.mockbukkit")

override suspend fun onLoadAsync() {
if (!isTest) {
Bukkit.spigot().config["world-settings.default.verbose"] = false
}
CustomSerializerRegistry.register(BlockVectorSerializer)
BukkitSerializerRegistry.addSerializer(BlockVectorSerializer)
}

override suspend fun onEnableAsync() {
Expand Down Expand Up @@ -91,7 +93,7 @@ open class Galactifun2 : AbstractAddon() {
}

if (!isTest) {
Metrics(this, 11613)
Metrics(this, 23447)
}

manager = PaperCommandManager(this)
Expand All @@ -113,6 +115,7 @@ open class Galactifun2 : AbstractAddon() {
manager.registerCommand(Gf2Command)

launchMessages = config.getStringList("rockets.launch-msgs")
structuresFolder = dataFolder.toPath().resolve("structures")

BaseUniverse.init()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ 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.impl.managers.RocketManager
import io.github.addoncommunity.galactifun.util.SerializedBlock
import io.github.addoncommunity.galactifun.util.bukkit.galactifunTeleport
import io.github.addoncommunity.galactifun.util.bukkit.toBlock
import io.github.addoncommunity.galactifun.util.menu.PlanetMenu
import io.github.seggan.sf4k.extensions.plusAssign
import kotlinx.datetime.Clock
Expand Down Expand Up @@ -47,7 +47,12 @@ object Gf2Command : BaseCommand() {
fun distance(player: Player, planet: PlanetaryObject) {
val start = PlanetManager.getByWorld(player.world) ?: return
val distance = start.getDeltaVForTransferTo(planet, Clock.System.now())
player.sendMessage("The delta-v required to transfer to %s is %.2f m/s".format(planet.name, distance.metersPerSecond))
player.sendMessage(
"The delta-v required to transfer to %s is %.2f m/s".format(
planet.name,
distance.metersPerSecond
)
)
}

@Subcommand("selector")
Expand All @@ -62,9 +67,7 @@ object Gf2Command : BaseCommand() {
var count = 0
for (entity in player.world.entities) {
if (entity is BlockDisplay) {
val block = SerializedBlock.loadFromDisplayEntity(entity)
if (block != null) {
block.place(entity.location)
if (entity.toBlock()) {
count++
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ import io.github.seggan.sf4k.extensions.plus
import io.github.seggan.sf4k.extensions.position
import io.github.seggan.sf4k.serial.blockstorage.getBlockStorage
import io.github.seggan.sf4k.serial.blockstorage.setBlockStorage
import io.github.seggan.sf4k.serial.pdc.get
import io.github.seggan.sf4k.serial.pdc.set
import io.github.seggan.sf4k.serial.pdc.getData
import io.github.seggan.sf4k.serial.pdc.setData
import io.github.thebusybiscuit.slimefun4.api.events.PlayerRightClickEvent
import io.github.thebusybiscuit.slimefun4.api.items.ItemGroup
import io.github.thebusybiscuit.slimefun4.api.items.SlimefunItemStack
Expand All @@ -45,7 +45,6 @@ import org.bukkit.event.block.BlockPlaceEvent
import org.bukkit.inventory.ItemStack
import org.bukkit.util.BoundingBox
import java.util.*
import java.util.concurrent.atomic.AtomicBoolean
import kotlin.collections.ArrayDeque
import kotlin.jvm.optionals.getOrNull
import kotlin.math.max
Expand All @@ -61,6 +60,10 @@ class CommandComputer(

private val counters = Object2IntOpenHashMap<BlockPosition>()

companion object {
val SERIALIZED_BLOCK_KEY = "serialized_block".key()
}

@Ticker
private fun tick(b: Block) {
val pos = b.position
Expand Down Expand Up @@ -167,20 +170,19 @@ class CommandComputer(
world.getNearbyEntities(BoundingBox.of(block.getRelative(BlockFace.UP)))
.filter { it.isOnGround }
)
val serialized = SerializedBlock.serialize(block)
val display = serialized.createDisplayEntity(b.location)
val display = block.toDisplay()
entities.add(display)
entities.addAll(foundEntities.filter { it.vehicle == null }.flatMap(::freezeEntity))
}

// Random launch messages
val launched = AtomicBoolean(false)
var launched = false
pluginInstance.launch {
val launchMessages = ArrayDeque(pluginInstance.launchMessages)
launchMessages.shuffle()
while (true) {
delayTicks(Random.nextInt(15, 30))
if (launched.get()) break
if (launched) break
for (player in entities.filterIsInstance<Player>()) {
player.sendMessage(launchMessages.removeFirst() + "...")
}
Expand All @@ -189,7 +191,7 @@ class CommandComputer(

// Engine smoke
pluginInstance.launch {
while (!launched.get()) {
while (!launched) {
for ((_, engine) in firstStage.engines) {
world.spawnParticle(
Particle.CAMPFIRE_SIGNAL_SMOKE,
Expand All @@ -211,7 +213,7 @@ class CommandComputer(
}
delayTicks(20)
}
launched.set(true)
launched = true

val offsets = entities.associateWith { it.location.subtract(pos.toLocation()) }
val maxHeight = world.maxHeight
Expand Down Expand Up @@ -249,14 +251,14 @@ private val FLIGHT_KEY = "is_flight".key()

fun freezeEntity(entity: Entity): List<Entity> {
val pdc = entity.persistentDataContainer
pdc.set(GRAVITY_KEY, entity.hasGravity())
pdc.setData(GRAVITY_KEY, entity.hasGravity())
entity.setGravity(false)
if (entity is LivingEntity) {
if (entity is Player) {
pdc.set(FLIGHT_KEY, entity.allowFlight)
pdc.setData(FLIGHT_KEY, entity.allowFlight)
entity.allowFlight = false
} else {
pdc.set(AI_KEY, entity.hasAI())
pdc.setData(AI_KEY, entity.hasAI())
entity.setAI(false)
}
}
Expand All @@ -268,16 +270,15 @@ fun unfreezeEntity(entity: Entity) {
when (entity) {
is Player -> {
entity.sendMessage("You have arrived at your destination")
entity.allowFlight = pdc.get(FLIGHT_KEY) ?: false
entity.allowFlight = pdc.getData(FLIGHT_KEY) ?: false
}

is BlockDisplay -> SerializedBlock.loadFromDisplayEntity(entity, true)
?.place(entity.location)
is BlockDisplay -> entity.toBlock()

else -> {
entity.setGravity(pdc.get(GRAVITY_KEY) ?: true)
entity.setGravity(pdc.getData(GRAVITY_KEY) ?: true)
if (entity is LivingEntity) {
entity.setAI(pdc.get(AI_KEY) ?: true)
entity.setAI(pdc.getData(AI_KEY) ?: true)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,14 @@ import org.bukkit.Location
import org.bukkit.Particle
import org.bukkit.block.Block
import org.bukkit.block.Structure
import org.bukkit.block.structure.Mirror
import org.bukkit.block.structure.StructureRotation
import org.bukkit.block.structure.UsageMode
import org.bukkit.event.block.BlockPlaceEvent
import org.bukkit.inventory.ItemStack
import org.bukkit.util.BlockVector
import org.bukkit.util.Vector
import java.util.*
import kotlin.io.path.exists
import kotlin.io.path.inputStream
import kotlin.io.path.outputStream

class SlimefunStructureBlock(
itemGroup: ItemGroup,
Expand Down Expand Up @@ -86,15 +86,15 @@ class SlimefunStructureBlock(
val offsetLocation = block.location + offset
structure.fill(offsetLocation, size, true)

val keyName = block.getBlockStorage<String>(KEY_NAME)
if (keyName.isNullOrBlank()) {
val name = block.getBlockStorage<String>(KEY_NAME)
if (name.isNullOrBlank()) {
p.sendMessage(NamedTextColor.RED + "Please set a name first")
return@onClick
}
val key = keyName.key()
structure.save(key)
pluginInstance.structuresFolder.resolve("$name.nbt")
.outputStream().use(structure::saveToStream)

p.sendMessage(NamedTextColor.GREEN + "Saved structure with key $key")
p.sendMessage(NamedTextColor.GREEN + "Saved structure $name")
}
}

Expand All @@ -117,20 +117,17 @@ class SlimefunStructureBlock(
return@onClick
}
val key = keyName.key()
val structure = SlimefunStructure.load(key)
val structureFile = pluginInstance.structuresFolder.resolve("$key.nbt")
val structure = if (structureFile.exists()) {
structureFile.inputStream().use(SlimefunStructure::loadFromStream)
} else {
null
}
if (structure == null) {
p.sendMessage(NamedTextColor.RED + "No structure found with key $key")
return@onClick
}
structure.place(
offsetLocation,
true,
StructureRotation.NONE,
Mirror.NONE,
0,
1f,
Random(block.world.seed)
)
structure.placeDefault(offsetLocation)

p.sendMessage(NamedTextColor.GREEN + "Loaded structure with key $key")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import io.github.addoncommunity.galactifun.units.Angle.Companion.radians
import io.github.addoncommunity.galactifun.util.bukkit.key
import io.github.addoncommunity.galactifun.util.bukkit.nearbyEntitiesByType
import io.github.addoncommunity.galactifun.util.bukkit.summon
import io.github.seggan.sf4k.serial.pdc.get
import io.github.seggan.sf4k.serial.pdc.set
import io.github.seggan.sf4k.serial.pdc.getData
import io.github.seggan.sf4k.serial.pdc.setData
import io.github.thebusybiscuit.slimefun4.api.events.PlayerRightClickEvent
import io.github.thebusybiscuit.slimefun4.api.items.ItemGroup
import io.github.thebusybiscuit.slimefun4.api.items.SlimefunItemStack
Expand Down Expand Up @@ -36,7 +36,7 @@ open class Seat(

fun getSitting(player: Player): Location? {
val armorStand = player.vehicle as? ArmorStand ?: return null
return armorStand.persistentDataContainer.get(armorStandKey)
return armorStand.persistentDataContainer.getData(armorStandKey)
}
}

Expand All @@ -50,7 +50,7 @@ open class Seat(
armorStand.setGravity(false)
armorStand.setAI(false)
armorStand.isMarker = true
armorStand.persistentDataContainer.set(armorStandKey, b.location)
armorStand.persistentDataContainer.setData(armorStandKey, b.location)
val data = b.blockData
if (data is Directional) {
val facing = data.facing
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,14 @@ import io.github.addoncommunity.galactifun.impl.Permissions
import io.github.addoncommunity.galactifun.impl.space.SpaceGenerator
import io.github.addoncommunity.galactifun.pluginInstance
import io.github.addoncommunity.galactifun.util.bukkit.key
import io.github.addoncommunity.galactifun.util.bukkit.locationZero
import io.github.addoncommunity.galactifun.util.bukkit.nearbyEntitiesByType
import io.github.addoncommunity.galactifun.util.bukkit.summon
import io.github.seggan.sf4k.serial.pdc.get
import io.github.seggan.sf4k.serial.pdc.set
import io.github.seggan.sf4k.serial.pdc.getData
import io.github.seggan.sf4k.serial.pdc.setData
import io.papermc.paper.event.entity.EntityMoveEvent
import org.bukkit.Bukkit
import org.bukkit.GameRule
import org.bukkit.World
import org.bukkit.WorldCreator
import org.bukkit.configuration.file.YamlConfiguration
import org.bukkit.entity.Entity
import org.bukkit.entity.Marker
import org.bukkit.event.EventHandler
import org.bukkit.event.Listener
import org.bukkit.event.player.PlayerMoveEvent
Expand All @@ -39,7 +34,6 @@ object PlanetManager : Listener {
get() = planets

val spaceWorld: World
private val spaceWorldMarker: Entity

private val orbits: MutableMap<String, OrbitPosition>
private val orbitsKey = "orbits".key()
Expand All @@ -65,12 +59,7 @@ object PlanetManager : Listener {
Atmosphere.NONE.applyEffects(spaceWorld)
spaceWorld.setGameRule(GameRule.DO_MOB_SPAWNING, false)

spaceWorldMarker = spaceWorld.nearbyEntitiesByType<Marker>(
locationZero(spaceWorld),
0.1
).firstOrNull() ?: spaceWorld.summon<Marker>(locationZero(spaceWorld))

orbits = spaceWorldMarker.persistentDataContainer.get(orbitsKey) ?: mutableMapOf()
orbits = spaceWorld.persistentDataContainer.getData(orbitsKey) ?: mutableMapOf()

Bukkit.getPluginManager().registerEvents(this, pluginInstance)
}
Expand All @@ -87,7 +76,7 @@ object PlanetManager : Listener {
orbits.size / maxOrbits + offset
)
orbits[planet.name] = orbitPos
spaceWorldMarker.persistentDataContainer.set(orbitsKey, orbits)
spaceWorld.persistentDataContainer.setData(orbitsKey, orbits)
}

if (planet is PlanetaryWorld) {
Expand Down
Loading

0 comments on commit 04c7d99

Please sign in to comment.