Skip to content

Commit

Permalink
Add builtin pack helper for AddPackFindersEvent to make registering p…
Browse files Browse the repository at this point in the history
…acks easier (#928)
  • Loading branch information
TelepathicGrunt authored May 7, 2024
1 parent 86876fa commit 85c9d49
Show file tree
Hide file tree
Showing 10 changed files with 107 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,21 @@

package net.neoforged.neoforge.event;

import java.util.Optional;
import java.util.function.Consumer;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.packs.PackLocationInfo;
import net.minecraft.server.packs.PackSelectionConfig;
import net.minecraft.server.packs.PackType;
import net.minecraft.server.packs.PathPackResources;
import net.minecraft.server.packs.repository.BuiltInPackSource;
import net.minecraft.server.packs.repository.Pack;
import net.minecraft.server.packs.repository.PackRepository;
import net.minecraft.server.packs.repository.PackSource;
import net.minecraft.server.packs.repository.RepositorySource;
import net.neoforged.bus.api.Event;
import net.neoforged.fml.ModList;
import net.neoforged.fml.event.IModBusEvent;

/**
Expand Down Expand Up @@ -44,4 +53,28 @@ public void addRepositorySource(RepositorySource source) {
public PackType getPackType() {
return packType;
}

/**
* Helper method to register a pack found under the `resources/` folder.
*
* @param packLocation Location of the pack to load. Namespace should be the modid of the pack owner and path is the location under `resources/` folder
* @param packType Whether pack is a resourcepack or datapack
* @param packNameDisplay The text that shows for the pack on the pack selection screen
* @param packSource Controls whether the datapack is enabled or disabled by default. Resourcepacks are always disabled by default unless you set alwaysActive to true.
* @param alwaysActive Whether the pack is forced active always. If false, players have to manually activate the pack themselves
* @param packPosition Where the pack goes for determining pack applying order
*/
public void addPackFinders(ResourceLocation packLocation, PackType packType, Component packNameDisplay, PackSource packSource, boolean alwaysActive, Pack.Position packPosition) {
if (getPackType() == packType) {
var resourcePath = ModList.get().getModFileById(packLocation.getNamespace()).getFile().findResource(packLocation.getPath());

var pack = Pack.readMetaAndCreate(
new PackLocationInfo("mod/" + packLocation, packNameDisplay, packSource, Optional.empty()),
BuiltInPackSource.fromName((path) -> new PathPackResources(path, resourcePath)),
packType,
new PackSelectionConfig(alwaysActive, packPosition, false));

addRepositorySource((packConsumer) -> packConsumer.accept(pack));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,13 @@

package net.neoforged.neoforge.oldtest;

import java.util.Optional;
import net.minecraft.network.chat.Component;
import net.minecraft.server.packs.PackLocationInfo;
import net.minecraft.server.packs.PackSelectionConfig;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.packs.PackType;
import net.minecraft.server.packs.PathPackResources;
import net.minecraft.server.packs.repository.BuiltInPackSource;
import net.minecraft.server.packs.repository.Pack;
import net.minecraft.server.packs.repository.PackSource;
import net.neoforged.bus.api.IEventBus;
import net.neoforged.bus.api.SubscribeEvent;
import net.neoforged.fml.ModList;
import net.neoforged.fml.common.Mod;
import net.neoforged.neoforge.event.AddPackFindersEvent;

Expand All @@ -34,10 +29,36 @@ public AddPackFinderEventTest(IEventBus modEventBus) {

@SubscribeEvent
public void addPackFinders(AddPackFindersEvent event) {
if (event.getPackType() == PackType.CLIENT_RESOURCES) {
var resourcePath = ModList.get().getModFileById(MODID).getFile().findResource("test_nested_resource_pack");
var pack = Pack.readMetaAndCreate(new PackLocationInfo("builtin/add_pack_finders_test", Component.literal("display name"), PackSource.BUILT_IN, Optional.empty()), BuiltInPackSource.fromName((path) -> new PathPackResources(path, resourcePath)), PackType.CLIENT_RESOURCES, new PackSelectionConfig(true, Pack.Position.BOTTOM, false));
event.addRepositorySource((packConsumer) -> packConsumer.accept(pack));
}
event.addPackFinders(
new ResourceLocation(MODID, "test_disabled_data_pack"),
PackType.SERVER_DATA,
Component.literal("Disabled-By-Default DataPack Name"),
PackSource.FEATURE,
false,
Pack.Position.TOP);

event.addPackFinders(
new ResourceLocation(MODID, "test_enabled_data_pack"),
PackType.SERVER_DATA,
Component.literal("Enabled-By-Default DataPack Name"),
PackSource.BUILT_IN,
false,
Pack.Position.TOP);

event.addPackFinders(
new ResourceLocation(MODID, "test_disabled_resource_pack"),
PackType.CLIENT_RESOURCES,
Component.literal("Disabled-By-Default ResourcePack Name"),
PackSource.BUILT_IN,
false,
Pack.Position.TOP);

event.addPackFinders(
new ResourceLocation(MODID, "test_always_enabled_resource_pack"),
PackType.CLIENT_RESOURCES,
Component.literal("Forced-Enabled-Always ResourcePack Name"),
PackSource.BUILT_IN,
true,
Pack.Position.TOP);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"pack": {
"pack_format": 12,
"description": "Forced-Enabled-Always ResourcePack",
"forge:resource_pack_format": 12,
"forge:data_pack_format": 10
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"replace": false,
"values": [
"minecraft:end_stone"
]
}
8 changes: 8 additions & 0 deletions tests/src/main/resources/test_disabled_data_pack/pack.mcmeta
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"pack": {
"pack_format": 12,
"description": "Disabled-By-Default DataPack",
"forge:resource_pack_format": 12,
"forge:data_pack_format": 10
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"variants": {
"": {"model": "block/diamond_block"}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"pack": {
"pack_format": 12,
"description": "Disabled-By-Default ResourcePack",
"forge:resource_pack_format": 12,
"forge:data_pack_format": 10
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"replace": false,
"values": [
"minecraft:end_stone"
]
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"pack": {
"pack_format": 12,
"description": "Nested resource pack",
"description": "Enabled-By-Default DataPack",
"forge:resource_pack_format": 12,
"forge:data_pack_format": 10
}
Expand Down

0 comments on commit 85c9d49

Please sign in to comment.