Skip to content

Commit

Permalink
update: 修复1.20/1.21地图不显示故障
Browse files Browse the repository at this point in the history
  • Loading branch information
BingZi-233 committed Oct 31, 2024
1 parent e3e5a32 commit c351cf5
Show file tree
Hide file tree
Showing 3 changed files with 148 additions and 9 deletions.
18 changes: 12 additions & 6 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

plugins {
java
id("io.izzel.taboolib") version "2.0.12"
id("io.izzel.taboolib") version "2.0.19"
id("org.jetbrains.kotlin.jvm") version "1.9.23"
}

Expand All @@ -23,13 +23,19 @@ taboolib {
}
}
env {
install(KETHER, DATABASE, METRICS, UI)
install(NMS, NMS_UTIL)
install(UNIVERSAL)
install(BUKKIT_ALL)
install(Kether)
install(Database)
install(Metrics)
install(Basic)
install(Bukkit)
install(BukkitNMS)
install(BukkitNMSUtil)
install(BukkitNMSItemTag)
install(CommandHelper)
install(BukkitHook)
}
version {
taboolib = "6.1.2-beta10"
taboolib = "6.2.0-beta26"
}
relocate("com.google.zxing", "online.bingzi.libs.zxing")
relocate("com.google.gson", "online.bingzi.libs.gson")
Expand Down
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.6.3-beta2
version=1.6.4
kotlin.incremental=true
kotlin.incremental.java=true
kotlin.caching.enabled=true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,24 @@ import org.bukkit.map.MapRenderer
import org.bukkit.map.MapView
import taboolib.common.platform.function.submit
import taboolib.common.util.unsafeLazy
import taboolib.library.reflex.Reflex.Companion.getProperty
import taboolib.library.reflex.Reflex.Companion.invokeConstructor
import taboolib.library.reflex.Reflex.Companion.invokeMethod
import taboolib.library.reflex.Reflex.Companion.setProperty
import taboolib.library.reflex.Reflex.Companion.unsafeInstance
import taboolib.library.xseries.XMaterial
import taboolib.module.nms.MinecraftVersion
import taboolib.module.nms.nmsClass
import taboolib.module.nms.obcClass
import taboolib.module.nms.sendPacket
import taboolib.platform.util.ItemBuilder
import taboolib.platform.util.buildItem
import taboolib.platform.util.modifyMeta
import java.awt.image.BufferedImage
import java.io.File
import java.lang.reflect.Array
import java.net.URL
import java.util.*
import java.util.concurrent.CompletableFuture
import javax.imageio.ImageIO

Expand Down Expand Up @@ -188,7 +195,22 @@ class NMSMap(val image: BufferedImage, var hand: Hand = Hand.MAIN, val builder:
val classPacketPlayOutMap = nmsClass("PacketPlayOutMap")
val classCraftItemStack = obcClass("inventory.CraftItemStack")
val classMapIcon by unsafeLazy { nmsClass("MapIcon") }
val classMapData: Class<*> by unsafeLazy { Class.forName("net.minecraft.world.level.saveddata.maps.WorldMap\$b") }

/**
* 鸣谢 [YsGqHY](https://github.com/YsGqHY) 提供的包寻找方法
*/
val classMapData: Class<*> by unsafeLazy {
try {
// 尝试找Spigot的WorldMap.b
Class.forName("net.minecraft.world.level.saveddata.maps.WorldMap\$b")
} catch (e: ClassNotFoundException) {
// 没有找到Spigot的WorldMap.b,尝试找Paper的MapItemSavedData.MapPatch
Class.forName("net.minecraft.world.level.saveddata.maps.MapItemSavedData\$MapPatch")
}
}

// 高版本把MapId改成了,这个东西主要是对1.20+的兼容
val classMapId: Class<*> by unsafeLazy { Class.forName("net.minecraft.world.level.saveddata.maps.MapId") }
}

