|
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;
|
26 | 30 | import net.minecraft.resources.io.Resource;
|
27 | 31 | import net.minecraft.resources.io.ResourceManager;
|
| 32 | +import net.minecraft.util.profiling.Profiler; |
28 | 33 | import net.minecraft.world.item.ItemStack;
|
29 | 34 | import net.minecraft.world.level.block.Block;
|
30 | 35 | import org.slf4j.Logger;
|
|
34 | 39 | import java.io.InputStreamReader;
|
35 | 40 | import java.util.ArrayList;
|
36 | 41 | import java.util.List;
|
| 42 | +import java.util.concurrent.CompletableFuture; |
| 43 | +import java.util.concurrent.Executor; |
37 | 44 |
|
38 | 45 | /**
|
39 | 46 | * Represents an item light sources manager.
|
40 | 47 | *
|
41 | 48 | * @author LambdAurora
|
42 |
| - * @version 3.0.0 |
| 49 | + * @version 3.1.1 |
43 | 50 | * @since 1.3.0
|
44 | 51 | */
|
45 |
| -public final class ItemLightSources implements ItemLightSourceManager { |
| 52 | +public final class ItemLightSources implements ItemLightSourceManager, IdentifiableResourceReloadListener { |
46 | 53 | private static final Logger LOGGER = LoggerFactory.getLogger("LambDynamicLights|ItemLightSources");
|
| 54 | + private static final Identifier RESOURCE_RELOADER_ID = Identifier.of(LambDynLightsConstants.NAMESPACE, "dynamiclights_resources"); |
47 | 55 | private static final String SILENCE_ERROR_KEY = "silence_error";
|
48 | 56 | private static final boolean FORCE_LOG_ERRORS = TriState.fromProperty("lambdynamiclights.resource.force_log_errors")
|
49 | 57 | .toBooleanOrElse(FabricLoader.getInstance().isDevelopmentEnvironment());
|
50 | 58 |
|
51 | 59 | private final Event<Identifier, OnRegister> onRegisterEvent = LambDynLights.EVENT_MANAGER.create(OnRegister.class);
|
| 60 | + private final Minecraft client = Minecraft.getInstance(); |
52 | 61 |
|
53 | 62 | private final List<LoadedItemLightSource> loadedLightSources = new ArrayList<>();
|
54 | 63 | private final List<ItemLightSource> lightSources = new ArrayList<>();
|
55 | 64 |
|
| 65 | + @Override |
| 66 | + public Identifier getFabricId() { |
| 67 | + return RESOURCE_RELOADER_ID; |
| 68 | + } |
| 69 | + |
| 70 | + @Override |
| 71 | + public CompletableFuture<Void> reload(Synchronizer synchronizer, ResourceManager resourceManager, Profiler prepareProfiler, Profiler applyProfiler, Executor prepareExecutor, Executor applyExecutor) { |
| 72 | + return CompletableFuture.supplyAsync(() -> { |
| 73 | + this.load(resourceManager); |
| 74 | + return Unit.INSTANCE; |
| 75 | + }, prepareExecutor) |
| 76 | + .thenCompose(synchronizer::whenPrepared) |
| 77 | + .thenAcceptAsync((reloadState) -> { |
| 78 | + if (this.client.level != null) { |
| 79 | + this.apply(this.client.level.registryAccess()); |
| 80 | + } |
| 81 | + }, applyExecutor); |
| 82 | + } |
| 83 | + |
56 | 84 | /**
|
57 | 85 | * Loads the item light source data from resource pack.
|
58 | 86 | *
|
|
0 commit comments