From df6816d2a838d52824d0931287ffdfa8d0104283 Mon Sep 17 00:00:00 2001 From: Vaishnav Date: Wed, 22 Jul 2020 18:32:50 +0530 Subject: [PATCH] Cleaned up Actions --- .../frcsty/frozenjoin/action/ActionHandler.kt | 82 +++++++------- .../action/actions/BroadcastAction.kt | 3 +- .../action/actions/MessageAction.kt | 3 +- .../frcsty/frozenjoin/action/time/TimeAPI.kt | 104 ++++++++---------- .../frozenjoin/action/time/TimeScanner.kt | 22 ++-- 5 files changed, 102 insertions(+), 112 deletions(-) diff --git a/src/main/kotlin/com/github/frcsty/frozenjoin/action/ActionHandler.kt b/src/main/kotlin/com/github/frcsty/frozenjoin/action/ActionHandler.kt index 4d86dff..587a71e 100644 --- a/src/main/kotlin/com/github/frcsty/frozenjoin/action/ActionHandler.kt +++ b/src/main/kotlin/com/github/frcsty/frozenjoin/action/ActionHandler.kt @@ -12,60 +12,60 @@ import java.util.regex.Matcher import java.util.regex.Pattern import java.util.stream.Stream +private val ACTION_PATTERN = Pattern.compile("(.*) ?\\[(?[A-Z]+?)] ?(?.+)", Pattern.CASE_INSENSITIVE) +private val DELAY_PATTERN = Pattern.compile("\\[DELAY=(?\\d+[a-z])]", Pattern.CASE_INSENSITIVE) +private val CHANCE_PATTERN = Pattern.compile("\\[CHANCE=(?\\d+)]", Pattern.CASE_INSENSITIVE) +private val RANDOM = SplittableRandom() + class ActionHandler(private val plugin: FrozenJoinPlugin) { - companion object { - private val ACTION_PATTERN = Pattern.compile("(.*) ?\\[(?[A-Z]+?)] ?(?.+)", Pattern.CASE_INSENSITIVE) - private val DELAY_PATTERN = Pattern.compile("\\[DELAY=(?\\d+[a-z])]", Pattern.CASE_INSENSITIVE) - private val CHANCE_PATTERN = Pattern.compile("\\[CHANCE=(?\\d+)]", Pattern.CASE_INSENSITIVE) - private val RANDOM = SplittableRandom() - } + private val actions: Map = setOf ( + MessageAction(), + BroadcastAction() + ).associateBy { it.id } - private val actions: MutableMap = mutableMapOf() - private var matcher: Matcher? = null - private var chanceMatcher: Matcher? = null - private var delayMatcher: Matcher? = null + private val matcher: Matcher = ACTION_PATTERN.matcher("") + private val chanceMatcher: Matcher = CHANCE_PATTERN.matcher("") + private val delayMatcher: Matcher = DELAY_PATTERN.matcher("") - init { - load() - } fun execute(player: Player, input: List) { input.forEach{ execute(player, it) } } fun execute(player: Player, input: String) { - var input = hasChanceAction(input) ?: return - val holder = getDelayAction(input) - input = holder.action - - if (matcher == null) { - matcher = ACTION_PATTERN.matcher(input) - } else { - matcher.reset(input) - } + val actionHolder = getDelayAction( + hasChanceAction(input) ?: return + ) + val inputAction = actionHolder.action + matcher.reset(inputAction) if (!matcher.matches()) { - println("Action does not matches regex $input") + println("Action does not matches regex $inputAction") return } + val arguments = matcher.group("arguments") - val delay = holder.delay + val delay = actionHolder.delay - val action: Action = actions[matcher.group("action").toUpperCase()] ?: return - Bukkit.getScheduler().runTaskLater(plugin, Runnable { action.run(player, arguments) }, delay) + val actionName = matcher.group("action").toUpperCase() + + val action: Action = actions[actionName] ?: return + Bukkit.getScheduler().runTaskLater( + plugin, + Runnable { action.run(player, arguments) }, + delay + ) } private fun hasChanceAction(input: String): String? { - if (chanceMatcher == null) { - chanceMatcher = CHANCE_PATTERN.matcher(input) - } else { - chanceMatcher.reset(input) - } + chanceMatcher.reset(input) if (!chanceMatcher.find()) { return input } - val chance: Int = Integer.valueOf(chanceMatcher.group("chance")) + val chance: Int = Integer.valueOf( + chanceMatcher.group("chance") + ) val value: Int = RANDOM.nextInt(100) + 1 return if (value <= chance) { input.replace(chanceMatcher.group(), "") @@ -73,28 +73,20 @@ class ActionHandler(private val plugin: FrozenJoinPlugin) { } private fun getDelayAction(input: String): ActionHolder { - if (delayMatcher == null) { - delayMatcher = DELAY_PATTERN.matcher(input) - } else { - delayMatcher.reset(input) - } + delayMatcher.reset(input) if (!delayMatcher.find()) { return ActionHolder(input, 0L) } val delay = delayMatcher.group("delay") return try { val time = TimeAPI(delay) - ActionHolder(input.replace(delayMatcher.group(), ""), time.getSeconds() * 20L) + ActionHolder( + action = input.replace(delayMatcher.group(), ""), + delay = time.seconds * 20L + ) } catch (ex: IllegalArgumentException) { ex.printStackTrace() ActionHolder(input, 0L) } } - - private fun load() { - Stream.of( - MessageAction(), - BroadcastAction() - ).forEach { actions[it.id] = it } - } } \ No newline at end of file diff --git a/src/main/kotlin/com/github/frcsty/frozenjoin/action/actions/BroadcastAction.kt b/src/main/kotlin/com/github/frcsty/frozenjoin/action/actions/BroadcastAction.kt index 6c3f1a3..54c6f57 100644 --- a/src/main/kotlin/com/github/frcsty/frozenjoin/action/actions/BroadcastAction.kt +++ b/src/main/kotlin/com/github/frcsty/frozenjoin/action/actions/BroadcastAction.kt @@ -3,8 +3,7 @@ package com.github.frcsty.frozenjoin.action.actions import org.bukkit.entity.Player class BroadcastAction : Action { - override val id: String - get() = "BROADCAST" + override val id: String = "BROADCAST" override fun run(player: Player, data: String) { for (i in 0..4) { diff --git a/src/main/kotlin/com/github/frcsty/frozenjoin/action/actions/MessageAction.kt b/src/main/kotlin/com/github/frcsty/frozenjoin/action/actions/MessageAction.kt index 983e244..48f6259 100644 --- a/src/main/kotlin/com/github/frcsty/frozenjoin/action/actions/MessageAction.kt +++ b/src/main/kotlin/com/github/frcsty/frozenjoin/action/actions/MessageAction.kt @@ -3,8 +3,7 @@ package com.github.frcsty.frozenjoin.action.actions import org.bukkit.entity.Player class MessageAction : Action { - override val id: String - get() = "MESSAGE" + override val id: String = "MESSAGE" override fun run(player: Player, data: String) { println(data) diff --git a/src/main/kotlin/com/github/frcsty/frozenjoin/action/time/TimeAPI.kt b/src/main/kotlin/com/github/frcsty/frozenjoin/action/time/TimeAPI.kt index 7543133..6cfd1a1 100644 --- a/src/main/kotlin/com/github/frcsty/frozenjoin/action/time/TimeAPI.kt +++ b/src/main/kotlin/com/github/frcsty/frozenjoin/action/time/TimeAPI.kt @@ -2,69 +2,61 @@ package com.github.frcsty.frozenjoin.action.time import java.util.concurrent.TimeUnit -class TimeAPI { - private var seconds: Long = 0 +private const val DAYS_IN_WEEK: Long = 7 +private const val DAYS_IN_MONTH: Long = 30 +private const val DAYS_IN_YEAR: Long = 365 - constructor(time: String) { - reparse(time) - } +class TimeAPI(val seconds: Long) { + constructor(time: String): this(parseTime(time)) - constructor(seconds: Long) { - this.seconds = seconds + val nanoseconds: Double by lazy { + TimeUnit.SECONDS.toNanos(seconds).toDouble() } - fun reparse(time: String): TimeAPI { - seconds = 0 - val scanner = TimeScanner(time - .replace(" ", "") - .replace("and", "") - .replace(",", "") - .toLowerCase()) - var next: Long - while (scanner.hasNext()) { - next = scanner.nextLong() - seconds += when (scanner.nextString()) { - "s", "sec", "secs", "second", "seconds" -> next - "m", "min", "mins", "minute", "minutes" -> TimeUnit.MINUTES.toSeconds(next) - "h", "hr", "hrs", "hour", "hours" -> TimeUnit.HOURS.toSeconds(next) - "d", "dy", "dys", "day", "days" -> TimeUnit.DAYS.toSeconds(next) - "w", "week", "weeks" -> TimeUnit.DAYS.toSeconds(next * DAYS_IN_WEEK) - "mo", "mon", "mnth", "month", "months" -> TimeUnit.DAYS.toSeconds(next * DAYS_IN_MONTH) - "y", "yr", "yrs", "year", "years" -> TimeUnit.DAYS.toSeconds(next * DAYS_IN_YEAR) - else -> throw IllegalArgumentException() - } - } - return this + val microseconds: Double by lazy { + TimeUnit.SECONDS.toMicros(seconds).toDouble() } - val nanoseconds: Double - get() = TimeUnit.SECONDS.toNanos(seconds).toDouble() - - val microseconds: Double - get() = TimeUnit.SECONDS.toMicros(seconds).toDouble() - - val milliseconds: Double - get() = TimeUnit.SECONDS.toMillis(seconds).toDouble() - - fun getSeconds(): Long { - return seconds + val milliseconds: Double by lazy { + TimeUnit.SECONDS.toMillis(seconds).toDouble() } - val minutes: Double get() = seconds / 60.0 - - val hours: Double get() = seconds / 3600.0 - - val days: Double get() = seconds / 86400.0 - - val weeks: Double get() = days / DAYS_IN_WEEK - - val months: Double get() = days / DAYS_IN_MONTH - - val years: Double get() = days / DAYS_IN_YEAR - - companion object { - private const val DAYS_IN_WEEK: Long = 7 - private const val DAYS_IN_MONTH: Long = 30 - private const val DAYS_IN_YEAR: Long = 365 + val minutes: Double by lazy { seconds / 60.0 } + + val hours: Double by lazy { seconds / 3600.0 } + + val days: Double by lazy { seconds / 86400.0 } + + val weeks: Double by lazy { days/ DAYS_IN_WEEK } + + val months: Double by lazy { days/ DAYS_IN_MONTH } + + val years: Double by lazy { days/ DAYS_IN_YEAR } +} + + +private fun parseTime(time: String): Long { + var seconds = 0L + val scanner = TimeScanner( + time + .replace(" ", "") + .replace("and", "") + .replace(",", "") + .toLowerCase() + ) + var next: Long + while (scanner.hasNext()) { + next = scanner.nextLong() + seconds += when (scanner.nextString()) { + "s", "sec", "secs", "second", "seconds" -> next + "m", "min", "mins", "minute", "minutes" -> TimeUnit.MINUTES.toSeconds(next) + "h", "hr", "hrs", "hour", "hours" -> TimeUnit.HOURS.toSeconds(next) + "d", "dy", "dys", "day", "days" -> TimeUnit.DAYS.toSeconds(next) + "w", "week", "weeks" -> TimeUnit.DAYS.toSeconds(next * DAYS_IN_WEEK) + "mo", "mon", "mnth", "month", "months" -> TimeUnit.DAYS.toSeconds(next * DAYS_IN_MONTH) + "y", "yr", "yrs", "year", "years" -> TimeUnit.DAYS.toSeconds(next * DAYS_IN_YEAR) + else -> throw IllegalArgumentException() + } } + return seconds } \ No newline at end of file diff --git a/src/main/kotlin/com/github/frcsty/frozenjoin/action/time/TimeScanner.kt b/src/main/kotlin/com/github/frcsty/frozenjoin/action/time/TimeScanner.kt index d9c48fe..8ce839e 100644 --- a/src/main/kotlin/com/github/frcsty/frozenjoin/action/time/TimeScanner.kt +++ b/src/main/kotlin/com/github/frcsty/frozenjoin/action/time/TimeScanner.kt @@ -4,27 +4,35 @@ import java.util.* import java.util.function.Predicate internal class TimeScanner(time: String) { - private val time: CharArray + private val time: CharArray = time.toCharArray() private var index = 0 + operator fun hasNext(): Boolean { return index < time.size - 1 } fun nextLong(): Long { - return String(next(Predicate { ch: Char? -> Character.isDigit(ch!!) })).toLong() + return String( + next( + Predicate(Char::isDigit) + ) + ).toLong() } fun nextString(): String { - return String(next(Predicate { codePoint: Char -> Character.isAlphabetic(codePoint.toInt()) })) + return String( + next( + Predicate { codePoint: Char -> + Character.isAlphabetic(codePoint.toInt()) + } + ) + ) } private fun next(whichSatisfies: Predicate): CharArray { val startIndex = index while (++index < time.size && whichSatisfies.test(time[index])); - return Arrays.copyOfRange(time, startIndex, index) + return time.copyOfRange(startIndex, index) } - init { - this.time = time.toCharArray() - } } \ No newline at end of file