Add protection around meta data being written that is immutable

This commit is contained in:
tastybento 2024-09-07 11:06:51 -07:00
parent dbf4bb75b1
commit 80219f38cb

View File

@ -253,10 +253,32 @@ public class Players implements DataObject, MetaDataAble {
public Optional<Map<String, MetaDataValue>> getMetaData() {
if (metaData == null) {
metaData = new HashMap<>();
} else if (isImmutable(metaData)) {
metaData = new HashMap<>(metaData); // Convert immutable map to mutable
}
return Optional.of(metaData);
}
private boolean isImmutable(Map<String, MetaDataValue> map) {
try {
String testKey = "testKey";
MetaDataValue testValue = new MetaDataValue("test");
// If the map already contains keys, use one of them
if (!map.isEmpty()) {
String existingKey = map.keySet().iterator().next();
map.put(existingKey, map.get(existingKey)); // Attempt to replace value
} else {
// Use a unique key-value pair
map.put(testKey, testValue);
map.remove(testKey);
}
return false; // No exception means the map is mutable
} catch (UnsupportedOperationException e) {
return true; // Exception means the map is immutable
}
}
/**
* @param metaData the metaData to set
* @since 1.15.4
@ -264,6 +286,9 @@ public class Players implements DataObject, MetaDataAble {
*/
@Override
public void setMetaData(Map<String, MetaDataValue> metaData) {
if (isImmutable(metaData)) {
throw new IllegalArgumentException("Provided map is immutable and cannot be set.");
}
this.metaData = metaData;
}