Commit 43c68b31 authored by Linus K.'s avatar Linus K.
Browse files

Add some commands

parent 0f594035
Pipeline #116 passed with stage
in 36 seconds
......@@ -6,3 +6,23 @@ depend:
- Kotlin
main: me.linuscde.mcplugins.teleportastic.Plugin
commands:
homes:
description: See own homes
usage: /<command>
sethome:
description: Set a home
usage: /<command> <name>
home:
description: Go to a home
usage: /<command> <name>
back:
description: Go to your last location
usage: /<command>
bed:
description: Go to your bed
usage: /<command>
spawn:
description: Go to the world spawn (or any as an op)
usage: /<command> [world]
\ No newline at end of file
package me.linuscde.mcplugins.teleportastic
import me.linuscde.mcplugins.teleportastic.commands.*
import me.linuscde.mcplugins.teleportastic.listeners.*
import org.bukkit.Bukkit
import org.bukkit.plugin.java.JavaPlugin
import java.io.File
import java.util.logging.Level
class Plugin: JavaPlugin() {
companion object {
lateinit var instance: Plugin
private set
val pluginDir: File
get() {
val dir = File("plugins/${instance.name}")
if(!dir.exists()) dir.mkdirs();
return dir
}
val prefix: String by lazy {
"§a[§2§o${instance.name}§r§a]§f"
}
}
override fun onEnable() {
instance = this
registerCommands()
registerTabCompleters()
registerListeners()
Bukkit.getScheduler().scheduleSyncRepeatingTask(this, { saveConfig() }, 20*60*5, 20*60*5)
logger.log(Level.INFO, "Plugin enabled")
}
fun registerCommands() {
getCommand("homes")?.setExecutor(HomesCommand())
getCommand("home")?.setExecutor(HomeCommand())
getCommand("sethome")?.setExecutor(SetHomeCommand())
getCommand("back")?.setExecutor(BackCommand())
getCommand("bed")?.setExecutor(BedCommand())
getCommand("spawn")?.setExecutor(SpawnCommand())
}
fun registerTabCompleters() {
getCommand("home")?.tabCompleter = HomeCommand()
getCommand("sethome")?.tabCompleter = SetHomeCommand()
getCommand("spawn")?.tabCompleter = SpawnCommand()
}
fun registerListeners() {
val pm = Bukkit.getPluginManager()
pm.registerEvents(AutoBackLocationListener(), this)
}
override fun onDisable() {
saveConfig()
logger.log(Level.INFO, "Plugin disabled")
}
}
\ No newline at end of file
package me.linuscde.mcplugins.teleportastic.commands
import me.linuscde.mcplugins.teleportastic.Plugin
import me.linuscde.mcplugins.teleportastic.utils.homes
import me.linuscde.mcplugins.teleportastic.utils.lastLocation
import me.linuscde.mcplugins.teleportastic.utils.teleportTo
import me.linuscde.mcplugins.teleportastic.utils.teleportToLast
import org.bukkit.command.Command
import org.bukkit.command.CommandExecutor
import org.bukkit.command.CommandSender
import org.bukkit.entity.Player
class BackCommand: CommandExecutor {
override fun onCommand(sender: CommandSender, cmd: Command, label: String, args: Array<out String>): Boolean {
if(sender !is Player) {
sender.sendMessage("This command is only for players.")
return true
}
if(sender.teleportToLast() ) {
sender.sendMessage("${Plugin.prefix} §aTeleported back.")
}else {
sender.sendMessage("${Plugin.prefix} §cYou don't have any last location to teleport back to.")
}
return true
}
}
\ No newline at end of file
package me.linuscde.mcplugins.teleportastic.commands
import me.linuscde.mcplugins.teleportastic.Plugin
import me.linuscde.mcplugins.teleportastic.utils.homes
import me.linuscde.mcplugins.teleportastic.utils.lastLocation
import me.linuscde.mcplugins.teleportastic.utils.teleportTo
import me.linuscde.mcplugins.teleportastic.utils.teleportToLast
import org.bukkit.Bukkit
import org.bukkit.command.Command
import org.bukkit.command.CommandExecutor
import org.bukkit.command.CommandSender
import org.bukkit.entity.Player
class BedCommand: CommandExecutor {
override fun onCommand(sender: CommandSender, cmd: Command, label: String, args: Array<out String>): Boolean {
if(sender !is Player) {
sender.sendMessage("This command is only for players.")
return true
}
val bedSpawnLocation = sender.bedSpawnLocation
if (bedSpawnLocation == null) {
sender.sendMessage("${Plugin.prefix} §cFailed to find your bed spawn.")
return true
}
sender.teleportTo(bedSpawnLocation)
sender.sendMessage("${Plugin.prefix} §aTeleported to your bed spawn")
return true
}
}
\ No newline at end of file
package me.linuscde.mcplugins.teleportastic.commands
import me.linuscde.mcplugins.teleportastic.Plugin
import me.linuscde.mcplugins.teleportastic.utils.homes
import me.linuscde.mcplugins.teleportastic.utils.teleportTo
import org.bukkit.command.Command
import org.bukkit.command.CommandExecutor
import org.bukkit.command.CommandSender
import org.bukkit.command.TabCompleter
import org.bukkit.entity.Player
class HomeCommand: CommandExecutor, TabCompleter {
override fun onCommand(sender: CommandSender, cmd: Command, label: String, args: Array<out String>): Boolean {
if(sender !is Player) {
sender.sendMessage("This command is only for players.")
return true
}
if(args.size != 1) return false
val location = sender.homes
.filter { (name, _) -> name.equals(args[0], true) }
.map { (_, location) -> location }
.firstOrNull()
if(location == null) {
sender.sendMessage("${Plugin.prefix} §cHome not found.")
return true
}
sender.teleportTo(location)
sender.sendMessage("${Plugin.prefix} §aTeleported to home §c${args[0]}")
return true
}
override fun onTabComplete(sender: CommandSender, cmd: Command, label: String, args: Array<out String>): MutableList<String> {
if(sender !is Player) return arrayListOf()
if(args.size != 1) return arrayListOf()
val homeNameBeginning = args[0]
return sender.homes.filter { (name, _) -> name.startsWith(homeNameBeginning, true) }.map { (name, _) -> name }.toMutableList()
}
}
\ No newline at end of file
package me.linuscde.mcplugins.teleportastic.commands
import me.linuscde.mcplugins.teleportastic.Plugin
import me.linuscde.mcplugins.teleportastic.utils.homes
import org.bukkit.command.Command
import org.bukkit.command.CommandExecutor
import org.bukkit.command.CommandSender
import org.bukkit.entity.Player
class HomesCommand: CommandExecutor {
override fun onCommand(sender: CommandSender, cmd: Command, label: String, args: Array<out String>): Boolean {
if(sender !is Player) {
sender.sendMessage("This command is only for players.")
return true
}
sender.sendMessage(Plugin.prefix + " §aHomes: §c" + sender.homes.keys.joinToString(", "))
return true
}
}
\ No newline at end of file
package me.linuscde.mcplugins.teleportastic.commands
import me.linuscde.mcplugins.teleportastic.Plugin
import me.linuscde.mcplugins.teleportastic.utils.homes
import me.linuscde.mcplugins.teleportastic.utils.setHome
import org.bukkit.command.Command
import org.bukkit.command.CommandExecutor
import org.bukkit.command.CommandSender
import org.bukkit.command.TabCompleter
import org.bukkit.entity.Player
class SetHomeCommand: CommandExecutor, TabCompleter {
override fun onCommand(sender: CommandSender, cmd: Command, label: String, args: Array<out String>): Boolean {
if(sender !is Player) {
sender.sendMessage("This command is only for players.")
return true
}
if(args.size != 1) return false
sender.setHome(args[0], sender.location)
sender.sendMessage("${Plugin.prefix} §aHome §c${args[0]}§a was set.")
return true
}
override fun onTabComplete(sender: CommandSender, cmd: Command, label: String, args: Array<out String>): MutableList<String> {
if(sender !is Player) return arrayListOf()
if(args.size != 1) return arrayListOf()
val homeNameBeginning = args[0]
return sender.homes.filter { (name, _) -> name.startsWith(homeNameBeginning, true) }.map { (name, _) -> name }.toMutableList()
}
}
\ No newline at end of file
package me.linuscde.mcplugins.teleportastic.commands
import me.linuscde.mcplugins.teleportastic.Plugin
import me.linuscde.mcplugins.teleportastic.utils.homes
import me.linuscde.mcplugins.teleportastic.utils.lastLocation
import me.linuscde.mcplugins.teleportastic.utils.teleportTo
import me.linuscde.mcplugins.teleportastic.utils.teleportToLast
import org.bukkit.Bukkit
import org.bukkit.command.Command
import org.bukkit.command.CommandExecutor
import org.bukkit.command.CommandSender
import org.bukkit.command.TabCompleter
import org.bukkit.entity.Player
class SpawnCommand: CommandExecutor, TabCompleter {
override fun onCommand(sender: CommandSender, cmd: Command, label: String, args: Array<out String>): Boolean {
if(sender !is Player) {
sender.sendMessage("This command is only for players.")
return true
}
val world = if (sender.hasPermission("teleportastic.spawn-anywhere") && args.size == 1) {
val world = Bukkit.getWorld(args[0])
if (world == null) {
sender.sendMessage("${Plugin.prefix} §cCould not find the world §c${args[0]}")
return true
}
world
}else {
sender.world
}
sender.teleportTo(world.spawnLocation)
if (world == sender.world) {
sender.sendMessage("${Plugin.prefix} §aTeleported to spawn location of this world")
}else {
sender.sendMessage("${Plugin.prefix} §aTeleported to spawn location world §c${world.name}")
}
return true
}
override fun onTabComplete(sender: CommandSender, cmd: Command, label: String, args: Array<out String>): MutableList<String> {
if(sender !is Player) return arrayListOf()
if(args.size != 1) return arrayListOf()
if(!sender.hasPermission("teleportastic.spawn-anywhere")) return arrayListOf()
val worldNameBeginning = args[0]
return Bukkit.getWorlds().filter { it.name.startsWith(worldNameBeginning, true) }.map { it.name }.toMutableList()
}
}
\ No newline at end of file
package me.linuscde.mcplugins.teleportastic.listeners
import me.linuscde.mcplugins.teleportastic.utils.lastLocation
import org.bukkit.event.EventHandler
import org.bukkit.event.Listener
import org.bukkit.event.entity.PlayerDeathEvent
class AutoBackLocationListener: Listener {
@EventHandler fun onDeath(e: PlayerDeathEvent) {
e.entity.lastLocation = e.entity.location
}
}
\ No newline at end of file
package me.linuscde.mcplugins.teleportastic.utils
import me.linuscde.mcplugins.teleportastic.Plugin
import org.bukkit.Location
import org.bukkit.entity.Player
import java.io.File
val Player.homes: Map<String, Location>
get() {
val sec = Plugin.instance.config.getConfigurationSection("${this.uniqueId}.homes") ?: return mapOf()
return sec.getKeys(false)
.map { Pair(it, sec.getLocation(it)!!) }
.toMap()
}
fun Player.setHome(name: String, location: Location) {
Plugin.instance.config.set("${this.uniqueId}.homes.${name.replace(".", "\\.")}", location)
Plugin.instance.saveConfig()
}
package me.linuscde.mcplugins.teleportastic.utils
import me.linuscde.mcplugins.teleportastic.Plugin
import org.bukkit.Location
import org.bukkit.entity.Player
var Player.lastLocation: Location?
get() = Plugin.instance.config.getLocation("${this.uniqueId}.last_location")
set(loc: Location?) = Plugin.instance.config.set("${this.uniqueId}.last_location", loc)
fun Player.teleportToLast(): Boolean {
val lastLocation = this.lastLocation
if (lastLocation != null) {
val currentLocation = this.location
this.teleport(lastLocation)
this.lastLocation = currentLocation
return true
}
return false
}
fun Player.teleportTo(location: Location) {
this.lastLocation = this.location
this.teleport(location)
}
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment