|
15 | 15 | import com.mojang.serialization.DynamicOps;
|
16 | 16 | import com.mojang.serialization.JsonOps;
|
17 | 17 | import dev.lambdaurora.lambdynlights.LambDynLights;
|
| 18 | +import dev.lambdaurora.lambdynlights.LambDynLightsConstants; |
18 | 19 | import dev.lambdaurora.lambdynlights.api.item.ItemLightSource;
|
19 | 20 | import dev.lambdaurora.lambdynlights.api.item.ItemLightSourceManager;
|
20 | 21 | import dev.yumi.commons.TriState;
|
| 22 | +import dev.yumi.commons.Unit; |
21 | 23 | import dev.yumi.commons.event.Event;
|
| 24 | +import net.fabricmc.fabric.api.resource.IdentifiableResourceReloadListener; |
22 | 25 | import net.fabricmc.loader.api.FabricLoader;
|
| 26 | +import net.minecraft.client.Minecraft; |
23 | 27 | import net.minecraft.core.RegistryAccess;
|
24 | 28 | import net.minecraft.resources.Identifier;
|
25 | 29 | import net.minecraft.resources.RegistryOps;
|
|
34 | 38 | import java.io.InputStreamReader;
|
35 | 39 | import java.util.ArrayList;
|
36 | 40 | import java.util.List;
|
| 41 | +import java.util.concurrent.CompletableFuture; |
| 42 | +import java.util.concurrent.Executor; |
37 | 43 |
|
38 | 44 | /**
|
39 | 45 | * Represents an item light sources manager.
|
40 | 46 | *
|
41 | 47 | * @author LambdAurora
|
42 |
| - * @version 3.0.0 |
| 48 | + * @version 3.2.1 |
43 | 49 | * @since 1.3.0
|
44 | 50 | */
|
45 |
| -public final class ItemLightSources implements ItemLightSourceManager { |
| 51 | +public final class ItemLightSources implements ItemLightSourceManager, IdentifiableResourceReloadListener { |
46 | 52 | private static final Logger LOGGER = LoggerFactory.getLogger("LambDynamicLights|ItemLightSources");
|
| 53 | + private static final Identifier RESOURCE_RELOADER_ID = Identifier.of(LambDynLightsConstants.NAMESPACE, "dynamiclights_resources"); |
47 | 54 | private static final String SILENCE_ERROR_KEY = "silence_error";
|
48 | 55 | private static final boolean FORCE_LOG_ERRORS = TriState.fromProperty("lambdynamiclights.resource.force_log_errors")
|
49 | 56 | .toBooleanOrElse(FabricLoader.getInstance().isDevelopmentEnvironment());
|
50 | 57 |
|
51 | 58 | private final Event<Identifier, OnRegister> onRegisterEvent = LambDynLights.EVENT_MANAGER.create(OnRegister.class);
|
| 59 | + private final Minecraft client = Minecraft.getInstance(); |
52 | 60 |
|
53 | 61 | private final List<LoadedItemLightSource> loadedLightSources = new ArrayList<>();
|
54 | 62 | private final List<ItemLightSource> lightSources = new ArrayList<>();
|
55 | 63 |
|
| 64 | + @Override |
| 65 | + public Identifier getFabricId() { |
| 66 | + return RESOURCE_RELOADER_ID; |
| 67 | + } |
| 68 | + |
| 69 | + @Override |
| 70 | + public CompletableFuture<Void> reload(Synchronizer synchronizer, ResourceManager resourceManager, Executor prepareExecutor, Executor applyExecutor) { |
| 71 | + return CompletableFuture.supplyAsync(() -> { |
| 72 | + this.load(resourceManager); |
| 73 | + return Unit.INSTANCE; |
| 74 | + }, prepareExecutor) |
| 75 | + .thenCompose(synchronizer::whenPrepared) |
| 76 | + .thenAcceptAsync((reloadState) -> { |
| 77 | + if (this.client.level != null) { |
| 78 | + this.apply(this.client.level.registryAccess()); |
| 79 | + } |
| 80 | + }, applyExecutor); |
| 81 | + } |
| 82 | + |
56 | 83 | /**
|
57 | 84 | * Loads the item light source data from resource pack.
|
58 | 85 | *
|
|
0 commit comments