From a9a5fb6396bd280dec4044151b6d1f3ea1cae921 Mon Sep 17 00:00:00 2001 From: Anuken Date: Sun, 7 Apr 2024 12:36:45 -0400 Subject: [PATCH] Support for adding to ObjectMaps in JSON with `add: true` --- core/src/mindustry/mod/ContentParser.java | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/core/src/mindustry/mod/ContentParser.java b/core/src/mindustry/mod/ContentParser.java index 40da31c7d1..5c2d59513f 100644 --- a/core/src/mindustry/mod/ContentParser.java +++ b/core/src/mindustry/mod/ContentParser.java @@ -1054,7 +1054,21 @@ public class ContentParser{ } Field field = metadata.field; try{ - field.set(object, parser.readValue(field.getType(), metadata.elementType, child, metadata.keyType)); + boolean mergeMap = ObjectMap.class.isAssignableFrom(field.getType()) && child.has("add") && child.get("add").isBoolean() && child.getBoolean("add", false); + + if(mergeMap){ + child.remove("add"); + } + + Object readField = parser.readValue(field.getType(), metadata.elementType, child, metadata.keyType); + + //if a map has add: true, add its contents to the map instead + if(mergeMap && field.get(object) instanceof ObjectMap baseMap){ + baseMap.putAll((ObjectMap)readField); + }else{ + field.set(object, readField); + } + }catch(IllegalAccessException ex){ throw new SerializationException("Error accessing field: " + field.getName() + " (" + type.getName() + ")", ex); }catch(SerializationException ex){