99import java .io .IOException ;
1010import java .nio .file .Files ;
1111import java .nio .file .Path ;
12+ import java .nio .file .Paths ;
1213import java .util .*;
1314
1415public class ItemTracker {
1516 private static final Gson GSON = new Gson ();
1617 private Dictionary <String , Integer > estimatedCounts ;
1718 private Dictionary <String , Integer > usages ;
19+ private Dictionary <String , Integer > crafted ;
1820
1921 public void tryUpdate (Path worldPath ) {
2022 try {
@@ -28,6 +30,7 @@ private void update(Path worldPath) throws IOException, JsonSyntaxException {
2830 // Clear tables (and trash old ones)
2931 this .estimatedCounts = new Hashtable <>();
3032 this .usages = new Hashtable <>();
33+ this .crafted = new Hashtable <>();
3134
3235 Path recordFile = worldPath .resolve ("speedrunigt" ).resolve ("record.json" );
3336 if (!Files .exists (recordFile )) { // No record file shouldn't happen, but I guess if it does then give up /shrug
@@ -75,27 +78,51 @@ private void update(Path worldPath) throws IOException, JsonSyntaxException {
7578 this .usages .put (entry .getKey (), entry .getValue ().getAsInt ());
7679 }
7780 }
81+
82+ if (stats .has ("minecraft:crafted" )) {
83+ JsonObject pickedUp = stats .getAsJsonObject ("minecraft:crafted" );
84+ for (Map .Entry <String , JsonElement > entry : pickedUp .entrySet ()) {
85+ // Set crafted count for this item
86+ this .crafted .put (entry .getKey (), entry .getValue ().getAsInt ());
87+ }
88+ }
7889 }
7990
8091 public int getEstimatedCount (String item ) {
8192 return Optional .ofNullable (this .estimatedCounts .get (item )).orElse (0 );
8293 }
8394
95+ private int getCraftedCount (String item ) {
96+ return Optional .ofNullable (this .crafted .get (item )).orElse (0 );
97+ }
98+
8499 public int getUsages (String item ) {
85100 return Optional .ofNullable (this .usages .get (item )).orElse (0 );
86101 }
87102
88- public Optional <JsonObject > constructItemData (Set <String > itemsToGetEstimate , Set <String > itemsToGetUsage ) {
103+ /**
104+ * @param itemsToGetEstimate items to get an estimated count for
105+ * @param itemsToGetUsage items to get an exact amount of times used for
106+ * @param itemsToGetCrafted items to get an exact amount of times crafted/traded for
107+ * @return a json object suitable to be sent as item data to PaceMan.gg
108+ */
109+ public Optional <JsonObject > constructItemData (Set <String > itemsToGetEstimate , Set <String > itemsToGetUsage , Set <String > itemsToGetCrafted ) {
89110 JsonObject estimatedCounts = new JsonObject ();
90111 itemsToGetEstimate .stream ().filter (s -> this .getEstimatedCount (s ) > 0 ).forEach (s -> estimatedCounts .addProperty (s , this .getEstimatedCount (s )));
112+
113+ JsonObject crafted = new JsonObject ();
114+ itemsToGetCrafted .stream ().filter (s -> this .getCraftedCount (s ) > 0 ).forEach (s -> crafted .addProperty (s , this .getCraftedCount (s )));
115+
91116 JsonObject usages = new JsonObject ();
92117 itemsToGetUsage .stream ().filter (s -> this .getUsages (s ) > 0 ).forEach (s -> usages .addProperty (s , this .getUsages (s )));
118+
93119 JsonObject itemData = new JsonObject ();
94- if (estimatedCounts .size () == 0 && usages .size () == 0 ) {
120+ if (estimatedCounts .size () == 0 && usages .size () == 0 && crafted . size () == 0 ) {
95121 return Optional .empty ();
96122 }
97- itemData .add ("estimatedCounts" , estimatedCounts );
98- itemData .add ("usages" , usages );
123+ if (!itemsToGetEstimate .isEmpty ()) itemData .add ("estimatedCounts" , estimatedCounts );
124+ if (!itemsToGetCrafted .isEmpty ()) itemData .add ("crafted" , crafted );
125+ if (!itemsToGetUsage .isEmpty ()) itemData .add ("usages" , usages );
99126 return Optional .of (itemData );
100127 }
101128}
0 commit comments