Skip to content
This repository has been archived by the owner on Sep 29, 2021. It is now read-only.
Maximilian Dorn edited this page Dec 26, 2020 · 2 revisions

Usage

This is a W.I.P

The DrawAdapter class

The DrawAdapter is used the draw stuff onto maps. You can fill rectangles, draw lines, set individual pixels or even draw whole images using the DrawAdapter.

You can obtain a DrawAdapter by using the DrawAdapterFactory: DrawAdapter drawAdapter = new DrawAdapterFactory().makeAdapter();

The NmsAdapter class

The NmsAdapter is mainly used for version specific operations. Most of its methods probably aren't that useful for you, but it contains two very convenient methods: the matchRgb(int r, int g, int b) method and the matchColor(byte color) method.

The matchRgb() method allows you to find the most suitable map color for your rgb values and the matchColor() does the exact opposite.

Example:

NmsAdapter nmsAdapter = new NmsAdapterFactory().makeAdapter();
DrawAdapter drawAdapter = new DrawAdapterFactory().makeAdapter();

byte blue = nmsAdapter.matchRgb(Color.BLUE.getRed(), Color.BLUE.getGreen(), color.BLUE.getBlue());
drawAdapter.fillRect(10, 10, 50, 20, blue);

Color color = nmsAdapter.matchColor(blue);
System.out.printf("Map color %d = R %d, G %d, B %d%n", blue, color.getRed(), color.getGreen(), color.getBlue());

You can obtain a NmsAdapter by using the NmsAdapterFactory: NmsAdapter nmsAdapter = new NmsAdapterFactory().makeAdapter();

The FakeMap class

The FakeMap class is basically a normal map, except that it is fake. Its contents are shown to players using custom packets and it won't affect any maps on the server. You can modify it by using a DrawAdapter. You can add observers to the fake map by calling the addObserver(Player player) method and you can broadcast the contents by calling the send() method.

Example:

BufferedImage catImage = ...;

NmsAdapter nmsAdapter = new NmsAdapterFactory().makeAdapter();
DrawAdapter drawAdapter = new DrawAdapterFactory().makeAdapter();

FakeMap map = new FakeMap(nmsAdapter, drawAdapter);
map.addObserver(Bukkit.getPlayer("Cerus_"));

drawAdapter.drawImage(catImage);
map.send();

The MapScreen class

The MapScreen class allows you to basically link multiple fake maps together into one big canvas (using item frames).

To create a map screen you need the width of the screen, the height of the screen and the item frame entities in a two dimensional array (Entity[width][height]). You can 'convert' a list of entities into a 2D array using this simple snippet:

// There's probably a way to do this with streams

final List<Entity> list = ...

final Entity[][] entities = new Entity[width][height];
int n = 0;
for (int x = 0; x < width; x++) {
    for (int z = 0; z < height; z++) {
        entities[x][z] = list.get(n++);
    }
}

After you've created a map screen you can get its ScreenGraphics instance by calling getScreenGraphics(). The ScreenGraphics class is basically the same as the DrawAdapter class.

When you're done with drawing you can update the screen by calling the update() method. To broadcast the changes call send(). Don't forget to add observers by calling addObserver(Player player)!

Important note: The item frame entities need to be sorted, otherwise you will experience unwanted behaviour. Sorting example for screens facing north:

final List<Entity> list = ...
list.sort(Comparator.comparingInt(value -> ((Entity) value).getLocation().getBlockX()).reversed()
    .thenComparing(Comparator.comparingInt(value -> ((Entity) value).getLocation().getBlockY()).reversed()));