From 5d8d6b764e982ab9aec737424c1d7058b23887cb Mon Sep 17 00:00:00 2001 From: Juliakn66 <117075450+Juliakn66@users.noreply.github.com> Date: Fri, 3 Feb 2023 15:27:30 +0100 Subject: [PATCH] refactoring: created method getLocationObject() --- .../bitbiome/commands/LookaroundCommand.java | 21 +++++++++++-------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/src/main/java/org/bitbiome/commands/LookaroundCommand.java b/src/main/java/org/bitbiome/commands/LookaroundCommand.java index a090061..ce18dda 100644 --- a/src/main/java/org/bitbiome/commands/LookaroundCommand.java +++ b/src/main/java/org/bitbiome/commands/LookaroundCommand.java @@ -20,15 +20,9 @@ public class LookaroundCommand implements CommandAPI{ StringBuilder s = new StringBuilder(); Location location = travelEngine.getPlayer().getLocation(); JsonParser jp = new JsonParser(); - JSONObject o = jp.getJSONObject("gameconfig.json"); - JSONArray locations = o.getJSONArray("locations"); - JSONObject locationObject = locations.getJSONObject(0); - - for (int i = 1; i < locations.length(); i++) { - if(locations.getJSONObject(i).getString("name").equals(location.getName())){ - locationObject = locations.getJSONObject(i); - } - } + JSONObject gameConfig = jp.getJSONObject("gameconfig.json"); + JSONArray locations = gameConfig.getJSONArray("locations"); + JSONObject locationObject = getLocationObject(location.getName(), locations); JSONArray items = locationObject.getJSONArray("items"); JSONArray mobs = locationObject.getJSONArray("mobs"); @@ -101,5 +95,14 @@ public class LookaroundCommand implements CommandAPI{ System.out.println(s); } + public JSONObject getLocationObject(String locationName, JSONArray locations) { + for (int i = 1; i < locations.length(); i++) { + if(locations.getJSONObject(i).getString("name").equals(locationName)){ + return locations.getJSONObject(i); + } + } + return locations.getJSONObject(0); + } + }