val mapRenderer = object : MapRenderer() {
Expand Down Expand Up @@ -228,7 +250,118 @@ class NMSMap(val image: BufferedImage, var hand: Hand = Hand.MAIN, val builder:

fun sendTo(player: Player) {
submit(delay = 3) {
player.sendVirtualItem(mapItem)
val container = if (MinecraftVersion.isUniversal) {
player.getProperty<Any>("entity/inventoryMenu")
} else {
player.getProperty<Any>("entity/defaultContainer")
}!!
val windowsId = if (MinecraftVersion.isUniversal) {
container.getProperty<Int>("containerId")
} else {
container.getProperty<Int>("windowId")
}!!
val nmsItem = classCraftItemStack.invokeMethod<Any>("asNMSCopy", mapItem, isStatic = true)
player.sendPacket(classPacketPlayOutSetSlot.unsafeInstance().also {
if (MinecraftVersion.isUniversal) {
it.setProperty("containerId", windowsId)
it.setProperty("stateId", 1)
it.setProperty("slot", getMainHandSlot(player))
it.setProperty("itemStack", nmsItem)
} else {
it.setProperty("a", windowsId)
it.setProperty("b", getMainHandSlot(player))
it.setProperty("c", nmsItem)
}
})
val buffer = mapView.invokeMethod<Any>("render", player)!!.getProperty<ByteArray>("buffer")
val packet = classPacketPlayOutMap.unsafeInstance()
when {
// 1.21+
MinecraftVersion.isHigherOrEqual(MinecraftVersion.V1_21) -> {
// 1.21+这里被改成了MapId对象
packet.setProperty("mapId", classMapId.invokeConstructor(mapView.id))
packet.setProperty("scale", mapView.scale.value)
packet.setProperty("locked", false)
// 1.21+这里被Optional包裹了
packet.setProperty("decorations", Optional.empty<List<Any>>())
packet.setProperty("colorPatch", Optional.of(classMapData.unsafeInstance().also {
it.setProperty("startX", 0)
it.setProperty("startY", 0)
it.setProperty("width", 128)
it.setProperty("height", 128)
it.setProperty("mapColors", buffer)
}))
}
// 1.20+
MinecraftVersion.isHigherOrEqual(MinecraftVersion.V1_20) -> {
packet.setProperty("mapId", mapView.id)
packet.setProperty("scale", mapView.scale.value)
packet.setProperty("locked", false)
packet.setProperty("decorations", ArrayList<Any>())
packet.setProperty("colorPatch", classMapData.unsafeInstance().also {
it.setProperty("startX", 0)
it.setProperty("startY", 0)
it.setProperty("width", 128)
it.setProperty("height", 128)
it.setProperty("mapColors", buffer)
})
}
// 1.17+
MinecraftVersion.isUniversal -> {
packet.setProperty("mapId", (mapItem.itemMeta as MapMeta).mapId)
packet.setProperty("scale", mapView.scale.value)
packet.setProperty("locked", false)
packet.setProperty("decorations", ArrayList<Any>())
packet.setProperty("colorPatch", classMapData.unsafeInstance().also {
it.setProperty("startX", 0)
it.setProperty("startY", 0)
it.setProperty("width", 128)
it.setProperty("height", 128)
it.setProperty("mapColors", buffer)
})
}
// 1.14+
MinecraftVersion.isHigherOrEqual(MinecraftVersion.V1_14) -> {
packet.setProperty("a", (mapItem.itemMeta as MapMeta).mapId)
packet.setProperty("b", mapView.scale.value)
packet.setProperty("c", false)
packet.setProperty("d", false)
packet.setProperty("e", Array.newInstance(classMapIcon, 0))
packet.setProperty("f", 0)
packet.setProperty("g", 0)
packet.setProperty("h", 128)
packet.setProperty("i", 128)
packet.setProperty("j", buffer)
}
// 1.12+
MinecraftVersion.isHigherOrEqual(MinecraftVersion.V1_12) -> {
if (MinecraftVersion.isHigherOrEqual(MinecraftVersion.V1_13)) {
packet.setProperty("a", (mapItem.itemMeta as MapMeta).mapId)
} else {
packet.setProperty("a", mapView.invokeMethod<Short>("getId")!!.toInt())
}
packet.setProperty("b", mapView.scale.value)
packet.setProperty("c", false)
packet.setProperty("d", Array.newInstance(classMapIcon, 0))
packet.setProperty("e", 0)
packet.setProperty("f", 0)
packet.setProperty("g", 128)
packet.setProperty("h", 128)
packet.setProperty("i", buffer)
}
// 1.12-
else -> {
packet.setProperty("a", mapView.id)
packet.setProperty("b", mapView.scale.value)
packet.setProperty("c", Array.newInstance(classMapIcon, 0))
packet.setProperty("d", 0)
packet.setProperty("e", 0)
packet.setProperty("f", 128)
packet.setProperty("g", 128)
packet.setProperty("h", buffer)
}
}
player.sendPacket(packet)
}
}

Expand Down

0 comments on commit c351cf5

Please sign in to comment.