From 8b5c0c1ae710a60cdbad761cf01fbfa486f5b229 Mon Sep 17 00:00:00 2001 From: RecursivePineapple Date: Sat, 4 Jan 2025 12:59:48 -0500 Subject: [PATCH] improve block spec null handling --- .../matter_manipulator/CommonProxy.java | 1 - .../common/building/BlockSpec.java | 13 ++++++++++--- .../common/data/WeightedSpecList.java | 6 ++++++ .../common/items/manipulator/MMConfig.java | 11 +++++++---- .../common/persist/WeightedListJsonAdapter.java | 7 +++++++ 5 files changed, 30 insertions(+), 8 deletions(-) diff --git a/src/main/java/com/recursive_pineapple/matter_manipulator/CommonProxy.java b/src/main/java/com/recursive_pineapple/matter_manipulator/CommonProxy.java index 7f3a526..fae6bbe 100644 --- a/src/main/java/com/recursive_pineapple/matter_manipulator/CommonProxy.java +++ b/src/main/java/com/recursive_pineapple/matter_manipulator/CommonProxy.java @@ -36,7 +36,6 @@ public void init(FMLInitializationEvent event) { public void postInit(FMLPostInitializationEvent event) { ManipulatorRecipes.addRecipes(); BlockPropertyRegistry.init(); - BlockSpec.init(); } public void serverStarting(FMLServerStartingEvent event) { diff --git a/src/main/java/com/recursive_pineapple/matter_manipulator/common/building/BlockSpec.java b/src/main/java/com/recursive_pineapple/matter_manipulator/common/building/BlockSpec.java index abdebeb..1fa757c 100644 --- a/src/main/java/com/recursive_pineapple/matter_manipulator/common/building/BlockSpec.java +++ b/src/main/java/com/recursive_pineapple/matter_manipulator/common/building/BlockSpec.java @@ -379,10 +379,17 @@ public static BlockSpec fromBlock(BlockSpec pooled, World world, int x, int y, i return spec; } - public static final ImmutableBlockSpec AIR = new BlockSpec(); + public static final ImmutableBlockSpec AIR = air(); - public static void init() { - ((BlockSpec)AIR).setObject(Blocks.air, 0); + public static BlockSpec air() { + BlockSpec spec = new BlockSpec(); + + spec.block = Blocks.air; + spec.item = Optional.empty(); + spec.itemId = Optional.empty(); + spec.stack = Optional.empty(); + + return spec; } public static ImmutableBlockSpec choose(List specs, Random rng) { diff --git a/src/main/java/com/recursive_pineapple/matter_manipulator/common/data/WeightedSpecList.java b/src/main/java/com/recursive_pineapple/matter_manipulator/common/data/WeightedSpecList.java index 7adf363..60dd336 100644 --- a/src/main/java/com/recursive_pineapple/matter_manipulator/common/data/WeightedSpecList.java +++ b/src/main/java/com/recursive_pineapple/matter_manipulator/common/data/WeightedSpecList.java @@ -13,6 +13,12 @@ public class WeightedSpecList { public ArrayList> specs = new ArrayList<>(); + public WeightedSpecList(BlockSpec... values) { + for (BlockSpec spec : values) { + add(spec); + } + } + public void add(BlockSpec spec) { for (var p : specs) { if (Objects.equals(p.first(), spec)) { diff --git a/src/main/java/com/recursive_pineapple/matter_manipulator/common/items/manipulator/MMConfig.java b/src/main/java/com/recursive_pineapple/matter_manipulator/common/items/manipulator/MMConfig.java index 54db37a..fa74cb7 100644 --- a/src/main/java/com/recursive_pineapple/matter_manipulator/common/items/manipulator/MMConfig.java +++ b/src/main/java/com/recursive_pineapple/matter_manipulator/common/items/manipulator/MMConfig.java @@ -30,13 +30,16 @@ public class MMConfig { // if any are non-null, then the corresponding block is being moved public Vector3i coordAOffset, coordBOffset, coordCOffset; - public WeightedSpecList corners, edges, faces, volumes; - public BlockSpec cables; + public WeightedSpecList corners = new WeightedSpecList(BlockSpec.air()); + public WeightedSpecList edges = new WeightedSpecList(BlockSpec.air()); + public WeightedSpecList faces = new WeightedSpecList(BlockSpec.air()); + public WeightedSpecList volumes = new WeightedSpecList(BlockSpec.air()); + public BlockSpec cables = BlockSpec.air(); /** These blocks are what gets removed when exchanging */ - public WeightedSpecList replaceWhitelist; + public WeightedSpecList replaceWhitelist = new WeightedSpecList(BlockSpec.air()); /** These blocks are what gets placed when exchanging */ - public WeightedSpecList replaceWith; + public WeightedSpecList replaceWith = new WeightedSpecList(BlockSpec.air()); @Nullable public Transform transform; diff --git a/src/main/java/com/recursive_pineapple/matter_manipulator/common/persist/WeightedListJsonAdapter.java b/src/main/java/com/recursive_pineapple/matter_manipulator/common/persist/WeightedListJsonAdapter.java index c511f11..aadae1d 100644 --- a/src/main/java/com/recursive_pineapple/matter_manipulator/common/persist/WeightedListJsonAdapter.java +++ b/src/main/java/com/recursive_pineapple/matter_manipulator/common/persist/WeightedListJsonAdapter.java @@ -7,6 +7,7 @@ import com.google.gson.JsonDeserializationContext; import com.google.gson.JsonDeserializer; import com.google.gson.JsonElement; +import com.google.gson.JsonNull; import com.google.gson.JsonObject; import com.google.gson.JsonParseException; import com.google.gson.JsonPrimitive; @@ -40,6 +41,12 @@ public WeightedSpecList deserialize(JsonElement json, Type typeOfT, JsonDeserial @Override public JsonElement serialize(WeightedSpecList src, Type typeOfSrc, JsonSerializationContext context) { + if (src.specs.size() == 1) { + BlockSpec spec = src.specs.get(0).left(); + + if (spec == null || spec.isAir()) return JsonNull.INSTANCE; + } + JsonArray array = new JsonArray(); for (var p : src.specs) {