~phroa/intercart unlisted

fe81c7a38fe135968fa74dc670e14fd98c6ece8f — Jack Stratton 8 months ago 2dadc2c main
Create routers from the imported map
M src/main/java/net/phroa/intercart/ImageLoader.java => src/main/java/net/phroa/intercart/ImageLoader.java +66 -12
@@ 8,8 8,17 @@ import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

public class ImageLoader {
    public static final int RAIL = 0xFF000000;
    public static final int BLANK = 0xFFFFFFFF;
    public static final int CACTUS = 0xFF00FF00;
    public static final int IRON = 0xFFC0C0C0;
    public static final int CHEST = 0xFFFFD800;

    private final BufferedImage image;

    public ImageLoader(String filepath) {


@@ 22,43 31,88 @@ public class ImageLoader {
        this.image = i;
    }

    public void construct(Location topLeftCorner) {
    public List<Router> construct(Location topLeftCorner, UUID creator) {
        var routers = new ArrayList<Router>();
        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;
                var pixel = image.getRGB(col, row);

                if (pixel == 0x000000) {
                if (pixel == RAIL) {
                    // 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) {
                    if (image.getRGB(col, row - 1) == RAIL && image.getRGB(col, row + 1) == BLANK
                            || image.getRGB(col, row + 1) == RAIL && image.getRGB(col, row - 1) == BLANK) {
                        // 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) {
                    } else if (image.getRGB(col - 1, row) == RAIL && image.getRGB(col + 1, row) == BLANK
                            || image.getRGB(col - 1, row) == RAIL && image.getRGB(col + 1, row) == BLANK) {
                        // the current rail has a west connection but no east connection etc
                        offset(topLeftCorner, col, 0, row).setType(Material.RAIL);
                    }
                } else if (pixel == 0x00FF00) {
                } else if (pixel == CACTUS) {
                    // cactus
                    offset(topLeftCorner, col, 0, row).setType(Material.CACTUS);
                    offset(topLeftCorner, col, -1, row).setType(Material.SAND);
                } else if (pixel == 0xC0C0C0) {
                } else if (pixel == IRON) {
                    // router base block
                    offset(topLeftCorner, col, 0, row).setType(Material.IRON_BLOCK);
                } else if (pixel == 0xFFD800) {
                } else if (pixel == CHEST) {
                    // router chest block
                    offset(topLeftCorner, col, 1, row).setType(Material.CHEST);
                    var chest = offset(topLeftCorner, col, 1, row);
                    chest.setType(Material.CHEST);
                    offset(topLeftCorner, col, 0, row).setType(Material.IRON_BLOCK);
                    // now that a chest has been found, look for the interfaces leading to the router and build a router
                    var minCol = col;
                    var minRow = row;
                    var maxCol = col;
                    var maxRow = row;
                    while (image.getRGB(minCol - 1, minRow) == IRON) {
                        minCol--;
                    }
                    while (image.getRGB(minCol, minRow - 1) == IRON) {
                        minRow--;
                    }
                    while (image.getRGB(maxCol + 1, maxRow) == IRON) {
                        maxCol++;
                    }
                    while (image.getRGB(maxCol, maxRow + 1) == IRON) {
                        maxRow++;
                    }

                    var interfaces = new ArrayList<Location>();

                    for (var c = minCol; c <= maxCol; c++) {
                        if (image.getRGB(c, minRow - 1) == RAIL) {
                            var iface = offset(topLeftCorner, c, 0, minRow - 1).getLocation();
                            interfaces.add(iface);
                        }
                        if (image.getRGB(c, maxRow + 1) == RAIL) {
                            var iface = offset(topLeftCorner, c, 0, maxRow + 1).getLocation();
                            interfaces.add(iface);
                        }
                    }

                    for (var r = minRow; r <= maxRow; r++) {
                        if (image.getRGB(minCol - 1, r) == RAIL) {
                            var iface = offset(topLeftCorner, minCol - 1, 0, r).getLocation();
                            interfaces.add(iface);
                        }
                        if (image.getRGB(maxCol + 1, r) == RAIL) {
                            var iface = offset(topLeftCorner, maxCol + 1, 0, r).getLocation();
                            interfaces.add(iface);
                        }
                    }

                    routers.add(new Router(creator, chest.getLocation(), interfaces));
                }
            }
        }
        return routers;
    }

    private Block offset(Location base, int x, int y, int z) {

M src/main/java/net/phroa/intercart/ImportListener.java => src/main/java/net/phroa/intercart/ImportListener.java +9 -1
@@ 25,7 25,15 @@ public class ImportListener implements CommandExecutor {
        }

        var i = new ImageLoader(Paths.get(intercart.getDataFolder().getPath(), args[0]).toString());
        i.construct(((Player) sender).getTargetBlock(10).getLocation().add(0, 1, 0));
        var player = (Player) sender;
        var block = player.getTargetBlock(10);
        if (block == null) {
            return false;
        }
        var routers = i.construct(block.getLocation().add(0, 1, 0), player.getUniqueId());
        for (var r : routers) {
            intercart.addRouter(r);
        }

        return true;
    }