mirror of
https://github.com/StarWishsama/Slimefun4.git
synced 2024-09-19 19:25:48 +00:00
Filter unlocked researches of player (#3996)
This commit is contained in:
parent
6ce0911a16
commit
5f9da5b146
@ -1,6 +1,7 @@
|
|||||||
package io.github.thebusybiscuit.slimefun4.api.player;
|
package io.github.thebusybiscuit.slimefun4.api.player;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.Collection;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
import java.util.Iterator;
|
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.events.AsyncProfileLoadEvent;
|
||||||
import io.github.thebusybiscuit.slimefun4.api.gps.Waypoint;
|
import io.github.thebusybiscuit.slimefun4.api.gps.Waypoint;
|
||||||
import io.github.thebusybiscuit.slimefun4.api.items.HashedArmorpiece;
|
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.api.researches.Research;
|
||||||
import io.github.thebusybiscuit.slimefun4.core.attributes.ProtectionType;
|
import io.github.thebusybiscuit.slimefun4.core.attributes.ProtectionType;
|
||||||
import io.github.thebusybiscuit.slimefun4.core.attributes.ProtectiveArmor;
|
import io.github.thebusybiscuit.slimefun4.core.attributes.ProtectiveArmor;
|
||||||
@ -325,35 +325,52 @@ public class PlayerProfile {
|
|||||||
return Optional.empty();
|
return Optional.empty();
|
||||||
}
|
}
|
||||||
|
|
||||||
// returns the amount of researches with at least 1 enabled item
|
private int countNonEmptyResearches(@Nonnull Collection<Research> researches) {
|
||||||
private int nonEmptyResearches() {
|
int count = 0;
|
||||||
return (int) Slimefun.getRegistry().getResearches()
|
for (Research research : researches) {
|
||||||
.stream()
|
if (research.hasEnabledItems()) {
|
||||||
.filter(research -> research.getAffectedItems().stream().anyMatch(item -> item.getState() == ItemState.ENABLED))
|
count++;
|
||||||
.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() {
|
public @Nonnull String getTitle() {
|
||||||
List<String> titles = Slimefun.getRegistry().getResearchRanks();
|
List<String> 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));
|
int index = (int) (fraction * (titles.size() - 1));
|
||||||
|
|
||||||
return titles.get(index);
|
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) {
|
public void sendStats(@Nonnull CommandSender sender) {
|
||||||
Set<Research> unlockedResearches = getResearches();
|
int unlockedResearches = countNonEmptyResearches(getResearches());
|
||||||
int levels = unlockedResearches.stream().mapToInt(Research::getCost).sum();
|
int levels = getResearches().stream().mapToInt(Research::getCost).sum();
|
||||||
int allResearches = nonEmptyResearches();
|
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("");
|
||||||
sender.sendMessage(ChatColors.color("&7Statistics for Player: &b" + name));
|
sender.sendMessage(ChatColors.color("&7Statistics for Player: &b" + name));
|
||||||
sender.sendMessage("");
|
sender.sendMessage("");
|
||||||
sender.sendMessage(ChatColors.color("&7Title: " + ChatColor.AQUA + getTitle()));
|
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));
|
sender.sendMessage(ChatColors.color("&7Total XP Levels spent: " + ChatColor.AQUA + levels));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -22,6 +22,7 @@ import org.bukkit.inventory.ItemStack;
|
|||||||
import io.github.thebusybiscuit.slimefun4.api.events.PlayerPreResearchEvent;
|
import io.github.thebusybiscuit.slimefun4.api.events.PlayerPreResearchEvent;
|
||||||
import io.github.thebusybiscuit.slimefun4.api.events.ResearchUnlockEvent;
|
import io.github.thebusybiscuit.slimefun4.api.events.ResearchUnlockEvent;
|
||||||
import io.github.thebusybiscuit.slimefun4.api.items.ItemGroup;
|
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.items.SlimefunItem;
|
||||||
import io.github.thebusybiscuit.slimefun4.api.player.PlayerProfile;
|
import io.github.thebusybiscuit.slimefun4.api.player.PlayerProfile;
|
||||||
import io.github.thebusybiscuit.slimefun4.core.guide.SlimefunGuideImplementation;
|
import io.github.thebusybiscuit.slimefun4.core.guide.SlimefunGuideImplementation;
|
||||||
@ -197,6 +198,22 @@ public class Research implements Keyed {
|
|||||||
return items;
|
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
|
* Handle what to do when a {@link Player} clicks on an un-researched item in
|
||||||
* a {@link SlimefunGuideImplementation}.
|
* a {@link SlimefunGuideImplementation}.
|
||||||
|
Loading…
Reference in New Issue
Block a user