Skip to content

Commit

Permalink
Add additional pack configuration options.
Browse files Browse the repository at this point in the history
This addresses the feature requests of #33
  • Loading branch information
Darkhax committed Mar 11, 2024
1 parent c2a5eac commit cc008cd
Show file tree
Hide file tree
Showing 6 changed files with 191 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ public class ConfigSchema {
@Expose
public PackConfig dataPacks = new PackConfig();

@Expose
public boolean appendSourceToPacks = true;

public static class PackConfig {

@Expose
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import net.minecraft.ChatFormatting;
import net.minecraft.network.chat.Component;
import net.minecraft.server.packs.FilePackResources;
import net.minecraft.server.packs.PackResources;
import net.minecraft.server.packs.PathPackResources;
import net.minecraft.server.packs.repository.Pack;
import net.minecraft.server.packs.repository.PackSource;
Expand All @@ -20,15 +19,16 @@

public class OpenLoaderRepositorySource implements RepositorySource {

private static final PackSource SOURCE = PackSource.create((name) -> Component.translatable("pack.nameAndSource", name, Component.translatable("pack.source.openloader")).withStyle(ChatFormatting.GREEN), true);
private final RepoType type;
private final List<File> directories;
private final ConfigSchema.PackConfig config;
private final boolean appendSourceToPacks;

public OpenLoaderRepositorySource(RepoType type, ConfigSchema.PackConfig config, Path configDir) {
public OpenLoaderRepositorySource(RepoType type, ConfigSchema.PackConfig config, Path configDir, boolean appendSources) {

this.type = type;
this.config = config;
this.appendSourceToPacks = appendSources;

this.directories = new ArrayList<>();
this.directories.add(configDir.resolve(type.getPath()).toFile());
Expand Down Expand Up @@ -74,15 +74,34 @@ public void loadPacks(Consumer<Pack> consumer) {
if (isArchivePack || isFolderPack) {

final String packName = this.type.getPath() + "/" + packCandidate.getName();
final Component displayName = Component.literal(packName);
final PackOptions options = PackOptions.readOptions(packCandidate);

final Pack pack = Pack.readMetaAndCreate(packName, displayName, true, createPackSupplier(packCandidate), this.type.getPackType(), Pack.Position.TOP, SOURCE);
if (options.enabled) {

if (pack != null) {
// return Component.translatable("pack.nameAndSource", name, Component.translatable("pack.source.openloader")).withStyle(ChatFormatting.GREEN);

consumer.accept(pack);
newPackCount++;
Constants.LOG.info("Loaded {} {} from {}.", typeName, this.type.getName(), packCandidate.getAbsolutePath());
final PackSource source = PackSource.create(rawDesc -> {
Component description = options.getDescription(packName, rawDesc);

if (options.addSourceToDescription && appendSourceToPacks) {
description = Component.translatable("pack.nameAndSource", description, Component.translatable("pack.source.openloader").withStyle(ChatFormatting.DARK_AQUA));

}
return description;
}, true);

final Pack pack = Pack.readMetaAndCreate(packName, options.getDisplayName(packName), options.required, createPackSupplier(packCandidate), this.type.getPackType(), options.getPosition(), source);

if (pack != null) {

consumer.accept(pack);
newPackCount++;
Constants.LOG.info("Loaded {} {} from {}.", typeName, this.type.getName(), packCandidate.getAbsolutePath());
}
}

else {
Constants.LOG.debug("Pack '{}' has been disabled by config file.", packName);
}
}

Expand All @@ -102,7 +121,7 @@ public void loadPacks(Consumer<Pack> consumer) {
}
}

private Pack.ResourcesSupplier createPackSupplier (File packFile) {
private Pack.ResourcesSupplier createPackSupplier(File packFile) {

return packFile.isDirectory() ? new PathPackResources.PathResourcesSupplier(packFile.toPath(), false) : new FilePackResources.FileResourcesSupplier(packFile, false);
}
Expand Down
150 changes: 150 additions & 0 deletions common/src/main/java/net/darkhax/openloader/packs/PackOptions.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
package net.darkhax.openloader.packs;

import com.google.gson.JsonElement;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
import com.mojang.serialization.JsonOps;
import net.darkhax.openloader.Constants;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.ComponentSerialization;
import net.minecraft.server.packs.repository.Pack;

import java.io.File;
import java.io.FileReader;
import java.io.IOException;

public class PackOptions {

@Expose
@SerializedName("enabled")
public boolean enabled = true;

@Expose
@SerializedName("required")
public boolean required = true;

@Expose
@SerializedName("position")
public String position = "top";

@Expose
@SerializedName("pack_name")
public JsonElement packName = null;

@Expose
@SerializedName("description")
public JsonElement description = null;

@Expose
@SerializedName("description_includes_source")
public boolean addSourceToDescription = true;

@Override
public String toString() {

return "PackOptions[enabled=" + enabled + ",required=" + required + ",position=" + position + "]";
}

public Pack.Position getPosition() {

if (position != null) {

if (position.equalsIgnoreCase("top")) {
return Pack.Position.TOP;
}

else if (position.equalsIgnoreCase("bottom")) {
return Pack.Position.BOTTOM;
}

else {
Constants.LOG.error("Position type '{}' is not valid. You must use 'top' or 'bottom'.", this.position);
}
}

return Pack.Position.TOP;
}

public Component getDisplayName(String defaultPackName) {

if (packName != null) {

if (packName.isJsonPrimitive() && packName.getAsJsonPrimitive().isString()) {

return Component.literal(packName.getAsString());
}

else if (packName.isJsonObject() || packName.isJsonArray()) {

try {

return ComponentSerialization.CODEC.decode(JsonOps.INSTANCE, this.packName).getOrThrow(false, error -> Constants.LOG.error("Invalid pack name for pack '{}': {}", defaultPackName, error)).getFirst();
}

catch (RuntimeException e) {

Constants.LOG.error(e);
}
}
}

return Component.literal(defaultPackName);
}

public Component getDescription(String pack, Component defaultDescription) {

if (description != null) {

if (description.isJsonPrimitive() && description.getAsJsonPrimitive().isString()) {

return Component.literal(description.getAsString());
}

else if (description.isJsonObject() || description.isJsonArray()) {

try {

return ComponentSerialization.CODEC.decode(JsonOps.INSTANCE, this.description).getOrThrow(false, error -> Constants.LOG.error("Invalid pack description for pack '{}': {}", pack, error)).getFirst();
}

catch (RuntimeException e) {

Constants.LOG.error(e);
}
}
}

return defaultDescription;
}

public static PackOptions readOptions(File packCandidate) {

final File optionsFile = new File(packCandidate.getParent(), packCandidate.getName() + ".packmeta");

if (optionsFile.exists()) {

if (optionsFile.isFile()) {

try (FileReader reader = new FileReader(optionsFile)) {

return Constants.GSON.fromJson(reader, PackOptions.class);
}

catch (IOException e) {

Constants.LOG.error("Failed to read pack options. The file is not formatted correctly! {}", optionsFile.getAbsolutePath());
Constants.LOG.catching(e);
}
}

else {

Constants.LOG.error("Pack options must be a file! {}", optionsFile.getAbsolutePath());
}
}

// defaults
Constants.LOG.debug("Using default pack options for {}", optionsFile.getAbsolutePath());
return new PackOptions();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@ private void onConstruction(PackType type, CallbackInfo callback) {

if (type == PackType.SERVER_DATA) {

this.newSource = new OpenLoaderRepositorySource(RepoType.DATA, OpenLoaderFabric.config.dataPacks, OpenLoaderFabric.configDir);
this.newSource = new OpenLoaderRepositorySource(RepoType.DATA, OpenLoaderFabric.config.dataPacks, OpenLoaderFabric.configDir, OpenLoaderFabric.config.appendSourceToPacks);
}

else if (type == PackType.CLIENT_RESOURCES) {

this.newSource = new OpenLoaderRepositorySource(RepoType.RESOURCES, OpenLoaderFabric.config.resourcePacks, OpenLoaderFabric.configDir);
this.newSource = new OpenLoaderRepositorySource(RepoType.RESOURCES, OpenLoaderFabric.config.resourcePacks, OpenLoaderFabric.configDir, OpenLoaderFabric.config.appendSourceToPacks);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@ private void injectPackRepositories(AddPackFindersEvent event) {

case CLIENT_RESOURCES -> {

event.addRepositorySource(new OpenLoaderRepositorySource(RepoType.RESOURCES, config.resourcePacks, configDir));
event.addRepositorySource(new OpenLoaderRepositorySource(RepoType.RESOURCES, config.resourcePacks, configDir, config.appendSourceToPacks));
}

case SERVER_DATA -> {

event.addRepositorySource(new OpenLoaderRepositorySource(RepoType.DATA, config.dataPacks,configDir));
event.addRepositorySource(new OpenLoaderRepositorySource(RepoType.DATA, config.dataPacks,configDir, config.appendSourceToPacks));
}

default -> Constants.LOG.warn("Encountered unknown pack type {}. Nothing will be loaded for this type.", event.getPackType().name());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import net.darkhax.openloader.config.ConfigSchema;
import net.darkhax.openloader.packs.OpenLoaderRepositorySource;
import net.darkhax.openloader.packs.RepoType;
import net.neoforged.bus.api.IEventBus;
import net.neoforged.fml.common.Mod;
import net.neoforged.fml.javafmlmod.FMLJavaModLoadingContext;
import net.neoforged.fml.loading.FMLPaths;
Expand All @@ -16,11 +17,11 @@ public class OpenLoaderNeoForge {
public static ConfigSchema config;
public static Path configDir;

public OpenLoaderNeoForge() {
public OpenLoaderNeoForge(IEventBus modBus) {

configDir = FMLPaths.CONFIGDIR.get().resolve("openloader");
config = ConfigSchema.load(configDir);
FMLJavaModLoadingContext.get().getModEventBus().addListener(this::injectPackRepositories);
modBus.addListener(this::injectPackRepositories);
}

private void injectPackRepositories(AddPackFindersEvent event) {
Expand All @@ -29,12 +30,12 @@ private void injectPackRepositories(AddPackFindersEvent event) {

case CLIENT_RESOURCES -> {

event.addRepositorySource(new OpenLoaderRepositorySource(RepoType.RESOURCES, config.resourcePacks, configDir));
event.addRepositorySource(new OpenLoaderRepositorySource(RepoType.RESOURCES, config.resourcePacks, configDir, config.appendSourceToPacks));
}

case SERVER_DATA -> {

event.addRepositorySource(new OpenLoaderRepositorySource(RepoType.DATA, config.dataPacks,configDir));
event.addRepositorySource(new OpenLoaderRepositorySource(RepoType.DATA, config.dataPacks,configDir, config.appendSourceToPacks));
}

default -> Constants.LOG.warn("Encountered unknown pack type {}. Nothing will be loaded for this type.", event.getPackType().name());
Expand Down

0 comments on commit cc008cd

Please sign in to comment.