Closed Andre601 closed 3 years ago
I think your problem is, that you re-use the entry
map if there is more than one element in the protocolVersions
list.
In the end, all the entries in your map
have the same value.
E.g.
Map<String, Map<String, Integer>> map = new HashMap<>();
Map<String, Integer> entry = new HashMap<>();
entry.put("one", 1);
map.put("x", entry);
entry.put("two", 2);
map.put("y", entry);
will not produce
{
"x": {
"one": 1
},
"y": {
"two": 2
}
}
but
{
"x": {
"one": 1,
"two": 2
},
"y": {
"one": 1,
"two": 2
}
}
So your code should probably look like this:
@Override
public void loadMetrics(){
Metrics metrics = new Metrics(this, 10340);
metrics.addCustomChart(new DrilldownPie("allowed_versions", () -> {
Map<String, Map<String, Integer>> map = new HashMap<>();
List<Integer> protocolVersions = getConfigHandler().getIntList("Protocol", "Versions");
if(protocolVersions.isEmpty()){
String unknown = ProtocolVersion.getFriendlyName(0);
Map<String, Integer> entry = new HashMap<>();
entry.put(unknown, 1);
map.put("other", entry);
return map;
}
for(int version : protocolVersions){
String major = ProtocolVersion.getMajor(version);
String name = ProtocolVersion.getFriendlyName(version);
Map<String, Integer> entry = new HashMap<>();
entry.put(name, 1);
if(major.equalsIgnoreCase("?")){
map.put("other", entry);
}else{
map.put(major, entry);
}
}
return map;
}));
}
This is ofc only the case if getConfigHandler().getIntList("Protocol", "Versions");
ever returns a list with more than 1 element. I'm not familiar with your project/plugin, so if this is not the case, please tell me and I'll look deeper into it :)
getIntList
does, as the name suggests (And as shown in the code) return a List<Integer>
so there's always a chance of having more than 1 entry in the returned list,
Anyway, thanks for this info. I now implemented the change and hope it will work.
Also, wouldn't your example produce this?
{
"x": {
"one": 1
},
"two": {
"one": 1,
"two": 2
}
}
Since your example does add the "two" after the map was already added to x (And I'm not sure but I don't believe java updates instances of something inside a map unless you directly call it)
No, because the entry
variable is just a reference to a mutable object. You are not actually putting the content of the entry
in the map
map but just a reference to the entry
map. If you modify the entry
map after putting it into map
, it will also change its content inside of map
.
I recommend to take a look at https://java-programming.mooc.fi/part-5/3-primitive-and-reference-variables if you are unfamilar with the concept of references.
I encounter a rather weird behaviour, where a Drilldown Pie displays "1.16.5" in the "1.12.x" section and "1.12.2" in "1.16.x"
The logic behind making this Pie doesn't suggest any failure on my end, so I can only assume bstats somehow mixing values up? Any help in solving this would be appreciated.
To the infos: I use the BungeeCord version of the Bstats library and have the following code used to apply a custom Drilldown Pie.
The ProtocolVersion is a enum with following content:
As you can see do both the
getMajor
andgetFriendlyName
method use the provided integer to determine what String to return.getMajor
is used for the parent Map while thegetFriendlyName
version is used for the child Map (The detailed view).The issue now is, like mentioned before, that
1.16.x
has1.12.2
and1.12.x
has1.16.5
as entries, which should never be the case by the above logic as each separate version integer is unique per version and would therefore not result in this.The only other case I could imagine is that the maps are somehow mixed up before the info is handled by Bstats to then send. Maybe a LinkedHashMap could be a solution? Other than that can I not think of any real idea what else could cause this and can only assume something on the BStats-end to cause this.
Here's a screenshot of what I mean: