From 5f8e2ed4d89e6d89cba0d7e9b0cb88efb3f94b7e Mon Sep 17 00:00:00 2001 From: Jesse Boyd Date: Mon, 3 Sep 2018 07:46:35 +1000 Subject: [PATCH] Allow local images for brushes --- .../com/boydti/fawe/util/image/ImageUtil.java | 16 +++++-- .../worldedit/command/BrushCommands.java | 42 +++++-------------- 2 files changed, 23 insertions(+), 35 deletions(-) diff --git a/worldedit-core/src/main/java/com/boydti/fawe/util/image/ImageUtil.java b/worldedit-core/src/main/java/com/boydti/fawe/util/image/ImageUtil.java index f42b731a2..84c2db655 100644 --- a/worldedit-core/src/main/java/com/boydti/fawe/util/image/ImageUtil.java +++ b/worldedit-core/src/main/java/com/boydti/fawe/util/image/ImageUtil.java @@ -13,7 +13,9 @@ import java.awt.Transparency; import java.awt.image.BufferedImage; import java.awt.image.DataBufferInt; import java.io.File; +import java.io.FileInputStream; import java.io.IOException; +import java.io.InputStream; import java.net.URI; import java.net.URISyntaxException; import java.net.URL; @@ -164,13 +166,21 @@ public class ImageUtil { } public static BufferedImage load(URI uri) throws ParameterException { + try { + return MainUtil.readImage(getInputStream(uri)); + } catch (IOException e) { + throw new ParameterException(e); + } + } + + public static InputStream getInputStream(URI uri) throws ParameterException { try { String uriStr = uri.toString(); if (uriStr.startsWith("file:/")) { File file = new File(uri.getPath()); - return MainUtil.readImage(file); + return new FileInputStream(file); } - return MainUtil.readImage(new URL(uriStr)); + return new URL(uriStr).openStream(); } catch (IOException e) { throw new ParameterException(e); } @@ -210,7 +220,7 @@ public class ImageUtil { } else if (arg.startsWith("file:/")) { arg = arg.replaceFirst("file:/+", ""); File file = MainUtil.getFile(MainUtil.getFile(Fawe.imp().getDirectory(), com.boydti.fawe.config.Settings.IMP.PATHS.HEIGHTMAP), arg); - if (file.exists()) { + if (!file.exists()) { throw new ParameterException("File not found " + file); } if (file.isDirectory()) { diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/command/BrushCommands.java b/worldedit-core/src/main/java/com/sk89q/worldedit/command/BrushCommands.java index 40bca34e1..7eef94b30 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/command/BrushCommands.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/command/BrushCommands.java @@ -62,6 +62,7 @@ import com.sk89q.worldedit.world.block.BlockStateHolder; import java.awt.*; import java.awt.image.BufferedImage; import java.io.*; +import java.net.URI; import java.net.URL; import java.nio.channels.Channels; import java.nio.channels.ReadableByteChannel; @@ -348,7 +349,7 @@ public class BrushCommands extends BrushProcessor { max = -1 ) @CommandPermissions("worldedit.brush.stencil") - public BrushSettings stencilBrush(Player player, EditSession editSession, LocalSession session, Pattern fill, @Optional("5") Expression radius, @Optional("") final String image, @Optional("0") @Step(90) @Range(min=0, max=360) final int rotation, @Optional("1") final double yscale, @Switch('w') boolean onlyWhite, @Switch('r') boolean randomRotate, CommandContext context) throws WorldEditException { + public BrushSettings stencilBrush(Player player, EditSession editSession, LocalSession session, Pattern fill, @Optional("5") Expression radius, @Optional("") final String image, @Optional("0") @Step(90) @Range(min=0, max=360) final int rotation, @Optional("1") final double yscale, @Switch('w') boolean onlyWhite, @Switch('r') boolean randomRotate, CommandContext context) throws WorldEditException, FileNotFoundException, ParameterException { getWorldEdit().checkMaxBrushRadius(radius); InputStream stream = getHeightmapStream(image); HeightBrush brush; @@ -689,7 +690,7 @@ public class BrushCommands extends BrushProcessor { max = 4 ) @CommandPermissions("worldedit.brush.height") - public BrushSettings heightBrush(Player player, LocalSession session, @Optional("5") Expression radius, @Optional("") final String image, @Optional("0") @Step(90) @Range(min=0, max=360) final int rotation, @Optional("1") final double yscale, @Switch('r') boolean randomRotate, @Switch('l') boolean layers, @Switch('s') boolean dontSmooth, CommandContext context) throws WorldEditException { + public BrushSettings heightBrush(Player player, LocalSession session, @Optional("5") Expression radius, @Optional("") final String image, @Optional("0") @Step(90) @Range(min=0, max=360) final int rotation, @Optional("1") final double yscale, @Switch('r') boolean randomRotate, @Switch('l') boolean layers, @Switch('s') boolean dontSmooth, CommandContext context) throws WorldEditException, FileNotFoundException, ParameterException { return terrainBrush(player, session, radius, image, rotation, yscale, false, randomRotate, layers, !dontSmooth, ScalableHeightMap.Shape.CONE, context); } @@ -707,7 +708,7 @@ public class BrushCommands extends BrushProcessor { max = 4 ) @CommandPermissions("worldedit.brush.height") - public BrushSettings cliffBrush(Player player, LocalSession session, @Optional("5") Expression radius, @Optional("") final String image, @Optional("0") @Step(90) @Range(min=0, max=360) final int rotation, @Optional("1") final double yscale, @Switch('r') boolean randomRotate, @Switch('l') boolean layers, @Switch('s') boolean dontSmooth, CommandContext context) throws WorldEditException { + public BrushSettings cliffBrush(Player player, LocalSession session, @Optional("5") Expression radius, @Optional("") final String image, @Optional("0") @Step(90) @Range(min=0, max=360) final int rotation, @Optional("1") final double yscale, @Switch('r') boolean randomRotate, @Switch('l') boolean layers, @Switch('s') boolean dontSmooth, CommandContext context) throws WorldEditException, FileNotFoundException, ParameterException { return terrainBrush(player, session, radius, image, rotation, yscale, true, randomRotate, layers, !dontSmooth, ScalableHeightMap.Shape.CYLINDER, context); } @@ -724,11 +725,11 @@ public class BrushCommands extends BrushProcessor { max = 4 ) @CommandPermissions("worldedit.brush.height") - public BrushSettings flattenBrush(Player player, LocalSession session, @Optional("5") Expression radius, @Optional("") final String image, @Optional("0") @Step(90) @Range(min=0, max=360) final int rotation, @Optional("1") final double yscale, @Switch('r') boolean randomRotate, @Switch('l') boolean layers, @Switch('s') boolean dontSmooth, CommandContext context) throws WorldEditException { + public BrushSettings flattenBrush(Player player, LocalSession session, @Optional("5") Expression radius, @Optional("") final String image, @Optional("0") @Step(90) @Range(min=0, max=360) final int rotation, @Optional("1") final double yscale, @Switch('r') boolean randomRotate, @Switch('l') boolean layers, @Switch('s') boolean dontSmooth, CommandContext context) throws WorldEditException, FileNotFoundException, ParameterException { return terrainBrush(player, session, radius, image, rotation, yscale, true, randomRotate, layers, !dontSmooth, ScalableHeightMap.Shape.CONE, context); } - private BrushSettings terrainBrush(Player player, LocalSession session, Expression radius, String image, int rotation, double yscale, boolean flat, boolean randomRotate, boolean layers, boolean smooth, ScalableHeightMap.Shape shape, CommandContext context) throws WorldEditException { + private BrushSettings terrainBrush(Player player, LocalSession session, Expression radius, String image, int rotation, double yscale, boolean flat, boolean randomRotate, boolean layers, boolean smooth, ScalableHeightMap.Shape shape, CommandContext context) throws WorldEditException, FileNotFoundException, ParameterException { getWorldEdit().checkMaxBrushRadius(radius); InputStream stream = getHeightmapStream(image); HeightBrush brush; @@ -753,35 +754,12 @@ public class BrushCommands extends BrushProcessor { .setSize(radius); } - private InputStream getHeightmapStream(String filename) { + private InputStream getHeightmapStream(String filename) throws FileNotFoundException, ParameterException { String filenamePng = (filename.endsWith(".png") ? filename : filename + ".png"); File file = new File(Fawe.imp().getDirectory(), Settings.IMP.PATHS.HEIGHTMAP + File.separator + filenamePng); - if (!file.exists()) { - if (!filename.equals("#clipboard") && filename.length() >= 7) { - try { - URL url; - if (filename.startsWith("http")) { - url = new URL(filename); - if (!url.getHost().equals("i.imgur.com")) { - throw new FileNotFoundException(filename); - } - } else { - url = new URL("https://i.imgur.com/" + filenamePng); - } - ReadableByteChannel rbc = Channels.newChannel(url.openStream()); - return Channels.newInputStream(rbc); - } catch (IOException e) { - throw new RuntimeException(e); - } - } - } else if (!filename.equalsIgnoreCase("#clipboard")) { - try { - return new FileInputStream(file); - } catch (FileNotFoundException e) { - throw new RuntimeException(e); - } - } - return null; + if (file.exists()) return new FileInputStream(file); + URI uri = ImageUtil.getImageURI(filename); + return ImageUtil.getInputStream(uri); }