-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
267 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
common/src/main/kotlin/org/valkyrienskies/tournament/blockentity/ChunkLoaderBlockEntity.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package org.valkyrienskies.tournament.blockentity | ||
|
||
import net.minecraft.core.BlockPos | ||
import net.minecraft.server.level.ServerLevel | ||
import net.minecraft.world.level.ChunkPos | ||
import net.minecraft.world.level.block.entity.BlockEntity | ||
import net.minecraft.world.level.block.entity.BlockEntityTicker | ||
import net.minecraft.world.level.block.state.BlockState | ||
import org.joml.Vector3d | ||
import org.valkyrienskies.mod.common.getShipObjectManagingPos | ||
import org.valkyrienskies.mod.common.util.toJOMLD | ||
import org.valkyrienskies.tournament.TournamentBlockEntities | ||
import org.valkyrienskies.tournament.chunk.ChunkLoader | ||
import org.valkyrienskies.tournament.chunk.ChunkLoaderManager | ||
import org.valkyrienskies.tournament.chunk.ChunkLoadingTicket | ||
|
||
class ChunkLoaderBlockEntity(pos: BlockPos, state: BlockState): | ||
BlockEntity(TournamentBlockEntities.CHUNK_LOADER.get(), pos, state), | ||
ChunkLoader | ||
{ | ||
|
||
internal var ticket: ChunkLoadingTicket? = null | ||
|
||
fun tick(level: ServerLevel) { | ||
if (ticket == null) { | ||
val manager = ChunkLoaderManager.getFor(level) | ||
ticket = manager.allocate(this, 200) | ||
} | ||
} | ||
|
||
private fun getCurrPos() = | ||
level | ||
?.getShipObjectManagingPos(blockPos) | ||
?.shipToWorld | ||
?.transformPosition(blockPos.toJOMLD()) | ||
|
||
override fun getCurrentChunk(): ChunkPos = | ||
getCurrPos() | ||
?.let { | ||
ChunkPos(it.x.toInt() shr 4, it.z.toInt() shr 4) | ||
} ?: ChunkPos(blockPos.x shr 4, blockPos.z shr 4) | ||
|
||
override fun getFutureChunk(): ChunkPos = | ||
level | ||
?.getShipObjectManagingPos(blockPos) | ||
?.velocity | ||
?.add(getCurrPos(), Vector3d()) | ||
?.let { | ||
ChunkPos(it.x.toInt() shr 4, it.z.toInt() shr 4) | ||
} ?: ChunkPos(blockPos.x shr 4, blockPos.z shr 4) | ||
|
||
companion object { | ||
val ticker = BlockEntityTicker<ChunkLoaderBlockEntity> { level, _, _, be -> | ||
if(level !is ServerLevel) | ||
return@BlockEntityTicker | ||
|
||
be.tick(level) | ||
} | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
common/src/main/kotlin/org/valkyrienskies/tournament/blocks/ChunkLoaderBlock.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package org.valkyrienskies.tournament.blocks | ||
|
||
import net.minecraft.core.BlockPos | ||
import net.minecraft.server.level.ServerLevel | ||
import net.minecraft.world.level.Level | ||
import net.minecraft.world.level.block.BaseEntityBlock | ||
import net.minecraft.world.level.block.entity.BlockEntity | ||
import net.minecraft.world.level.block.entity.BlockEntityTicker | ||
import net.minecraft.world.level.block.entity.BlockEntityType | ||
import net.minecraft.world.level.block.state.BlockState | ||
import net.minecraft.world.level.material.Material | ||
import org.valkyrienskies.tournament.blockentity.ChunkLoaderBlockEntity | ||
|
||
class ChunkLoaderBlock: BaseEntityBlock( | ||
Properties.of(Material.METAL) | ||
) { | ||
override fun newBlockEntity(pos: BlockPos, state: BlockState): BlockEntity = | ||
ChunkLoaderBlockEntity(pos, state) | ||
|
||
@Suppress("UNCHECKED_CAST") | ||
override fun <T: BlockEntity> getTicker( | ||
level: Level, | ||
state: BlockState, | ||
blockEntityType: BlockEntityType<T> | ||
): BlockEntityTicker<T> = | ||
ChunkLoaderBlockEntity.ticker as BlockEntityTicker<T> | ||
|
||
@Deprecated("Deprecated in Java") | ||
override fun onRemove(state: BlockState, level: Level, pos: BlockPos, newState: BlockState, isMoving: Boolean) { | ||
if (level is ServerLevel) { | ||
val be = level.getBlockEntity(pos) as? ChunkLoaderBlockEntity | ||
?: return | ||
|
||
be.ticket?.dispose() | ||
} | ||
|
||
super.onRemove(state, level, pos, newState, isMoving) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 2 additions & 0 deletions
2
common/src/main/kotlin/org/valkyrienskies/tournament/util/extension/IterableExtension.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
common/src/main/kotlin/org/valkyrienskies/tournament/util/extension/RectangleExtensions.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package org.valkyrienskies.tournament.util.extension | ||
|
||
import org.joml.Vector2i | ||
import org.joml.primitives.Rectanglei | ||
|
||
fun Rectanglei.fix(): Rectanglei = | ||
this.also { | ||
if (it.minX > it.maxX) { | ||
val temp = it.minX | ||
it.minX = it.maxX | ||
it.maxX = temp | ||
} | ||
if (it.minY > it.maxY) { | ||
val temp = it.minY | ||
it.minY = it.maxY | ||
it.maxY = temp | ||
} | ||
} | ||
|
||
fun Rectanglei.scaleFrom(factor: Float, center: Vector2i): Rectanglei = | ||
this.also { | ||
val midX = center.x | ||
val midY = center.y | ||
val width = it.maxX - it.minX | ||
val height = it.maxY - it.minY | ||
val newWidth = (width * factor).toInt() | ||
val newHeight = (height * factor).toInt() | ||
it.minX = midX - newWidth / 2 | ||
it.maxX = midX + newWidth / 2 | ||
it.minY = midY - newHeight / 2 | ||
it.maxY = midY + newHeight / 2 | ||
} | ||
|
||
fun Rectanglei.values(): Sequence<Vector2i> = | ||
sequence { | ||
for (x in minX..maxX) { | ||
for (y in minY..maxY) { | ||
yield(Vector2i(x, y)) | ||
} | ||
} | ||
} |
Oops, something went wrong.