mirror of
https://github.com/BentoBoxWorld/BentoBox.git
synced 2024-11-21 18:25:12 +01:00
Add protection around meta data being written that is immutable
This commit is contained in:
parent
dbf4bb75b1
commit
80219f38cb
@ -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;
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user