Add heightmap from imgur

This commit is contained in:
Jesse Boyd 2017-02-08 12:01:44 +11:00
parent 295baa4217
commit a3b8f4c917
No known key found for this signature in database
GPG Key ID: 59F1DE6293AF6E1F
3 changed files with 42 additions and 17 deletions

View File

@ -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();
}
}

View File

@ -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();

View File

@ -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));
}