Skip to content

Commit

Permalink
Fix some SettingsUtils regressions
Browse files Browse the repository at this point in the history
- Add the new 1.17 gamerules
- Don't hide exceptions in form responses
- Fix the settings form silently failing
- Fix some translation strings in the settings form
  • Loading branch information
Camotoy committed Jun 27, 2021
1 parent 5b1d815 commit 215ffc6
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
import lombok.RequiredArgsConstructor;
import org.geysermc.connector.GeyserConnector;
import org.geysermc.connector.network.session.GeyserSession;
import org.geysermc.cumulus.Form;
import org.geysermc.cumulus.SimpleForm;
Expand Down Expand Up @@ -73,17 +74,19 @@ public int showForm(Form form) {
}

public void handleResponse(ModalFormResponsePacket response) {
Form form = forms.get(response.getFormId());
Form form = forms.remove(response.getFormId());
if (form == null) {
return;
}

Consumer<String> responseConsumer = form.getResponseHandler();
if (responseConsumer != null) {
responseConsumer.accept(response.getFormData());
try {
responseConsumer.accept(response.getFormData());
} catch (Exception e) {
GeyserConnector.getInstance().getLogger().error("Error while processing form response!", e);
}
}

removeWindow(response.getFormId());
}

public boolean removeWindow(int id) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,15 @@ public enum GameRule {
DROWNINGDAMAGE("drowningDamage", Boolean.class, true),
FALLDAMAGE("fallDamage", Boolean.class, true),
FIREDAMAGE("fireDamage", Boolean.class, true),
FREEZEDAMAGE("freezeDamage", Boolean.class, true),
FORGIVEDEADPLAYERS("forgiveDeadPlayers", Boolean.class, true), // JE only
KEEPINVENTORY("keepInventory", Boolean.class, false),
LOGADMINCOMMANDS("logAdminCommands", Boolean.class, true), // JE only
MAXCOMMANDCHAINLENGTH("maxCommandChainLength", Integer.class, 65536),
MAXENTITYCRAMMING("maxEntityCramming", Integer.class, 24), // JE only
MOBGRIEFING("mobGriefing", Boolean.class, true),
NATURALREGENERATION("naturalRegeneration", Boolean.class, true),
PLAYERSSLEEPINGPERCENTAGE("playersSleepingPercentage", Integer.class, 100), // JE only
RANDOMTICKSPEED("randomTickSpeed", Integer.class, 3),
REDUCEDDEBUGINFO("reducedDebugInfo", Boolean.class, false), // JE only
SENDCOMMANDFEEDBACK("sendCommandFeedback", Boolean.class, true),
Expand All @@ -68,7 +70,7 @@ public enum GameRule {

UNKNOWN("unknown", Object.class);

private static final GameRule[] VALUES = values();
public static final GameRule[] VALUES = values();

@Getter
private final String javaID;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,13 @@ public static CustomForm buildForm(GeyserSession session) {
String language = session.getLocale();

CustomForm.Builder builder = CustomForm.builder()
.translator(LanguageUtils::getPlayerLocaleString, language)
.translator(SettingsUtils::translateEntry, language)
.title("geyser.settings.title.main")
.iconPath("textures/ui/settings_glyph_color_2x.png");

// Only show the client title if any of the client settings are available
if (session.getPreferencesCache().isAllowShowCoordinates() || CooldownUtils.getDefaultShowCooldown() != CooldownUtils.CooldownType.DISABLED) {
boolean showClientSettings = session.getPreferencesCache().isAllowShowCoordinates() || CooldownUtils.getDefaultShowCooldown() != CooldownUtils.CooldownType.DISABLED;
if (showClientSettings) {
builder.label("geyser.settings.title.client");

// Client can only see its coordinates if reducedDebugInfo is disabled and coordinates are enabled in geyser config.
Expand All @@ -67,7 +68,8 @@ public static CustomForm buildForm(GeyserSession session) {
}
}

if (session.getOpPermissionLevel() >= 2 || session.hasPermission("geyser.settings.server")) {
boolean canModifyServer = session.getOpPermissionLevel() >= 2 || session.hasPermission("geyser.settings.server");
if (canModifyServer) {
builder.label("geyser.settings.title.server");

DropdownComponent.Builder gamemodeDropdown = DropdownComponent.builder("%createWorldScreen.gameMode.personal");
Expand All @@ -83,7 +85,8 @@ public static CustomForm buildForm(GeyserSession session) {
builder.dropdown(difficultyDropdown);
}

if (session.getOpPermissionLevel() >= 2 || session.hasPermission("geyser.settings.gamerules")) {
boolean showGamerules = session.getOpPermissionLevel() >= 2 || session.hasPermission("geyser.settings.gamerules");
if (showGamerules) {
builder.label("geyser.settings.title.game_rules")
.translator(LocaleUtils::getLocaleString); // we need translate gamerules next

Expand All @@ -108,24 +111,20 @@ public static CustomForm buildForm(GeyserSession session) {
return;
}

if (session.getPreferencesCache().isAllowShowCoordinates() || CooldownUtils.getDefaultShowCooldown() != CooldownUtils.CooldownType.DISABLED) {
response.skip(); // Client settings title

if (showClientSettings) {
// Client can only see its coordinates if reducedDebugInfo is disabled and coordinates are enabled in geyser config.
if (session.getPreferencesCache().isAllowShowCoordinates()) {
session.getPreferencesCache().setPrefersShowCoordinates(response.next());
session.getPreferencesCache().updateShowCoordinates();
response.skip();
}

if (CooldownUtils.getDefaultShowCooldown() != CooldownUtils.CooldownType.DISABLED) {
CooldownUtils.CooldownType cooldownType = CooldownUtils.CooldownType.VALUES[(int) response.next()];
session.getPreferencesCache().setCooldownPreference(cooldownType);
response.skip();
}
}

if (session.getOpPermissionLevel() >= 2 || session.hasPermission("geyser.settings.server")) {
if (canModifyServer) {
GameMode gameMode = GameMode.values()[(int) response.next()];
if (gameMode != null && gameMode != session.getGameMode()) {
session.getConnector().getWorldManager().setPlayerGameMode(session, gameMode);
Expand All @@ -137,8 +136,8 @@ public static CustomForm buildForm(GeyserSession session) {
}
}

if (session.getOpPermissionLevel() >= 2 || session.hasPermission("geyser.settings.gamerules")) {
for (GameRule gamerule : GameRule.values()) {
if (showGamerules) {
for (GameRule gamerule : GameRule.VALUES) {
if (gamerule.equals(GameRule.UNKNOWN)) {
continue;
}
Expand All @@ -160,4 +159,11 @@ public static CustomForm buildForm(GeyserSession session) {

return builder.build();
}

private static String translateEntry(String key, String locale) {
if (key.startsWith("geyser.")) {
return LanguageUtils.getPlayerLocaleString(key, locale);
}
return LocaleUtils.getLocaleString(key, locale);
}
}

0 comments on commit 215ffc6

Please sign in to comment.