Search in sources :

Example 1 with BufferedMapCanvas

use of com.elmakers.mine.bukkit.maps.BufferedMapCanvas in project MagicPlugin by elBukkit.

the class MaterialBrush method update.

@Override
public boolean update(final Mage fromMage, final Location target) {
    // Chain up to parent
    if (parent != null) {
        parent.update(fromMage, target);
    }
    if (mode == BrushMode.CLONE || mode == BrushMode.REPLICATE) {
        if (cloneSource == null) {
            isTargetValid = false;
            return true;
        }
        if (cloneTarget == null)
            cloneTarget = target;
        materialTarget = toTargetLocation(target);
        if (materialTarget.getY() < 0 || materialTarget.getWorld() == null || materialTarget.getY() > materialTarget.getWorld().getMaxHeight()) {
            isTargetValid = false;
        } else {
            Block block = materialTarget.getBlock();
            if (!CompatibilityLib.getCompatibilityUtils().isChunkLoaded(block))
                return false;
            updateFromBlock(block, fromMage.getRestrictedMaterialSet());
            isTargetValid = fillWithAir || material != Material.AIR;
        }
    }
    if (mode == BrushMode.SCHEMATIC) {
        if (!checkSchematic()) {
            return true;
        }
        if (cloneTarget == null) {
            isTargetValid = false;
            return true;
        }
        Vector diff = target.toVector().subtract(cloneTarget.toVector());
        com.elmakers.mine.bukkit.api.block.MaterialAndData newMaterial = schematic.getBlock(diff);
        if (newMaterial == null) {
            isTargetValid = false;
        } else {
            updateFrom(newMaterial);
            isTargetValid = fillWithAir || newMaterial.getMaterial() != Material.AIR;
            // Check for command overrides
            if (commandMap != null && DefaultMaterials.isCommand(material)) {
                String commandKey = getCommandLine();
                if (commandKey != null && commandKey.length() > 0 && commandMap.containsKey(commandKey)) {
                    setCommandLine(commandMap.get(commandKey));
                }
            }
        }
    }
    if (mode == BrushMode.MAP && mapId >= 0) {
        if (mapCanvas == null) {
            try {
                MapView mapView = CompatibilityLib.getDeprecatedUtils().getMap(mapId);
                if (mapView != null) {
                    Player player = fromMage != null ? fromMage.getPlayer() : null;
                    List<MapRenderer> renderers = mapView.getRenderers();
                    if (renderers.size() > 0) {
                        mapCanvas = new BufferedMapCanvas();
                        MapRenderer renderer = renderers.get(0);
                        // This is mainly here as a hack for my own urlmaps that do their own caching
                        // Bukkit *seems* to want to do caching at the MapView level, but looking at the code-
                        // they cache but never use the cache?
                        // Anyway render gets called constantly so I'm not re-rendering on each render... but then
                        // how to force a render to a canvas? So we re-initialize.
                        renderer.initialize(mapView);
                        renderer.render(mapView, mapCanvas, player);
                    }
                }
            } catch (Exception ex) {
                controller.getLogger().log(Level.WARNING, "Error reading map id " + mapId, ex);
            }
        }
        isTargetValid = false;
        if (mapCanvas != null && cloneTarget != null) {
            Vector diff = target.toVector().subtract(cloneTarget.toVector());
            // TODO : Different orientations, centering, scaling, etc
            // We default to 1/8 scaling for now to make the portraits work well.
            DyeColor mapColor = DyeColor.WHITE;
            if (orientVector.getBlockY() > orientVector.getBlockZ() || orientVector.getBlockY() > orientVector.getBlockX()) {
                if (orientVector.getBlockX() > orientVector.getBlockZ()) {
                    mapColor = mapCanvas.getDyeColor(Math.abs((int) (diff.getBlockX() * scale + BufferedMapCanvas.CANVAS_WIDTH / 2) % BufferedMapCanvas.CANVAS_WIDTH), Math.abs((int) (-diff.getBlockY() * scale + BufferedMapCanvas.CANVAS_HEIGHT / 2) % BufferedMapCanvas.CANVAS_HEIGHT));
                } else {
                    mapColor = mapCanvas.getDyeColor(Math.abs((int) (diff.getBlockZ() * scale + BufferedMapCanvas.CANVAS_WIDTH / 2) % BufferedMapCanvas.CANVAS_WIDTH), Math.abs((int) (-diff.getBlockY() * scale + BufferedMapCanvas.CANVAS_HEIGHT / 2) % BufferedMapCanvas.CANVAS_HEIGHT));
                }
            } else {
                mapColor = mapCanvas.getDyeColor(Math.abs((int) (diff.getBlockX() * scale + BufferedMapCanvas.CANVAS_WIDTH / 2) % BufferedMapCanvas.CANVAS_WIDTH), Math.abs((int) (diff.getBlockZ() * scale + BufferedMapCanvas.CANVAS_HEIGHT / 2) % BufferedMapCanvas.CANVAS_HEIGHT));
            }
            if (mapColor != null) {
                this.material = mapMaterialBase;
                DefaultMaterials.getInstance().colorize(this, mapColor);
                isTargetValid = this.material != null;
            } else if (mapMaterialDefault != null) {
                this.material = mapMaterialDefault;
                isTargetValid = true;
            }
        }
    }
    return true;
}
Also used : Player(org.bukkit.entity.Player) DyeColor(org.bukkit.DyeColor) BufferedMapCanvas(com.elmakers.mine.bukkit.maps.BufferedMapCanvas) MapRenderer(org.bukkit.map.MapRenderer) Block(org.bukkit.block.Block) MapView(org.bukkit.map.MapView) Vector(org.bukkit.util.Vector)

Aggregations

BufferedMapCanvas (com.elmakers.mine.bukkit.maps.BufferedMapCanvas)1 DyeColor (org.bukkit.DyeColor)1 Block (org.bukkit.block.Block)1 Player (org.bukkit.entity.Player)1 MapRenderer (org.bukkit.map.MapRenderer)1 MapView (org.bukkit.map.MapView)1 Vector (org.bukkit.util.Vector)1