Skip to content

Commit

Permalink
Add the ability to prevent a pack from being reordered by users.
Browse files Browse the repository at this point in the history
This addresses #18
  • Loading branch information
Darkhax committed Mar 11, 2024
1 parent cc008cd commit fc2e725
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@
import net.darkhax.openloader.Constants;
import net.darkhax.openloader.config.ConfigSchema;
import net.minecraft.ChatFormatting;
import net.minecraft.SharedConstants;
import net.minecraft.network.chat.Component;
import net.minecraft.server.packs.FilePackResources;
import net.minecraft.server.packs.PackType;
import net.minecraft.server.packs.PathPackResources;
import net.minecraft.server.packs.repository.Pack;
import net.minecraft.server.packs.repository.PackSource;
import net.minecraft.server.packs.repository.RepositorySource;

import javax.annotation.Nullable;
import java.io.File;
import java.nio.file.Path;
import java.util.ArrayList;
Expand Down Expand Up @@ -78,19 +81,21 @@ public void loadPacks(Consumer<Pack> consumer) {

if (options.enabled) {

// return Component.translatable("pack.nameAndSource", name, Component.translatable("pack.source.openloader")).withStyle(ChatFormatting.GREEN);
if (options.fixedPosition && !options.required) {

Constants.LOG.error("Pack '{}' has a fixed position but is not required. This is not allowed! The pack can not be loaded.", packName);
continue;
}

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);
final Pack pack = readMetaAndCreate(packName, options.getDisplayName(packName), options.required, createPackSupplier(packCandidate), this.type.getPackType(), options.getPosition(), source, options.fixedPosition);

if (pack != null) {

Expand Down Expand Up @@ -154,4 +159,11 @@ private static boolean endsWithIgnoreCase(String str, String suffix) {
final int suffixLength = suffix.length();
return str.regionMatches(true, str.length() - suffixLength, suffix, 0, suffixLength);
}

@Nullable
public static Pack readMetaAndCreate(String id, Component title, boolean required, Pack.ResourcesSupplier resourceSupplier, PackType type, Pack.Position position, PackSource source, boolean fixedPosition) {
int $$7 = SharedConstants.getCurrentVersion().getPackVersion(type);
Pack.Info $$8 = Pack.readPackInfo(id, resourceSupplier, $$7);
return $$8 != null ? Pack.create(id, title, required, resourceSupplier, $$8, position, fixedPosition, source) : null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ public class PackOptions {
@SerializedName("description_includes_source")
public boolean addSourceToDescription = true;

@Expose
@SerializedName("fixed_position")
public boolean fixedPosition = false;

@Override
public String toString() {

Expand Down

0 comments on commit fc2e725

Please sign in to comment.