Commit de74c639 authored by Linus K.'s avatar Linus K.
Browse files

Add tpa command and fight checking

parent 43c68b31
Pipeline #117 passed with stage
in 42 seconds
......@@ -25,4 +25,13 @@ commands:
usage: /<command>
spawn:
description: Go to the world spawn (or any as an op)
usage: /<command> [world]
\ No newline at end of file
usage: /<command> [world]
tpa:
description: Request to teleport to another player
usage: /<command> <player>
tpaccept:
description: Accept the current teleporation request
usage: /<command>
tpdeny:
description: Deny the current teleporation request
usage: /<command>
......@@ -3,6 +3,7 @@ package me.linuscde.mcplugins.teleportastic
import me.linuscde.mcplugins.teleportastic.commands.*
import me.linuscde.mcplugins.teleportastic.listeners.*
import org.bukkit.Bukkit
import org.bukkit.command.TabCompleter
import org.bukkit.plugin.java.JavaPlugin
import java.io.File
import java.util.logging.Level
......@@ -30,6 +31,7 @@ class Plugin: JavaPlugin() {
registerCommands()
registerTabCompleters()
registerListeners()
registerSchedulers()
Bukkit.getScheduler().scheduleSyncRepeatingTask(this, { saveConfig() }, 20*60*5, 20*60*5)
......@@ -43,17 +45,26 @@ class Plugin: JavaPlugin() {
getCommand("back")?.setExecutor(BackCommand())
getCommand("bed")?.setExecutor(BedCommand())
getCommand("spawn")?.setExecutor(SpawnCommand())
getCommand("tpa")?.setExecutor(TpaCommand())
getCommand("tpaccept")?.setExecutor(TpAcceptCommand())
getCommand("tpdeny")?.setExecutor(TpDenyCommand())
}
fun registerTabCompleters() {
getCommand("home")?.tabCompleter = HomeCommand()
getCommand("sethome")?.tabCompleter = SetHomeCommand()
getCommand("spawn")?.tabCompleter = SpawnCommand()
for (commandName in arrayOf("home", "sethome", "spawn", "tpaccept", "tpdeny")) {
getCommand(commandName)?.tabCompleter = getCommand(commandName)?.executor as TabCompleter
}
}
fun registerListeners() {
val pm = Bukkit.getPluginManager()
pm.registerEvents(AutoBackLocationListener(), this)
pm.registerEvents(FightListener(), this)
}
fun registerSchedulers() {
TpaCommand.initScheduler(this)
FightListener.initScheduler(this)
}
override fun onDisable() {
......
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 TpAcceptCommand: 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
}
TpaCommand.acceptRequest(sender)
return true
}
override fun onTabComplete(p0: CommandSender, p1: Command, p2: String, p3: Array<out String>) =
mutableListOf<String>()
}
\ 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 TpDenyCommand: 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
}
TpaCommand.denyRequest(sender)
return true
}
override fun onTabComplete(p0: CommandSender, p1: Command, p2: String, p3: Array<out String>) =
mutableListOf<String>()
}
\ 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 TpaCommand: CommandExecutor {
companion object {
data class TpaRequest(val sender: Player, val requested: Player, val expiresAt: Long) {
fun isValid() = System.currentTimeMillis() < expiresAt
}
val REQUESTS = arrayListOf<TpaRequest>()
const val REQUEST_DURATION = 30*1000
fun requestTpa(sender: Player, requested: Player): Boolean {
if (REQUESTS.any { it.requested == requested && it.isValid() }) {
sender.sendMessage("${Plugin.prefix} §cThis player has already a pending request.")
return false
}
if (REQUESTS.any { it.sender == sender && it.isValid() }) {
sender.sendMessage("${Plugin.prefix} §cYou have already a pending request to someone.")
return false
}
val req = TpaRequest(sender, requested, System.currentTimeMillis() + REQUEST_DURATION)
REQUESTS.add(req)
sender.sendMessage("${Plugin.prefix} §6Teleport request sent to §c${requested.name}§6.")
sender.sendMessage("${Plugin.prefix} §e${sender.name}§6 requested to teleport to you.")
sender.sendMessage("${Plugin.prefix} §6You can accept this with §c/tpaccept §a or deny it with §c/tpdeny§a.")
return true
}
fun acceptRequest(player: Player): Boolean {
val request = REQUESTS.firstOrNull { it.requested == player && it.isValid() }
if (request == null) {
player.sendMessage("${Plugin.prefix} §aYou have no request to accept.")
return false;
}
REQUESTS.remove(request);
request.sender.sendMessage("${Plugin.prefix} §c${request.requested.name}§a has accepted your request. Teleporting...");
request.requested.sendMessage("${Plugin.prefix} §aRequest from §c${request.sender.name}§a accepted. Teleporting him to you...")
request.sender.teleportTo(request.requested.location)
return true
}
fun denyRequest(player: Player): Boolean {
val request = REQUESTS.firstOrNull { it.requested == player && it.isValid() }
if (request == null) {
player.sendMessage("${Plugin.prefix} §cYou have no request to deny.")
return false;
}
REQUESTS.remove(request);
request.sender.sendMessage("${Plugin.prefix} §4${request.requested.name}§c has denied your request.");
request.requested.sendMessage("${Plugin.prefix} §aRequest from §c${request.sender.name}§a denied.")
return true
}
fun initScheduler(plugin: Plugin) {
Bukkit.getScheduler().scheduleSyncRepeatingTask(plugin, {
for(request in REQUESTS.toTypedArray()) {
if(!request.isValid()) {
request.sender.sendMessage("${Plugin.prefix} §cYour teleport request to §4${request.requested.name}§c has timed out.")
request.sender.sendMessage("${Plugin.prefix} §e${request.requested.name}§6's teleport request to you has timed out.")
REQUESTS.remove(request)
}
}
}, 20, 20)
}
}
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 player = Bukkit.getPlayer(args[0])
if(player == null) {
sender.sendMessage("${Plugin.prefix} §4${args[0]}§c is not online.")
return true
}
requestTpa(sender, player)
return true
}
}
\ No newline at end of file
package me.linuscde.mcplugins.teleportastic.listeners
import org.bukkit.Bukkit
import org.bukkit.entity.Player
import org.bukkit.event.EventHandler
import org.bukkit.event.Listener
import org.bukkit.event.entity.EntityDamageByEntityEvent
import org.bukkit.event.player.PlayerQuitEvent
class FightListener: Listener {
companion object {
val FIGHT_TIMEOUTS = HashMap<Player, Long>()
const val FIGHT_DURATION = 15*1000
fun Player.isInFight() = FIGHT_TIMEOUTS.any { it.key == this && it.value >= System.currentTimeMillis() }
fun initScheduler(plugin: me.linuscde.mcplugins.teleportastic.Plugin) {
Bukkit.getScheduler().scheduleSyncRepeatingTask(plugin, {
for((player, expiresAt) in FIGHT_TIMEOUTS.entries.toTypedArray()) {
if(expiresAt <= System.currentTimeMillis())
FIGHT_TIMEOUTS.remove(player)
}
}, 20, 20)
}
}
@EventHandler
fun onDamage(e: EntityDamageByEntityEvent) {
val player = e.entity
if(player is Player)
FIGHT_TIMEOUTS[player] = System.currentTimeMillis() + FIGHT_DURATION
}
@EventHandler
fun onLeave(e: PlayerQuitEvent) {
FIGHT_TIMEOUTS.remove(e.player)
}
}
\ No newline at end of file
package me.linuscde.mcplugins.teleportastic.utils
import me.linuscde.mcplugins.teleportastic.Plugin
import me.linuscde.mcplugins.teleportastic.listeners.FightListener
import me.linuscde.mcplugins.teleportastic.listeners.FightListener.Companion.isInFight
import org.bukkit.Location
import org.bukkit.entity.Player
......@@ -20,6 +22,10 @@ fun Player.teleportToLast(): Boolean {
}
fun Player.teleportTo(location: Location) {
if(this.isInFight()) {
this.sendMessage("${Plugin.prefix} §cTeleport failed because you're in a fight.")
return
}
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