Skip to content

Commit

Permalink
Add deferral for blocks not yet registered
Browse files Browse the repository at this point in the history
  • Loading branch information
falkreon committed Jan 4, 2024
1 parent 37de87e commit 0377c50
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 6 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ org.gradle.jvmargs=-Xmx1G
loader_version = 0.15.3

# Mod Properties
mod_version = 1.3.2
mod_version = 1.3.3
maven_group = blue.endless
archives_base_name = scarves

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package blue.endless.scarves.integration;

import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

import blue.endless.jankson.Jankson;
Expand All @@ -10,30 +12,53 @@
import blue.endless.scarves.api.FabricSquare;
import blue.endless.scarves.api.FabricSquareRegistry;
import gay.debuggy.staticdata.api.StaticData;
import net.fabricmc.fabric.api.event.registry.RegistryEntryAddedCallback;
import net.minecraft.block.MapColor;
import net.minecraft.item.BlockItem;
import net.minecraft.item.Item;
import net.minecraft.item.Items;
import net.minecraft.registry.Registries;
import net.minecraft.util.Identifier;

public class StaticDataIntegration {
private static Map<Identifier, JsonElement> deferrals = new HashMap<>();

public static void init() {
Jankson jankson = Jankson.builder().build();
for(var dataItem : StaticData.getDataInDirectory(new Identifier("scarves:fabric_squares"), true)) {
try {
JsonElement elem = jankson.loadElement(dataItem.getAsStream());
if (elem instanceof JsonObject obj) {
obj.forEach((itemId, squareSpec) -> {
Item item = Registries.ITEM.get(new Identifier(itemId));
getFabricSquare(squareSpec, getColorHint(item), getDefaultEmissive(item)).ifPresent(square -> {
FabricSquareRegistry.register(item, square);
});
obj.forEach((itemIdString, squareSpec) -> {
Identifier itemId = new Identifier(itemIdString);
Item item = Registries.ITEM.get(itemId);
if (item == null || item == Items.AIR) {
//defer
deferrals.put(itemId, squareSpec);
} else {
getFabricSquare(squareSpec, getColorHint(item), getDefaultEmissive(item)).ifPresent(square -> {
FabricSquareRegistry.register(item, square);
});
}
});
}
} catch (Throwable t) {
ScarvesMod.LOGGER.error("Could not load static data \"" + dataItem.getResourceId() + "\"", t);
}
}

RegistryEntryAddedCallback.event(Registries.ITEM).register((rawId, id, item) -> {
JsonElement elem = deferrals.remove(id);
if (elem == null) return;

try {
getFabricSquare(elem, getColorHint(item), getDefaultEmissive(item)).ifPresent(square -> {
FabricSquareRegistry.register(item, square);
});
} catch (Throwable t) {
ScarvesMod.LOGGER.error("Could not load static data for deferred item \"" + id + "\"", t);
}
});
}

public static Optional<FabricSquare> getFabricSquare(JsonElement elem, int defaultColor, boolean defaultEmissive) {
Expand Down

0 comments on commit 0377c50

Please sign in to comment.