add resource loading (/injection)

This commit is contained in:
moehreag 2024-06-05 20:12:16 +02:00
parent 9069cc271f
commit 2bf62501c9
12 changed files with 152 additions and 35 deletions

View file

@ -14,34 +14,4 @@ repositories {
mavenLocal() mavenLocal()
} }
ext.set("mcVer", libs.versions.minecraft)
ext.set("loaderVer", libs.versions.frogloader)
minecraft(project.libs.versions.minecraft).loader(project.libs.versions.frogloader) minecraft(project.libs.versions.minecraft).loader(project.libs.versions.frogloader)
subprojects {
}
@Suppress("UNCHECKED_CAST")
fun setUpLibrary(project: Project) {
with(project) {
println("Configuring: ${project.name}")
apply(plugin = "dev.frogmc.phytotelma")
group = "dev.frogmc"
version = "0.0.1-SNAPSHOT"
repositories {
mavenLocal()
}
minecraft(rootProject.ext.get("mcVer") as Provider<String>).loader(rootProject.ext.get("loaderVer") as Provider<String>)
java {
sourceCompatibility = JavaVersion.VERSION_21
targetCompatibility = JavaVersion.VERSION_21
}
}
}

View file

@ -5,7 +5,6 @@ import dev.frogmc.frogloader.api.FrogLoader;
public class ExtensionLauncher { public class ExtensionLauncher {
public static void invokeClient() { public static void invokeClient() {
System.out.println("Starting client extension");
FrogLoader.getInstance().getMods().forEach(mod -> { FrogLoader.getInstance().getMods().forEach(mod -> {
mod.extensions().runIfPresent("client", ClientExtension.class, ext -> ext.onClientInit(mod)); mod.extensions().runIfPresent("client", ClientExtension.class, ext -> ext.onClientInit(mod));
mod.extensions().runIfPresent("init", MainExtension.class, ext -> ext.onInit(mod)); mod.extensions().runIfPresent("init", MainExtension.class, ext -> ext.onInit(mod));
@ -13,7 +12,6 @@ public class ExtensionLauncher {
} }
public static void invokeServer() { public static void invokeServer() {
System.out.println("Starting server extension");
FrogLoader.getInstance().getMods().forEach(mod -> { FrogLoader.getInstance().getMods().forEach(mod -> {
mod.extensions().runIfPresent("server", ServerExtension.class, ext -> ext.onServerInit(mod)); mod.extensions().runIfPresent("server", ServerExtension.class, ext -> ext.onServerInit(mod));
mod.extensions().runIfPresent("init", MainExtension.class, ext -> ext.onInit(mod)); mod.extensions().runIfPresent("init", MainExtension.class, ext -> ext.onInit(mod));

View file

@ -0,0 +1,7 @@
package dev.frogmc.froglib.resources.api;
import net.minecraft.server.packs.resources.ResourceManager;
public interface ResourceLoaderEvent {
void onReload(ResourceManager manager);
}

View file

@ -0,0 +1,7 @@
package dev.frogmc.froglib.resources.api;
import dev.frogmc.froglib.events.api.Event;
public class ResourceLoaderEvents {
public static final Event<ResourceLoaderEvent> RESOURCE_RELOAD = Event.of(ResourceLoaderEvent.class);
}

View file

@ -0,0 +1,20 @@
package dev.frogmc.froglib.resources.impl;
import net.minecraft.ChatFormatting;
import net.minecraft.network.chat.Component;
import net.minecraft.server.packs.repository.PackSource;
import org.jetbrains.annotations.NotNull;
public class ModPackSource implements PackSource {
@Override
public @NotNull Component decorate(Component component) {
component.getStyle().applyFormat(ChatFormatting.DARK_GREEN);
return component;
}
@Override
public boolean shouldAddAutomatically() {
return true;
}
}

View file

@ -0,0 +1,14 @@
package dev.frogmc.froglib.resources.impl;
import dev.frogmc.froglib.entrypoints.ClientExtension;
import dev.frogmc.froglib.events.api.Events;
import dev.frogmc.frogloader.api.mod.ModProperties;
import net.minecraft.server.packs.resources.ReloadableResourceManager;
public class ResourceLoaderClient implements ClientExtension {
@Override
public void onClientInit(ModProperties mod) {
Events.CLIENT_STARTUP.register((m, c) ->
((ReloadableResourceManager) m.getResourceManager()).registerReloadListener(new ResourceLoaderReloadListener()));
}
}

View file

@ -0,0 +1,28 @@
package dev.frogmc.froglib.resources.impl;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import dev.frogmc.frogloader.api.FrogLoader;
import net.minecraft.network.chat.Component;
import net.minecraft.server.packs.PackLocationInfo;
import net.minecraft.server.packs.PackSelectionConfig;
import net.minecraft.server.packs.PathPackResources;
import net.minecraft.server.packs.repository.Pack;
import net.minecraft.server.packs.repository.PackCompatibility;
import net.minecraft.world.flag.FeatureFlagSet;
public class ResourceLoaderImpl {
public static void addModPacks(List<Pack> packs) {
FrogLoader.getInstance().getMods().forEach(mod ->
mod.paths().stream().filter(Objects::nonNull)
.map(PathPackResources.PathResourcesSupplier::new).forEach(sup ->
packs.add(new Pack(new PackLocationInfo(mod.id(), Component.literal(mod.name()),
new ModPackSource(), Optional.empty()), sup,
new Pack.Metadata(Component.empty(), PackCompatibility.COMPATIBLE, FeatureFlagSet.of(), Collections.emptyList()),
new PackSelectionConfig(true, Pack.Position.TOP, true)))));
}
}

View file

@ -0,0 +1,13 @@
package dev.frogmc.froglib.resources.impl;
import dev.frogmc.froglib.resources.api.ResourceLoaderEvents;
import net.minecraft.server.packs.resources.ResourceManager;
import net.minecraft.server.packs.resources.ResourceManagerReloadListener;
import org.jetbrains.annotations.NotNull;
public class ResourceLoaderReloadListener implements ResourceManagerReloadListener {
@Override
public void onResourceManagerReload(@NotNull ResourceManager resourceManager) {
ResourceLoaderEvents.RESOURCE_RELOAD.invoker().onReload(resourceManager);
}
}

View file

@ -0,0 +1,14 @@
package dev.frogmc.froglib.resources.impl;
import dev.frogmc.froglib.entrypoints.ServerExtension;
import dev.frogmc.froglib.events.api.Events;
import dev.frogmc.frogloader.api.mod.ModProperties;
import net.minecraft.server.packs.resources.ReloadableResourceManager;
public class ResourceLoaderServer implements ServerExtension {
@Override
public void onServerInit(ModProperties mod) {
Events.SERVER_STARTUP.register(server ->
((ReloadableResourceManager)server.getResourceManager()).registerReloadListener(new ResourceLoaderReloadListener()));
}
}

View file

@ -0,0 +1,22 @@
package dev.frogmc.froglib.resources.impl.mixin;
import java.util.Collection;
import java.util.List;
import com.llamalad7.mixinextras.sugar.Local;
import dev.frogmc.froglib.resources.impl.ResourceLoaderImpl;
import net.minecraft.server.packs.repository.Pack;
import net.minecraft.server.packs.repository.PackRepository;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
@Mixin(PackRepository.class)
public class PackRepositoryMixin {
@Inject(method = "rebuildSelected", at = @At(value = "INVOKE", target = "Lcom/google/common/collect/ImmutableList;copyOf(Ljava/util/Collection;)Lcom/google/common/collect/ImmutableList;"))
private void injectModResources(Collection<String> ids, CallbackInfoReturnable<List<Pack>> cir, @Local List<Pack> packs){
ResourceLoaderImpl.addModPacks(packs);
}
}

View file

@ -12,12 +12,19 @@ credits = [
[frog.dependencies] [frog.dependencies]
depends = [ depends = [
{ id = "minecraft", versions = "~1.20.6", name = "Minecraft", link = "https://frogmc.dev" } { id = "minecraft", versions = "~1.20.6", name = "Minecraft" }
] ]
[frog.extensions] [frog.extensions]
client = [
"dev.frogmc.froglib.resources.impl.ResourceLoaderClient"
]
server = [
"dev.frogmc.froglib.resources.impl.ResourceLoaderServer"
]
mixin_config = [ mixin_config = [
"froglib.entrypoints.mixins.json", "froglib.entrypoints.mixins.json",
"froglib.events.mixins.json" "froglib.events.mixins.json",
"froglib.resources.mixins.json"
] ]

View file

@ -0,0 +1,17 @@
{
"required": true,
"minVersion": "0.8",
"package": "dev.frogmc.froglib.resources.impl.mixin",
"compatibilityLevel": "JAVA_21",
"mixins": [
"PackRepositoryMixin"
],
"server": [
],
"client": [
],
"injectors": {
"defaultRequire": 1
}
}