Skip to content

Commit d4d0149

Browse files
authored
feat: depth-calculation (#100)
* Introduce depth calculation logic * Move depth calculation logic to WorldManager methods
1 parent f2cea84 commit d4d0149

File tree

5 files changed

+85
-10
lines changed

5 files changed

+85
-10
lines changed

src/main/kotlin/com/mineinabyss/deeperworld/DeeperCommands.kt

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,8 @@ import com.mineinabyss.deeperworld.MinecraftConstants.FULL_DAY_TIME
55
import com.mineinabyss.deeperworld.services.WorldManager
66
import com.mineinabyss.deeperworld.services.canMoveSections
77
import com.mineinabyss.deeperworld.synchronization.sync
8-
import com.mineinabyss.deeperworld.world.section.correspondingSection
9-
import com.mineinabyss.deeperworld.world.section.getCorrespondingLocation
10-
import com.mineinabyss.deeperworld.world.section.section
11-
import com.mineinabyss.idofront.commands.brigadier.Args
12-
import com.mineinabyss.idofront.commands.brigadier.commands
13-
import com.mineinabyss.idofront.commands.brigadier.executes
14-
import com.mineinabyss.idofront.commands.brigadier.playerExecutes
15-
import com.mineinabyss.idofront.commands.execution.stopCommand
8+
import com.mineinabyss.deeperworld.world.section.*
9+
import com.mineinabyss.idofront.commands.brigadier.*
1610
import com.mineinabyss.idofront.messaging.error
1711
import com.mineinabyss.idofront.messaging.info
1812
import com.mineinabyss.idofront.messaging.success
@@ -29,6 +23,7 @@ import com.sk89q.worldedit.regions.CuboidRegion
2923
import com.sk89q.worldedit.session.ClipboardHolder
3024
import com.sk89q.worldedit.world.World
3125
import io.papermc.paper.command.brigadier.argument.ArgumentTypes
26+
import org.bukkit.entity.Player
3227

3328
object DeeperCommands {
3429
fun registerCommands() {
@@ -166,7 +161,8 @@ object DeeperCommands {
166161
)
167162

168163
block.sync { original, corr ->
169-
if (original.type != corr.type) corr.blockData = original.blockData.clone()
164+
if (original.type != corr.type) corr.blockData =
165+
original.blockData.clone()
170166
}
171167
}
172168
}
@@ -179,6 +175,25 @@ object DeeperCommands {
179175
}
180176
}
181177
}
178+
"depth" {
179+
val playerArg = ArgumentTypes.player()
180+
.resolve()
181+
.default { listOf(executor as? Player ?: fail("Receiver must be a player or pass a player as argument")) }
182+
183+
executes(playerArg) { players ->
184+
val player = players.single()
185+
186+
WorldManager.getDepthFor(player.location)?.let{
187+
if (sender is Player){
188+
sender.success("Your depth is $it blocks")
189+
}
190+
else{
191+
sender.success("Depth of player ${player.name} is $it blocks")
192+
}
193+
194+
} ?: sender.error("${player.name} is not in a managed section")
195+
}
196+
}
182197
}
183198
}
184199
}

