Skip to content

Commit

Permalink
🌟 资源包注册API添加注册类型
Browse files Browse the repository at this point in the history
  • Loading branch information
Gu-ZT committed Aug 11, 2024
1 parent addaee1 commit 6d38c54
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,19 @@

public class ResourcePacksHelper {
@ExpectPlatform
public static void registerBuiltinResourcePack(@NotNull ResourceLocation pack) {
public static void registerBuiltinResourcePack(@NotNull ResourceLocation pack, PackType type) {
throw new AssertionError();
}

public enum PackType {
CLIENT, SERVER, BOTH;

public boolean isClient() {
return this == CLIENT || this == BOTH;
}

public boolean isServer() {
return this == SERVER || this == BOTH;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package dev.anvilcraft.lib.registrar.fabric;

import dev.anvilcraft.lib.registrar.ResourcePacksHelper;
import net.fabricmc.fabric.api.resource.ResourceManagerHelper;
import net.fabricmc.fabric.api.resource.ResourcePackActivationType;
import net.fabricmc.loader.api.FabricLoader;
Expand All @@ -9,7 +10,7 @@
import org.jetbrains.annotations.NotNull;

public class ResourcePacksHelperImpl {
public static void registerBuiltinResourcePack(@NotNull ResourceLocation pack) {
public static void registerBuiltinResourcePack(@NotNull ResourceLocation pack, ResourcePacksHelper.PackType type) {
String modid = pack.getNamespace();
ModContainer modContainer = FabricLoader.getInstance()
.getModContainer(modid)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package dev.anvilcraft.lib.registrar.forge;

import dev.anvilcraft.lib.registrar.ResourcePacksHelper;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.packs.PackType;
Expand All @@ -16,17 +17,17 @@
import java.nio.file.Path;

public class ResourcePacksHelperImpl {
public static void registerBuiltinResourcePack(@NotNull ResourceLocation pack) {
public static void registerBuiltinResourcePack(@NotNull ResourceLocation pack, ResourcePacksHelper.PackType type) {
String modid = pack.getNamespace();
IModFileInfo modFileInfo = ModList.get().getModFileById(modid);
if (modFileInfo == null) {
throw new IllegalStateException("%s's ModContainer couldn't be found!".formatted(modid));
}
IModFile modFile = modFileInfo.getFile();
MinecraftForge.EVENT_BUS.addListener((AddPackFindersEvent event) -> {
if (event.getPackType() == PackType.SERVER_DATA) {
if (event.getPackType() == PackType.SERVER_DATA && type.isServer()) {
registerPack(event, modFile, pack, PackType.SERVER_DATA);
} else {
} else if (type.isClient()) {
registerPack(event, modFile, pack, PackType.CLIENT_RESOURCES);
}
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package dev.anvilcraft.lib.registrar.forge;

import dev.anvilcraft.lib.registrar.ResourcePacksHelper;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.packs.PackType;
Expand All @@ -16,17 +17,17 @@
import java.nio.file.Path;

public class ResourcePacksHelperImpl {
public static void registerBuiltinResourcePack(@NotNull ResourceLocation pack) {
public static void registerBuiltinResourcePack(@NotNull ResourceLocation pack, ResourcePacksHelper.PackType type) {
String modid = pack.getNamespace();
IModFileInfo modFileInfo = ModList.get().getModFileById(modid);
if (modFileInfo == null) {
throw new IllegalStateException("%s's ModContainer couldn't be found!".formatted(modid));
}
IModFile modFile = modFileInfo.getFile();
MinecraftForge.EVENT_BUS.addListener((AddPackFindersEvent event) -> {
if (event.getPackType() == PackType.SERVER_DATA) {
if (event.getPackType() == PackType.SERVER_DATA && type.isServer()) {
registerPack(event, modFile, pack, PackType.SERVER_DATA);
} else {
} else if (type.isClient()) {
registerPack(event, modFile, pack, PackType.CLIENT_RESOURCES);
}
});
Expand Down

0 comments on commit 6d38c54

Please sign in to comment.