-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e2d1a70
commit 1f7685e
Showing
9 changed files
with
252 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
src/main/java/com/github/mori01231/lifecore/map/SerializedMapCanvas.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package com.github.mori01231.lifecore.map | ||
|
||
import kotlinx.serialization.Serializable | ||
import org.bukkit.craftbukkit.v1_15_R1.map.CraftMapCanvas | ||
|
||
@Serializable | ||
data class SerializedMapCanvas(val buffer: ByteArray, val base: ByteArray) { | ||
companion object { | ||
fun asSerializableMirror(canvas: CraftMapCanvas): SerializedMapCanvas { | ||
val buffer = CraftMapCanvas::class.java.getDeclaredField("buffer").apply { isAccessible = true }[canvas] as ByteArray | ||
val base = CraftMapCanvas::class.java.getDeclaredField("base").apply { isAccessible = true }[canvas] as ByteArray | ||
return SerializedMapCanvas(buffer, base) | ||
} | ||
} | ||
|
||
fun injectToCraftMapCanvas(canvas: CraftMapCanvas) { | ||
val nmsBuffer = CraftMapCanvas::class.java.getDeclaredField("buffer").apply { isAccessible = true }[canvas] as ByteArray | ||
val nmsBase = CraftMapCanvas::class.java.getDeclaredField("base").apply { isAccessible = true }[canvas] as ByteArray | ||
if (buffer.size != nmsBuffer.size) error("buffer size doesn't match") | ||
if (base.size != nmsBase.size) error("base size doesn't match") | ||
for ((index, byte) in buffer.withIndex()) { | ||
nmsBuffer[index] = byte | ||
} | ||
for ((index, byte) in base.withIndex()) { | ||
nmsBase[index] = byte | ||
} | ||
} | ||
|
||
override fun equals(other: Any?): Boolean { | ||
if (this === other) return true | ||
if (javaClass != other?.javaClass) return false | ||
|
||
other as SerializedMapCanvas | ||
|
||
if (!buffer.contentEquals(other.buffer)) return false | ||
if (!base.contentEquals(other.base)) return false | ||
|
||
return true | ||
} | ||
|
||
override fun hashCode(): Int { | ||
var result = buffer.contentHashCode() | ||
result = 31 * result + base.contentHashCode() | ||
return result | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
src/main/java/com/github/mori01231/lifecore/map/SerializedMapDataRenderer.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package com.github.mori01231.lifecore.map | ||
|
||
import net.minecraft.server.v1_15_R1.WorldMap | ||
import org.bukkit.craftbukkit.v1_15_R1.map.CraftMapCanvas | ||
import org.bukkit.craftbukkit.v1_15_R1.map.CraftMapView | ||
import org.bukkit.entity.Player | ||
import org.bukkit.map.MapCanvas | ||
import org.bukkit.map.MapRenderer | ||
import org.bukkit.map.MapView | ||
|
||
class SerializedMapDataRenderer(private val canvas: SerializedMapCanvas) : MapRenderer() { | ||
private var init = false | ||
|
||
override fun render(view: MapView, canvas: MapCanvas, player: Player) { | ||
this.canvas.injectToCraftMapCanvas(canvas as CraftMapCanvas) | ||
for (i in 0 until canvas.cursors.size()) { | ||
canvas.cursors.removeCursor(canvas.cursors.getCursor(i)) | ||
} | ||
val worldMap = CraftMapView::class.java.getDeclaredField("worldMap").apply { isAccessible = true }[view] as WorldMap | ||
for (x in 0..127) { | ||
for (y in 0..127) { | ||
worldMap.decorations.clear() | ||
worldMap.flagDirty(x, y) | ||
} | ||
} | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
src/main/java/com/github/mori01231/lifecore/util/EncodeUtil.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package com.github.mori01231.lifecore.util | ||
|
||
import java.io.ByteArrayInputStream | ||
import java.io.ByteArrayOutputStream | ||
import java.util.Base64 | ||
import java.util.zip.GZIPInputStream | ||
import java.util.zip.GZIPOutputStream | ||
|
||
object EncodeUtil { | ||
fun encodeBase64AndGzip(data: ByteArray): ByteArray = Base64.getEncoder().encode(gzipByteArray(data)) | ||
|
||
fun decodeBase64AndGunzip(data: ByteArray): ByteArray = gunzipByteArray(Base64.getDecoder().decode(data)) | ||
|
||
fun gzipByteArray(data: ByteArray): ByteArray = | ||
ByteArrayOutputStream().use { | ||
it.apply { GZIPOutputStream(this).use { gz -> gz.write(data) } }.toByteArray() | ||
} | ||
|
||
fun gunzipByteArray(data: ByteArray): ByteArray = | ||
GZIPInputStream(ByteArrayInputStream(data)).use { it.readBytes() } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
src/main/java/com/github/mori01231/lifecore/util/MapUtil.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package com.github.mori01231.lifecore.util | ||
|
||
import com.github.mori01231.lifecore.map.SerializedMapCanvas | ||
import com.github.mori01231.lifecore.map.SerializedMapDataRenderer | ||
import kotlinx.serialization.encodeToString | ||
import kotlinx.serialization.json.Json | ||
import kotlinx.serialization.json.JsonElement | ||
import kotlinx.serialization.json.decodeFromJsonElement | ||
import kotlinx.serialization.json.encodeToJsonElement | ||
import org.bukkit.Material | ||
import org.bukkit.craftbukkit.v1_15_R1.entity.CraftPlayer | ||
import org.bukkit.craftbukkit.v1_15_R1.map.CraftMapCanvas | ||
import org.bukkit.craftbukkit.v1_15_R1.map.CraftMapRenderer | ||
import org.bukkit.craftbukkit.v1_15_R1.map.CraftMapView | ||
import org.bukkit.entity.Player | ||
import org.bukkit.inventory.ItemStack | ||
import org.bukkit.inventory.meta.MapMeta | ||
import org.bukkit.map.MapCanvas | ||
import org.bukkit.map.MapRenderer | ||
import org.bukkit.map.MapView | ||
|
||
object MapUtil { | ||
private fun convertCanvasToSerializable(canvas: MapCanvas) = | ||
when (canvas) { | ||
is CraftMapCanvas -> SerializedMapCanvas.asSerializableMirror(canvas) | ||
else -> error("Unsupported type: ${canvas::class.java.name}") | ||
} | ||
|
||
fun serializeCanvasToJsonElement(canvas: MapCanvas): JsonElement = | ||
Json.encodeToJsonElement(convertCanvasToSerializable(canvas)) | ||
|
||
fun serializeCanvasToString(canvas: MapCanvas): String = | ||
Json.encodeToString(convertCanvasToSerializable(canvas)) | ||
|
||
fun deserializeCanvasFromJsonElement(element: JsonElement): SerializedMapCanvas = | ||
Json.decodeFromJsonElement(element) | ||
|
||
fun deserializeCanvasFromString(json: String): SerializedMapCanvas = | ||
Json.decodeFromString(json) | ||
|
||
@Suppress("UNCHECKED_CAST") | ||
fun MapView.getCanvases() = | ||
CraftMapView::class.java.getDeclaredField("canvases").apply { isAccessible = true }[this] as Map<MapRenderer, Map<CraftPlayer, CraftMapCanvas>> | ||
|
||
fun initializeMapRenderer(player: Player, item: ItemStack) { | ||
if (item.type != Material.FILLED_MAP) return | ||
val meta = item.itemMeta as? MapMeta? ?: return | ||
val mapView = meta.mapView ?: return | ||
if (mapView.renderers.getOrNull(0) !is CraftMapRenderer) { | ||
if (mapView is CraftMapView) mapView.render(player as CraftPlayer) | ||
return | ||
} | ||
val serializedMapData = | ||
try { | ||
item.let { ItemUtil.getByteArrayTag(it, "SerializedMapData") ?: return } | ||
.let { EncodeUtil.decodeBase64AndGunzip(it) } | ||
.let { String(it) } | ||
.let { deserializeCanvasFromString(it) } | ||
} catch (_: Exception) { | ||
return | ||
} | ||
mapView.removeRenderer(mapView.renderers[0]) | ||
mapView.addRenderer(SerializedMapDataRenderer(serializedMapData)) | ||
if (mapView is CraftMapView) mapView.render(player as CraftPlayer) | ||
} | ||
} |