Skip to content

Commit

Permalink
Add sorting for ItemOverride in FileResourceMerger
Browse files Browse the repository at this point in the history
  • Loading branch information
portlek committed Jul 12, 2024
1 parent 2e779d1 commit 974575f
Showing 1 changed file with 19 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,30 @@

import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.Optional;
import java.util.stream.Collectors;
import net.kyori.adventure.key.Key;
import team.unnamed.creative.atlas.Atlas;
import team.unnamed.creative.model.ItemOverride;
import team.unnamed.creative.model.ItemPredicate;
import team.unnamed.creative.model.Model;

final class FileResourceMergerDefault implements FileResourceMerger {

static final FileResourceMerger INSTANCE = new FileResourceMergerDefault();
static final Comparator<ItemOverride> OVERRIDE_COMPARATOR = Comparator.comparingInt(value -> {
final Optional<ItemPredicate> first = value.predicate().stream().findFirst();
if (!first.isPresent()) {
return 0;
}
final ItemPredicate predicate = first.get();
if (predicate.name().equals("custom_model_data")) {
return (int) predicate.value();
}
return 0;
});

private FileResourceMergerDefault() {}

Expand All @@ -23,7 +38,7 @@ public Collection<FileResource> merge(final Collection<FileResource> resources)

final MultiMap<Key, Atlas> atlases = new MultiMap<>();
final MultiMap<Key, Model> models = new MultiMap<>();
final HashSet<FileResource> remainings = new HashSet<>();
final HashSet<FileResource> remaining = new HashSet<>();
for (final FileResource resource : simplified) {
if (resource instanceof FileResourceAtlas) {
final Atlas atlas = ((FileResourceAtlas) resource).atlas;
Expand All @@ -34,7 +49,7 @@ public Collection<FileResource> merge(final Collection<FileResource> resources)
}
// TODO: portlek, Merge more things.
else {
remainings.add(resource);
remaining.add(resource);
}
}

Expand Down Expand Up @@ -73,6 +88,7 @@ public Collection<FileResource> merge(final Collection<FileResource> resources)
.stream()
.map(Model::overrides)
.flatMap(Collection::stream)
.sorted(FileResourceMergerDefault.OVERRIDE_COMPARATOR)
.collect(Collectors.toList())
);
mergedModels.add(builder.build());
Expand All @@ -85,7 +101,7 @@ public Collection<FileResource> merge(final Collection<FileResource> resources)
mergedResources.addAll(
mergedModels.stream().map(FileResources::model).collect(Collectors.toSet())
);
mergedResources.addAll(remainings);
mergedResources.addAll(remaining);
return mergedResources;
}

Expand Down

0 comments on commit 974575f

Please sign in to comment.