From ee6f9a1fe80b0da4df051f7c36b67271f805662a Mon Sep 17 00:00:00 2001 From: "Weston Rye (Demod)" Date: Mon, 14 May 2018 12:27:41 -0400 Subject: [PATCH] Ignoring invalid modules on entities. --- .../com/demod/fbsr/EntityRendererFactory.java | 4 ++-- .../src/com/demod/fbsr/FBSR.java | 23 ++++--------------- .../src/com/demod/fbsr/RenderUtils.java | 10 +++++++- 3 files changed, 15 insertions(+), 22 deletions(-) diff --git a/FactorioBlueprintStringRenderer/src/com/demod/fbsr/EntityRendererFactory.java b/FactorioBlueprintStringRenderer/src/com/demod/fbsr/EntityRendererFactory.java index 1b72aec..88b1078 100644 --- a/FactorioBlueprintStringRenderer/src/com/demod/fbsr/EntityRendererFactory.java +++ b/FactorioBlueprintStringRenderer/src/com/demod/fbsr/EntityRendererFactory.java @@ -212,7 +212,7 @@ public class EntityRendererFactory { public void createModuleIcons(Consumer register, WorldMap map, DataTable table, BlueprintEntity entity, EntityPrototype prototype) { - Optional> modulesOpt = RenderUtils.getModules(entity); + Optional> modulesOpt = RenderUtils.getModules(entity, table); if (modulesOpt.isPresent()) { Multiset modules = modulesOpt.get(); @@ -272,7 +272,7 @@ public class EntityRendererFactory { Map beaconModules = new LinkedHashMap<>(); for (BlueprintEntity beacon : beacons) { double distributionEffectivity = beacon.json().getDouble("distribution_effectivity"); - Optional> modulesOpt2 = RenderUtils.getModules(beacon); + Optional> modulesOpt2 = RenderUtils.getModules(beacon, table); if (modulesOpt2.isPresent()) { Multiset modules = modulesOpt2.get(); for (Multiset.Entry entry : modules.entrySet()) { diff --git a/FactorioBlueprintStringRenderer/src/com/demod/fbsr/FBSR.java b/FactorioBlueprintStringRenderer/src/com/demod/fbsr/FBSR.java index e735174..c64f1f2 100644 --- a/FactorioBlueprintStringRenderer/src/com/demod/fbsr/FBSR.java +++ b/FactorioBlueprintStringRenderer/src/com/demod/fbsr/FBSR.java @@ -35,9 +35,7 @@ import java.util.function.Consumer; import javax.imageio.ImageIO; -import org.json.JSONArray; import org.json.JSONException; -import org.json.JSONObject; import com.demod.factorio.DataTable; import com.demod.factorio.FactorioData; @@ -539,23 +537,10 @@ public class FBSR { addToItemAmount(ret, i.getName(), 1); }); - if (entity.json().has("items")) { - Object itemsJson = entity.json().get("items"); - if (itemsJson instanceof JSONObject) { - Utils.forEach(entity.json().getJSONObject("items"), (String moduleName, Integer count) -> { - if (!table.getItem(moduleName).isPresent()) { - return; - } - addToItemAmount(ret, moduleName, count); - }); - } else if (itemsJson instanceof JSONArray) { - Utils.forEach(entity.json().getJSONArray("items"), j -> { - String moduleName = j.getString("item"); - if (!table.getItem(moduleName).isPresent()) { - return; - } - addToItemAmount(ret, moduleName, j.getInt("count")); - }); + Optional> modules = RenderUtils.getModules(entity, table); + if (modules.isPresent()) { + for (Multiset.Entry entry : modules.get().entrySet()) { + addToItemAmount(ret, entry.getElement(), entry.getCount()); } } } diff --git a/FactorioBlueprintStringRenderer/src/com/demod/fbsr/RenderUtils.java b/FactorioBlueprintStringRenderer/src/com/demod/fbsr/RenderUtils.java index 43ee56d..5153dde 100644 --- a/FactorioBlueprintStringRenderer/src/com/demod/fbsr/RenderUtils.java +++ b/FactorioBlueprintStringRenderer/src/com/demod/fbsr/RenderUtils.java @@ -20,9 +20,11 @@ import org.json.JSONArray; import org.json.JSONObject; import org.luaj.vm2.LuaValue; +import com.demod.factorio.DataTable; import com.demod.factorio.FactorioData; import com.demod.factorio.Utils; import com.demod.factorio.prototype.EntityPrototype; +import com.demod.factorio.prototype.ItemPrototype; import com.demod.factorio.prototype.TilePrototype; import com.demod.fbsr.Renderer.Layer; import com.google.common.collect.ImmutableList; @@ -139,7 +141,7 @@ public final class RenderUtils { return new Color(sumR / sumA, sumG / sumA, sumB / sumA); } - public static Optional> getModules(BlueprintEntity entity) { + public static Optional> getModules(BlueprintEntity entity, DataTable table) { if (!entity.json().has("items")) { return Optional.empty(); } @@ -156,6 +158,12 @@ public final class RenderUtils { modules.add(j.getString("item"), j.getInt("count")); }); } + + modules.entrySet().removeIf(e -> { + Optional item = table.getItem(e.getElement()); + return !item.isPresent() || !item.get().getType().equals("module"); + }); + return Optional.of(modules); }