Skip to content

Commit

Permalink
update: 额外的地图展示逻辑 #18
Browse files Browse the repository at this point in the history
  • Loading branch information
BingZi-233 committed May 21, 2024
1 parent 028fdfb commit 44cac9e
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 6 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
group=online.bingzi.bilibili.video
version=1.5.12
version=1.6.0
kotlin.experimental.tryK2=true
kapt.use.k2=true
kotlin.incremental=true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ object SettingConfig {
*/
var chainOperations: List<ApiType> = listOf()

/**
* Virtualization
* 是否虚拟化地图
*/
var virtualization: Boolean = true

@Awake(LifeCycle.ENABLE)
fun registerAutoReload() {
config.onReload { reloadAction() }
Expand All @@ -44,5 +50,7 @@ object SettingConfig {
chainOperations = config.getEnumList("chainOperations", ApiType::class.java)
// 调试模式是否开启
debugStatus = config.getBoolean("debug")
// 虚拟化地图
virtualization = config.getBoolean("virtualization")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import taboolib.common.platform.ProxyPlayer
import taboolib.common.platform.function.submit
import taboolib.common.platform.function.warning
import taboolib.module.chat.colored
import taboolib.module.nms.NMSMap

/**
* Network engine
Expand All @@ -34,7 +33,8 @@ object NetworkEngine {
* 哔哩哔哩API驱动
*/
val bilibiliAPI: BilibiliApiDrive by lazy {
Retrofit.Builder().baseUrl("https://api.bilibili.com/x/").addConverterFactory(GsonConverterFactory.create()).client(client).build().create(BilibiliApiDrive::class.java)
Retrofit.Builder().baseUrl("https://api.bilibili.com/x/").addConverterFactory(GsonConverterFactory.create()).client(client).build()
.create(BilibiliApiDrive::class.java)
}

/**
Expand All @@ -51,7 +51,8 @@ object NetworkEngine {
* 哔哩哔哩网站驱动API
*/
val bilibiliWebsiteAPI: BilibiliPassportDrive by lazy {
Retrofit.Builder().baseUrl("https://www.bilibili.com/").addConverterFactory(GsonConverterFactory.create()).client(client).build().create(BilibiliPassportDrive::class.java)
Retrofit.Builder().baseUrl("https://www.bilibili.com/").addConverterFactory(GsonConverterFactory.create()).client(client).build()
.create(BilibiliPassportDrive::class.java)
}

/**
Expand All @@ -77,7 +78,7 @@ object NetworkEngine {
val body = response.body()
if (body != null && body.code == 0) {
// 向玩家副手发送二维码地图
player.sendMapVersionCompatible(body.data.url.toBufferedImage(128), NMSMap.Hand.MAIN) {
player.sendMap(body.data.url.toBufferedImage(128)) {
name = "&a&lBilibili扫码登陆".colored()
shiny()
lore.clear()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,34 @@
package online.bingzi.bilibili.video.internal.helper

import online.bingzi.bilibili.video.internal.config.SettingConfig
import org.bukkit.Bukkit
import org.bukkit.entity.Player
import org.bukkit.map.MapCanvas
import org.bukkit.map.MapRenderer
import org.bukkit.map.MapView
import taboolib.common.platform.ProxyPlayer
import taboolib.common.util.unsafeLazy
import taboolib.library.reflex.Reflex.Companion.invokeMethod
import taboolib.library.xseries.XMaterial
import taboolib.module.nms.MinecraftVersion
import taboolib.module.nms.NMSMap
import taboolib.module.nms.buildMap
import taboolib.module.nms.sendMap
import taboolib.platform.util.ItemBuilder
import taboolib.platform.util.buildItem
import taboolib.platform.util.isNotAir
import taboolib.type.BukkitEquipment
import java.awt.image.BufferedImage

fun ProxyPlayer.sendMapVersionCompatible(
fun ProxyPlayer.sendMap(image: BufferedImage, builder: ItemBuilder.() -> Unit = {}) {
if (SettingConfig.virtualization) {
this.sendMapVersionCompatible(image, builder = builder)
} else {
this.castSafely<Player>()?.buildMapItem(image, builder = builder)
}
}

private fun ProxyPlayer.sendMapVersionCompatible(
image: BufferedImage,
hand: NMSMap.Hand = NMSMap.Hand.MAIN,
width: Int = 128,
Expand All @@ -32,3 +51,33 @@ fun ProxyPlayer.sendMapVersionCompatible(
}
}
}

private fun Player.buildMapItem(image: BufferedImage, builder: ItemBuilder.() -> Unit = {}) {
val handItem = BukkitEquipment.HAND.getItem(this)
if (handItem.isNotAir()) {
this.sendMessage("请清空主手物品")
return
}
val mapRenderer = object : MapRenderer() {

var rendered = false

override fun render(mapView: MapView, mapCanvas: MapCanvas, player: Player) {
if (rendered) {
return
}
mapCanvas.drawImage(0, 0, image)
rendered = true
}
}
val mapView by unsafeLazy {
val mapView = Bukkit.createMap(Bukkit.getWorlds()[0])
mapView.addRenderer(mapRenderer)
mapView
}
val mapItem = buildItem(XMaterial.FILLED_MAP) {
damage = mapView.invokeMethod<Short>("getId")!!.toInt()
builder(this)
}
BukkitEquipment.HAND.setItem(player, mapItem)
}
5 changes: 5 additions & 0 deletions src/main/resources/setting.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@
# 单位: 秒
cooldown: 60

# 地图虚拟化
# 虚拟化之后将会有更高的安全性,但兼容性较差
# 若无法正常获取地图,请设置为false关闭虚拟化
virtualization: true

chainOperations:
- COINS
- LIKE
Expand Down

0 comments on commit 44cac9e

Please sign in to comment.