src/main/kotlin/com/mineinabyss/deeperworld/services/WorldManager.kt

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ interface WorldManager {
2424
* Given x,y coords and a world, return the Section it is within
2525
*
2626
* @param x The x coordinate
27+
* @param y The y coordinate
2728
* @param z The z coordinate
2829
* @param world The world
2930
* @return The section or null if the location is not within a section.
@@ -71,6 +72,24 @@ interface WorldManager {
7172
*/
7273
fun unregisterSection(key: SectionKey)
7374

75+
/**
76+
* Gets the depth in blocks of the given location, taking sections into account
77+
*
78+
* @param location The location
79+
* @return The depth of the given location in blocks, or null if location is not in a managed section
80+
*/
81+
fun getDepthFor(location: Location): Int?
82+
83+
/**
84+
* Gets the depth in blocks of the given position, taking sections into account
85+
* @param x The x coordinate
86+
* @param y The y coordinate
87+
* @param z The z coordinate
88+
* @param world The world
89+
* @return The depth of the given position in blocks, or null if position is not in a managed section
90+
*/
91+
fun getDepthFor(x: Double, y: Double, z: Double, world: World): Int?
92+
7493
/**
7594
* Gets an immutable copy of the currently loaded sections.
7695
*/

src/main/kotlin/com/mineinabyss/deeperworld/world/WorldManagerImpl.kt

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,12 @@ import com.mineinabyss.deeperworld.services.WorldManager
55
import com.mineinabyss.deeperworld.world.section.AbstractSectionKey.CustomSectionKey
66
import com.mineinabyss.deeperworld.world.section.Section
77
import com.mineinabyss.deeperworld.world.section.SectionKey
8+
import com.mineinabyss.deeperworld.world.section.overlapWith
9+
import com.mineinabyss.deeperworld.world.section.section
10+
import com.mineinabyss.idofront.messaging.info
811
import org.bukkit.Location
912
import org.bukkit.World
13+
import kotlin.math.floor
1014

1115
class WorldManagerImpl : WorldManager {
1216
override val sections get() = sectionMap.values.toSet()
@@ -28,6 +32,40 @@ class WorldManagerImpl : WorldManager {
2832

2933
override fun unregisterSection(key: SectionKey) = TODO()
3034

35+
override fun getDepthFor(location: Location): Int? {
36+
val section = WorldManager.getSectionFor(location) ?: return null
37+
38+
return this.internalGetDepthFor(floor(location.y).toInt(), section)
39+
}
40+
41+
override fun getDepthFor(x: Double, y: Double, z: Double, world: World): Int? {
42+
val yFloored = floor(y).toInt()
43+
val section = WorldManager.getSectionFor(floor(x).toInt(), yFloored, floor(z).toInt(), world) ?: return null
44+
45+
return this.internalGetDepthFor(yFloored, section)
46+
}
47+
48+
private fun internalGetDepthFor(y: Int, section: Section): Int {
49+
var depth = (section.region.max.y - y)
50+
51+
var currentSection: Section = section
52+
var aboveSection = section.aboveKey.section
53+
54+
if (aboveSection != null) {
55+
depth += 1 // Account for one unit of depth missing when calculating depth with multiple sections
56+
}
57+
58+
while (aboveSection != null) {
59+
depth += aboveSection.height
60+
depth -= aboveSection.overlapWith(currentSection) ?: 0
61+
62+
currentSection = aboveSection
63+
aboveSection = currentSection.aboveKey.section
64+
}
65+
66+
return depth
67+
}
68+
3169
override fun getSectionFor(location: Location): Section? {
3270
return getSectionFor(location.blockX, location.blockY, location.blockZ, location.world!!)
3371
}

src/main/kotlin/com/mineinabyss/deeperworld/world/section/Section.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,5 +56,8 @@ data class Section(
5656
@Transient
5757
internal var belowKey: SectionKey = SectionKey.TERMINAL
5858

59+
@Transient
60+
val height: Int = this.region.max.y - this.region.min.y
61+
5962
override fun toString() = key.toString()
6063
}

src/main/kotlin/com/mineinabyss/deeperworld/world/section/SectionUtils.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ fun Location.sharedBetween(section: Section, otherSection: Section): Boolean {
8787

8888
fun Section.overlapWith(other: Section): Int? {
8989
if (!isAdjacentTo(other)) return null
90-
if(min(this.region.max.y, other.region.max.y) <= max(this.region.min.y, other.region.min.y)) return null
90+
if (min(this.region.max.y, other.region.max.y) <= max(this.region.min.y, other.region.min.y)) return null
9191
// We decide which two points we are translating between.
9292
val (locA, locB) = when (isOnTopOf(other)) {
9393
true -> referenceBottom to other.referenceTop

0 commit comments

Comments
 (0)