Skip to content

Commit

Permalink
1.0.3
Browse files Browse the repository at this point in the history
  • Loading branch information
4o4E committed Feb 16, 2022
1 parent b8bab03 commit 90861be
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 90 deletions.
4 changes: 2 additions & 2 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ plugins {
}

group = "com.e404"
version = "1.0.1"
version = "1.0.3"

repositories {
maven(url = "https://hub.spigotmc.org/nexus/content/repositories/snapshots/")
Expand All @@ -17,7 +17,7 @@ repositories {
dependencies {
compileOnly("org.spigotmc:spigot-api:1.13.2-R0.1-SNAPSHOT")
// Bstats
implementation("org.bstats:bstats-bukkit:2.2.1")
implementation("org.bstats:bstats-bukkit:3.0.0")
}

tasks.withType<KotlinCompile>() {
Expand Down
8 changes: 6 additions & 2 deletions src/main/kotlin/com/e404/clean/EClean.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package com.e404.clean

import com.e404.clean.util.*
import com.e404.clean.util.Log
import com.e404.clean.util.Log.color
import com.e404.clean.util.checkPerm
import com.e404.clean.util.logo
import com.e404.clean.util.sendMsgWithPrefix
import org.bstats.bukkit.Metrics
import org.bukkit.command.Command
import org.bukkit.command.CommandSender
Expand Down Expand Up @@ -38,9 +41,10 @@ class EClean : JavaPlugin() {

override fun onEnable() {
instance = this
Metrics(instance, 13581)
Metrics(instance, 14312)
Log.info(logo())
load(null)
Update.init()
Log.info("&aEClean已启用, 作者404E".color())
}

Expand Down
63 changes: 63 additions & 0 deletions src/main/kotlin/com/e404/clean/Update.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package com.e404.clean

import com.e404.clean.util.Log
import com.e404.clean.util.Log.color
import com.e404.clean.util.instance
import com.e404.clean.util.sendMsgWithPrefix
import com.google.gson.JsonParser
import org.bukkit.Bukkit
import org.bukkit.event.EventHandler
import org.bukkit.event.Listener
import org.bukkit.event.player.PlayerJoinEvent
import java.net.URL

@Suppress("deprecation")
object Update : Listener {
private const val url = "https://api.github.com/repos/4o4E/EClean/releases"
private const val post = "https://www.mcbbs.net/thread-1305548-1-1.html"
private val jp = JsonParser()
private var latest: String? = null
private lateinit var nowVer: String

// 返回最新的版本
private fun getLatest() = jp.parse(URL(url).readText())
.asJsonArray[0]
.asJsonObject["tag_name"]
.asString!!

private fun String.asVersion() = replace(".", "").toInt()

fun init() {
nowVer = instance().description.version
Bukkit.getPluginManager().registerEvents(this, instance())
Bukkit.getScheduler().runTaskTimerAsynchronously(instance(), Runnable {
runCatching {
val v = getLatest()
val now = instance().description.version
if (v.asVersion() > now.asVersion()) {
latest = v
Log.info("""有新版本, 当前版本: &c$nowVer&f, 最新版本: &a$latest
|&f更新发布于:&b $post"""
.trimMargin()
.color())
return@runCatching
}
}.onFailure {
Log.warn("检查版本更新时出现异常, 若需要手动更新请前往&b $post")
}
Log.info("当前版本: &a${nowVer}已是最新版本")
}, 0, 20 * 60 * 60 * 6)
}

@EventHandler
fun onOpJoinGame(event: PlayerJoinEvent) {
val p = event.player
if (!p.isOp
|| latest == null
) return
p.sendMsgWithPrefix("""&f插件有更新哦, 当前版本: &c$nowVer&f, 最新版本: &a$latest
|&f更新发布于:&b $post"""
.trimMargin()
.color())
}
}
86 changes: 0 additions & 86 deletions src/main/kotlin/com/e404/clean/util/Util.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,7 @@ package com.e404.clean.util
import com.e404.clean.EClean
import com.e404.clean.util.Log.color
import org.bukkit.Bukkit
import org.bukkit.Material
import org.bukkit.command.CommandSender
import org.bukkit.command.ConsoleCommandSender
import org.bukkit.entity.Player
import org.bukkit.inventory.ItemStack

/**
* 发送带前缀并且替换&的消息
Expand Down Expand Up @@ -38,17 +34,6 @@ fun sendAllMsg(s: String) {
for (p in Bukkit.getOnlinePlayers()) p.sendMsgWithPrefix(s)
}

/**
* 获取玩家主手的物品
*
* @return 物品, 如果是AIR返回null
*/
fun Player.getItemInMainHand(): ItemStack? {
val item = inventory.itemInMainHand
if (item.type == Material.AIR) return null
return item
}

/**
* 检查权限节点, 若没有权限则发送提醒消息
*
Expand All @@ -61,66 +46,13 @@ fun CommandSender.checkPerm(perm: String): Boolean {
return false
}

/**
* 检查sender是否是玩家, 若不是玩家则发送提醒消息
*
* @return 若sender是player返回true
*/
fun CommandSender.isPlayer(): Boolean {
if (this is Player) return true
sendNotPlayer()
return false
}

/**
* 未知指令
*/
fun CommandSender.sendUnknow() {
sendMsgWithPrefix("&c未知指令, 使用&a/ew help&f查看帮助")
}

/**
* 仅玩家可用
*/
fun CommandSender.sendNotPlayer() {
sendMsgWithPrefix("&c仅玩家可用")
}

/**
* 无权限
*/
fun CommandSender.sendNoperm() {
sendMsgWithPrefix("&c无权限")
}

/**
* 无效参数
*/
fun CommandSender.sendInvalidArgs() {
sendMsgWithPrefix("&c无效参数, 使用&a/ew help&f查看帮助")
}

/**
* 给sender发送消息并在sender不是控制台时在控制台发送info
*
* @param s 消息
*/
fun CommandSender?.sendAndInfo(s: String) {
if (this !is ConsoleCommandSender) Log.info(s)
this?.sendMsgWithPrefix(s)
}

/**
* 给sender发送消息并在sender不是控制台时在控制台发送warn
*
* @param s 消息
* @param e 异常
*/
fun CommandSender?.sendAndWarn(s: String, e: Exception) {
if (this !is ConsoleCommandSender) Log.warn(s, e)
this?.sendMsgWithPrefix("&c$s")
}

/**
* 文本匹配正则列表
*
Expand All @@ -132,15 +64,6 @@ fun String.isMatch(list: List<String>): Boolean {
return false
}

/**
* 执行指令
*
* @param sender 执行的对象
*/
fun String.exec(sender: CommandSender = Bukkit.getConsoleSender()) {
Bukkit.dispatchCommand(sender, this)
}

/**
* 字符串批量替换占位符
*
Expand All @@ -153,15 +76,6 @@ fun String.placeholder(placeholder: Map<String, Any>): String {
return s
}

fun doWarnable(description: String, listener: CommandSender?, task: () -> Unit) {
try {
task()
} catch (e: Exception) {
val s = "${description}时出现异常"
listener.sendAndWarn(s, e)
}
}

fun instance() = EClean.instance
fun config() = EClean.instance.config
fun prefix() = config().getString("prefix") ?: "&7[&2清理&7]"
Expand Down

0 comments on commit 90861be

Please sign in to comment.