Skip to content

Commit

Permalink
Clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
Seggan committed Sep 27, 2024
1 parent 4af312e commit 1bd3a55
Show file tree
Hide file tree
Showing 10 changed files with 72 additions and 138 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import io.github.addoncommunity.galactifun.EARTH_GRAVITY
import io.github.addoncommunity.galactifun.api.blocks.HeatResistant
import io.github.addoncommunity.galactifun.api.blocks.wetMass
import io.github.addoncommunity.galactifun.api.objects.properties.atmosphere.Gas
import io.github.addoncommunity.galactifun.impl.items.FuelTank
import io.github.addoncommunity.galactifun.impl.items.RocketEngine
import io.github.addoncommunity.galactifun.impl.items.rocket.FuelTank
import io.github.addoncommunity.galactifun.impl.items.rocket.RocketEngine
import io.github.addoncommunity.galactifun.impl.managers.PlanetManager
import io.github.addoncommunity.galactifun.units.*
import io.github.addoncommunity.galactifun.util.general.mergeMaps
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ package io.github.addoncommunity.galactifun.impl

import io.github.addoncommunity.galactifun.Galactifun2
import io.github.addoncommunity.galactifun.api.objects.properties.atmosphere.Gas
import io.github.addoncommunity.galactifun.impl.items.*
import io.github.addoncommunity.galactifun.impl.items.SlimefunStructureBlock
import io.github.addoncommunity.galactifun.impl.items.rocket.*
import io.github.addoncommunity.galactifun.units.Force.Companion.kilonewtons
import io.github.addoncommunity.galactifun.units.Volume.Companion.liters
import io.github.addoncommunity.galactifun.util.items.buildSlimefunItem
Expand Down Expand Up @@ -59,15 +60,15 @@ object GalactifunItems {
+"A heat shield that requires replacement"
}

