From b91f940ce02f8f04b30d5b70121d45694a3b6cb4 Mon Sep 17 00:00:00 2001 From: Cyborger1 <45152844+Cyborger1@users.noreply.github.com> Date: Tue, 5 Mar 2024 16:03:29 -0500 Subject: [PATCH 1/4] Fix for when breakdown does not contain main label --- .../java/com/botdetector/http/BotDetectorClient.java | 12 +++++++++++- .../java/com/botdetector/ui/BotDetectorPanel.java | 4 ++-- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/botdetector/http/BotDetectorClient.java b/src/main/java/com/botdetector/http/BotDetectorClient.java index ed00e175..07887379 100644 --- a/src/main/java/com/botdetector/http/BotDetectorClient.java +++ b/src/main/java/com/botdetector/http/BotDetectorClient.java @@ -398,7 +398,17 @@ public void onResponse(Call call, Response response) { try { - future.complete(processResponse(gson, response, Prediction.class)); + Prediction p = processResponse(gson, response, Prediction.class); + // Sanity check + if (p != null && p.getPredictionBreakdown() != null && !p.getPredictionBreakdown().isEmpty()) + { + if (p.getPredictionBreakdown().keySet().stream() + .noneMatch(x -> p.getPredictionLabel().equalsIgnoreCase(x))) + { + p.getPredictionBreakdown().put(p.getPredictionLabel(), p.getConfidence()); + } + } + future.complete(p); } catch (IOException e) { diff --git a/src/main/java/com/botdetector/ui/BotDetectorPanel.java b/src/main/java/com/botdetector/ui/BotDetectorPanel.java index e7cb4278..b8ac7ec4 100644 --- a/src/main/java/com/botdetector/ui/BotDetectorPanel.java +++ b/src/main/java/com/botdetector/ui/BotDetectorPanel.java @@ -1107,7 +1107,7 @@ public void setPrediction(Prediction pred, PlayerSighting sighting) feedbackLabelComboBox.setSelectedItem(UNSURE_PREDICTION_LABEL); feedbackLabelComboBox.addItem(SOMETHING_ELSE_PREDICTION_LABEL); - if (pred.getPredictionBreakdown() == null || pred.getPredictionBreakdown().size() == 0) + if (pred.getPredictionBreakdown() == null || pred.getPredictionBreakdown().isEmpty()) { predictionBreakdownLabel.setText(EMPTY_LABEL); predictionBreakdownPanel.setVisible(false); @@ -1133,7 +1133,7 @@ public void setPrediction(Prediction pred, PlayerSighting sighting) entry -> { FeedbackPredictionLabel pLabel = new FeedbackPredictionLabel(entry.getKey(), entry.getValue(), - entry.getKey().equals(primaryLabel) ? FeedbackValue.POSITIVE : FeedbackValue.NEGATIVE); + entry.getKey().equalsIgnoreCase(primaryLabel) ? FeedbackValue.POSITIVE : FeedbackValue.NEGATIVE); feedbackLabelComboBox.addItem(pLabel); if (pLabel.getFeedbackValue() == FeedbackValue.POSITIVE) { From 09c72a740a30bfe007399faaa926372c9303fc94 Mon Sep 17 00:00:00 2001 From: Cyborger1 <45152844+Cyborger1@users.noreply.github.com> Date: Tue, 5 Mar 2024 16:37:27 -0500 Subject: [PATCH 2/4] Logging and a few more isEmpty replacements --- src/main/java/com/botdetector/BotDetectorPlugin.java | 2 +- src/main/java/com/botdetector/http/BotDetectorClient.java | 4 +++- src/main/java/com/botdetector/ui/BotDetectorPanel.java | 4 ++-- 3 files changed, 6 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/botdetector/BotDetectorPlugin.java b/src/main/java/com/botdetector/BotDetectorPlugin.java index 2042dfbb..ee0519a4 100644 --- a/src/main/java/com/botdetector/BotDetectorPlugin.java +++ b/src/main/java/com/botdetector/BotDetectorPlugin.java @@ -680,7 +680,7 @@ private void processPlayer(Player player) String rawName = player.getName(); - boolean invalidName = rawName == null || rawName.length() == 0 || rawName.charAt(0) == '#' || rawName.charAt(0) == '['; + boolean invalidName = rawName == null || rawName.isEmpty() || rawName.charAt(0) == '#' || rawName.charAt(0) == '['; if (player == client.getLocalPlayer()) { diff --git a/src/main/java/com/botdetector/http/BotDetectorClient.java b/src/main/java/com/botdetector/http/BotDetectorClient.java index 07887379..025589ac 100644 --- a/src/main/java/com/botdetector/http/BotDetectorClient.java +++ b/src/main/java/com/botdetector/http/BotDetectorClient.java @@ -399,13 +399,15 @@ public void onResponse(Call call, Response response) try { Prediction p = processResponse(gson, response, Prediction.class); - // Sanity check + // Sanity check for if the primary label does not appear in the breakdown if (p != null && p.getPredictionBreakdown() != null && !p.getPredictionBreakdown().isEmpty()) { if (p.getPredictionBreakdown().keySet().stream() .noneMatch(x -> p.getPredictionLabel().equalsIgnoreCase(x))) { p.getPredictionBreakdown().put(p.getPredictionLabel(), p.getConfidence()); + log.warn(String.format("Primary prediction label missing from breakdown! Added missing label. (pl:'%s', id:'%d', lb:'%s', cf:'%.4f')", + p.getPlayerName(), p.getPlayerId(), p.getPredictionLabel(), p.getConfidence())); } } future.complete(p); diff --git a/src/main/java/com/botdetector/ui/BotDetectorPanel.java b/src/main/java/com/botdetector/ui/BotDetectorPanel.java index b8ac7ec4..d12c3a8c 100644 --- a/src/main/java/com/botdetector/ui/BotDetectorPanel.java +++ b/src/main/java/com/botdetector/ui/BotDetectorPanel.java @@ -1253,7 +1253,7 @@ private void predictPlayer() { String target = sanitize(searchBar.getText()); - if (target.length() <= 0) + if (target.isEmpty()) { return; } @@ -1654,7 +1654,7 @@ private static String toColoredPercentSpan(double percent) */ private static String toPredictionBreakdownString(Map predictionMap) { - if (predictionMap == null || predictionMap.size() == 0) + if (predictionMap == null || predictionMap.isEmpty()) { return null; } From d2b3d7cd4406c5dc7dda105232ee8d575f4f14be Mon Sep 17 00:00:00 2001 From: Cyborger1 <45152844+Cyborger1@users.noreply.github.com> Date: Tue, 5 Mar 2024 16:53:07 -0500 Subject: [PATCH 3/4] Only check if prediction confidence isn't null --- .../com/botdetector/http/BotDetectorClient.java | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/main/java/com/botdetector/http/BotDetectorClient.java b/src/main/java/com/botdetector/http/BotDetectorClient.java index 025589ac..d5ce6d0f 100644 --- a/src/main/java/com/botdetector/http/BotDetectorClient.java +++ b/src/main/java/com/botdetector/http/BotDetectorClient.java @@ -400,15 +400,15 @@ public void onResponse(Call call, Response response) { Prediction p = processResponse(gson, response, Prediction.class); // Sanity check for if the primary label does not appear in the breakdown - if (p != null && p.getPredictionBreakdown() != null && !p.getPredictionBreakdown().isEmpty()) + if (p != null + && p.getConfidence() != null // Some 'debug' labels such as 'Stats_Too_Low' will have null confidence, ignore these! + && p.getPredictionBreakdown() != null + && !p.getPredictionBreakdown().isEmpty() + && p.getPredictionBreakdown().keySet().stream().noneMatch(x -> p.getPredictionLabel().equalsIgnoreCase(x))) { - if (p.getPredictionBreakdown().keySet().stream() - .noneMatch(x -> p.getPredictionLabel().equalsIgnoreCase(x))) - { - p.getPredictionBreakdown().put(p.getPredictionLabel(), p.getConfidence()); - log.warn(String.format("Primary prediction label missing from breakdown! Added missing label. (pl:'%s', id:'%d', lb:'%s', cf:'%.4f')", - p.getPlayerName(), p.getPlayerId(), p.getPredictionLabel(), p.getConfidence())); - } + p.getPredictionBreakdown().put(p.getPredictionLabel(), p.getConfidence()); + log.warn(String.format("Primary prediction label missing from breakdown! Added missing label. (pl:'%s', id:'%d', lb:'%s', cf:'%.4f')", + p.getPlayerName(), p.getPlayerId(), p.getPredictionLabel(), p.getConfidence())); } future.complete(p); } From c0e2f3dc92c87042dfd48824d7f597b3d946dc4a Mon Sep 17 00:00:00 2001 From: Cyborger1 <45152844+Cyborger1@users.noreply.github.com> Date: Wed, 6 Mar 2024 09:38:39 -0500 Subject: [PATCH 4/4] Invert equality check to avoid potential nullpointer --- src/main/java/com/botdetector/http/BotDetectorClient.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/botdetector/http/BotDetectorClient.java b/src/main/java/com/botdetector/http/BotDetectorClient.java index d5ce6d0f..d84bb2e2 100644 --- a/src/main/java/com/botdetector/http/BotDetectorClient.java +++ b/src/main/java/com/botdetector/http/BotDetectorClient.java @@ -404,7 +404,7 @@ public void onResponse(Call call, Response response) && p.getConfidence() != null // Some 'debug' labels such as 'Stats_Too_Low' will have null confidence, ignore these! && p.getPredictionBreakdown() != null && !p.getPredictionBreakdown().isEmpty() - && p.getPredictionBreakdown().keySet().stream().noneMatch(x -> p.getPredictionLabel().equalsIgnoreCase(x))) + && p.getPredictionBreakdown().keySet().stream().noneMatch(x -> x.equalsIgnoreCase(p.getPredictionLabel()))) { p.getPredictionBreakdown().put(p.getPredictionLabel(), p.getConfidence()); log.warn(String.format("Primary prediction label missing from breakdown! Added missing label. (pl:'%s', id:'%d', lb:'%s', cf:'%.4f')",