From 95faf00467e4ac088519eff9491a2b2c5b31398b Mon Sep 17 00:00:00 2001 From: Jesse Boyd Date: Mon, 1 May 2017 08:36:45 +1000 Subject: [PATCH] Fixes #521 --- core/src/main/java/com/boydti/fawe/Fawe.java | 3 -- .../fawe/object/pattern/ColorPattern.java | 39 +++++++++++++++++++ .../fawe/object/pattern/ShadePattern.java | 27 +++++++++++++ .../boydti/fawe/util/DelegateTextureUtil.java | 7 ++-- .../com/boydti/fawe/util/TextureUtil.java | 27 +++++++------ 5 files changed, 84 insertions(+), 19 deletions(-) create mode 100644 core/src/main/java/com/boydti/fawe/object/pattern/ColorPattern.java create mode 100644 core/src/main/java/com/boydti/fawe/object/pattern/ShadePattern.java diff --git a/core/src/main/java/com/boydti/fawe/Fawe.java b/core/src/main/java/com/boydti/fawe/Fawe.java index aeb58063..a245acc6 100644 --- a/core/src/main/java/com/boydti/fawe/Fawe.java +++ b/core/src/main/java/com/boydti/fawe/Fawe.java @@ -136,7 +136,6 @@ import javax.management.InstanceAlreadyExistsException; import javax.management.Notification; import javax.management.NotificationEmitter; import javax.management.NotificationListener; -import org.json.simple.parser.ParseException; /**[ WorldEdit action] * | @@ -333,8 +332,6 @@ public class Fawe { tmp.loadModTextures(); } catch (IOException e) { e.printStackTrace(); - } catch (ParseException e) { - e.printStackTrace(); } } } diff --git a/core/src/main/java/com/boydti/fawe/object/pattern/ColorPattern.java b/core/src/main/java/com/boydti/fawe/object/pattern/ColorPattern.java new file mode 100644 index 00000000..3b9fbcb0 --- /dev/null +++ b/core/src/main/java/com/boydti/fawe/object/pattern/ColorPattern.java @@ -0,0 +1,39 @@ +package com.boydti.fawe.object.pattern; + +import com.boydti.fawe.util.TextureUtil; +import com.sk89q.worldedit.Vector; +import com.sk89q.worldedit.WorldEditException; +import com.sk89q.worldedit.blocks.BaseBlock; +import com.sk89q.worldedit.extent.Extent; +import com.sk89q.worldedit.function.pattern.AbstractPattern; + +public class ColorPattern extends AbstractPattern { + private final int color; + private final Extent extent; + private final TextureUtil util; + + public ColorPattern(Extent extent, TextureUtil util, int color) { + this.extent = extent; + this.util = util; + this.color = color; + } + + @Override + public BaseBlock apply(Vector position) { + BaseBlock block = extent.getBlock(position); + int currentColor = util.getColor(block); + int newColor = util.multiplyColor(currentColor, color); + return util.getNearestBlock(newColor); + } + + @Override + public boolean apply(Extent extent, Vector setPosition, Vector getPosition) throws WorldEditException { + BaseBlock block = extent.getBlock(setPosition); + int currentColor = util.getColor(block); + if (currentColor == 0) return false; + int newColor = util.multiplyColor(currentColor, color); + BaseBlock newBlock = util.getNearestBlock(newColor); + if (newBlock.equals(block)) return false; + return extent.setBlock(setPosition, newBlock); + } +} diff --git a/core/src/main/java/com/boydti/fawe/object/pattern/ShadePattern.java b/core/src/main/java/com/boydti/fawe/object/pattern/ShadePattern.java new file mode 100644 index 00000000..a282a7da --- /dev/null +++ b/core/src/main/java/com/boydti/fawe/object/pattern/ShadePattern.java @@ -0,0 +1,27 @@ +package com.boydti.fawe.object.pattern; + +import com.boydti.fawe.util.TextureUtil; +import com.sk89q.worldedit.Vector; +import com.sk89q.worldedit.WorldEditException; +import com.sk89q.worldedit.blocks.BaseBlock; +import com.sk89q.worldedit.extent.Extent; +import com.sk89q.worldedit.function.pattern.AbstractPattern; + +public class ShadePattern extends AbstractPattern{ + private final TextureUtil util; + private final Extent extent; + + public ShadePattern(Extent extent, TextureUtil util) { + this.extent = extent; + this.util = util; + } + @Override + public BaseBlock apply(Vector position) { + return null; + } + + @Override + public boolean apply(Extent extent, Vector setPosition, Vector getPosition) throws WorldEditException { + return false; + } +} \ No newline at end of file diff --git a/core/src/main/java/com/boydti/fawe/util/DelegateTextureUtil.java b/core/src/main/java/com/boydti/fawe/util/DelegateTextureUtil.java index 43c22e7a..af3a7f0a 100644 --- a/core/src/main/java/com/boydti/fawe/util/DelegateTextureUtil.java +++ b/core/src/main/java/com/boydti/fawe/util/DelegateTextureUtil.java @@ -4,7 +4,6 @@ import com.sk89q.worldedit.blocks.BaseBlock; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; -import org.json.simple.parser.ParseException; public class DelegateTextureUtil extends TextureUtil { private final TextureUtil parent; @@ -65,13 +64,13 @@ public class DelegateTextureUtil extends TextureUtil { } @Override - public void loadModTextures() throws IOException, ParseException { + public void loadModTextures() throws IOException { parent.loadModTextures(); } @Override - public int multiply(int c1, int c2) { - return parent.multiply(c1, c2); + public int multiplyColor(int c1, int c2) { + return parent.multiplyColor(c1, c2); } @Override diff --git a/core/src/main/java/com/boydti/fawe/util/TextureUtil.java b/core/src/main/java/com/boydti/fawe/util/TextureUtil.java index 581975f8..269e6534 100644 --- a/core/src/main/java/com/boydti/fawe/util/TextureUtil.java +++ b/core/src/main/java/com/boydti/fawe/util/TextureUtil.java @@ -3,6 +3,9 @@ package com.boydti.fawe.util; import com.boydti.fawe.Fawe; import com.boydti.fawe.FaweCache; import com.boydti.fawe.config.Settings; +import com.google.gson.Gson; +import com.google.gson.reflect.TypeToken; +import com.google.gson.stream.JsonReader; import com.sk89q.worldedit.blocks.BaseBlock; import com.sk89q.worldedit.blocks.BlockID; import com.sk89q.worldedit.world.registry.BundledBlockData; @@ -14,6 +17,7 @@ import java.io.FilenameFilter; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; +import java.lang.reflect.Type; import java.nio.file.Path; import java.nio.file.Paths; import java.util.ArrayList; @@ -29,9 +33,6 @@ import java.util.regex.Pattern; import java.util.zip.ZipEntry; import java.util.zip.ZipFile; import javax.imageio.ImageIO; -import org.json.simple.JSONObject; -import org.json.simple.parser.JSONParser; -import org.json.simple.parser.ParseException; public class TextureUtil { private final File folder; @@ -416,9 +417,10 @@ public class TextureUtil { return colorDistance(red1, green1, blue1, c2); } - public void loadModTextures() throws IOException, ParseException { + public void loadModTextures() throws IOException { Int2ObjectOpenHashMap colorMap = new Int2ObjectOpenHashMap<>(); Int2ObjectOpenHashMap distanceMap = new Int2ObjectOpenHashMap<>(); + Gson gson = new Gson(); if (folder.exists()) { // Get all the jar files for (File file : folder.listFiles(new FilenameFilter() { @@ -475,8 +477,9 @@ public class TextureUtil { continue; } try (InputStream is = zipFile.getInputStream(entry)) { //Read from a file, or a HttpRequest, or whatever. - JSONParser parser = new JSONParser(); - JSONObject root = (JSONObject) parser.parse(new InputStreamReader(is, "UTF-8")); + JsonReader reader = new JsonReader(new InputStreamReader(is, "UTF-8")); + Type type = new TypeToken>(){}.getType(); + Map root = gson.fromJson(reader, type); // Try to work out the texture names for this file addTextureNames(blockName, root, texturesMap); } @@ -581,8 +584,8 @@ public class TextureUtil { biomes[6].grass = 0; biomes[134].grass = 0; // roofed forest: averaged w/ 0x28340A - biomes[29].grass = multiply(biomes[29].grass, 0x28340A + (255 << 24)); - biomes[157].grass = multiply(biomes[157].grass, 0x28340A + (255 << 24)); + biomes[29].grass = multiplyColor(biomes[29].grass, 0x28340A + (255 << 24)); + biomes[157].grass = multiplyColor(biomes[157].grass, 0x28340A + (255 << 24)); // mesa : 0x90814D biomes[37].grass = 0x90814D + (255 << 24); biomes[38].grass = 0x90814D + (255 << 24); @@ -593,7 +596,7 @@ public class TextureUtil { List valid = new ArrayList<>(); for (int i = 0; i < biomes.length; i++) { BiomeColor biome = biomes[i]; - biome.grass = multiply(biome.grass, grass); + biome.grass = multiplyColor(biome.grass, grass); if (biome.grass != 0 && !biome.name.equalsIgnoreCase("Unknown Biome")) { valid.add(biome); } @@ -631,7 +634,7 @@ public class TextureUtil { calculateLayerArrays(); } - protected int multiply(int c1, int c2) { + public int multiplyColor(int c1, int c2) { int alpha1 = (c1 >> 24) & 0xFF; int alpha2 = (c2 >> 24) & 0xFF; int red1 = (c1 >> 16) & 0xFF; @@ -752,8 +755,8 @@ public class TextureUtil { * - Match by appending / removing
* - Match by hardcoded values
*/ - private void addTextureNames(String modelName, JSONObject root, Map texturesMap) { - JSONObject textures = (JSONObject) root.get("textures"); + private void addTextureNames(String modelName, Map root, Map texturesMap) { + Map textures = (Map) root.get("textures"); if (textures == null) { return; }