Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
To make this work, added a new sub-interface of Loadable: StringLoadable. Represents any loadable that can be written directly into a string or read from a string. MapLoadables require a string loadable as a key, but accept any loadable as a value. Intentionally did not implement RecordLoadable for them as the map keys will conflict with other objects Strings, resource locations, booleans, enums, colors, and tag keys all implement StringLoadable and work as keys Registries and named component registry both implement StringLoadable via the helper ResourceLocationLoadable IntLoadable does not implement StringLoadable, but has a new asString() method to create a loadable for strings as int, with a radix argument. Since StringLoadable used to be a record for different max lengths, that logic was moved to a package private class, can be built using a static helper on StringLoadable
- Loading branch information
1 parent
b8d0147
commit 24eb72c
Showing
13 changed files
with
400 additions
and
125 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
src/main/java/slimeknights/mantle/data/loadable/mapping/MapLoadable.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package slimeknights.mantle.data.loadable.mapping; | ||
|
||
import com.google.common.collect.ImmutableMap; | ||
import com.google.gson.JsonElement; | ||
import com.google.gson.JsonObject; | ||
import com.google.gson.JsonSyntaxException; | ||
import net.minecraft.network.FriendlyByteBuf; | ||
import net.minecraft.util.GsonHelper; | ||
import slimeknights.mantle.data.loadable.Loadable; | ||
import slimeknights.mantle.data.loadable.primitive.StringLoadable; | ||
|
||
import java.util.Map; | ||
import java.util.Map.Entry; | ||
|
||
/** | ||
* Loadable for a map type. | ||
* @param keyLoadable Loadable for the map keys, parsed from strings | ||
* @param valueLoadable Loadable for map values, parsed from elements | ||
* @param <K> Key type | ||
* @param <V> Value type | ||
*/ | ||
public record MapLoadable<K,V>(StringLoadable<K> keyLoadable, Loadable<V> valueLoadable, int minSize) implements Loadable<Map<K,V>> { | ||
@Override | ||
public Map<K,V> convert(JsonElement element, String key) { | ||
JsonObject json = GsonHelper.convertToJsonObject(element, key); | ||
if (json.size() < minSize) { | ||
throw new JsonSyntaxException(key + " must have at least " + minSize + " elements"); | ||
} | ||
ImmutableMap.Builder<K,V> builder = ImmutableMap.builder(); | ||
String mapKey = key + "'s key"; | ||
for (Entry<String, JsonElement> entry : json.entrySet()) { | ||
String entryKey = entry.getKey(); | ||
builder.put( | ||
keyLoadable.parseString(entryKey, mapKey), | ||
valueLoadable.convert(entry.getValue(), entryKey)); | ||
} | ||
return builder.build(); | ||
} | ||
|
||
@Override | ||
public JsonElement serialize(Map<K,V> map) { | ||
if (map.size() < minSize) { | ||
throw new RuntimeException("Collection must have at least " + minSize + " elements"); | ||
} | ||
JsonObject json = new JsonObject(); | ||
for (Entry<K,V> entry : map.entrySet()) { | ||
json.add( | ||
keyLoadable.getString(entry.getKey()), | ||
valueLoadable.serialize(entry.getValue())); | ||
} | ||
return json; | ||
} | ||
|
||
@Override | ||
public Map<K,V> decode(FriendlyByteBuf buffer) { | ||
int size = buffer.readVarInt(); | ||
ImmutableMap.Builder<K,V> builder = ImmutableMap.builder(); | ||
for (int i = 0; i < size; i++) { | ||
builder.put( | ||
keyLoadable.decode(buffer), | ||
valueLoadable.decode(buffer)); | ||
} | ||
return builder.build(); | ||
} | ||
|
||
@Override | ||
public void encode(FriendlyByteBuf buffer, Map<K,V> map) { | ||
buffer.writeVarInt(map.size()); | ||
for (Entry<K,V> entry : map.entrySet()) { | ||
keyLoadable.encode(buffer, entry.getKey()); | ||
valueLoadable.encode(buffer, entry.getValue()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.