Skip to content

Commit

Permalink
refactor json inventory
Browse files Browse the repository at this point in the history
  • Loading branch information
noskill committed Jul 17, 2023
1 parent 5a6349a commit 1f869fe
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);

Expand All @@ -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);
Expand Down
36 changes: 36 additions & 0 deletions src/main/java/io/singularitynet/utils/JSONWorldDataHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -191,11 +194,44 @@ public static void buildControllableMobsData(JsonObject json, Map<String, MobEnt
if (entity instanceof InventoryOwner){
InventoryOwner owner = (InventoryOwner) entity;
SimpleInventory inventory = owner.getInventory();
JsonArray arr = new JsonArray();
getInventoryJSON(arr, inventory);
json.add("inventory", arr);
}
mobObj.addProperty("safeFallDistance", entity.getSafeFallDistance());
mobObj.addProperty("name", entity.getEntityName());
mobObj.addProperty("type", entity.getType().getUntranslatedName());
controllableEntities.add(key, mobObj);
}
}


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);
}
}
}
}

0 comments on commit 1f869fe

Please sign in to comment.