From a3b8f4c91779847ced593824100c79e333d021e3 Mon Sep 17 00:00:00 2001 From: Jesse Boyd Date: Wed, 8 Feb 2017 12:01:44 +1100 Subject: [PATCH] Add heightmap from imgur --- .../boydti/fawe/object/brush/HeightBrush.java | 19 +++++------ .../brush/heightmap/ScalableHeightMap.java | 6 ++-- .../worldedit/command/BrushCommands.java | 34 +++++++++++++++++-- 3 files changed, 42 insertions(+), 17 deletions(-) diff --git a/core/src/main/java/com/boydti/fawe/object/brush/HeightBrush.java b/core/src/main/java/com/boydti/fawe/object/brush/HeightBrush.java index 031c27b8..3c736978 100644 --- a/core/src/main/java/com/boydti/fawe/object/brush/HeightBrush.java +++ b/core/src/main/java/com/boydti/fawe/object/brush/HeightBrush.java @@ -10,8 +10,8 @@ import com.sk89q.worldedit.extent.clipboard.Clipboard; import com.sk89q.worldedit.function.mask.Mask; import com.sk89q.worldedit.function.mask.Masks; import com.sk89q.worldedit.function.pattern.Pattern; -import java.io.File; import java.io.IOException; +import java.io.InputStream; public class HeightBrush implements DoubleActionBrush { @@ -20,23 +20,20 @@ public class HeightBrush implements DoubleActionBrush { double yscale = 1; private final DoubleActionBrushTool tool; - public HeightBrush(File file, int rotation, double yscale, DoubleActionBrushTool tool, Clipboard clipboard) { + public HeightBrush(InputStream stream, int rotation, double yscale, DoubleActionBrushTool tool, Clipboard clipboard) { this.tool = tool; this.rotation = (rotation / 90) % 4; this.yscale = yscale; - if (file == null || !file.exists()) { - // Since I can't be bothered using separate args, we'll get it from the filename - if (file.getName().equalsIgnoreCase("#clipboard.png") && clipboard != null) { - heightMap = ScalableHeightMap.fromClipboard(clipboard); - } else { - heightMap = new ScalableHeightMap(); - } - } else { + if (stream != null) { try { - heightMap = ScalableHeightMap.fromPNG(file); + heightMap = ScalableHeightMap.fromPNG(stream); } catch (IOException e) { throw new FaweException(BBC.BRUSH_HEIGHT_INVALID); } + } else if (clipboard != null) { + heightMap = ScalableHeightMap.fromClipboard(clipboard); + } else { + heightMap = new ScalableHeightMap(); } } diff --git a/core/src/main/java/com/boydti/fawe/object/brush/heightmap/ScalableHeightMap.java b/core/src/main/java/com/boydti/fawe/object/brush/heightmap/ScalableHeightMap.java index 62d53167..56e1109b 100644 --- a/core/src/main/java/com/boydti/fawe/object/brush/heightmap/ScalableHeightMap.java +++ b/core/src/main/java/com/boydti/fawe/object/brush/heightmap/ScalableHeightMap.java @@ -19,8 +19,8 @@ import com.sk89q.worldedit.regions.CuboidRegion; import com.sk89q.worldedit.regions.Region; import java.awt.image.BufferedImage; import java.awt.image.Raster; -import java.io.File; import java.io.IOException; +import java.io.InputStream; import java.util.HashSet; import javax.imageio.ImageIO; @@ -84,8 +84,8 @@ public class ScalableHeightMap { return new ArrayHeightMap(heightArray); } - public static ScalableHeightMap fromPNG(File file) throws IOException { - BufferedImage heightFile = ImageIO.read(file); + public static ScalableHeightMap fromPNG(InputStream stream) throws IOException { + BufferedImage heightFile = ImageIO.read(stream); int width = heightFile.getWidth(); int length = heightFile.getHeight(); Raster data = heightFile.getData(); diff --git a/core/src/main/java/com/sk89q/worldedit/command/BrushCommands.java b/core/src/main/java/com/sk89q/worldedit/command/BrushCommands.java index 1753fe01..cfb86fac 100644 --- a/core/src/main/java/com/sk89q/worldedit/command/BrushCommands.java +++ b/core/src/main/java/com/sk89q/worldedit/command/BrushCommands.java @@ -65,6 +65,13 @@ import com.sk89q.worldedit.session.ClipboardHolder; import com.sk89q.worldedit.util.command.binding.Switch; import com.sk89q.worldedit.util.command.parametric.Optional; import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.io.InputStream; +import java.net.URL; +import java.nio.channels.Channels; +import java.nio.channels.ReadableByteChannel; import static com.google.common.base.Preconditions.checkNotNull; @@ -359,13 +366,34 @@ public class BrushCommands { @CommandPermissions("worldedit.brush.height") public void heightBrush(Player player, LocalSession session, @Optional("5") double radius, @Optional("") final String filename, @Optional("0") final int rotation, @Optional("1") final double yscale) throws WorldEditException { worldEdit.checkMaxBrushRadius(radius); - File file = new File(Fawe.imp().getDirectory(), "heightmap" + File.separator + (filename.endsWith(".png") ? filename : filename + ".png")); + String filenamePng = (filename.endsWith(".png") ? filename : filename + ".png"); + File file = new File(Fawe.imp().getDirectory(), "heightmap" + File.separator + filenamePng); + InputStream stream = null; + if (!file.exists()) { + if (!filename.equals("#clipboard") && filename.length() >= 7) { + try { + URL url = new URL("https://i.imgur.com/" + filenamePng); + ReadableByteChannel rbc = Channels.newChannel(url.openStream()); + stream = Channels.newInputStream(rbc); + System.out.println("Loaded " + url); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + } else if (!filename.equalsIgnoreCase("#clipboard")){ + try { + stream = new FileInputStream(file); + } catch (FileNotFoundException e) { + throw new RuntimeException(e); + } + } + DoubleActionBrushTool tool = session.getDoubleActionBrushTool(player.getItemInHand()); tool.setSize(radius); try { - tool.setBrush(new HeightBrush(file, rotation, yscale, tool, session.getClipboard().getClipboard()), "worldedit.brush.height"); + tool.setBrush(new HeightBrush(stream, rotation, yscale, tool, filename.equalsIgnoreCase("#clipboard") ? session.getClipboard().getClipboard() : null), "worldedit.brush.height"); } catch (EmptyClipboardException ignore) { - tool.setBrush(new HeightBrush(file, rotation, yscale, tool, null), "worldedit.brush.height"); + tool.setBrush(new HeightBrush(stream, rotation, yscale, tool, null), "worldedit.brush.height"); } player.print(BBC.getPrefix() + BBC.BRUSH_HEIGHT.f(radius)); }