Skip to content

Commit

Permalink
Merge pull request #2498 from BentoBoxWorld/2497_Blueprint_and_mobs
Browse files Browse the repository at this point in the history
2497 blueprint and mobs
  • Loading branch information
tastybento authored Sep 7, 2024
2 parents 86c9a8f + 80219f3 commit a716feb
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,12 @@
public class ProfessionTypeAdapter extends TypeAdapter<Profession> {

@Override
public void write(JsonWriter out, Profession profession) throws IOException {
out.value(profession.name());
public void write(JsonWriter out, Profession profession) throws IOException {
if (profession != null) {
out.value(profession.name());
return;
}
out.nullValue();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@
public class VillagerTypeAdapter extends TypeAdapter<Villager.Type> {

@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());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -253,17 +253,42 @@ public void addToPendingKick(World world)
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
* @see User#setMetaData(Map)
*/
@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;
}

Expand Down

0 comments on commit a716feb

Please sign in to comment.