From 5f9da5b146e12dde22b6023b859889f7f3340df0 Mon Sep 17 00:00:00 2001 From: Jeroen <48769316+iTwins@users.noreply.github.com> Date: Tue, 5 Dec 2023 22:56:23 +0100 Subject: [PATCH] Filter unlocked researches of player (#3996) --- .../slimefun4/api/player/PlayerProfile.java | 43 +++++++++++++------ .../slimefun4/api/researches/Research.java | 17 ++++++++ 2 files changed, 47 insertions(+), 13 deletions(-) diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/api/player/PlayerProfile.java b/src/main/java/io/github/thebusybiscuit/slimefun4/api/player/PlayerProfile.java index 2362a91d4..7b975f570 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/api/player/PlayerProfile.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/api/player/PlayerProfile.java @@ -1,6 +1,7 @@ package io.github.thebusybiscuit.slimefun4.api.player; import java.util.ArrayList; +import java.util.Collection; import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; @@ -36,7 +37,6 @@ import io.github.bakedlibs.dough.config.Config; import io.github.thebusybiscuit.slimefun4.api.events.AsyncProfileLoadEvent; import io.github.thebusybiscuit.slimefun4.api.gps.Waypoint; import io.github.thebusybiscuit.slimefun4.api.items.HashedArmorpiece; -import io.github.thebusybiscuit.slimefun4.api.items.ItemState; import io.github.thebusybiscuit.slimefun4.api.researches.Research; import io.github.thebusybiscuit.slimefun4.core.attributes.ProtectionType; import io.github.thebusybiscuit.slimefun4.core.attributes.ProtectiveArmor; @@ -325,35 +325,52 @@ public class PlayerProfile { return Optional.empty(); } - // returns the amount of researches with at least 1 enabled item - private int nonEmptyResearches() { - return (int) Slimefun.getRegistry().getResearches() - .stream() - .filter(research -> research.getAffectedItems().stream().anyMatch(item -> item.getState() == ItemState.ENABLED)) - .count(); + private int countNonEmptyResearches(@Nonnull Collection researches) { + int count = 0; + for (Research research : researches) { + if (research.hasEnabledItems()) { + count++; + } + } + return count; } + /** + * This method gets the research title, as defined in {@code config.yml}, + * of this {@link PlayerProfile} based on the fraction + * of unlocked {@link Research}es of this player. + * + * @return The research title of this {@link PlayerProfile} + */ public @Nonnull String getTitle() { List titles = Slimefun.getRegistry().getResearchRanks(); - float fraction = (float) researches.size() / nonEmptyResearches(); + int allResearches = countNonEmptyResearches(Slimefun.getRegistry().getResearches()); + float fraction = (float) countNonEmptyResearches(researches) / allResearches; int index = (int) (fraction * (titles.size() - 1)); return titles.get(index); } + /** + * This sends the statistics for the specified {@link CommandSender} + * to the {@link CommandSender}. This includes research title, research progress + * and total xp spent. + * + * @param sender The {@link CommandSender} for which to get the statistics and send them to. + */ public void sendStats(@Nonnull CommandSender sender) { - Set unlockedResearches = getResearches(); - int levels = unlockedResearches.stream().mapToInt(Research::getCost).sum(); - int allResearches = nonEmptyResearches(); + int unlockedResearches = countNonEmptyResearches(getResearches()); + int levels = getResearches().stream().mapToInt(Research::getCost).sum(); + int allResearches = countNonEmptyResearches(Slimefun.getRegistry().getResearches()); - float progress = Math.round(((unlockedResearches.size() * 100.0F) / allResearches) * 100.0F) / 100.0F; + float progress = Math.round(((unlockedResearches * 100.0F) / allResearches) * 100.0F) / 100.0F; sender.sendMessage(""); sender.sendMessage(ChatColors.color("&7Statistics for Player: &b" + name)); sender.sendMessage(""); sender.sendMessage(ChatColors.color("&7Title: " + ChatColor.AQUA + getTitle())); - sender.sendMessage(ChatColors.color("&7Research Progress: " + NumberUtils.getColorFromPercentage(progress) + progress + " &r% " + ChatColor.YELLOW + '(' + unlockedResearches.size() + " / " + allResearches + ')')); + sender.sendMessage(ChatColors.color("&7Research Progress: " + NumberUtils.getColorFromPercentage(progress) + progress + " &r% " + ChatColor.YELLOW + '(' + unlockedResearches + " / " + allResearches + ')')); sender.sendMessage(ChatColors.color("&7Total XP Levels spent: " + ChatColor.AQUA + levels)); } diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/api/researches/Research.java b/src/main/java/io/github/thebusybiscuit/slimefun4/api/researches/Research.java index 6a4ee6b52..c4c96e4e7 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/api/researches/Research.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/api/researches/Research.java @@ -22,6 +22,7 @@ import org.bukkit.inventory.ItemStack; import io.github.thebusybiscuit.slimefun4.api.events.PlayerPreResearchEvent; import io.github.thebusybiscuit.slimefun4.api.events.ResearchUnlockEvent; import io.github.thebusybiscuit.slimefun4.api.items.ItemGroup; +import io.github.thebusybiscuit.slimefun4.api.items.ItemState; import io.github.thebusybiscuit.slimefun4.api.items.SlimefunItem; import io.github.thebusybiscuit.slimefun4.api.player.PlayerProfile; import io.github.thebusybiscuit.slimefun4.core.guide.SlimefunGuideImplementation; @@ -197,6 +198,22 @@ public class Research implements Keyed { return items; } + /** + * This method checks whether there is at least one enabled {@link SlimefunItem} + * included in this {@link Research}. + * + * @return whether there is at least one enabled {@link SlimefunItem} + * included in this {@link Research}. + */ + public boolean hasEnabledItems() { + for (SlimefunItem item : items) { + if (item.getState() == ItemState.ENABLED) { + return true; + } + } + return false; + } + /** * Handle what to do when a {@link Player} clicks on an un-researched item in * a {@link SlimefunGuideImplementation}.