Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change advancement form mechanics to match Java behaviour, fix NPE #5396

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,6 @@ public AdvancementsCommand(String name, String description, String permission) {
@Override
public void execute(CommandContext<GeyserCommandSource> context) {
GeyserSession session = Objects.requireNonNull(context.sender().connection());
session.getAdvancementsCache().buildAndShowMenuForm();
session.getAdvancementsCache().buildAndShowForm();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import org.geysermc.mcprotocollib.protocol.data.game.advancement.Advancement;
import org.geysermc.mcprotocollib.protocol.packet.ingame.serverbound.inventory.ServerboundSeenAdvancementsPacket;
import lombok.Getter;
import lombok.Setter;
import org.geysermc.cumulus.form.SimpleForm;
import org.geysermc.geyser.level.GeyserAdvancement;
import org.geysermc.geyser.session.GeyserSession;
Expand All @@ -41,6 +40,7 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

public class AdvancementsCache {
/**
Expand All @@ -58,15 +58,39 @@ public class AdvancementsCache {
/**
* Stores player's chosen advancement's ID and title for use in form creators.
*/
@Setter
private String currentAdvancementCategoryId = null;

/**
* Stores if the player is currently viewing advancements.
*/
private boolean formOpen = false;

private final GeyserSession session;

public AdvancementsCache(GeyserSession session) {
this.session = session;
}

public void setCurrentAdvancementCategoryId(String categoryId) {
if (!Objects.equals(currentAdvancementCategoryId, categoryId)) {
// Only open and show list form if we're going to a different category
currentAdvancementCategoryId = categoryId;
if (formOpen) {
session.closeForm();
buildAndShowForm();
formOpen = true;
}
}
}

public void buildAndShowForm() {
if (currentAdvancementCategoryId == null) {
buildAndShowMenuForm();
} else {
buildAndShowListForm();
}
}

/**
* Build and send a form with all advancement categories
*/
Expand All @@ -88,9 +112,11 @@ public void buildAndShowMenuForm() {
builder.content("advancements.empty");
}

builder.validResultHandler((response) -> {
builder.closedResultHandler(() -> {
formOpen = false;
}).validResultHandler((response) -> {
String id = rootAdvancementIds.get(response.clickedButtonId());
if (!id.equals("")) {
if (!id.isEmpty()) {
// Send a packet indicating that we are opening this particular advancement window
ServerboundSeenAdvancementsPacket packet = new ServerboundSeenAdvancementsPacket(id);
session.sendDownstreamGamePacket(packet);
Expand All @@ -99,6 +125,7 @@ public void buildAndShowMenuForm() {
}
});

formOpen = true;
session.sendForm(builder);
}

Expand Down Expand Up @@ -133,6 +160,9 @@ public void buildAndShowListForm() {

builder.closedResultHandler(() -> {
// Indicate that we have closed the current advancement tab
// Don't set currentAdvancementCategoryId to null here, so that when the advancements form is shown again (buildAndShowForm),
// the tab that was last open is opened again, which matches Java behaviour
formOpen = false;
session.sendDownstreamGamePacket(new ServerboundSeenAdvancementsPacket());

}).validResultHandler((response) -> {
Expand All @@ -142,6 +172,7 @@ public void buildAndShowListForm() {
} else {
buildAndShowMenuForm();
// Indicate that we have closed the current advancement tab
currentAdvancementCategoryId = null;
session.sendDownstreamGamePacket(new ServerboundSeenAdvancementsPacket());
}
});
Expand Down Expand Up @@ -206,6 +237,7 @@ public void buildAndShowInfoForm(GeyserAdvancement advancement) {
.validResultHandler((response) -> buildAndShowListForm())
.closedResultHandler(() -> {
// Indicate that we have closed the current advancement tab
formOpen = false;
session.sendDownstreamGamePacket(new ServerboundSeenAdvancementsPacket());
})
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@

import org.geysermc.mcprotocollib.protocol.packet.ingame.clientbound.ClientboundSelectAdvancementsTabPacket;
import org.geysermc.geyser.session.GeyserSession;
import org.geysermc.geyser.session.cache.AdvancementsCache;
import org.geysermc.geyser.translator.protocol.PacketTranslator;
import org.geysermc.geyser.translator.protocol.Translator;

Expand All @@ -39,8 +38,6 @@ public class JavaSelectAdvancementsTabTranslator extends PacketTranslator<Client

@Override
public void translate(GeyserSession session, ClientboundSelectAdvancementsTabPacket packet) {
AdvancementsCache advancementsCache = session.getAdvancementsCache();
advancementsCache.setCurrentAdvancementCategoryId(packet.getTabId());
advancementsCache.buildAndShowListForm();
session.getAdvancementsCache().setCurrentAdvancementCategoryId(packet.getTabId());
}
}