Skip to content

Commit

Permalink
A bit of refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
Seggan committed Sep 23, 2024
1 parent 04c7d99 commit b0032a8
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,7 @@ package io.github.addoncommunity.galactifun.impl.items.abstract
import io.github.addoncommunity.galactifun.api.betteritem.BetterSlimefunItem
import io.github.addoncommunity.galactifun.api.betteritem.ItemHandler
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.getData
import io.github.seggan.sf4k.serial.pdc.setData
import io.github.addoncommunity.galactifun.util.bukkit.*
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 +32,7 @@ open class Seat(

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

Expand All @@ -50,7 +46,7 @@ open class Seat(
armorStand.setGravity(false)
armorStand.setAI(false)
armorStand.isMarker = true
armorStand.persistentDataContainer.setData(armorStandKey, b.location)
armorStand.setPdc(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 @@ -8,9 +8,9 @@ import io.github.addoncommunity.galactifun.api.objects.properties.atmosphere.Atm
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.getPdc
import io.github.addoncommunity.galactifun.util.bukkit.key
import io.github.seggan.sf4k.serial.pdc.getData
import io.github.seggan.sf4k.serial.pdc.setData
import io.github.addoncommunity.galactifun.util.bukkit.setPdc
import io.papermc.paper.event.entity.EntityMoveEvent
import org.bukkit.Bukkit
import org.bukkit.GameRule
Expand Down Expand Up @@ -59,7 +59,7 @@ object PlanetManager : Listener {
Atmosphere.NONE.applyEffects(spaceWorld)
spaceWorld.setGameRule(GameRule.DO_MOB_SPAWNING, false)

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

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

if (planet is PlanetaryWorld) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package io.github.addoncommunity.galactifun.util

import io.github.addoncommunity.galactifun.util.bukkit.copy
import io.github.addoncommunity.galactifun.util.bukkit.getPdc
import io.github.addoncommunity.galactifun.util.bukkit.key
import io.github.addoncommunity.galactifun.util.bukkit.setPdc
import io.github.seggan.sf4k.extensions.div
import io.github.seggan.sf4k.extensions.minus
import io.github.seggan.sf4k.extensions.plus
import io.github.seggan.sf4k.serial.pdc.getData
import io.github.seggan.sf4k.serial.pdc.setData
import me.mrCookieSlime.Slimefun.api.BlockStorage
import org.bukkit.*
import org.bukkit.block.structure.Mirror
Expand Down Expand Up @@ -123,7 +123,7 @@ class SlimefunStructure(
blockTransformers: MutableCollection<BlockTransformer>,
entityTransformers: MutableCollection<EntityTransformer>
) {
val data = persistentDataContainer.getData<Map<BlockVector, Pair<String, Material>>>(blockStorageKey) ?: emptyMap()
val data = getPdc<Map<BlockVector, Pair<String, Material>>>(blockStorageKey) ?: emptyMap()
val rotated = data.mapKeys { (vector, _) -> vector.applyStructureTransforms(structureRotation, mirror) }
blockTransformers.add(BlockTransformer { region, x, y, z, current, _ ->
val vector = BlockVector(x, y, z).subtract(location)
Expand Down Expand Up @@ -162,7 +162,7 @@ class SlimefunStructure(
}
delegate.fill(origin, size, includeEntities)
persistentDataContainer.remove(blockStorageKey)
persistentDataContainer.setData(blockStorageKey, data)
setPdc(blockStorageKey, data)
center = getNewCenter()
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,28 +1,16 @@
package io.github.addoncommunity.galactifun.util.bukkit

import io.github.addoncommunity.galactifun.impl.items.CommandComputer
import io.github.addoncommunity.galactifun.pluginInstance
import io.github.addoncommunity.galactifun.util.SlimefunStructure
import io.github.seggan.sf4k.serial.pdc.getData
import io.github.seggan.sf4k.serial.pdc.setData
import io.papermc.paper.entity.TeleportFlag.EntityState
import net.kyori.adventure.text.Component
import net.kyori.adventure.text.TextComponent
import net.kyori.adventure.text.format.TextColor
import net.kyori.adventure.text.format.TextDecoration
import org.bukkit.*
import org.bukkit.block.Block
import org.bukkit.block.structure.Mirror
import org.bukkit.block.structure.StructureRotation
import org.bukkit.entity.BlockDisplay
import org.bukkit.entity.Entity
import org.bukkit.event.player.PlayerTeleportEvent
import org.bukkit.inventory.ItemStack
import org.bukkit.persistence.PersistentDataContainer
import org.bukkit.structure.Structure
import org.bukkit.util.BlockVector
import org.bukkit.util.Vector
import java.io.ByteArrayOutputStream
import java.util.*
import java.util.concurrent.CompletableFuture

Expand Down Expand Up @@ -79,12 +67,6 @@ fun Entity.galactifunTeleport(
}
}

operator fun TextColor.plus(s: String): TextComponent = Component.text()
.color(this)
.decorations(EnumSet.allOf(TextDecoration::class.java), false)
.content(s)
.build()

inline fun ItemStack.modifyLore(modifier: (MutableList<Component>) -> Unit) {
val meta = itemMeta ?: Bukkit.getItemFactory().getItemMeta(type)
val lore = meta.lore() ?: mutableListOf()
Expand All @@ -95,8 +77,6 @@ inline fun ItemStack.modifyLore(modifier: (MutableList<Component>) -> Unit) {

operator fun <T : Keyed> Tag<T>.contains(item: T): Boolean = isTagged(item)

operator fun PersistentDataContainer.contains(key: NamespacedKey): Boolean = has(key)

fun Structure.placeDefault(
location: Location,
includeEntities: Boolean = true,
Expand All @@ -105,23 +85,4 @@ fun Structure.placeDefault(
palette: Int = 0,
integrity: Float = 1.0f,
random: Random = Random(location.world.seed)
) = place(location, includeEntities, rotation, mirror, palette, integrity, random)

fun Block.toDisplay(spawnLocation: Location = location): BlockDisplay {
val display = world.summon<BlockDisplay>(spawnLocation)
display.block = blockData
val structure = SlimefunStructure()
structure.fill(location, BlockVector(1, 1, 1), false)
val bytes = ByteArrayOutputStream().apply(structure::saveToStream).toByteArray()
display.persistentDataContainer.setData(CommandComputer.SERIALIZED_BLOCK_KEY, bytes)
this.type = Material.AIR
return display
}

fun BlockDisplay.toBlock(placeLocation: Location = location): Boolean {
val bytes = persistentDataContainer.getData<ByteArray>(CommandComputer.SERIALIZED_BLOCK_KEY) ?: return false
val block = SlimefunStructure.loadFromStream(bytes.inputStream())
block.placeDefault(placeLocation)
remove()
return true
}
) = place(location, includeEntities, rotation, mirror, palette, integrity, random)
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package io.github.addoncommunity.galactifun.util.bukkit

import io.github.addoncommunity.galactifun.impl.items.CommandComputer
import io.github.addoncommunity.galactifun.util.SlimefunStructure
import io.github.seggan.sf4k.serial.pdc.getData
import io.github.seggan.sf4k.serial.pdc.setData
import org.bukkit.Location
import org.bukkit.Material
import org.bukkit.NamespacedKey
import org.bukkit.block.Block
import org.bukkit.entity.BlockDisplay
import org.bukkit.persistence.PersistentDataContainer
import org.bukkit.persistence.PersistentDataHolder
import org.bukkit.util.BlockVector
import java.io.ByteArrayOutputStream

operator fun PersistentDataContainer.contains(key: NamespacedKey): Boolean = has(key)

inline fun <reified T> PersistentDataHolder.getPdc(key: NamespacedKey): T? {
return persistentDataContainer.getData<T>(key)
}

inline fun <reified T> PersistentDataHolder.setPdc(key: NamespacedKey, value: T) {
persistentDataContainer.setData(key, value)
}

fun Block.toDisplay(spawnLocation: Location = location): BlockDisplay {
val display = world.summon<BlockDisplay>(spawnLocation)
display.block = blockData
val structure = SlimefunStructure()
structure.fill(location, BlockVector(1, 1, 1), false)
val bytes = ByteArrayOutputStream().apply(structure::saveToStream).toByteArray()
display.persistentDataContainer.setData(CommandComputer.SERIALIZED_BLOCK_KEY, bytes)
this.type = Material.AIR
return display
}

fun BlockDisplay.toBlock(placeLocation: Location = location): Boolean {
val bytes = persistentDataContainer.getData<ByteArray>(CommandComputer.SERIALIZED_BLOCK_KEY) ?: return false
val block = SlimefunStructure.loadFromStream(bytes.inputStream())
block.placeDefault(placeLocation)
remove()
return true
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package io.github.addoncommunity.galactifun.util.bukkit

import net.kyori.adventure.text.Component
import net.kyori.adventure.text.TextComponent
import net.kyori.adventure.text.format.TextColor
import net.kyori.adventure.text.format.TextDecoration
import net.kyori.adventure.text.minimessage.MiniMessage
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer
import java.util.*

fun String.miniMessageToLegacy(): String = LegacyComponentSerializer.legacyAmpersand()
.serialize(MiniMessage.miniMessage().deserialize(this))
Expand All @@ -17,4 +20,10 @@ fun String.miniComponent(): Component =
fun String.legacyDefaultColor(color: Char): String {
if (startsWith('&') || isBlank()) return this
return "&$color$this"
}
}

operator fun TextColor.plus(s: String): TextComponent = Component.text()
.color(this)
.decorations(EnumSet.allOf(TextDecoration::class.java), false)
.content(s)
.build()

0 comments on commit b0032a8

Please sign in to comment.