3
0
Mirror von https://github.com/GeyserMC/Geyser.git synchronisiert 2024-11-03 14:50:19 +01:00

Use new toasts for advancements and simplify advancement form response

Dieser Commit ist enthalten in:
davchoo 2022-05-26 02:11:25 -04:00
Ursprung 9183897d52
Commit 2c77306937
3 geänderte Dateien mit 38 neuen und 60 gelöschten Zeilen

Datei anzeigen

@ -28,6 +28,7 @@ package org.geysermc.geyser.level;
import com.github.steveice10.mc.protocol.data.game.advancement.Advancement; import com.github.steveice10.mc.protocol.data.game.advancement.Advancement;
import lombok.NonNull; import lombok.NonNull;
import org.geysermc.geyser.session.cache.AdvancementsCache; import org.geysermc.geyser.session.cache.AdvancementsCache;
import org.geysermc.geyser.text.ChatColor;
import java.util.List; import java.util.List;
@ -69,6 +70,14 @@ public class GeyserAdvancement {
return this.advancement.getDisplayData(); return this.advancement.getDisplayData();
} }
/**
* @return Purple for challenges and green for normal advancements
*/
public String getDisplayColor() {
Advancement.DisplayData displayData = getDisplayData();
return displayData != null && displayData.getFrameType() == Advancement.DisplayData.FrameType.CHALLENGE ? ChatColor.LIGHT_PURPLE : ChatColor.GREEN;
}
public String getRootId(AdvancementsCache advancementsCache) { public String getRootId(AdvancementsCache advancementsCache) {
if (rootId == null) { if (rootId == null) {
if (this.advancement.getParentId() == null) { if (this.advancement.getParentId() == null) {

Datei anzeigen

@ -38,6 +38,7 @@ import org.geysermc.geyser.text.MinecraftLocale;
import org.geysermc.cumulus.SimpleForm; import org.geysermc.cumulus.SimpleForm;
import org.geysermc.cumulus.response.SimpleFormResponse; import org.geysermc.cumulus.response.SimpleFormResponse;
import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@ -76,15 +77,15 @@ public class AdvancementsCache {
.translator(MinecraftLocale::getLocaleString, session.getLocale()) .translator(MinecraftLocale::getLocaleString, session.getLocale())
.title("gui.advancements"); .title("gui.advancements");
boolean hasAdvancements = false; List<String> rootAdvancementIds = new ArrayList<>();
for (Map.Entry<String, GeyserAdvancement> advancement : storedAdvancements.entrySet()) { for (Map.Entry<String, GeyserAdvancement> advancement : storedAdvancements.entrySet()) {
if (advancement.getValue().getParentId() == null) { // No parent means this is a root advancement if (advancement.getValue().getParentId() == null) { // No parent means this is a root advancement
hasAdvancements = true;
builder.button(MessageTranslator.convertMessage(advancement.getValue().getDisplayData().getTitle(), session.getLocale())); builder.button(MessageTranslator.convertMessage(advancement.getValue().getDisplayData().getTitle(), session.getLocale()));
rootAdvancementIds.add(advancement.getKey());
} }
} }
if (!hasAdvancements) { if (rootAdvancementIds.isEmpty()) {
builder.content("advancements.empty"); builder.content("advancements.empty");
} }
@ -94,20 +95,7 @@ public class AdvancementsCache {
return; return;
} }
String id = ""; String id = rootAdvancementIds.get(response.getClickedButtonId());
int advancementIndex = 0;
for (Map.Entry<String, GeyserAdvancement> advancement : storedAdvancements.entrySet()) {
if (advancement.getValue().getParentId() == null) { // Root advancement
if (advancementIndex == response.getClickedButtonId()) {
id = advancement.getKey();
break;
} else {
advancementIndex++;
}
}
}
if (!id.equals("")) { if (!id.equals("")) {
if (id.equals(currentAdvancementCategoryId)) { if (id.equals(currentAdvancementCategoryId)) {
// The server thinks we are already on this tab // The server thinks we are already on this tab
@ -136,12 +124,16 @@ public class AdvancementsCache {
.title(MessageTranslator.convertMessage(categoryAdvancement.getDisplayData().getTitle(), language)) .title(MessageTranslator.convertMessage(categoryAdvancement.getDisplayData().getTitle(), language))
.content(MessageTranslator.convertMessage(categoryAdvancement.getDisplayData().getDescription(), language)); .content(MessageTranslator.convertMessage(categoryAdvancement.getDisplayData().getDescription(), language));
List<GeyserAdvancement> visibleAdvancements = new ArrayList<>();
if (currentAdvancementCategoryId != null) { if (currentAdvancementCategoryId != null) {
for (GeyserAdvancement advancement : storedAdvancements.values()) { for (GeyserAdvancement advancement : storedAdvancements.values()) {
if (advancement != null) { boolean earned = isEarned(advancement);
if (earned || !advancement.getDisplayData().isHidden()) {
if (advancement.getParentId() != null && currentAdvancementCategoryId.equals(advancement.getRootId(this))) { if (advancement.getParentId() != null && currentAdvancementCategoryId.equals(advancement.getRootId(this))) {
boolean color = isEarned(advancement) || !advancement.getDisplayData().isShowToast(); String color = earned ? advancement.getDisplayColor() : "";
builder.button((color ? ChatColor.DARK_GREEN : "") + MessageTranslator.convertMessage(advancement.getDisplayData().getTitle()) + '\n'); builder.button(color + MessageTranslator.convertMessage(advancement.getDisplayData().getTitle()) + '\n');
visibleAdvancements.add(advancement);
} }
} }
} }
@ -157,22 +149,8 @@ public class AdvancementsCache {
return; return;
} }
GeyserAdvancement advancement = null; if (response.getClickedButtonId() < visibleAdvancements.size()) {
int advancementIndex = 0; GeyserAdvancement advancement = visibleAdvancements.get(response.getClickedButtonId());
// Loop around to find the advancement that the client pressed
for (GeyserAdvancement advancementEntry : storedAdvancements.values()) {
if (advancementEntry.getParentId() != null &&
currentAdvancementCategoryId.equals(advancementEntry.getRootId(this))) {
if (advancementIndex == response.getClickedButtonId()) {
advancement = advancementEntry;
break;
} else {
advancementIndex++;
}
}
}
if (advancement != null) {
buildAndShowInfoForm(advancement); buildAndShowInfoForm(advancement);
} else { } else {
buildAndShowMenuForm(); buildAndShowMenuForm();

Datei anzeigen

@ -27,7 +27,7 @@ package org.geysermc.geyser.translator.protocol.java;
import com.github.steveice10.mc.protocol.data.game.advancement.Advancement; import com.github.steveice10.mc.protocol.data.game.advancement.Advancement;
import com.github.steveice10.mc.protocol.packet.ingame.clientbound.ClientboundUpdateAdvancementsPacket; import com.github.steveice10.mc.protocol.packet.ingame.clientbound.ClientboundUpdateAdvancementsPacket;
import com.nukkitx.protocol.bedrock.packet.SetTitlePacket; import com.nukkitx.protocol.bedrock.packet.ToastRequestPacket;
import org.geysermc.geyser.session.GeyserSession; import org.geysermc.geyser.session.GeyserSession;
import org.geysermc.geyser.translator.protocol.PacketTranslator; import org.geysermc.geyser.translator.protocol.PacketTranslator;
import org.geysermc.geyser.translator.protocol.Translator; import org.geysermc.geyser.translator.protocol.Translator;
@ -36,7 +36,7 @@ import org.geysermc.geyser.session.cache.AdvancementsCache;
import org.geysermc.geyser.level.GeyserAdvancement; import org.geysermc.geyser.level.GeyserAdvancement;
import org.geysermc.geyser.text.MinecraftLocale; import org.geysermc.geyser.text.MinecraftLocale;
import java.util.Map; import java.util.Locale;
@Translator(packet = ClientboundUpdateAdvancementsPacket.class) @Translator(packet = ClientboundUpdateAdvancementsPacket.class)
public class JavaUpdateAdvancementsTranslator extends PacketTranslator<ClientboundUpdateAdvancementsPacket> { public class JavaUpdateAdvancementsTranslator extends PacketTranslator<ClientboundUpdateAdvancementsPacket> {
@ -56,48 +56,39 @@ public class JavaUpdateAdvancementsTranslator extends PacketTranslator<Clientbou
advancementsCache.getStoredAdvancementProgress().putAll(packet.getProgress()); advancementsCache.getStoredAdvancementProgress().putAll(packet.getProgress());
sendToolbarAdvancementUpdates(session, packet);
// Adds advancements to the player's stored advancements when advancements are sent // Adds advancements to the player's stored advancements when advancements are sent
for (Advancement advancement : packet.getAdvancements()) { for (Advancement advancement : packet.getAdvancements()) {
if (advancement.getDisplayData() != null && !advancement.getDisplayData().isHidden()) { if (advancement.getDisplayData() != null && (!advancement.getDisplayData().isHidden() || advancement.getDisplayData().isShowToast())) {
GeyserAdvancement geyserAdvancement = GeyserAdvancement.from(advancement); GeyserAdvancement geyserAdvancement = GeyserAdvancement.from(advancement);
advancementsCache.getStoredAdvancements().put(advancement.getId(), geyserAdvancement); advancementsCache.getStoredAdvancements().put(advancement.getId(), geyserAdvancement);
} else { } else {
advancementsCache.getStoredAdvancements().remove(advancement.getId()); advancementsCache.getStoredAdvancements().remove(advancement.getId());
} }
} }
sendAdvancementToasts(session, packet);
} }
/** /**
* Handle all advancements progress updates * Handle all advancements progress updates
*/ */
public void sendToolbarAdvancementUpdates(GeyserSession session, ClientboundUpdateAdvancementsPacket packet) { public void sendAdvancementToasts(GeyserSession session, ClientboundUpdateAdvancementsPacket packet) {
if (packet.isReset()) { if (packet.isReset()) {
// Advancements are being cleared, so they can't be granted // Advancements are being cleared, so they can't be granted
return; return;
} }
for (Map.Entry<String, Map<String, Long>> progress : packet.getProgress().entrySet()) { for (String advancementId : packet.getProgress().keySet()) {
GeyserAdvancement advancement = session.getAdvancementsCache().getStoredAdvancements().get(progress.getKey()); GeyserAdvancement advancement = session.getAdvancementsCache().getStoredAdvancements().get(advancementId);
if (advancement != null && advancement.getDisplayData() != null) { if (advancement != null && advancement.getDisplayData() != null) {
if (session.getAdvancementsCache().isEarned(advancement)) { if (advancement.getDisplayData().isShowToast() && session.getAdvancementsCache().isEarned(advancement)) {
// Java uses some pink color for toast challenge completes String frameType = advancement.getDisplayData().getFrameType().toString().toLowerCase(Locale.ROOT);
String color = advancement.getDisplayData().getFrameType() == Advancement.DisplayData.FrameType.CHALLENGE ? String frameTitle = advancement.getDisplayColor() + MinecraftLocale.getLocaleString("advancements.toast." + frameType, session.getLocale());
"§d" : "§a";
String advancementName = MessageTranslator.convertMessage(advancement.getDisplayData().getTitle(), session.getLocale()); String advancementName = MessageTranslator.convertMessage(advancement.getDisplayData().getTitle(), session.getLocale());
// Send an action bar message stating they earned an achievement ToastRequestPacket toastRequestPacket = new ToastRequestPacket();
// Sent for instances where broadcasting advancements through chat are disabled toastRequestPacket.setTitle(frameTitle);
SetTitlePacket titlePacket = new SetTitlePacket(); toastRequestPacket.setContent(advancementName);
titlePacket.setText(color + "[" + MinecraftLocale.getLocaleString("advancements.toast." + session.sendUpstreamPacket(toastRequestPacket);
advancement.getDisplayData().getFrameType().toString().toLowerCase(), session.getLocale()) + "]§f " + advancementName);
titlePacket.setType(SetTitlePacket.Type.ACTIONBAR);
titlePacket.setFadeOutTime(3);
titlePacket.setFadeInTime(3);
titlePacket.setStayTime(3);
titlePacket.setXuid("");
titlePacket.setPlatformOnlineId("");
session.sendUpstreamPacket(titlePacket);
} }
} }
} }