Skip to content

Commit

Permalink
Merge pull request #484 from BingZi-233/patch-2
Browse files Browse the repository at this point in the history
update: 兼容的1.20/1.21版本NMSMap#sendTo
  • Loading branch information
Bkm016 authored Oct 30, 2024
2 parents 1b4d147 + 70376e9 commit 4300c8d
Showing 1 changed file with 43 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import org.bukkit.map.MapCanvas
import org.bukkit.map.MapRenderer
import org.bukkit.map.MapView
import org.tabooproject.reflex.Reflex.Companion.getProperty
import org.tabooproject.reflex.Reflex.Companion.invokeConstructor
import org.tabooproject.reflex.Reflex.Companion.invokeMethod
import org.tabooproject.reflex.Reflex.Companion.setProperty
import org.tabooproject.reflex.Reflex.Companion.unsafeInstance
Expand Down Expand Up @@ -190,7 +191,7 @@ fun BufferedImage.zoomed(width: Int = 128, height: Int = 128): BufferedImage {
class NMSMap(val image: BufferedImage, var hand: Hand = Hand.MAIN, val builder: ItemBuilder.() -> Unit = {}) {

enum class Hand {

MAIN, OFF
}

Expand All @@ -200,7 +201,18 @@ 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") }
val classMapData: Class<*> by unsafeLazy {
when {
MinecraftVersion.isHigherOrEqual(MinecraftVersion.V1_20) -> {
Class.forName("net.minecraft.world.level.saveddata.maps.WorldMap.b")
}

else -> {
Class.forName("net.minecraft.world.level.saveddata.maps.WorldMap\$b")
}
}
}
val classMapId: Class<*> by unsafeLazy { Class.forName("net.minecraft.world.level.saveddata.maps.MapId") }
}

val mapRenderer = object : MapRenderer() {
Expand Down Expand Up @@ -266,6 +278,34 @@ class NMSMap(val image: BufferedImage, var hand: Hand = Hand.MAIN, val builder:
val buffer = mapView.invokeMethod<Any>("render", player)!!.getProperty<ByteArray>("buffer")
val packet = classPacketPlayOutMap.unsafeInstance()
when {
// 1.21+
MinecraftVersion.isHigherOrEqual(MinecraftVersion.V1_21) -> {
packet.setProperty("mapId", classMapId.invokeConstructor((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.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)
Expand Down Expand Up @@ -331,4 +371,4 @@ class NMSMap(val image: BufferedImage, var hand: Hand = Hand.MAIN, val builder:
}
return player.inventory.heldItemSlot + 36
}
}
}

0 comments on commit 4300c8d

Please sign in to comment.