Skip to content

Commit 20fda72

Browse files
committed
Added "/setspawn" and "/spawn" command and command aliases.
todo: add check on is server command sender.
1 parent 957a327 commit 20fda72

6 files changed

Lines changed: 93 additions & 11 deletions

File tree

src/main/kotlin/com/mairwunnx/projectessentials/projectessentialsspawn/EntryPoint.kt

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ import com.mairwunnx.projectessentials.projectessentialsspawn.helpers.validateFo
66
import com.mairwunnx.projectessentials.projectessentialsspawn.models.SpawnModelBase
77
import com.mojang.brigadier.CommandDispatcher
88
import net.minecraft.command.CommandSource
9+
import net.minecraft.util.math.BlockPos
910
import net.minecraftforge.common.MinecraftForge
11+
import net.minecraftforge.eventbus.api.EventPriority
1012
import net.minecraftforge.eventbus.api.SubscribeEvent
1113
import net.minecraftforge.fml.common.Mod
1214
import net.minecraftforge.fml.event.server.FMLServerStartingEvent
@@ -49,10 +51,11 @@ class EntryPoint {
4951
logger.info(" - Telegram chat: $MOD_TELEGRAM_LINK")
5052
}
5153

52-
@SubscribeEvent
53-
internal fun onServerStarting(it: FMLServerStartingEvent) {
54+
@SubscribeEvent(priority = EventPriority.HIGH)
55+
fun onServerStarting(event: FMLServerStartingEvent) {
5456
logger.info("$MOD_NAME starting mod loading ...")
55-
registerCommands(it.server.commandManager.dispatcher)
57+
registerCommands(event.server.commandManager.dispatcher)
58+
SpawnModelBase.assignSpawn(event)
5659
}
5760

5861
private fun registerCommands(
@@ -65,7 +68,7 @@ class EntryPoint {
6568

6669
@Suppress("UNUSED_PARAMETER")
6770
@SubscribeEvent
68-
internal fun onServerStopping(it: FMLServerStoppingEvent) {
71+
fun onServerStopping(it: FMLServerStoppingEvent) {
6972
logger.info("Shutting down $MOD_NAME mod ...")
7073
logger.info(" - Saving world spawn data ...")
7174
SpawnModelBase.saveData()
Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,38 @@
11
package com.mairwunnx.projectessentials.projectessentialsspawn.commands
22

3+
import com.mairwunnx.projectessentials.projectessentialsspawn.models.SpawnModelBase
4+
import com.mairwunnx.projectessentialspermissions.permissions.PermissionsAPI
35
import com.mojang.brigadier.CommandDispatcher
6+
import com.mojang.brigadier.builder.LiteralArgumentBuilder
7+
import com.mojang.brigadier.context.CommandContext
48
import net.minecraft.command.CommandSource
9+
import org.apache.logging.log4j.LogManager
510

611
object SetSpawnCommand {
7-
fun register(dispatcher: CommandDispatcher<CommandSource>) {
12+
private val aliases = arrayOf("setspawn", "esetspawn")
13+
private val logger = LogManager.getLogger()
814

15+
fun register(dispatcher: CommandDispatcher<CommandSource>) {
16+
aliases.forEach { command ->
17+
dispatcher.register(
18+
LiteralArgumentBuilder.literal<CommandSource>(command).executes {
19+
return@executes execute(it)
20+
}
21+
)
22+
}
923
}
1024

11-
private fun execute() {
12-
25+
private fun execute(c: CommandContext<CommandSource>): Int {
26+
// if source is server return
27+
val player = c.source.asPlayer()
28+
if (PermissionsAPI.hasPermission(player.name.string, "ess.spawn.set")) {
29+
SpawnModelBase.spawnModel.xPos = player.posX
30+
SpawnModelBase.spawnModel.yPos = player.posY
31+
SpawnModelBase.spawnModel.zPos = player.posZ
32+
SpawnModelBase.spawnModel.yaw = player.rotationYaw
33+
SpawnModelBase.spawnModel.pitch = player.rotationPitch
34+
SpawnModelBase.spawnModel.worldId = player.serverWorld.worldType.id
35+
}
36+
return 0
1337
}
1438
}
Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,43 @@
11
package com.mairwunnx.projectessentials.projectessentialsspawn.commands
22

3+
import com.mairwunnx.projectessentials.projectessentialsspawn.models.SpawnModelBase
4+
import com.mairwunnx.projectessentialspermissions.permissions.PermissionsAPI
35
import com.mojang.brigadier.CommandDispatcher
6+
import com.mojang.brigadier.builder.LiteralArgumentBuilder.literal
7+
import com.mojang.brigadier.context.CommandContext
48
import net.minecraft.command.CommandSource
9+
import net.minecraft.world.dimension.DimensionType
10+
import org.apache.logging.log4j.LogManager
511

612
object SpawnCommand {
7-
fun register(dispatcher: CommandDispatcher<CommandSource>) {
13+
private val aliases = arrayOf("spawn", "espawn")
14+
private val logger = LogManager.getLogger()
815

16+
fun register(dispatcher: CommandDispatcher<CommandSource>) {
17+
aliases.forEach { command ->
18+
dispatcher.register(
19+
literal<CommandSource>(command).executes {
20+
return@executes execute(it)
21+
}
22+
)
23+
}
924
}
1025

11-
private fun execute() {
12-
26+
private fun execute(c: CommandContext<CommandSource>): Int {
27+
// if not player sender return
28+
val playerName = c.source.asPlayer().name.string
29+
if (PermissionsAPI.hasPermission(playerName, "ess.spawn")) {
30+
val xPos = SpawnModelBase.spawnModel.xPos
31+
val yPos = SpawnModelBase.spawnModel.yPos
32+
val zPos = SpawnModelBase.spawnModel.zPos
33+
val yaw = SpawnModelBase.spawnModel.yaw
34+
val pitch = SpawnModelBase.spawnModel.pitch
35+
val dimId = SpawnModelBase.spawnModel.worldId
36+
val targetWorld = c.source.server.getWorld(
37+
DimensionType.getById(dimId) ?: DimensionType.OVERWORLD
38+
)
39+
c.source.asPlayer().teleport(targetWorld, xPos, yPos, zPos, yaw, pitch)
40+
}
41+
return 0
1342
}
1443
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package com.mairwunnx.projectessentials.projectessentialsspawn.commands
2+
3+
object SpawnReloadCommand {
4+
}

src/main/kotlin/com/mairwunnx/projectessentials/projectessentialsspawn/models/SpawnModel.kt

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,12 @@ package com.mairwunnx.projectessentials.projectessentialsspawn.models
33
import kotlinx.serialization.Serializable
44

55
@Serializable
6-
data class SpawnModel(var x: Int = 0, var y: Int = 100, var z: Int = 0)
6+
data class SpawnModel(
7+
var firstSession: Boolean = false,
8+
var worldId: Int = 0,
9+
var xPos: Double = 0.5,
10+
var yPos: Double = 100.0,
11+
var zPos: Double = 0.5,
12+
var yaw: Float = 0f,
13+
var pitch: Float = 0f
14+
)

src/main/kotlin/com/mairwunnx/projectessentials/projectessentialsspawn/models/SpawnModelBase.kt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ import com.mairwunnx.projectessentials.projectessentialsspawn.helpers.SPAWN_CONF
55
import kotlinx.serialization.UnstableDefault
66
import kotlinx.serialization.json.Json
77
import kotlinx.serialization.json.JsonConfiguration
8+
import net.minecraft.util.math.BlockPos
9+
import net.minecraft.world.dimension.DimensionType
10+
import net.minecraftforge.fml.event.server.FMLServerStartingEvent
811
import org.apache.logging.log4j.LogManager
912
import java.io.File
1013

@@ -39,6 +42,17 @@ object SpawnModelBase {
3942
spawnModel = Json.parse(SpawnModel.serializer(), spawnConfigRaw)
4043
}
4144

45+
fun assignSpawn(event: FMLServerStartingEvent) {
46+
event.server.worlds.forEach {
47+
val dim = it.dimension.type.id
48+
event.server.getWorld()
49+
if (dim == 0) {
50+
val pos = spawnModel
51+
it.spawnPoint = BlockPos(pos.x, pos.y, pos.z)
52+
}
53+
}
54+
}
55+
4256
fun saveData() {
4357
logger.info(" - saving world spawn data ...")
4458
createConfigDirs(MOD_CONFIG_FOLDER)

0 commit comments

Comments
 (0)