Skip to content

Commit

Permalink
Fix: Resources incorrectly listed for plugin packs (#4069)
Browse files Browse the repository at this point in the history
* fix: Resources incorrectly listed for plugin packs

* fix: Add invalid plugin resource path logging

* refactor: Remove unused imports
  • Loading branch information
pandier committed Jun 28, 2024
1 parent 3036983 commit bad1d61
Showing 1 changed file with 18 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
*/
package org.spongepowered.vanilla.server.packs;

import net.minecraft.ResourceLocationException;
import com.mojang.logging.LogUtils;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.packs.AbstractPackResources;
Expand All @@ -34,6 +34,7 @@
import net.minecraft.server.packs.metadata.pack.PackMetadataSection;
import net.minecraft.server.packs.resources.IoSupplier;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.slf4j.Logger;
import org.spongepowered.common.SpongeCommon;
import org.spongepowered.plugin.PluginContainer;

Expand All @@ -44,16 +45,13 @@
import java.nio.file.FileSystem;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Collections;
import java.util.Locale;
import java.util.Optional;
import java.util.Set;
import java.util.function.Predicate;
import java.util.*;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public final class PluginPackResources extends AbstractPackResources {
private static final Logger LOGGER = LogUtils.getLogger();

private final PluginContainer container;
private final PackMetadataSection metadata;
Expand Down Expand Up @@ -91,34 +89,27 @@ public IoSupplier<InputStream> getResource(final PackType type, final ResourceLo
public void listResources(final PackType type, final String namespace, final String path, final ResourceOutput out) {
try {
final Path root = this.typeRoot(type);
final Path namespaceDir = root.resolve(namespace).toAbsolutePath();
try (final Stream<Path> stream = Files.walk(namespaceDir)) {
final Path namespaceDir = root.resolve(namespace);
final Path resourcesDir = namespaceDir.resolve(path);
try (final Stream<Path> stream = Files.walk(resourcesDir)) {
stream.filter(Files::isRegularFile)
.filter(s -> !s.getFileName().toString().endsWith(".mcmeta"))
.filter(filePath -> !filePath.getFileName().toString().endsWith(".mcmeta"))
.map(namespaceDir::relativize)
.map(Object::toString)
// TODO filter needed? .filter(p -> filterValidPath(namespace, p, fileNameValidator))
.map(s -> new ResourceLocation(namespace, s))
.forEach(loc -> {
out.accept(loc, this.getResource(type, loc));
});
.map(filePath -> convertResourcePath(namespace, filePath))
.filter(Objects::nonNull)
.forEach(loc -> out.accept(loc, this.getResource(type, loc)));
}
} catch (final IOException ignored) {
}
}

private boolean filterValidPath(final String namespace, final String path, final Predicate<ResourceLocation> fileNameValidator) {
try {
final ResourceLocation loc = ResourceLocation.tryBuild(namespace, path);
if (loc == null) {
// LOGGER.warn("Invalid path in datapack: {}:{}, ignoring", $$1, $$7);
return false;
}
return fileNameValidator.test(loc);
} catch (ResourceLocationException e) {
// LOGGER.error(var13.getMessage());
return false;
}
@Nullable
private ResourceLocation convertResourcePath(final String namespace, final Path resourcePath) {
final String path = resourcePath.toString();
final ResourceLocation location = ResourceLocation.tryBuild(namespace, path);
if (location == null)
LOGGER.warn("Invalid path in plugin pack: {}:{}, ignoring", namespace, path);
return location;
}

@Nullable
Expand Down

0 comments on commit bad1d61

Please sign in to comment.