Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/rtp #9

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion src/main/kotlin/me/gserv/fabrikommander/Common.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,18 @@ object Common : ModInitializer {
HomesCommand(dispatcher).register()
SetHomeCommand(dispatcher).register()

// Misc comands
// TPA commands
spaceclouds42 marked this conversation as resolved.
Show resolved Hide resolved
TpaCommand(dispatcher).register()
TpaHereCommand(dispatcher).register()
TpAcceptCommand(dispatcher).register()
TpCancelCommand(dispatcher).register()
TpDenyCommand(dispatcher).register()

// Teleport commands
BackCommand(dispatcher).register()
RtpCommand(dispatcher).register()

// Misc commands
PingCommand(dispatcher).register()
}
}
93 changes: 93 additions & 0 deletions src/main/kotlin/me/gserv/fabrikommander/commands/RtpCommand.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package me.gserv.fabrikommander.commands

import me.gserv.fabrikommander.data.PlayerDataManager
import me.gserv.fabrikommander.data.spec.Pos
import me.gserv.fabrikommander.utils.Context
import me.gserv.fabrikommander.utils.Dispatcher
import me.gserv.fabrikommander.utils.gold
import me.gserv.fabrikommander.utils.gray
import me.gserv.fabrikommander.utils.yellow
import me.gserv.fabrikommander.utils.plus
import me.gserv.fabrikommander.utils.aqua
import net.minecraft.server.command.CommandManager
import net.minecraft.util.Identifier
import net.minecraft.server.world.ServerWorld
import net.minecraft.util.registry.Registry
import net.minecraft.util.registry.RegistryKey
import net.minecraft.util.math.BlockPos

class RtpCommand(val dispatcher: Dispatcher) {
private val rtpRange = -5000..5000

fun register() {
spaceclouds42 marked this conversation as resolved.
Show resolved Hide resolved
dispatcher.register(
CommandManager.literal("rtp")
.executes { rtpCommand(it) }
)
dispatcher.register(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't do this - see @gdude2002's comment on my tpa PR

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm confused as to what not to do and can't seem to find his comment pertaining to registering commands, which is what I'm assuming this is about

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, are you referring to him saying that each command should have its own class? /rtp and /wild are the same command, wild is just an alias for rtp, so I thought this was how to do that.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It doesn't look like an alias - since it executes the same code. The way Minecraft does aliases is creating a CommandNode or whatever it's called (I don't remember Brigadier names) and using .redirect on the alias and passing in that node.

Copy link
Author

@spaceclouds42 spaceclouds42 Dec 22, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, well I didn't really know how to create a proper alias, but what I did is how the Fabric wiki showed how to do it. I can look into it and change it though
edit: Turns out, I managed to completely misread the wiki. I don't even know how I came up with what's here right now

Copy link
Author

@spaceclouds42 spaceclouds42 Dec 22, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fun register() {
        val node = registerMain()
        dispatcher.register(
            CommandManager.literal("wild")
                .redirect(node)
        )
    }

    fun registerMain(): LiteralCommandNode<ServerCommandSource> {
        return dispatcher.register(
            CommandManager.literal("rtp")
                .executes { rtpCommand(it) }
        )
    }

I did this based on what I could understand from the wiki. In game, it creates a /wild command, but when it is run, it gives the 'unknown command' error. Got any suggestions? (also, /rtp still works as expected)

CommandManager.literal("wild")
.executes { rtpCommand(it) }
)
}

private fun generateCoordinates(world: ServerWorld?): List<Int> {
val x = rtpRange.random()
val z = rtpRange.random()
val y = getHighestBlock(world, x, z)

if (world?.isWater(BlockPos(x, y, z)) == true) {
return generateCoordinates(world)
}

return listOf(x, y + 1, z)
}

private fun getHighestBlock(world: ServerWorld?, x: Int, z: Int): Int {
val heightLimit = world!!.heightLimit
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The auto build check keeps failing, saying that heightLimit cannot be found, but it works when I build it on my computer. From what I can tell, the file I have is the same as this one except for the few /back related lines.


for (height in heightLimit downTo 0)
if (!world.isAir(BlockPos(x, height, z))) {
return height
}

return 0
}

fun rtpCommand(context: Context): Int {
val player = context.source.player
val overworld = player.server.getWorld(RegistryKey.of(Registry.DIMENSION, Identifier("minecraft:overworld")))
val coordinates = generateCoordinates(overworld)

PlayerDataManager.setBackPos(
player.uuid,
Pos(
x = player.x,
y = player.y,
z = player.z,
world = player.world.registryKey.value,
yaw = player.yaw,
pitch = player.pitch
)
)

player.teleport(
overworld,
coordinates[0].toDouble(),
coordinates[1].toDouble(),
coordinates[2].toDouble(),
player.yaw,
player.pitch
)
context.source.sendFeedback(
gray("[") +
yellow("RTP") +
gray("] ") +
gold("Randomly teleported to ") +
aqua("[${player.x.toInt()}, ${player.y.toInt()}, ${player.z.toInt()}]"),
true
)


return 1
}
}