Merge pull request #2498 from BentoBoxWorld/2497_Blueprint_and_mobs

2497 blueprint and mobs
This commit is contained in:
tastybento 2024-09-07 11:15:11 -07:00 committed by GitHub
commit a716feb9a6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 36 additions and 3 deletions

View File

@ -13,7 +13,11 @@ public class ProfessionTypeAdapter extends TypeAdapter<Profession> {
@Override @Override
public void write(JsonWriter out, Profession profession) throws IOException { public void write(JsonWriter out, Profession profession) throws IOException {
if (profession != null) {
out.value(profession.name()); out.value(profession.name());
return;
}
out.nullValue();
} }
@Override @Override

View File

@ -13,6 +13,10 @@ public class VillagerTypeAdapter extends TypeAdapter<Villager.Type> {
@Override @Override
public void write(JsonWriter out, Villager.Type type) throws IOException { public void write(JsonWriter out, Villager.Type type) throws IOException {
if (type == null) {
out.nullValue();
return;
}
out.value(type.name()); out.value(type.name());
} }

View File

@ -253,10 +253,32 @@ public class Players implements DataObject, MetaDataAble {
public Optional<Map<String, MetaDataValue>> getMetaData() { public Optional<Map<String, MetaDataValue>> getMetaData() {
if (metaData == null) { if (metaData == null) {
metaData = new HashMap<>(); metaData = new HashMap<>();
} else if (isImmutable(metaData)) {
metaData = new HashMap<>(metaData); // Convert immutable map to mutable
} }
return Optional.of(metaData); 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 * @param metaData the metaData to set
* @since 1.15.4 * @since 1.15.4
@ -264,6 +286,9 @@ public class Players implements DataObject, MetaDataAble {
*/ */
@Override @Override
public void setMetaData(Map<String, MetaDataValue> metaData) { public void setMetaData(Map<String, MetaDataValue> metaData) {
if (isImmutable(metaData)) {
throw new IllegalArgumentException("Provided map is immutable and cannot be set.");
}
this.metaData = metaData; this.metaData = metaData;
} }