From 1f869fe40fb4fe02692f6a642eab3cb9513c8045 Mon Sep 17 00:00:00 2001 From: Anatoly Belikov Date: Wed, 5 Jul 2023 18:03:30 +0300 Subject: [PATCH] refactor json inventory --- ...vationFromFullInventoryImplementation.java | 34 ++---------------- .../utils/JSONWorldDataHelper.java | 36 +++++++++++++++++++ 2 files changed, 39 insertions(+), 31 deletions(-) diff --git a/src/main/java/io/singularitynet/MissionHandlers/ObservationFromFullInventoryImplementation.java b/src/main/java/io/singularitynet/MissionHandlers/ObservationFromFullInventoryImplementation.java index 968c5e3f..a48cfba0 100644 --- a/src/main/java/io/singularitynet/MissionHandlers/ObservationFromFullInventoryImplementation.java +++ b/src/main/java/io/singularitynet/MissionHandlers/ObservationFromFullInventoryImplementation.java @@ -23,6 +23,7 @@ import io.singularitynet.projectmalmo.DrawItem; import io.singularitynet.projectmalmo.MissionInit; import io.singularitynet.projectmalmo.ObservationFromFullInventory; +import io.singularitynet.utils.JSONWorldDataHelper; import net.minecraft.client.MinecraftClient; import net.minecraft.entity.player.PlayerEntity; import net.minecraft.entity.player.PlayerInventory; @@ -49,13 +50,13 @@ void buildJson(JsonObject json, PlayerEntity player) // b) any chest-type objects the player is looking at. todo // Newer approach - an array of objects. JsonArray arr = new JsonArray(); - getInventoryJSON(arr, player.getInventory()); + JSONWorldDataHelper.getInventoryJSON(arr, player.getInventory()); json.add("inventory", arr); // Also add an entry for each type of inventory available. JsonArray arrInvs = new JsonArray(); JsonObject jobjPlayer = new JsonObject(); - jobjPlayer.add("name", new JsonPrimitive(getInventoryName(player.getInventory()))); + jobjPlayer.add("name", new JsonPrimitive(JSONWorldDataHelper.getInventoryName(player.getInventory()))); jobjPlayer.add("size", new JsonPrimitive(player.getInventory().size())); arrInvs.add(jobjPlayer); @@ -77,35 +78,6 @@ public boolean parseParameters(Object params) return true; } - public static String getInventoryName(Inventory inv) - { - String invName = "inventory"; - String prefix = "container."; - if (invName.startsWith(prefix)) - invName = invName.substring(prefix.length()); - return invName; - } - - public static void getInventoryJSON(JsonArray arr, Inventory inventory) - { - String invName = getInventoryName(inventory); - for (int i = 0; i < inventory.size(); i++) - { - ItemStack is = inventory.getStack(i); - if (is != null && !is.isEmpty()) - { - Item item = is.getItem(); - JsonObject jobj = new JsonObject(); - String name = item.toString(); - jobj.add("type", new JsonPrimitive(name)); - jobj.add("index", new JsonPrimitive(i)); - jobj.add("quantity", new JsonPrimitive(is.getCount())); - jobj.add("inventory", new JsonPrimitive(invName)); - arr.add(jobj); - } - } - } - public static void getInventoryJSON(JsonObject json, String prefix, Inventory inventory, int maxSlot) { int nSlots = Math.min(inventory.size(), maxSlot); diff --git a/src/main/java/io/singularitynet/utils/JSONWorldDataHelper.java b/src/main/java/io/singularitynet/utils/JSONWorldDataHelper.java index b841fdca..b00e359f 100644 --- a/src/main/java/io/singularitynet/utils/JSONWorldDataHelper.java +++ b/src/main/java/io/singularitynet/utils/JSONWorldDataHelper.java @@ -31,7 +31,10 @@ import net.minecraft.entity.LivingEntity; import net.minecraft.entity.mob.MobEntity; import net.minecraft.entity.player.PlayerEntity; +import net.minecraft.inventory.Inventory; import net.minecraft.inventory.SimpleInventory; +import net.minecraft.item.Item; +import net.minecraft.item.ItemStack; import net.minecraft.registry.Registries; import net.minecraft.util.Identifier; import net.minecraft.util.math.BlockPos; @@ -191,6 +194,9 @@ public static void buildControllableMobsData(JsonObject json, Map