1- // Copyright 2021 The Terasology Foundation
1+ // Copyright 2022 The Terasology Foundation
22// SPDX-License-Identifier: Apache-2.0
33package org .terasology .engine .logic .behavior .core ;
44
2121import org .apache .commons .codec .Charsets ;
2222import org .slf4j .Logger ;
2323import org .slf4j .LoggerFactory ;
24+ import org .terasology .engine .context .Context ;
2425import org .terasology .engine .core .module .ModuleManager ;
2526import org .terasology .engine .logic .behavior .BehaviorAction ;
2627import org .terasology .engine .logic .behavior .asset .BehaviorTree ;
27- import org .terasology .engine .registry .CoreRegistry ;
2828import org .terasology .engine .registry .InjectionHelper ;
2929import org .terasology .engine .utilities .gson .UriTypeAdapterFactory ;
3030import org .terasology .gestalt .assets .ResourceUrn ;
@@ -54,10 +54,12 @@ public class BehaviorTreeBuilder implements JsonDeserializer<BehaviorNode>, Json
5454 private Gson gson ;
5555
5656 private int nextId = 1 ;
57+ private final Context context ;
5758
58- public BehaviorTreeBuilder () {
59+ public BehaviorTreeBuilder (Context context ) {
60+ this .context = context ;
5961
60- ModuleManager moduleManager = CoreRegistry .get (ModuleManager .class );
62+ ModuleManager moduleManager = context .get (ModuleManager .class );
6163
6264 if (moduleManager != null ) {
6365 ModuleEnvironment environment = moduleManager .getEnvironment ();
@@ -127,7 +129,7 @@ public void write(JsonWriter out, BehaviorTree value) throws IOException {
127129 @ Override
128130 public BehaviorTree read (JsonReader in ) throws IOException {
129131 String uri = in .nextString ();
130- AssetManager assetManager = CoreRegistry . get (AssetManager .class );
132+ AssetManager assetManager = context . getValue (AssetManager .class );
131133 return assetManager .getAsset (new ResourceUrn (uri ), BehaviorTree .class )
132134 .orElse (assetManager .getAsset (new ResourceUrn ("engine:default" ), BehaviorTree .class ).get ());
133135
@@ -138,37 +140,37 @@ public BehaviorTree read(JsonReader in) throws IOException {
138140 }
139141
140142 @ Override
141- public BehaviorNode deserialize (JsonElement json , Type typeOfT , JsonDeserializationContext context ) throws JsonParseException {
143+ public BehaviorNode deserialize (JsonElement json , Type typeOfT , JsonDeserializationContext jsonContext ) throws JsonParseException {
142144 BehaviorNode node ;
143145 if (json .isJsonPrimitive ()) {
144- node = getPrimitiveNode (json , context );
146+ node = getPrimitiveNode (json , jsonContext );
145147 } else {
146- node = getCompositeNode (json , context );
148+ node = getCompositeNode (json , jsonContext );
147149 }
148150 node = createNode (node );
149151 return node ;
150152 }
151153
152154 @ Override
153- public JsonElement serialize (BehaviorNode src , Type typeOfSrc , JsonSerializationContext context ) {
155+ public JsonElement serialize (BehaviorNode src , Type typeOfSrc , JsonSerializationContext jsonContext ) {
154156 JsonObject node = new JsonObject ();
155157 if (src instanceof DelegateNode ) {
156158 DelegateNode delegateNode = (DelegateNode ) src ;
157- return serialize (delegateNode .delegate , BehaviorNode .class , context );
159+ return serialize (delegateNode .delegate , BehaviorNode .class , jsonContext );
158160 }
159161 if (src instanceof CompositeNode ) {
160162 String name = src .getName ();
161163 JsonArray array = new JsonArray ();
162164 for (int i = 0 ; i < src .getChildrenCount (); i ++) {
163- array .add (serialize (src .getChild (i ), BehaviorNode .class , context ));
165+ array .add (serialize (src .getChild (i ), BehaviorNode .class , jsonContext ));
164166 }
165167 node .add (name , array );
166168 } else if (src instanceof ActionNode ) {
167169 ActionNode actionNode = (ActionNode ) src ;
168170 JsonObject content ;
169171 String name ;
170172 if (actionNode .action != null ) {
171- content = (JsonObject ) context .serialize (actionNode .action );
173+ content = (JsonObject ) jsonContext .serialize (actionNode .action );
172174 name = actionNode .action .getName ();
173175 } else {
174176 content = new JsonObject ();
@@ -178,7 +180,7 @@ public JsonElement serialize(BehaviorNode src, Type typeOfSrc, JsonSerialization
178180 if (src instanceof DecoratorNode ) {
179181 DecoratorNode decoratorNode = (DecoratorNode ) src ;
180182 if (decoratorNode .getChildrenCount () > 0 ) {
181- content .add ("child" , serialize (decoratorNode .getChild (0 ), BehaviorNode .class , context ));
183+ content .add ("child" , serialize (decoratorNode .getChild (0 ), BehaviorNode .class , jsonContext ));
182184 }
183185 }
184186
@@ -193,14 +195,14 @@ public BehaviorNode createNode(BehaviorNode node) {
193195 return node ;
194196 }
195197
196- private BehaviorNode getPrimitiveNode (JsonElement json , JsonDeserializationContext context ) {
198+ private BehaviorNode getPrimitiveNode (JsonElement json , JsonDeserializationContext jsonContext ) {
197199 String type = json .getAsString ();
198200 BehaviorNode node = createNode (type );
199201 if (actions .containsKey (type )) {
200- Action action = context .deserialize (new JsonObject (), actions .get (type ));
202+ Action action = jsonContext .deserialize (new JsonObject (), actions .get (type ));
201203 addAction ((ActionNode ) node , action );
202204 } else if (decorators .containsKey (type )) {
203- Action action = context .deserialize (new JsonObject (), decorators .get (type ));
205+ Action action = jsonContext .deserialize (new JsonObject (), decorators .get (type ));
204206 addAction ((ActionNode ) node , action );
205207 }
206208 return node ;
@@ -210,11 +212,11 @@ private void addAction(ActionNode node, Action action) {
210212 action .setId (nextId );
211213 nextId ++;
212214 node .setAction (action );
213- InjectionHelper .inject (action );
215+ InjectionHelper .inject (action , context );
214216 action .setup ();
215217 }
216218
217- private BehaviorNode getCompositeNode (JsonElement json , JsonDeserializationContext context ) {
219+ private BehaviorNode getCompositeNode (JsonElement json , JsonDeserializationContext jsonContext ) {
218220 String type ;
219221 JsonObject obj = json .getAsJsonObject ();
220222 Map .Entry <String , JsonElement > entry = obj .entrySet ().iterator ().next ();
@@ -224,16 +226,16 @@ private BehaviorNode getCompositeNode(JsonElement json, JsonDeserializationConte
224226 BehaviorNode node = createNode (type );
225227
226228 if (actions .containsKey (type )) {
227- Action action = context .deserialize (jsonElement , actions .get (type ));
229+ Action action = jsonContext .deserialize (jsonElement , actions .get (type ));
228230 addAction ((ActionNode ) node , action );
229231 } else if (decorators .containsKey (type )) {
230- Action action = context .deserialize (jsonElement , decorators .get (type ));
232+ Action action = jsonContext .deserialize (jsonElement , decorators .get (type ));
231233 addAction ((ActionNode ) node , action );
232234 JsonElement childJson = jsonElement .getAsJsonObject ().get ("child" );
233- BehaviorNode child = context .deserialize (childJson , BehaviorNode .class );
235+ BehaviorNode child = jsonContext .deserialize (childJson , BehaviorNode .class );
234236 node .insertChild (0 , child );
235237 } else if (jsonElement .isJsonArray ()) {
236- List <BehaviorNode > children = context .deserialize (jsonElement , new TypeToken <List <BehaviorNode >>() {
238+ List <BehaviorNode > children = jsonContext .deserialize (jsonElement , new TypeToken <List <BehaviorNode >>() {
237239 }.getType ());
238240 for (int i = 0 ; i < children .size (); i ++) {
239241 BehaviorNode child = children .get (i );
0 commit comments