Allow updating only a part of the map

This commit is contained in:
jglrxavpok 2020-08-03 17:03:34 +02:00
parent 07aaab77a9
commit 5971db5b92
2 changed files with 33 additions and 9 deletions

View File

@ -63,17 +63,12 @@ public class MapAnimationDemo {
int y = (int) (Math.sin(time*speed) * 10 + 64) - 10;
renderer.fillRoundRect(x, y, 50, 20, 10, 10);
renderer.setColor(Color.ORANGE);
renderer.drawString("Hi :-)", x+16, y+15);
MapDataPacket mapDataPacket = new MapDataPacket();
mapDataPacket.mapId = MAP_ID;
mapDataPacket.columns = 128;
mapDataPacket.rows = 128;
mapDataPacket.icons = new MapDataPacket.Icon[0];
mapDataPacket.x = 0;
mapDataPacket.z = 0;
mapDataPacket.scale = 0;
mapDataPacket.locked = true;
mapDataPacket.trackingPosition = true;
mapDataPacket.data = framebuffer.toMapColors();
framebuffer.preparePacket(mapDataPacket, 32, 32, 64+32, 64+32);
MinecraftServer.getConnectionManager().getOnlinePlayers().forEach(p -> {
p.getPlayerConnection().sendPacket(mapDataPacket);
});

View File

@ -1,5 +1,7 @@
package net.minestom.server.map;
import net.minestom.server.network.packet.server.play.MapDataPacket;
/**
* Framebuffer to render to a map
*/
@ -10,6 +12,33 @@ public interface Framebuffer {
byte[] toMapColors();
default void preparePacket(MapDataPacket packet) {
preparePacket(packet, 0, 0, WIDTH, HEIGHT);
}
default void preparePacket(MapDataPacket packet, int minX, int minY, int width, int height) {
byte[] colors;
if(minX == 0 && minY == 0 && width == WIDTH && height == HEIGHT) {
colors = toMapColors();
} else {
colors = new byte[width*height];
byte[] mapColors = toMapColors();
for (int y = minY; y < Math.min(HEIGHT, minY+height); y++) {
for (int x = minX; x < Math.min(WIDTH, minX+width); x++) {
byte color = mapColors[index(x, y, WIDTH)];
colors[index(x-minX, y-minY, width)] = color;
}
}
}
packet.columns = (short) width;
packet.rows = (short) height;
packet.icons = new MapDataPacket.Icon[0];
packet.x = (byte) minX;
packet.z = (byte) minY;
packet.data = colors;
}
static int index(int x, int z) {
return index(x, z, WIDTH);
}