val CAPTAINS_CHAIR = buildSlimefunItem<CaptainsChair> {
val SEAT = buildSlimefunItem<Seat> {
category = GalactifunCategories.ROCKET_COMPONENTS
id = "CAPTAINS_CHAIR"
name = "Captain's Chair"
id = "SEAT"
name = "Seat"
material = Material.OAK_STAIRS.materialType
recipeType = RecipeType.NULL
recipe = emptyArray()

+"The captain's chair"
+"What do you mean, \"operate the rocket standing\"?"
}

val FUEL_TANK_I = buildSlimefunItem<FuelTank>(1000.liters) {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,49 +1,48 @@
package io.github.addoncommunity.galactifun.impl.items.abstract
package io.github.addoncommunity.galactifun.impl.items.rocket

import io.github.addoncommunity.galactifun.api.betteritem.BetterSlimefunItem
import io.github.addoncommunity.galactifun.api.betteritem.ItemHandler
import io.github.addoncommunity.galactifun.api.rockets.RocketInfo
import io.github.addoncommunity.galactifun.impl.managers.RocketManager
import io.github.addoncommunity.galactifun.units.Angle.Companion.radians
import io.github.addoncommunity.galactifun.util.bukkit.*
import io.github.seggan.sf4k.serial.blockstorage.getBlockStorage
import io.github.seggan.sf4k.serial.blockstorage.setBlockStorage
import io.github.thebusybiscuit.slimefun4.api.events.PlayerRightClickEvent
import io.github.thebusybiscuit.slimefun4.api.items.ItemGroup
import io.github.thebusybiscuit.slimefun4.api.items.SlimefunItemStack
import io.github.thebusybiscuit.slimefun4.api.recipes.RecipeType
import io.github.thebusybiscuit.slimefun4.core.handlers.BlockPlaceHandler
import io.github.thebusybiscuit.slimefun4.core.handlers.BlockUseHandler
import io.github.thebusybiscuit.slimefun4.implementation.handlers.SimpleBlockBreakHandler
import io.github.thebusybiscuit.slimefun4.libraries.dough.blocks.BlockPosition
import me.mrCookieSlime.Slimefun.api.BlockStorage
import org.bukkit.Location
import org.bukkit.block.Block
import org.bukkit.block.data.Directional
import org.bukkit.entity.Arrow
import org.bukkit.entity.Pig
import org.bukkit.entity.Player
import org.bukkit.event.block.BlockPlaceEvent
import org.bukkit.inventory.ItemStack
import kotlin.math.atan2

open class Seat(
class Seat(
itemGroup: ItemGroup,
item: SlimefunItemStack,
recipeType: RecipeType,
recipe: Array<out ItemStack?>
) : BetterSlimefunItem(itemGroup, item, recipeType, recipe) {

companion object {
private val armorStandKey = "seat".key()

fun getSitting(player: Player): Location? {
val entity = player.vehicle as? Arrow ?: return null
return entity.getPdc(armorStandKey)
}
}

@ItemHandler(BlockPlaceHandler::class)
private fun onPlace(e: BlockPlaceEvent) {
val b = e.block
val entity = b.world.summon<Arrow>(b.location.toStandLocation())
val entity = b.world.summon<Pig>(b.location.toStandLocation())
entity.isInvisible = true
entity.isInvulnerable = true
entity.setAI(false)
entity.setGravity(false)
entity.setPdc(armorStandKey, b.location)
entity.setPdc(SEAT_KEY, b.location)
val data = b.blockData
if (data is Directional) {
val facing = data.facing
Expand All @@ -57,7 +56,7 @@ open class Seat(
@ItemHandler(SimpleBlockBreakHandler::class)
private fun onBreak(b: Block) {
val entity = b.world.nearbyEntitiesByType<Arrow>(b.location.toStandLocation(), 0.5) {
it.persistentDataContainer.has(armorStandKey)
it.persistentDataContainer.has(SEAT_KEY)
}.firstOrNull() ?: return
entity.remove()
}
Expand All @@ -66,13 +65,32 @@ open class Seat(
private fun onUse(e: PlayerRightClickEvent) {
val block = e.clickedBlock.get()
val entity = block.world.nearbyEntitiesByType<Arrow>(block.location.toStandLocation(), 0.5) {
it.persistentDataContainer.has(armorStandKey)
it.persistentDataContainer.has(SEAT_KEY)
}.firstOrNull() ?: return
entity.addPassenger(e.player)
onSit(e.player, block)
}

protected open fun onSit(p: Player, b: Block) {}
companion object {

private const val ROCKET_KEY = "rocket"
private val SEAT_KEY = "seat".key()

fun getSitting(player: Player): Location? {
val entity = player.vehicle as? Arrow ?: return null
val location = entity.getPdc<Location>(SEAT_KEY) ?: return null
if (BlockStorage.check(location) !is Seat) return null
return location
}

fun getRocket(l: Location): RocketInfo? {
val commandComputer = l.getBlockStorage<BlockPosition>(ROCKET_KEY) ?: return null
return RocketManager.getInfo(commandComputer)
}

fun setRocket(seat: Location, pos: BlockPosition) {
seat.setBlockStorage<BlockPosition>(ROCKET_KEY, pos)
}
}
}

private fun Location.toStandLocation() = add(0.5, 0.4, 0.5)
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.github.addoncommunity.galactifun.impl.items
package io.github.addoncommunity.galactifun.impl.items.rocket

import io.github.addoncommunity.galactifun.api.betteritem.BetterSlimefunItem
import io.github.addoncommunity.galactifun.api.betteritem.ItemHandler
Expand All @@ -7,15 +7,11 @@ import io.github.addoncommunity.galactifun.api.objects.WorldType
import io.github.addoncommunity.galactifun.api.objects.planet.PlanetaryWorld
import io.github.addoncommunity.galactifun.api.rockets.RocketInfo
import io.github.addoncommunity.galactifun.impl.GalactifunHeads
import io.github.addoncommunity.galactifun.impl.items.abstract.Seat
import io.github.addoncommunity.galactifun.impl.managers.PlanetManager
import io.github.addoncommunity.galactifun.impl.managers.RocketManager
import io.github.addoncommunity.galactifun.util.*
import io.github.addoncommunity.galactifun.util.bukkit.*
import io.github.addoncommunity.galactifun.util.menu.buildMenu
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.thebusybiscuit.slimefun4.api.events.PlayerRightClickEvent
import io.github.thebusybiscuit.slimefun4.api.items.ItemGroup
import io.github.thebusybiscuit.slimefun4.api.items.SlimefunItemStack
Expand All @@ -26,7 +22,6 @@ import io.github.thebusybiscuit.slimefun4.core.handlers.BlockUseHandler
import io.github.thebusybiscuit.slimefun4.libraries.dough.blocks.BlockPosition
import it.unimi.dsi.fastutil.objects.Object2IntOpenHashMap
import me.mrCookieSlime.CSCoreLibPlugin.general.Inventory.ChestMenu
import me.mrCookieSlime.Slimefun.api.BlockStorage
import net.kyori.adventure.text.format.NamedTextColor
import org.bukkit.Location
import org.bukkit.block.Block
Expand Down Expand Up @@ -75,9 +70,7 @@ class CommandComputer(
val rocketBlocks = pos.floodSearch { this.checkBlock<RocketEngine>() == null }
val detected = if (!rocketBlocks.exceededMax) rocketBlocks.found else emptySet()
val blocks = detected.map(BlockPosition::getBlock)
blocks.processSlimefunBlocks<CaptainsChair, Unit> {
it.setBlockStorage("rocket", pos)
}
blocks.processSlimefunBlocks<Seat, _> { Seat.setRocket(it.location, pos) }
val engines = blocks.processSlimefunBlocks<RocketEngine, _> { b ->
val fuel = b.position.floodSearch { it.checkBlock<FuelTank>() != null }.found.toMutableSet()
fuel.remove(b.position)
Expand All @@ -99,28 +92,14 @@ class CommandComputer(
return
}
getMenu(block, info).open(p)
val seat = Seat.getSitting(p)
if (seat != null
&& BlockStorage.check(seat) is CaptainsChair
&& seat.getBlockStorage<BlockPosition>("rocket") == pos
) {
if (p.world == PlanetManager.spaceWorld) {
} else {
RocketManager.launchRocket(p, info, seat)
}
} else {
e.player.sendMessage(NamedTextColor.GOLD + info.info)
}
}

@OptIn(ExperimentalContracts::class)
private fun canLaunch(seat: Location?, info: RocketInfo): Boolean {
contract {
returns(true) implies (seat != null)
}
return seat != null
&& BlockStorage.check(seat) is CaptainsChair
&& seat.getBlockStorage<BlockPosition>("rocket") == info.commandComputer
return seat != null && Seat.getRocket(seat)?.commandComputer == info.commandComputer
}

private fun getMenu(b: Block, rocket: RocketInfo): ChestMenu {
Expand Down Expand Up @@ -210,34 +189,33 @@ class CommandComputer(

null -> {
name = "Invalid world"
return@item
}
}

if (worldType != null) {
onClick { p, action ->
val seat = Seat.getSitting(p)
if (canLaunch(seat, rocket)) {
when (worldType) {
is WorldType.Planet -> RocketManager.launchRocket(p, rocket, seat)
is WorldType.Space -> {
val orbiting = worldType.orbiting
if (orbiting is PlanetaryWorld) {
RocketManager.landRocket(
p,
rocket,
seat,
action.isRightClicked
&& orbiting.atmosphere.canAerobrake
&& rocket.isFullyShielded
)
} else {
p.sendMessage(NamedTextColor.RED + "You cannot land on ${orbiting.name}")
}
onClick { p, action ->
val seat = Seat.getSitting(p)
if (canLaunch(seat, rocket)) {
when (worldType) {
is WorldType.Planet -> RocketManager.launchRocket(p, rocket, seat)
is WorldType.Space -> {
val orbiting = worldType.orbiting
if (orbiting is PlanetaryWorld) {
RocketManager.landRocket(
p,
rocket,
seat,
action.isRightClicked
&& orbiting.atmosphere.canAerobrake
&& rocket.isFullyShielded
)
} else {
p.sendMessage(NamedTextColor.RED + "You cannot land on ${orbiting.name}")
}
}
} else {
p.sendMessage(NamedTextColor.RED + "You must be sitting in the captain's chair to operate the rocket")
}
} else {
p.sendMessage(NamedTextColor.RED + "You must be sitting in the captain's chair to operate the rocket")
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.github.addoncommunity.galactifun.impl.items
package io.github.addoncommunity.galactifun.impl.items.rocket

import io.github.addoncommunity.galactifun.api.betteritem.BetterSlimefunItem
import io.github.addoncommunity.galactifun.api.betteritem.Ticker
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.github.addoncommunity.galactifun.impl.items
package io.github.addoncommunity.galactifun.impl.items.rocket

import io.github.addoncommunity.galactifun.api.blocks.BasicMassedBlock
import io.github.addoncommunity.galactifun.api.blocks.HeatResistant
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.github.addoncommunity.galactifun.impl.items
package io.github.addoncommunity.galactifun.impl.items.rocket

import io.github.addoncommunity.galactifun.api.blocks.HeatResistant
import io.github.addoncommunity.galactifun.api.objects.properties.atmosphere.Gas
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import io.github.addoncommunity.galactifun.Galactifun2
import io.github.addoncommunity.galactifun.api.blocks.ReentryBurnable
import io.github.addoncommunity.galactifun.api.objects.planet.PlanetaryWorld
import io.github.addoncommunity.galactifun.api.rockets.RocketInfo
import io.github.addoncommunity.galactifun.impl.items.abstract.Seat
import io.github.addoncommunity.galactifun.impl.items.rocket.Seat
import io.github.addoncommunity.galactifun.units.abs
import io.github.addoncommunity.galactifun.util.bukkit.*
import io.github.seggan.sf4k.extensions.minus
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package io.github.addoncommunity.galactifun.util.bukkit

import io.github.addoncommunity.galactifun.impl.items.CommandComputer
import io.github.addoncommunity.galactifun.impl.items.rocket.CommandComputer
import io.github.addoncommunity.galactifun.util.SlimefunStructure
import io.github.seggan.sf4k.extensions.minus
import io.github.seggan.sf4k.extensions.plus
Expand Down

0 comments on commit 1bd3a55

Please sign in to comment.