Created
October 27, 2017 14:58
-
-
Save MrPowerGamerBR/72b97eb9efb18641f8825b9355d1ac89 to your computer and use it in GitHub Desktop.
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
package net.pocketdreams.dreampocketimagehq | |
import io.netty.buffer.Unpooled | |
import net.pocketdreams.dreamcore.utils.KotlinPlugin | |
import org.bukkit.Bukkit | |
import org.bukkit.event.EventHandler | |
import org.bukkit.event.Listener | |
import protocolsupport.api.Connection | |
import protocolsupport.api.events.ConnectionHandshakeEvent | |
import protocolsupport.protocol.serializer.VarNumberSerializer | |
import protocolsupport.protocol.typeremapper.mapcolor.MapColorHelper | |
import protocolsupport.protocol.typeremapper.pe.PEPacketIDs | |
import protocolsupportpocketstuff.api.util.PocketCon | |
import java.awt.Color | |
import java.io.File | |
import javax.imageio.ImageIO | |
class DreamPocketImageHQ : KotlinPlugin(), Listener { | |
override fun softEnable() { | |
super.softEnable() | |
Bukkit.getPluginManager().registerEvents(this, this) | |
} | |
override fun softDisable() { | |
super.softDisable() | |
} | |
@EventHandler | |
fun onConnection(e: ConnectionHandshakeEvent) { | |
val con = e.connection | |
if (PocketCon.isPocketConnection(con)) { | |
con.addPacketListener(MapListener()) | |
} | |
} | |
} | |
class MapListener : Connection.PacketListener() { | |
override fun onRawPacketSending(event: RawPacketEvent) { | |
super.onRawPacketSending(event) | |
val data = event.data | |
val packetId = VarNumberSerializer.readVarInt(data) | |
if (packetId != PEPacketIDs.MAP_ITEM_DATA) | |
return | |
// event.isCancelled = true | |
data.readByte() | |
data.readByte() | |
val mapId = VarNumberSerializer.readSVarLong(data) | |
val image = File(Bukkit.getPluginManager().getPlugin("MCPainter").dataFolder, "img/${mapId}.png") | |
if (!image.exists()) | |
return | |
val flags = VarNumberSerializer.readVarInt(data) | |
println("Update: $flags") | |
if (flags != 2) { | |
println("Cancelling non-texture update...") | |
event.isCancelled = true | |
return | |
} | |
println("Converting $mapId to high quality...") | |
val dimension = data.readByte() | |
val scale = data.readByte() | |
val columns = VarNumberSerializer.readSVarInt(data) | |
val rows = VarNumberSerializer.readSVarInt(data) | |
val xstart = VarNumberSerializer.readSVarInt(data) | |
val zstart = VarNumberSerializer.readSVarInt(data) | |
val length = VarNumberSerializer.readVarInt(data) | |
println("Dimension: $dimension") | |
println("Scale: $scale") | |
println("Col: $columns") | |
println("Rows: $rows") | |
println("xstart: $xstart") | |
println("zstart: $zstart") | |
println("colors length: $length") | |
val serializer = Unpooled.buffer() // Recreating the packet... | |
VarNumberSerializer.writeVarInt(serializer, PEPacketIDs.MAP_ITEM_DATA) | |
serializer.writeByte(0) | |
serializer.writeByte(0) | |
VarNumberSerializer.writeSVarLong(serializer, mapId) | |
VarNumberSerializer.writeVarInt(serializer, flags) | |
serializer.writeByte(dimension.toInt()) | |
serializer.writeByte(scale.toInt()) | |
VarNumberSerializer.writeSVarInt(serializer, columns) | |
VarNumberSerializer.writeSVarInt(serializer, rows) | |
VarNumberSerializer.writeSVarInt(serializer, xstart) | |
VarNumberSerializer.writeSVarInt(serializer, zstart) | |
VarNumberSerializer.writeVarInt(serializer, 16384) | |
val bufferedImage = ImageIO.read(image) | |
var idx = 0 | |
for (y in 0 until bufferedImage.height) { | |
for (x in 0 until bufferedImage.width) { | |
val color = Color(bufferedImage.getRGB(x, y)) | |
VarNumberSerializer.writeVarInt(serializer, MapColorHelper.toARGB(color.red.toByte(), color.green.toByte(), color.blue.toByte(), 255.toByte())) | |
idx++ | |
} | |
} | |
println("Colors printed: $idx") | |
event.data = serializer | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment