Skip to content
This repository has been archived by the owner on Nov 24, 2022. It is now read-only.

Implement #7 teleport delay #13

Merged
merged 17 commits into from
Jun 20, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Pre Implement home command delay
  • Loading branch information
MasahiroSaito committed Jun 18, 2017
commit 683d4e46dfbdcbdf591349364f26e0ceef86004f
6 changes: 2 additions & 4 deletions src/main/kotlin/com/masahirosaito/spigot/homes/Homes.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@ package com.masahirosaito.spigot.homes

import com.masahirosaito.spigot.homes.commands.maincommands.homecommands.HomeCommand
import com.masahirosaito.spigot.homes.datas.FeeData
import com.masahirosaito.spigot.homes.listeners.ChunkLoadListener
import com.masahirosaito.spigot.homes.listeners.ChunkUnLoadListener
import com.masahirosaito.spigot.homes.listeners.PlayerJoinListener
import com.masahirosaito.spigot.homes.listeners.PlayerRespawnListener
import com.masahirosaito.spigot.homes.listeners.*
import com.masahirosaito.spigot.homes.strings.ErrorStrings.NO_ECONOMY
import com.masahirosaito.spigot.homes.strings.ErrorStrings.NO_VAULT
import com.masahirosaito.spigot.homes.strings.Strings
Expand Down Expand Up @@ -81,5 +78,6 @@ class Homes : JavaPlugin {
PlayerJoinListener(this).register()
ChunkLoadListener(this).register()
ChunkUnLoadListener(this).register()
PlayerMoveListener(this).register()
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.masahirosaito.spigot.homes.commands.maincommands.homecommands

import com.masahirosaito.spigot.homes.Homes.Companion.homes
import com.masahirosaito.spigot.homes.Messenger
import com.masahirosaito.spigot.homes.Permission
import com.masahirosaito.spigot.homes.PlayerDataManager
import com.masahirosaito.spigot.homes.commands.CommandUsage
Expand All @@ -27,6 +28,8 @@ import com.masahirosaito.spigot.homes.strings.commands.HomeCommandStrings.USAGE_
import org.bukkit.Location
import org.bukkit.OfflinePlayer
import org.bukkit.entity.Player
import org.bukkit.metadata.FixedMetadataValue
import kotlin.concurrent.thread

class HomeCommand : MainCommand, PlayerCommand {
override val name: String = "home"
Expand Down Expand Up @@ -60,9 +63,14 @@ class HomeCommand : MainCommand, PlayerCommand {
HomeNamePlayerCommand(this)
)

init { homeCommand = this }
init {
homeCommand = this
}

companion object { lateinit var homeCommand: HomeCommand }
companion object {
val HOME_DELAY_META = "homes.delay"
lateinit var homeCommand: HomeCommand
}

override fun fee(): Double = homes.fee.HOME

Expand All @@ -71,7 +79,21 @@ class HomeCommand : MainCommand, PlayerCommand {
override fun isValidArgs(args: List<String>): Boolean = args.isEmpty()

override fun execute(player: Player, args: List<String>) {
player.teleport(getTeleportLocation(player))
if (player.hasMetadata(HOME_DELAY_META)) {
Messenger.send(player, "既にホームを実行済みです")
return
}
val th = thread {
try {
Thread.sleep(5000)
player.teleport(getTeleportLocation(player))
player.removeMetadata(HOME_DELAY_META, homes)
} catch (e: InterruptedException) {
player.removeMetadata(HOME_DELAY_META, homes)
Messenger.send(player, "ホームの実行がキャンセルされました")
}
}
player.setMetadata(HOME_DELAY_META, FixedMetadataValue(homes, th))
}

fun getTeleportLocation(player: OfflinePlayer, homeName: String? = null): Location {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.masahirosaito.spigot.homes.listeners

import com.masahirosaito.spigot.homes.Homes
import com.masahirosaito.spigot.homes.commands.maincommands.homecommands.HomeCommand
import org.bukkit.event.EventHandler
import org.bukkit.event.player.PlayerMoveEvent

class PlayerMoveListener(override val plugin: Homes) : HomesListener {
val HOME_DELAY_META: String by lazy { HomeCommand.HOME_DELAY_META }

@EventHandler
fun onPlayerMove(event: PlayerMoveEvent) {
if (!event.player.hasMetadata(HOME_DELAY_META)) return

(event.player.getMetadata(HOME_DELAY_META).first().value() as Thread).run {
if (isAlive) {
interrupt()
}
}
}
}