~phroa/intercart

cc1a921fdcc3c856bb0a87266948e7c950f52514 — Jack Stratton 9 months ago 2fbaca1
Add a rail layout builder from a pixel art image
A src/main/java/net/phroa/intercart/ImageLoader.java => src/main/java/net/phroa/intercart/ImageLoader.java +71 -0
@@ 0,0 1,71 @@
package net.phroa.intercart;

import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.block.Block;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

public class ImageLoader {
    private final BufferedImage image;

    public ImageLoader(String filepath) {
        BufferedImage i = null;
        try {
            i = ImageIO.read(new File(filepath));
        } catch (IOException e) {
            e.printStackTrace();
        }
        this.image = i;
    }

    public void construct(Location topLeftCorner) {
        topLeftCorner = topLeftCorner.toBlockLocation();
        var world = topLeftCorner.getWorld();
        // row 0, col 0, at the top left of the image, is the northwesternmost point.
        // south is +z, east is +x
        for (var row = 0; row < image.getHeight(); row++) {
            for (var col = 0; col < image.getWidth(); col++) {
                var pixel = image.getRGB(col, row) & 0xFFFFFF;

                if (pixel == 0x000000) {
                    // rail
                    offset(topLeftCorner, col, 0, row).setType(Material.POWERED_RAIL);
                    offset(topLeftCorner, col, -1, row).setType(Material.REDSTONE_BLOCK);

                    if ((image.getRGB(col, row - 1) & 0xFFFFFF) == 0x000000 && (image.getRGB(col, row + 1) & 0xFFFFFF) == 0xFFFFFF
                            || (image.getRGB(col, row + 1) & 0xFFFFFF) == 0x000000 && (image.getRGB(col, row - 1) & 0xFFFFFF) == 0xFFFFFF) {
                        // the current rail has a north connection but no south connection etc
                        offset(topLeftCorner, col, 0, row).setType(Material.RAIL);
                    } else if ((image.getRGB(col - 1, row) & 0xFFFFFF) == 0x000000 && (image.getRGB(col + 1, row) & 0xFFFFFF) == 0xFFFFFF
                            || (image.getRGB(col - 1, row) & 0xFFFFFF) == 0x000000 && (image.getRGB(col + 1, row) & 0xFFFFFF) == 0xFFFFFF) {
                        // the current rail has a west connection but no east connection etc
                        offset(topLeftCorner, col, 0, row).setType(Material.RAIL);
                    }
                } else if (pixel == 0x00FF00) {
                    // cactus
                    offset(topLeftCorner, col, 0, row).setType(Material.CACTUS);
                    offset(topLeftCorner, col, -1, row).setType(Material.SAND);
                } else if (pixel == 0xC0C0C0) {
                    // router base block
                    offset(topLeftCorner, col, 0, row).setType(Material.IRON_BLOCK);
                } else if (pixel == 0xFFD800) {
                    // router chest block
                    offset(topLeftCorner, col, 1, row).setType(Material.CHEST);
                    offset(topLeftCorner, col, 0, row).setType(Material.IRON_BLOCK);
                }
            }
        }
    }

    private Block offset(Location base, int x, int y, int z) {
        return base.getWorld().getBlockAt(
                base.getBlockX() + x,
                base.getBlockY() + y,
                base.getBlockZ() + z
        );
    }
}

A src/main/java/net/phroa/intercart/ImportListener.java => src/main/java/net/phroa/intercart/ImportListener.java +32 -0
@@ 0,0 1,32 @@
package net.phroa.intercart;

import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

import java.nio.file.Paths;

public class ImportListener implements CommandExecutor {
    private final Intercart intercart;

    public ImportListener(Intercart intercart) {
        this.intercart = intercart;
    }

    @Override
    public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
        if (!(sender instanceof Player)) {
            return false;
        }

        if (args.length != 1) {
            return false;
        }

        var i = new ImageLoader(Paths.get(intercart.getDataFolder().getPath(), args[0]).toString());
        i.construct(((Player) sender).getTargetBlock(10).getLocation().add(0, 1, 0));

        return true;
    }
}

M src/main/java/net/phroa/intercart/Intercart.java => src/main/java/net/phroa/intercart/Intercart.java +2 -0
@@ 26,6 26,7 @@ public final class Intercart extends JavaPlugin {
    private final RouterBuildingListener routerBuildingListener = new RouterBuildingListener(this);
    private final RouterClickListener routerClickListener = new RouterClickListener(this);
    private final RouteTableListener routeTableListener = new RouteTableListener(this);
    private final ImportListener importListener = new ImportListener(this);

    @Override
    public void onEnable() {


@@ 38,6 39,7 @@ public final class Intercart extends JavaPlugin {
        getCommand("ic-build").setExecutor(routerBuildingListener);
        getCommand("ic-table").setExecutor(routeTableListener);
        getCommand("ic-go").setExecutor(cartDestinationListener);
        getCommand("ic-import").setExecutor(importListener);

        ConfigurationSerialization.registerClass(CIDR.class, "intercart-cidr");
        ConfigurationSerialization.registerClass(Destination.class, "intercart-destination");

M src/main/resources/plugin.yml => src/main/resources/plugin.yml +4 -1
@@ 12,4 12,7 @@ commands:
    usage: /<command>
  ic-table:
    description: Edit route table
    usage: /<command>
\ No newline at end of file
    usage: /<command>
  ic-import:
    description: Import track layout from image
    usage: /<command> <filepath>
\ No newline at end of file