Skip to content

Quickstart

Maximilian Dorn edited this page Aug 31, 2022 · 2 revisions

Quickstart

Maven Setup

import dev.cerus.maps.api.ClientsideMap;
import dev.cerus.maps.api.MapScreen;
import dev.cerus.maps.api.graphics.ClientsideMapGraphics;
import dev.cerus.maps.api.graphics.ColorCache;
import dev.cerus.maps.api.graphics.MapGraphics;
import dev.cerus.maps.plugin.map.MapScreenRegistry;
import dev.cerus.maps.version.VersionAdapterFactory;
import org.bukkit.Bukkit;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.MapMeta;
import org.bukkit.map.MinecraftFont;
import org.bukkit.plugin.java.JavaPlugin;

public class MyPlugin extends JavaPlugin {

    @Override
    public void onEnable() {
        // This example depends on the "common" and "plugin" dependency.

        // Something important to keep in mind when using the plugin for storage:
        // The plugin loads the map screens 3 seconds after startup. (Check out
        // the MapsPlugin.java file for an explanation)

        this.getServer().getScheduler().runTaskTimerAsynchronously(this, () -> {
            for (final MapScreen screen : MapScreenRegistry.getScreens()) {
                final MapGraphics<?, ?> graphics = screen.getGraphics();
                graphics.fillComplete(ColorCache.rgbToMap(255, 255, 255)); // Convert rgb(255, 255, 255) to map color and fill the screen
                graphics.drawText(5, 5, "There are " + Bukkit.getOnlinePlayers().size() + " players on the server", ColorCache.rgbToMap(0, 0, 0), 2);
                screen.spawnFrames(Bukkit.getOnlinePlayers().toArray(new Player[0])); // Send the screen frames to all online players
                screen.sendMaps(true); // Send map data to all online players
            }
        }, 4 * 20, 20);
        getCommand("mapstest").setExecutor(this);
    }

    @Override
    public boolean onCommand(final CommandSender sender, final Command command, final String label, final String[] args) {
        // It's been ages since I've used the normal Bukkit command system 
        // so please forgive me if I use any bad practices here
        if (!(sender instanceof Player player)) {
            return true;
        }
        if (!command.getName().equals("mapstest")) {
            return true;
        }

        // This is really unsafe. Always do proper checks before casting 
        // things, but this will do for the sake of this quick start.
        final ItemStack item = player.getInventory().getItemInMainHand();
        final MapMeta mapMeta = (MapMeta) item.getItemMeta();
        final int mapId = mapMeta.getMapView().getId();

        final ClientsideMap clientsideMap = new ClientsideMap(mapId); // Create clientside map with given id
        final ClientsideMapGraphics graphics = new ClientsideMapGraphics(); // Create graphics buffer

        graphics.fillComplete(ColorCache.rgbToMap(0, 0, 0)); // Fill with rgb(0, 0, 0)
        graphics.drawText(5, 5, "Hello,", ColorCache.rgbToMap(255, 255, 255), 1); // Draw text
        graphics.drawText(5, 5 + MinecraftFont.Font.getHeight() + 5, player.getName(), ColorCache.rgbToMap(255, 255, 255), 2);

        clientsideMap.draw(graphics); // Draw the buffer onto the map
        clientsideMap.sendTo(new VersionAdapterFactory().makeAdapter(), player); // Send the map to the player
        return true;
    }

}