Commit 6df64277 authored by Linus K.'s avatar Linus K.
Browse files

Add teleport delay

parent ea7cbf69
......@@ -2,6 +2,7 @@ package me.linuscde.mcplugins.teleportastic
import me.linuscde.mcplugins.teleportastic.commands.*
import me.linuscde.mcplugins.teleportastic.listeners.*
import me.linuscde.mcplugins.teleportastic.utils.TeleportingPlayer
import org.bukkit.Bukkit
import org.bukkit.command.TabCompleter
import org.bukkit.plugin.java.JavaPlugin
......@@ -66,6 +67,7 @@ class Plugin: JavaPlugin() {
fun registerSchedulers() {
TpaCommand.initScheduler(this)
FightListener.initScheduler(this)
TeleportingPlayer.initializeScheduler(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 me.linuscde.mcplugins.teleportastic.utils.*
import org.bukkit.command.Command
import org.bukkit.command.CommandExecutor
import org.bukkit.command.CommandSender
......@@ -18,11 +15,17 @@ class BackCommand: CommandExecutor {
return true
}
if(sender.teleportToLast() ) {
if(sender.teleportDurationTicks > 0)
sender.sendMessage("${Plugin.prefix} §6Starting teleport. Please be patient...")
sender.teleportToLast( onSuccess = {
sender.sendMessage("${Plugin.prefix} §aTeleported back.")
}else {
sender.sendMessage("${Plugin.prefix} §cYou don't have any last location to teleport back to.")
}
}, onFail = {
if(sender.lastLocation == null) {
sender.sendMessage("${Plugin.prefix} §cYou don't have any last location to teleport back to.")
}else {
getDefaultTeleportFailHandler(sender)()
}
})
return true
}
......
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 me.linuscde.mcplugins.teleportastic.utils.*
import org.bukkit.Bukkit
import org.bukkit.command.Command
import org.bukkit.command.CommandExecutor
......@@ -25,8 +22,11 @@ class BedCommand: CommandExecutor {
return true
}
sender.teleportTo(bedSpawnLocation)
sender.sendMessage("${Plugin.prefix} §aTeleported to your bed spawn")
if(sender.teleportDurationTicks > 0)
sender.sendMessage("${Plugin.prefix} §6Starting teleport. Please be patient...")
sender.teleportTo(bedSpawnLocation, {
sender.sendMessage("${Plugin.prefix} §aTeleported to your bed spawn")
})
return true
}
......
......@@ -2,6 +2,7 @@ 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.teleportDurationTicks
import me.linuscde.mcplugins.teleportastic.utils.teleportTo
import org.bukkit.command.Command
import org.bukkit.command.CommandExecutor
......@@ -27,9 +28,11 @@ class HomeCommand: CommandExecutor, TabCompleter {
sender.sendMessage("${Plugin.prefix} §cHome not found.")
return true
}
sender.teleportTo(location)
sender.sendMessage("${Plugin.prefix} §aTeleported to home §c${args[0]}")
if(sender.teleportDurationTicks > 0)
sender.sendMessage("${Plugin.prefix} §6Starting teleport. Please be patient...")
sender.teleportTo(location, {
sender.sendMessage("${Plugin.prefix} §aTeleported to home §c${args[0]}")
})
return true
}
......
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 me.linuscde.mcplugins.teleportastic.utils.*
import org.bukkit.Bukkit
import org.bukkit.command.Command
import org.bukkit.command.CommandExecutor
......@@ -31,13 +28,15 @@ class SpawnCommand: CommandExecutor, TabCompleter {
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}")
}
if(sender.teleportDurationTicks > 0)
sender.sendMessage("${Plugin.prefix} §6Starting teleport. Please be patient...")
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
}
......
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 me.linuscde.mcplugins.teleportastic.utils.*
import net.md_5.bungee.api.ChatColor
import net.md_5.bungee.api.chat.ClickEvent
import net.md_5.bungee.api.chat.HoverEvent
......@@ -69,7 +66,11 @@ class TpaCommand: CommandExecutor {
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)
request.sender.teleportTo(request.requested.location, {
if(request.sender.teleportDurationTicks > 0)
request.sender.sendMessage("${Plugin.prefix} §aTeleported to §c${request.requested.name}")
})
return true
}
......
package me.linuscde.mcplugins.teleportastic.listeners
import org.bukkit.Bukkit
import org.bukkit.entity.LivingEntity
import org.bukkit.entity.Player
import org.bukkit.event.EventHandler
import org.bukkit.event.Listener
......@@ -11,6 +12,7 @@ class FightListener: Listener {
companion object {
val FIGHT_TIMEOUTS = HashMap<Player, Long>()
val FIGHT_LEAVE_MESSAGE = HashMap<Player, String>()
const val FIGHT_DURATION = 15*1000
fun Player.isInFight() = FIGHT_TIMEOUTS.any { it.key == this && it.value >= System.currentTimeMillis() }
......@@ -18,8 +20,13 @@ class FightListener: Listener {
fun initScheduler(plugin: me.linuscde.mcplugins.teleportastic.Plugin) {
Bukkit.getScheduler().scheduleSyncRepeatingTask(plugin, {
for((player, expiresAt) in FIGHT_TIMEOUTS.entries.toTypedArray()) {
if(expiresAt <= System.currentTimeMillis())
if(expiresAt <= System.currentTimeMillis()) {
FIGHT_TIMEOUTS.remove(player)
if(player in FIGHT_LEAVE_MESSAGE) {
player.sendMessage(FIGHT_LEAVE_MESSAGE[player]!!)
FIGHT_LEAVE_MESSAGE.remove(player)
}
}
}
}, 20, 20)
}
......@@ -28,13 +35,14 @@ class FightListener: Listener {
@EventHandler
fun onDamage(e: EntityDamageByEntityEvent) {
val player = e.entity
if(player is Player)
if(player is Player && e.damager is LivingEntity)
FIGHT_TIMEOUTS[player] = System.currentTimeMillis() + FIGHT_DURATION
}
@EventHandler
fun onLeave(e: PlayerQuitEvent) {
FIGHT_TIMEOUTS.remove(e.player)
FIGHT_LEAVE_MESSAGE.remove(e.player)
}
}
\ No newline at end of file
......@@ -3,29 +3,112 @@ 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.Bukkit
import org.bukkit.Location
import org.bukkit.entity.Player
import kotlin.math.abs
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 {
fun Player.teleportToLast(onSuccess: () -> Unit = {}, onFail: () -> Unit = {}) {
if(this.isInFight()) {
onFail()
FightListener.FIGHT_LEAVE_MESSAGE[this] = "${Plugin.prefix} §aYou can now teleport again."
}
val lastLocation = this.lastLocation
if (lastLocation != null) {
val currentLocation = this.location
this.teleport(lastLocation)
this.lastLocation = currentLocation
return true
TeleportingPlayer(this, currentLocation, lastLocation, 0L, {
this.lastLocation = currentLocation
onSuccess()
}, onFail)
}
return false
}
fun Player.teleportTo(location: Location) {
fun Player.teleportTo(location: Location, onSuccess: () -> Unit = {}, onFail: () -> Unit = getDefaultTeleportFailHandler(this)) {
if(this.isInFight()) {
this.sendMessage("${Plugin.prefix} §cTeleport failed because you're in a fight.")
onFail()
FightListener.FIGHT_LEAVE_MESSAGE[this] = "${Plugin.prefix} §aYou can now teleport again."
return
}
this.lastLocation = this.location
this.teleport(location)
val startLocation = this.location
TeleportingPlayer(this, startLocation, location, 0L, {
this.lastLocation = startLocation
onSuccess()
}, onFail = onFail);
}
val Player.teleportDurationTicks
get() = if(this.isOp) 0L else 20L*5
fun getDefaultTeleportFailHandler(player: Player): () -> Unit = {
if(player.isInFight()) {
player.sendMessage("${Plugin.prefix} §cTeleport failed because you are in a fight.")
}else {
player.sendMessage("${Plugin.prefix} §cTeleport failed. Please stand still while teleporting.")
}
}
data class TeleportingPlayer(val player: Player, val startLocation: Location,
val targetLocation: Location, var ticksPassed: Long,
val onSuccess: () -> Unit, val onFail: () -> Unit) {
init {
TELEPORTING_PLAYERS.add(this)
}
companion object {
private val TELEPORTING_PLAYERS = arrayListOf<TeleportingPlayer>()
fun initializeScheduler(plugin: org.bukkit.plugin.Plugin) {
val tickDelta = 2L
Bukkit.getScheduler().scheduleSyncRepeatingTask(plugin, {
for(tp in TELEPORTING_PLAYERS.toTypedArray()) {
if(!tp.player.isOnline) {
TELEPORTING_PLAYERS.remove(tp)
continue
}
val startLoc = tp.startLocation
val loc = tp.player.location
// Check if player moved too much
if (abs(loc.x - startLoc.x) > 0.1 || abs(loc.y - startLoc.y) > 0.1 || abs(loc.z - startLoc.z) > 0.1) {
tp.player.sendTitle("", " ", 0, 1, 0)
TELEPORTING_PLAYERS.remove(tp)
tp.onFail()
continue
}
val lastDurationInSecs = tp.ticksPassed / 20
tp.ticksPassed += tickDelta
val currentDurationInSecs = tp.ticksPassed / 20
val teleportTicks = tp.player.teleportDurationTicks
if((tp.ticksPassed == tickDelta || lastDurationInSecs != currentDurationInSecs) && tp.ticksPassed < teleportTicks) {
val remainSecs = teleportTicks / 20 - currentDurationInSecs
tp.player.sendTitle("", "§6Teleporting in §e$remainSecs second${if(remainSecs != 1L) "s" else " "}", 0, 20, 0)
}
// Teleport player
if(tp.ticksPassed >= teleportTicks) {
tp.player.sendTitle("", " ", 0, 1, 0)
if (tp.player.teleport(tp.targetLocation)) {
tp.onSuccess()
} else {
tp.onFail()
}
TELEPORTING_PLAYERS.remove(tp)
}
}
}, tickDelta, tickDelta)
}
}
}
\ No newline at end of file
Supports Markdown
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

Du besuchst diese Seite mit einem veralteten IPv4-Internetzugang. Möglicherweise treten in Zukunft Probleme mit der Erreichbarkeit und Performance auf. Bitte Frage deinen Internetanbieter oder Netzwerkadministrator nach IPv6-Unterstützung.
You are visiting this site with an outdated IPv4 internet access. You may experience problems with accessibility and performance in the future. Please ask your ISP or network administrator for IPv6 support.
Weitere Infos | More Information
Klicke zum schließen | Click to close