From 1538fd973853b4248f3c364c8712f53527d702d5 Mon Sep 17 00:00:00 2001 From: KennyTV Date: Sat, 22 Aug 2020 11:06:06 +0200 Subject: [PATCH] Fix VersionData getFilteredDependencies and isRecommended --- .../java/io/papermc/hangar/model/Platform.java | 16 ++++++++++++++++ .../hangar/model/viewhelpers/VersionData.java | 17 +++++++++++------ 2 files changed, 27 insertions(+), 6 deletions(-) diff --git a/src/main/java/io/papermc/hangar/model/Platform.java b/src/main/java/io/papermc/hangar/model/Platform.java index a193de136..37f4ef855 100644 --- a/src/main/java/io/papermc/hangar/model/Platform.java +++ b/src/main/java/io/papermc/hangar/model/Platform.java @@ -3,10 +3,13 @@ package io.papermc.hangar.model; import io.papermc.hangar.db.model.ProjectVersionTagsTable; import io.papermc.hangar.model.generated.Dependency; import io.papermc.hangar.service.VersionService; +import org.jetbrains.annotations.Nullable; import java.util.Arrays; import java.util.Comparator; +import java.util.HashMap; import java.util.List; +import java.util.Map; import java.util.Map.Entry; import java.util.stream.Collectors; @@ -16,6 +19,14 @@ public enum Platform { WATERFALL("Waterfall", PlatformCategory.PROXY_CATEGORY, 1, "waterfall", TagColor.WATERFALL, "https://papermc.io/downloads#Waterfall"), VELOCITY("Velocity", PlatformCategory.PROXY_CATEGORY, 1, "velocity", TagColor.VELOCITY, "https://www.velocitypowered.com/downloads"); + private static final Map PLATFORMS_BY_DEPENDENDY = new HashMap<>(); + + static { + for (Platform platform : values()) { + PLATFORMS_BY_DEPENDENDY.put(platform.dependencyId, platform); + } + } + private final String name; private final PlatformCategory platformCategory; private final int priority; @@ -102,6 +113,11 @@ public enum Platform { .collect(Collectors.toList()); } + @Nullable + public static Platform getByDependencyId(String dependencyId) { + return PLATFORMS_BY_DEPENDENDY.get(dependencyId.toLowerCase()); + } + public static List createPlatformTags(VersionService versionService, long versionId, List dependencies) { return versionService.insertTags(getGhostTags(versionId, dependencies)); } diff --git a/src/main/java/io/papermc/hangar/model/viewhelpers/VersionData.java b/src/main/java/io/papermc/hangar/model/viewhelpers/VersionData.java index 78407d194..4bf5066c5 100644 --- a/src/main/java/io/papermc/hangar/model/viewhelpers/VersionData.java +++ b/src/main/java/io/papermc/hangar/model/viewhelpers/VersionData.java @@ -6,12 +6,10 @@ import io.papermc.hangar.db.model.ProjectsTable; import io.papermc.hangar.model.Platform; import io.papermc.hangar.model.generated.Dependency; -import java.util.Arrays; -import java.util.List; +import java.util.HashMap; import java.util.Map; import java.util.Map.Entry; import java.util.Set; -import java.util.stream.Collectors; public class VersionData { @@ -50,11 +48,18 @@ public class VersionData { } public Map getFilteredDependencies() { - List platformIds = Arrays.stream(Platform.getValues()).map(Platform::getDependencyId).collect(Collectors.toList()); - return dependencies.entrySet().stream().filter(entry -> !platformIds.contains(entry.getKey().getPluginId())).collect(Collectors.toMap(Entry::getKey, Entry::getValue)); + // Value is nullable, so we can't use Collectors#toMap + Map map = new HashMap<>(); + for (Entry entry : dependencies.entrySet()) { + if (Platform.getByDependencyId(entry.getKey().getPluginId()) == null) { // Exclude the platform dependency + map.put(entry.getKey(), entry.getValue()); + } + } + return map; } public boolean isRecommended() { - return p.getProject().getRecommendedVersionId() == v.getId(); + final Long recommendedVersionId = p.getProject().getRecommendedVersionId(); + return recommendedVersionId != null && recommendedVersionId == v.getId(); } }