ResourceManagerHelper registerReloadListener problem #2117
-
|
I use fabric api package net.fabricmc.example;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import net.fabricmc.api.ModInitializer;
import net.fabricmc.example.event.listener.MagicaListeners;
import net.fabricmc.example.item.MagicaItems;
import net.fabricmc.example.util.ModelsJson;
import net.fabricmc.fabric.api.client.model.ModelLoadingRegistry;
import net.fabricmc.fabric.api.resource.ResourceManagerHelper;
import net.fabricmc.fabric.api.resource.SimpleSynchronousResourceReloadListener;
import net.minecraft.resource.Resource;
import net.minecraft.resource.ResourceManager;
import net.minecraft.resource.ResourceType;
import net.minecraft.util.Identifier;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.nio.charset.StandardCharsets;
import java.util.List;
public class Magica implements ModInitializer {
// This logger is used to write text to the console and the log file.
// It is considered best practice to use your mod id as the logger's name.
// That way, it's clear which mod wrote info, warnings, and errors.
public static final Logger LOGGER = LoggerFactory.getLogger("modid");
public static final String MODID = "modid";
public final static Gson GSON = new GsonBuilder().setPrettyPrinting().create();
public static final String EXTRA_MODEL_SETTINGS_PATH = "extra/models.json";
@Override
public void onInitialize() {
// This code runs as soon as Minecraft is in a mod-load-ready state.
// However, some things (like resources) may still be uninitialized.
// Proceed with mild caution.
LOGGER.info("Hello Fabric world!");
MagicaItems.load();
MagicaListeners.load();
ResourceManagerHelper.get(ResourceType.CLIENT_RESOURCES).registerReloadListener(
new SimpleSynchronousResourceReloadListener() {
@Override
public Identifier getFabricId() {
return new Identifier("modid","load_extra_models");
}
@Override
public void reload(ResourceManager manager) {
for (String namespace : manager.getAllNamespaces()){
List<Resource> resources = null;
try {
resources = manager.getAllResources(new Identifier(namespace,EXTRA_MODEL_SETTINGS_PATH));
} catch (IOException e) {
// don't need to throw, it's normal.
}
if (resources == null){
continue;
}
for (Resource resource : resources){
Reader reader = new InputStreamReader(resource.getInputStream(), StandardCharsets.UTF_8);
ModelsJson modelsJson = GSON.fromJson(reader, ModelsJson.class);
for ( String model : modelsJson.models){
ModelLoadingRegistry.INSTANCE.registerModelProvider(
((manager1, out) -> {
out.accept(new Identifier(model));
Magica.LOGGER.info("load extra model : " + model);
})
);
}
}
}
}
}
);
}
}
|
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 3 replies
-
Beta Was this translation helpful? Give feedback.
-
|
I AM A FOOL! |
Beta Was this translation helpful? Give feedback.


I AM A FOOL!
This is an existing
ResourceManagerin ExtraModelProvider's parameters so why I wanted to use a ResourceReloadListener to get my config file!