Add advancement progress tracker (#4568)

* Fix fetching advancements with invalid parents
* Add progress tracker to advancements
* Use Java language key for progress counter
This commit is contained in:
Eclipse 2024-08-01 01:18:49 +00:00 committed by GitHub
parent 5019b5aded
commit 95c6f7c9cf
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 50 additions and 18 deletions

View file

@ -82,11 +82,15 @@ public class GeyserAdvancement {
this.rootId = this.advancement.getId(); this.rootId = this.advancement.getId();
} else { } else {
// Go through our cache, and descend until we find the root ID // Go through our cache, and descend until we find the root ID
GeyserAdvancement advancement = advancementsCache.getStoredAdvancements().get(this.advancement.getParentId()); GeyserAdvancement parent = advancementsCache.getStoredAdvancements().get(this.advancement.getParentId());
if (advancement.getParentId() == null) { if (parent == null) {
this.rootId = advancement.getId(); // Parent doesn't exist, is invalid, or couldn't be found for another reason
// So assuming there is no parent and this is the root
this.rootId = this.advancement.getId();
} else if (parent.getParentId() == null) {
this.rootId = parent.getId();
} else { } else {
this.rootId = advancement.getRootId(advancementsCache); this.rootId = parent.getRootId(advancementsCache);
} }
} }
} }

View file

@ -158,7 +158,15 @@ public class AdvancementsCache {
// Cache language for easier access // Cache language for easier access
String language = session.locale(); String language = session.locale();
String earned = isEarned(advancement) ? "yes" : "no"; boolean advancementHasProgress = advancement.getRequirements().size() > 1;
int advancementProgress = getProgress(advancement);
int advancementRequirements = advancement.getRequirements().size();
boolean advancementEarned = advancementRequirements > 0
&& advancementProgress >= advancementRequirements;
String earned = advancementEarned ? "yes" : "no";
String description = getColorFromAdvancementFrameType(advancement) + MessageTranslator.convertMessage(advancement.getDisplayData().getDescription(), language); String description = getColorFromAdvancementFrameType(advancement) + MessageTranslator.convertMessage(advancement.getDisplayData().getDescription(), language);
String earnedString = GeyserLocale.getPlayerLocaleString("geyser.advancements.earned", language, MinecraftLocale.getLocaleString("gui." + earned, language)); String earnedString = GeyserLocale.getPlayerLocaleString("geyser.advancements.earned", language, MinecraftLocale.getLocaleString("gui." + earned, language));
@ -171,10 +179,20 @@ public class AdvancementsCache {
(Description) Mine stone with your new pickaxe (Description) Mine stone with your new pickaxe
Earned: Yes Earned: Yes
Progress: 1/4 // When advancement has multiple requirements
Parent Advancement: Minecraft // If relevant Parent Advancement: Minecraft // If relevant
*/ */
String content = description + "\n\n§f" + earnedString + "\n"; String content = description + "\n\n§f" + earnedString + "\n";
if (advancementHasProgress) {
// Only display progress with multiple requirements
String progress = MinecraftLocale.getLocaleString("advancements.progress", language)
.replaceFirst("%s", String.valueOf(advancementProgress))
.replaceFirst("%s", String.valueOf(advancementRequirements));
content += GeyserLocale.getPlayerLocaleString("geyser.advancements.progress", language, progress) + "\n";
}
if (!currentAdvancementCategoryId.equals(advancement.getParentId())) { if (!currentAdvancementCategoryId.equals(advancement.getParentId())) {
// Only display the parent if it is not the category // Only display the parent if it is not the category
content += GeyserLocale.getPlayerLocaleString("geyser.advancements.parentid", language, MessageTranslator.convertMessage(storedAdvancements.get(advancement.getParentId()).getDisplayData().getTitle(), language)); content += GeyserLocale.getPlayerLocaleString("geyser.advancements.parentid", language, MessageTranslator.convertMessage(storedAdvancements.get(advancement.getParentId()).getDisplayData().getTitle(), language));
@ -200,34 +218,44 @@ public class AdvancementsCache {
* @return true if the advancement has been earned. * @return true if the advancement has been earned.
*/ */
public boolean isEarned(GeyserAdvancement advancement) { public boolean isEarned(GeyserAdvancement advancement) {
boolean earned = false; if (advancement.getRequirements().isEmpty()) {
if (advancement.getRequirements().size() == 0) {
// Minecraft handles this case, so we better as well // Minecraft handles this case, so we better as well
return false; return false;
} }
Map<String, Long> progress = storedAdvancementProgress.get(advancement.getId()); // Progress should never be above requirements count, but you never know
if (progress != null) { return getProgress(advancement) >= advancement.getRequirements().size();
}
/**
* Determine the progress on an advancement.
*
* @param advancement the advancement to determine
* @return the progress on the advancement.
*/
public int getProgress(GeyserAdvancement advancement) {
if (advancement.getRequirements().isEmpty()) {
// Minecraft handles this case
return 0;
}
int progress = 0;
Map<String, Long> progressMap = storedAdvancementProgress.get(advancement.getId());
if (progressMap != null) {
// Each advancement's requirement must be fulfilled // Each advancement's requirement must be fulfilled
// For example, [[zombie, blaze, skeleton]] means that one of those three categories must be achieved // For example, [[zombie, blaze, skeleton]] means that one of those three categories must be achieved
// But [[zombie], [blaze], [skeleton]] means that all three requirements must be completed // But [[zombie], [blaze], [skeleton]] means that all three requirements must be completed
for (List<String> requirements : advancement.getRequirements()) { for (List<String> requirements : advancement.getRequirements()) {
boolean requirementsDone = false;
for (String requirement : requirements) { for (String requirement : requirements) {
Long obtained = progress.get(requirement); Long obtained = progressMap.get(requirement);
// -1 means that this particular component required for completing the advancement // -1 means that this particular component required for completing the advancement
// has yet to be fulfilled // has yet to be fulfilled
if (obtained != null && !obtained.equals(-1L)) { if (obtained != null && !obtained.equals(-1L)) {
requirementsDone = true; progress++;
break;
} }
} }
if (!requirementsDone) {
return false;
} }
} }
earned = true;
} return progress;
return earned;
} }
public String getColorFromAdvancementFrameType(GeyserAdvancement advancement) { public String getColorFromAdvancementFrameType(GeyserAdvancement advancement) {