From 84bcadc68735fb3a762d6d029eb1029ab700e362 Mon Sep 17 00:00:00 2001 From: Tim203 Date: Sat, 28 May 2022 23:45:35 +0200 Subject: [PATCH] Converted the new forms --- ap/pom.xml | 4 +- api/base/pom.xml | 2 +- api/geyser/pom.xml | 4 +- api/pom.xml | 2 +- bootstrap/bungeecord/pom.xml | 4 +- bootstrap/pom.xml | 4 +- bootstrap/spigot/pom.xml | 4 +- bootstrap/sponge/pom.xml | 4 +- bootstrap/standalone/pom.xml | 4 +- bootstrap/velocity/pom.xml | 4 +- common/pom.xml | 4 +- core/pom.xml | 8 +- .../geyser/session/cache/FormCache.java | 22 ++--- .../geyser/util/LoginEncryptionUtils.java | 87 +++++++++++++------ pom.xml | 2 +- 15 files changed, 99 insertions(+), 60 deletions(-) diff --git a/ap/pom.xml b/ap/pom.xml index 0644044a1..b60f37558 100644 --- a/ap/pom.xml +++ b/ap/pom.xml @@ -6,9 +6,9 @@ org.geysermc geyser-parent - 2.0.3-SNAPSHOT + 2.0.3-cumulus-SNAPSHOT ap - 2.0.3-SNAPSHOT + 2.0.3-cumulus-SNAPSHOT \ No newline at end of file diff --git a/api/base/pom.xml b/api/base/pom.xml index 1d051eaa3..4d4d12d90 100644 --- a/api/base/pom.xml +++ b/api/base/pom.xml @@ -5,7 +5,7 @@ org.geysermc api-parent - 2.0.3-SNAPSHOT + 2.0.3-cumulus-SNAPSHOT 4.0.0 diff --git a/api/geyser/pom.xml b/api/geyser/pom.xml index 2a933a2e0..9b67bea9e 100644 --- a/api/geyser/pom.xml +++ b/api/geyser/pom.xml @@ -5,7 +5,7 @@ org.geysermc api-parent - 2.0.3-SNAPSHOT + 2.0.3-cumulus-SNAPSHOT 4.0.0 @@ -26,7 +26,7 @@ org.geysermc base-api - 2.0.3-SNAPSHOT + 2.0.3-cumulus-SNAPSHOT compile diff --git a/api/pom.xml b/api/pom.xml index 5d078fba5..2ff8b4906 100644 --- a/api/pom.xml +++ b/api/pom.xml @@ -6,7 +6,7 @@ org.geysermc geyser-parent - 2.0.3-SNAPSHOT + 2.0.3-cumulus-SNAPSHOT api-parent diff --git a/bootstrap/bungeecord/pom.xml b/bootstrap/bungeecord/pom.xml index b2c661447..0c04f139e 100644 --- a/bootstrap/bungeecord/pom.xml +++ b/bootstrap/bungeecord/pom.xml @@ -6,7 +6,7 @@ org.geysermc bootstrap-parent - 2.0.3-SNAPSHOT + 2.0.3-cumulus-SNAPSHOT bootstrap-bungeecord @@ -14,7 +14,7 @@ org.geysermc core - 2.0.3-SNAPSHOT + 2.0.3-cumulus-SNAPSHOT compile diff --git a/bootstrap/pom.xml b/bootstrap/pom.xml index 7d6ac8f98..7c5ac68a1 100644 --- a/bootstrap/pom.xml +++ b/bootstrap/pom.xml @@ -6,7 +6,7 @@ org.geysermc geyser-parent - 2.0.3-SNAPSHOT + 2.0.3-cumulus-SNAPSHOT bootstrap-parent pom @@ -34,7 +34,7 @@ org.geysermc ap - 2.0.3-SNAPSHOT + 2.0.3-cumulus-SNAPSHOT provided diff --git a/bootstrap/spigot/pom.xml b/bootstrap/spigot/pom.xml index e9e7687f4..1f08bf146 100644 --- a/bootstrap/spigot/pom.xml +++ b/bootstrap/spigot/pom.xml @@ -6,7 +6,7 @@ org.geysermc bootstrap-parent - 2.0.3-SNAPSHOT + 2.0.3-cumulus-SNAPSHOT bootstrap-spigot @@ -30,7 +30,7 @@ org.geysermc core - 2.0.3-SNAPSHOT + 2.0.3-cumulus-SNAPSHOT compile diff --git a/bootstrap/sponge/pom.xml b/bootstrap/sponge/pom.xml index 8eba4d73d..96013e69a 100644 --- a/bootstrap/sponge/pom.xml +++ b/bootstrap/sponge/pom.xml @@ -6,7 +6,7 @@ org.geysermc bootstrap-parent - 2.0.3-SNAPSHOT + 2.0.3-cumulus-SNAPSHOT bootstrap-sponge @@ -14,7 +14,7 @@ org.geysermc core - 2.0.3-SNAPSHOT + 2.0.3-cumulus-SNAPSHOT compile diff --git a/bootstrap/standalone/pom.xml b/bootstrap/standalone/pom.xml index 8ee94b793..b3b32cc87 100644 --- a/bootstrap/standalone/pom.xml +++ b/bootstrap/standalone/pom.xml @@ -6,7 +6,7 @@ org.geysermc bootstrap-parent - 2.0.3-SNAPSHOT + 2.0.3-cumulus-SNAPSHOT bootstrap-standalone @@ -18,7 +18,7 @@ org.geysermc core - 2.0.3-SNAPSHOT + 2.0.3-cumulus-SNAPSHOT compile diff --git a/bootstrap/velocity/pom.xml b/bootstrap/velocity/pom.xml index 36882a19e..d81994522 100644 --- a/bootstrap/velocity/pom.xml +++ b/bootstrap/velocity/pom.xml @@ -6,7 +6,7 @@ org.geysermc bootstrap-parent - 2.0.3-SNAPSHOT + 2.0.3-cumulus-SNAPSHOT bootstrap-velocity @@ -14,7 +14,7 @@ org.geysermc core - 2.0.3-SNAPSHOT + 2.0.3-cumulus-SNAPSHOT compile diff --git a/common/pom.xml b/common/pom.xml index 0786f3f4d..fc00d4031 100644 --- a/common/pom.xml +++ b/common/pom.xml @@ -6,7 +6,7 @@ org.geysermc geyser-parent - 2.0.3-SNAPSHOT + 2.0.3-cumulus-SNAPSHOT common @@ -20,7 +20,7 @@ org.geysermc.cumulus cumulus - 1.0-SNAPSHOT + 1.1-SNAPSHOT com.google.code.gson diff --git a/core/pom.xml b/core/pom.xml index 8af2aa907..6334615d3 100644 --- a/core/pom.xml +++ b/core/pom.xml @@ -6,7 +6,7 @@ org.geysermc geyser-parent - 2.0.3-SNAPSHOT + 2.0.3-cumulus-SNAPSHOT core @@ -21,19 +21,19 @@ org.geysermc ap - 2.0.3-SNAPSHOT + 2.0.3-cumulus-SNAPSHOT provided org.geysermc geyser-api - 2.0.3-SNAPSHOT + 2.0.3-cumulus-SNAPSHOT compile org.geysermc common - 2.0.3-SNAPSHOT + 2.0.3-cumulus-SNAPSHOT compile diff --git a/core/src/main/java/org/geysermc/geyser/session/cache/FormCache.java b/core/src/main/java/org/geysermc/geyser/session/cache/FormCache.java index d44f1a9d1..24acebce0 100644 --- a/core/src/main/java/org/geysermc/geyser/session/cache/FormCache.java +++ b/core/src/main/java/org/geysermc/geyser/session/cache/FormCache.java @@ -43,29 +43,29 @@ import java.util.concurrent.atomic.AtomicInteger; @RequiredArgsConstructor public class FormCache { private final FormDefinitions formDefinitions = FormDefinitions.instance(); - private final AtomicInteger formId = new AtomicInteger(0); + private final AtomicInteger formIdCounter = new AtomicInteger(0); private final Int2ObjectMap
forms = new Int2ObjectOpenHashMap<>(); private final GeyserSession session; public int addForm(Form form) { - int windowId = formId.getAndIncrement(); - forms.put(windowId, form); - return windowId; + int formId = formIdCounter.getAndIncrement(); + forms.put(formId, form); + return formId; } public void showForm(Form form) { - int windowId = addForm(form); + int formId = addForm(form); if (session.getUpstream().isInitialized()) { - sendForm(windowId, form); + sendForm(formId, form); } } - private void sendForm(int windowId, Form form) { + private void sendForm(int formId, Form form) { String jsonData = formDefinitions.codecFor(form).jsonData(form); ModalFormRequestPacket formRequestPacket = new ModalFormRequestPacket(); - formRequestPacket.setFormId(windowId); + formRequestPacket.setFormId(formId); formRequestPacket.setFormData(jsonData); session.sendUpstreamPacket(formRequestPacket); @@ -74,8 +74,10 @@ public class FormCache { NetworkStackLatencyPacket latencyPacket = new NetworkStackLatencyPacket(); latencyPacket.setFromServer(true); latencyPacket.setTimestamp(-System.currentTimeMillis()); - session.scheduleInEventLoop(() -> session.sendUpstreamPacket(latencyPacket), - 500, TimeUnit.MILLISECONDS); + session.scheduleInEventLoop( + () -> session.sendUpstreamPacket(latencyPacket), + 500, TimeUnit.MILLISECONDS + ); } } diff --git a/core/src/main/java/org/geysermc/geyser/util/LoginEncryptionUtils.java b/core/src/main/java/org/geysermc/geyser/util/LoginEncryptionUtils.java index 0d369ac19..0d60ad457 100644 --- a/core/src/main/java/org/geysermc/geyser/util/LoginEncryptionUtils.java +++ b/core/src/main/java/org/geysermc/geyser/util/LoginEncryptionUtils.java @@ -40,7 +40,10 @@ import com.nukkitx.protocol.bedrock.util.EncryptionUtils; import org.geysermc.cumulus.form.CustomForm; import org.geysermc.cumulus.form.ModalForm; import org.geysermc.cumulus.form.SimpleForm; +import org.geysermc.cumulus.response.SimpleFormResponse; +import org.geysermc.cumulus.response.result.FormResponseResult; import org.geysermc.cumulus.response.result.ResultType; +import org.geysermc.cumulus.response.result.ValidFormResponseResult; import org.geysermc.geyser.GeyserImpl; import org.geysermc.geyser.configuration.GeyserConfiguration; import org.geysermc.geyser.session.GeyserSession; @@ -59,6 +62,7 @@ import java.security.interfaces.ECPublicKey; import java.security.spec.ECGenParameterSpec; import java.util.Iterator; import java.util.UUID; +import java.util.function.BiConsumer; public class LoginEncryptionUtils { private static final ObjectMapper JSON_MAPPER = new ObjectMapper().disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES); @@ -264,42 +268,49 @@ public class LoginEncryptionUtils { * Build a window that explains the user's credentials will be saved to the system. */ public static void buildAndShowConsentWindow(GeyserSession session) { - String locale = session.getLocale(); session.sendForm( SimpleForm.builder() + .translator(LoginEncryptionUtils::translate, session.getLocale()) .title("%gui.signIn") - .content(GeyserLocale.getPlayerLocaleString("geyser.auth.login.save_token.warning", locale) + - "\n\n" + - GeyserLocale.getPlayerLocaleString("geyser.auth.login.save_token.proceed", locale)) + .content(""" + geyser.auth.login.save_token.warning + + geyser.auth.login.save_token.proceed""") .button("%gui.ok") .button("%gui.decline") - .responseHandler((form, responseData) -> { - SimpleFormResponse response = form.parseResponse(responseData); - if (response.isCorrect() && response.getClickedButtonId() == 0) { - session.authenticateWithMicrosoftCode(true); - } else { - session.disconnect("%disconnect.quitting"); - } - })); + .resultHandler( + authenticateOrKickHandler(session), + ResultType.CLOSED, ResultType.INVALID, ResultType.VALID + ) + ); } public static void buildAndShowTokenExpiredWindow(GeyserSession session) { - String locale = session.getLocale(); session.sendForm( SimpleForm.builder() - .title(GeyserLocale.getPlayerLocaleString("geyser.auth.login.form.expired", locale)) - .content(GeyserLocale.getPlayerLocaleString("geyser.auth.login.save_token.expired", locale) + - "\n\n" + - GeyserLocale.getPlayerLocaleString("geyser.auth.login.save_token.proceed", locale)) + .translator(LoginEncryptionUtils::translate, session.getLocale()) + .title("geyser.auth.login.form.expired") + .content(""" + geyser.auth.login.save_token.expired + + geyser.auth.login.save_token.proceed""") .button("%gui.ok") - .responseHandler((form, responseData) -> { - SimpleFormResponse response = form.parseResponse(responseData); - if (response.isCorrect()) { - session.authenticateWithMicrosoftCode(true); - } else { - session.disconnect("%disconnect.quitting"); - } - })); + .resultHandler( + authenticateOrKickHandler(session), + ResultType.CLOSED, ResultType.INVALID, ResultType.VALID + ) + ); + } + + private static BiConsumer> authenticateOrKickHandler(GeyserSession session) { + return (form, genericResult) -> { + if (genericResult instanceof ValidFormResponseResult result && + result.response().clickedButtonId() == 0) { + session.authenticateWithMicrosoftCode(true); + } else { + session.disconnect("%disconnect.quitting"); + } + }; } public static void buildAndShowLoginDetailsWindow(GeyserSession session) { @@ -378,4 +389,30 @@ public class LoginEncryptionUtils { }) ); } + + /* + This checks per line if there is something to be translated, and it skips Bedrock translation keys (%) + */ + private static String translate(String key, String locale) { + StringBuilder newValue = new StringBuilder(); + int previousIndex = 0; + while (previousIndex < key.length()) { + int nextIndex = key.indexOf('\n', previousIndex); + int endIndex = nextIndex == -1 ? key.length() : nextIndex; + + // if there is more to this line than just a new line char + if (endIndex - previousIndex > 1) { + String substring = key.substring(previousIndex, endIndex); + if (key.charAt(previousIndex) != '%') { + newValue.append(GeyserLocale.getPlayerLocaleString(substring, locale)); + } else { + newValue.append(substring); + } + } + newValue.append('\n'); + + previousIndex = endIndex + 1; + } + return newValue.toString(); + } } diff --git a/pom.xml b/pom.xml index f4930959d..f0b8f21ab 100644 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ 4.0.0 org.geysermc geyser-parent - 2.0.3-SNAPSHOT + 2.0.3-cumulus-SNAPSHOT pom Geyser Allows for players from Minecraft Bedrock Edition to join Minecraft Java Edition servers.