2020-08-08 22:41:12 +00:00
|
|
|
/*
|
2022-01-01 19:03:05 +00:00
|
|
|
* Copyright (c) 2019-2022 GeyserMC. http://geysermc.org
|
2020-08-08 22:41:12 +00:00
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*
|
|
|
|
* @author GeyserMC
|
|
|
|
* @link https://github.com/GeyserMC/Geyser
|
|
|
|
*/
|
|
|
|
|
2021-11-20 23:29:46 +00:00
|
|
|
package org.geysermc.geyser.util;
|
2020-08-08 22:41:12 +00:00
|
|
|
|
|
|
|
import com.github.steveice10.mc.protocol.data.game.entity.player.GameMode;
|
|
|
|
import com.github.steveice10.mc.protocol.data.game.setting.Difficulty;
|
2022-01-22 11:20:52 +00:00
|
|
|
import org.geysermc.cumulus.component.DropdownComponent;
|
|
|
|
import org.geysermc.cumulus.form.CustomForm;
|
2021-11-20 21:34:30 +00:00
|
|
|
import org.geysermc.geyser.GeyserImpl;
|
2021-11-20 23:29:46 +00:00
|
|
|
import org.geysermc.geyser.level.GameRule;
|
|
|
|
import org.geysermc.geyser.level.WorldManager;
|
2022-01-22 11:20:52 +00:00
|
|
|
import org.geysermc.geyser.session.GeyserSession;
|
2021-11-20 23:29:46 +00:00
|
|
|
import org.geysermc.geyser.text.GeyserLocale;
|
|
|
|
import org.geysermc.geyser.text.MinecraftLocale;
|
2020-08-08 22:41:12 +00:00
|
|
|
|
|
|
|
public class SettingsUtils {
|
|
|
|
/**
|
|
|
|
* Build a settings form for the given session and store it for later
|
|
|
|
*
|
|
|
|
* @param session The session to build the form for
|
|
|
|
*/
|
2021-11-22 19:52:26 +00:00
|
|
|
public static CustomForm buildForm(GeyserSession session) {
|
2020-08-08 22:41:12 +00:00
|
|
|
// Cache the language for cleaner access
|
2022-01-16 21:09:53 +00:00
|
|
|
String language = session.locale();
|
2020-08-08 22:41:12 +00:00
|
|
|
|
2020-10-29 23:58:17 +00:00
|
|
|
CustomForm.Builder builder = CustomForm.builder()
|
2021-06-27 23:57:53 +00:00
|
|
|
.translator(SettingsUtils::translateEntry, language)
|
2020-10-29 23:58:17 +00:00
|
|
|
.title("geyser.settings.title.main")
|
2021-05-01 10:16:25 +00:00
|
|
|
.iconPath("textures/ui/settings_glyph_color_2x.png");
|
2020-08-08 22:41:12 +00:00
|
|
|
|
2021-05-15 02:48:34 +00:00
|
|
|
// Only show the client title if any of the client settings are available
|
2021-10-31 03:22:04 +00:00
|
|
|
boolean showClientSettings = session.getPreferencesCache().isAllowShowCoordinates()
|
|
|
|
|| CooldownUtils.getDefaultShowCooldown() != CooldownUtils.CooldownType.DISABLED
|
2021-11-20 21:34:30 +00:00
|
|
|
|| session.getGeyser().getConfig().isAllowCustomSkulls();
|
2021-10-31 03:22:04 +00:00
|
|
|
|
2021-06-27 23:57:53 +00:00
|
|
|
if (showClientSettings) {
|
2021-05-26 00:17:33 +00:00
|
|
|
builder.label("geyser.settings.title.client");
|
2021-04-21 20:57:01 +00:00
|
|
|
|
2021-05-15 02:48:34 +00:00
|
|
|
// Client can only see its coordinates if reducedDebugInfo is disabled and coordinates are enabled in geyser config.
|
|
|
|
if (session.getPreferencesCache().isAllowShowCoordinates()) {
|
2022-03-23 03:01:00 +00:00
|
|
|
builder.toggle("%createWorldScreen.showCoordinates", session.getPreferencesCache().isPrefersShowCoordinates());
|
2021-05-15 02:48:34 +00:00
|
|
|
}
|
2020-08-08 22:41:12 +00:00
|
|
|
|
2021-05-15 02:48:34 +00:00
|
|
|
if (CooldownUtils.getDefaultShowCooldown() != CooldownUtils.CooldownType.DISABLED) {
|
2021-05-26 00:17:33 +00:00
|
|
|
DropdownComponent.Builder cooldownDropdown = DropdownComponent.builder("options.attackIndicator");
|
|
|
|
cooldownDropdown.option("options.attack.crosshair", session.getPreferencesCache().getCooldownPreference() == CooldownUtils.CooldownType.TITLE);
|
|
|
|
cooldownDropdown.option("options.attack.hotbar", session.getPreferencesCache().getCooldownPreference() == CooldownUtils.CooldownType.ACTIONBAR);
|
|
|
|
cooldownDropdown.option("options.off", session.getPreferencesCache().getCooldownPreference() == CooldownUtils.CooldownType.DISABLED);
|
|
|
|
builder.dropdown(cooldownDropdown);
|
2021-05-15 02:48:34 +00:00
|
|
|
}
|
2021-10-31 03:22:04 +00:00
|
|
|
|
2021-11-20 21:34:30 +00:00
|
|
|
if (session.getGeyser().getConfig().isAllowCustomSkulls()) {
|
2021-10-31 03:22:04 +00:00
|
|
|
builder.toggle("geyser.settings.option.customSkulls", session.getPreferencesCache().isPrefersCustomSkulls());
|
|
|
|
}
|
2021-04-21 20:57:01 +00:00
|
|
|
}
|
2020-08-08 22:41:12 +00:00
|
|
|
|
2021-06-27 23:57:53 +00:00
|
|
|
boolean canModifyServer = session.getOpPermissionLevel() >= 2 || session.hasPermission("geyser.settings.server");
|
|
|
|
if (canModifyServer) {
|
2020-10-29 23:58:17 +00:00
|
|
|
builder.label("geyser.settings.title.server");
|
2020-08-08 22:41:12 +00:00
|
|
|
|
2020-10-29 23:58:17 +00:00
|
|
|
DropdownComponent.Builder gamemodeDropdown = DropdownComponent.builder("%createWorldScreen.gameMode.personal");
|
2020-08-08 22:41:12 +00:00
|
|
|
for (GameMode gamemode : GameMode.values()) {
|
2020-10-29 23:58:17 +00:00
|
|
|
gamemodeDropdown.option("selectWorld.gameMode." + gamemode.name().toLowerCase(), session.getGameMode() == gamemode);
|
2020-08-08 22:41:12 +00:00
|
|
|
}
|
2020-10-29 23:58:17 +00:00
|
|
|
builder.dropdown(gamemodeDropdown);
|
2020-08-08 22:41:12 +00:00
|
|
|
|
2020-10-29 23:58:17 +00:00
|
|
|
DropdownComponent.Builder difficultyDropdown = DropdownComponent.builder("%options.difficulty");
|
2020-08-08 22:41:12 +00:00
|
|
|
for (Difficulty difficulty : Difficulty.values()) {
|
2020-10-29 23:58:17 +00:00
|
|
|
difficultyDropdown.option("%options.difficulty." + difficulty.name().toLowerCase(), session.getWorldCache().getDifficulty() == difficulty);
|
2020-08-08 22:41:12 +00:00
|
|
|
}
|
2020-10-29 23:58:17 +00:00
|
|
|
builder.dropdown(difficultyDropdown);
|
2020-08-08 22:41:12 +00:00
|
|
|
}
|
|
|
|
|
2021-06-27 23:57:53 +00:00
|
|
|
boolean showGamerules = session.getOpPermissionLevel() >= 2 || session.hasPermission("geyser.settings.gamerules");
|
|
|
|
if (showGamerules) {
|
2020-10-29 23:58:17 +00:00
|
|
|
builder.label("geyser.settings.title.game_rules")
|
2021-11-20 23:29:46 +00:00
|
|
|
.translator(MinecraftLocale::getLocaleString); // we need translate gamerules next
|
2020-10-29 23:58:17 +00:00
|
|
|
|
2021-11-20 21:34:30 +00:00
|
|
|
WorldManager worldManager = GeyserImpl.getInstance().getWorldManager();
|
2020-08-08 22:41:12 +00:00
|
|
|
for (GameRule gamerule : GameRule.values()) {
|
|
|
|
if (gamerule.equals(GameRule.UNKNOWN)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add the relevant form item based on the gamerule type
|
|
|
|
if (Boolean.class.equals(gamerule.getType())) {
|
2020-10-29 23:58:17 +00:00
|
|
|
builder.toggle("gamerule." + gamerule.getJavaID(), worldManager.getGameRuleBool(session, gamerule));
|
2020-08-08 22:41:12 +00:00
|
|
|
} else if (Integer.class.equals(gamerule.getType())) {
|
2020-10-29 23:58:17 +00:00
|
|
|
builder.input("gamerule." + gamerule.getJavaID(), "", String.valueOf(worldManager.getGameRuleInt(session, gamerule)));
|
2020-08-08 22:41:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-29 21:39:40 +00:00
|
|
|
builder.validResultHandler((response) -> {
|
2021-06-27 23:57:53 +00:00
|
|
|
if (showClientSettings) {
|
2021-05-26 00:17:33 +00:00
|
|
|
// 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();
|
|
|
|
}
|
2020-08-08 22:41:12 +00:00
|
|
|
|
2021-05-26 00:17:33 +00:00
|
|
|
if (CooldownUtils.getDefaultShowCooldown() != CooldownUtils.CooldownType.DISABLED) {
|
|
|
|
CooldownUtils.CooldownType cooldownType = CooldownUtils.CooldownType.VALUES[(int) response.next()];
|
|
|
|
session.getPreferencesCache().setCooldownPreference(cooldownType);
|
|
|
|
}
|
2021-10-31 03:22:04 +00:00
|
|
|
|
2021-11-20 21:34:30 +00:00
|
|
|
if (session.getGeyser().getConfig().isAllowCustomSkulls()) {
|
2021-10-31 03:22:04 +00:00
|
|
|
session.getPreferencesCache().setPrefersCustomSkulls(response.next());
|
|
|
|
}
|
2020-08-08 22:41:12 +00:00
|
|
|
}
|
|
|
|
|
2021-06-27 23:57:53 +00:00
|
|
|
if (canModifyServer) {
|
2020-10-29 23:58:17 +00:00
|
|
|
GameMode gameMode = GameMode.values()[(int) response.next()];
|
|
|
|
if (gameMode != null && gameMode != session.getGameMode()) {
|
2021-11-20 21:34:30 +00:00
|
|
|
session.getGeyser().getWorldManager().setPlayerGameMode(session, gameMode);
|
2020-10-29 23:58:17 +00:00
|
|
|
}
|
2020-08-08 22:41:12 +00:00
|
|
|
|
2020-10-29 23:58:17 +00:00
|
|
|
Difficulty difficulty = Difficulty.values()[(int) response.next()];
|
|
|
|
if (difficulty != null && difficulty != session.getWorldCache().getDifficulty()) {
|
2021-11-20 21:34:30 +00:00
|
|
|
session.getGeyser().getWorldManager().setDifficulty(session, difficulty);
|
2020-08-08 22:41:12 +00:00
|
|
|
}
|
2020-10-29 23:58:17 +00:00
|
|
|
}
|
2020-08-08 22:41:12 +00:00
|
|
|
|
2021-06-27 23:57:53 +00:00
|
|
|
if (showGamerules) {
|
|
|
|
for (GameRule gamerule : GameRule.VALUES) {
|
2020-10-29 23:58:17 +00:00
|
|
|
if (gamerule.equals(GameRule.UNKNOWN)) {
|
|
|
|
continue;
|
2020-08-08 22:41:12 +00:00
|
|
|
}
|
2020-10-29 23:58:17 +00:00
|
|
|
|
|
|
|
if (Boolean.class.equals(gamerule.getType())) {
|
|
|
|
boolean value = response.next();
|
2021-11-20 21:34:30 +00:00
|
|
|
if (value != session.getGeyser().getWorldManager().getGameRuleBool(session, gamerule)) {
|
|
|
|
session.getGeyser().getWorldManager().setGameRule(session, gamerule.getJavaID(), value);
|
2020-10-29 23:58:17 +00:00
|
|
|
}
|
|
|
|
} else if (Integer.class.equals(gamerule.getType())) {
|
|
|
|
int value = Integer.parseInt(response.next());
|
2021-11-20 21:34:30 +00:00
|
|
|
if (value != session.getGeyser().getWorldManager().getGameRuleInt(session, gamerule)) {
|
|
|
|
session.getGeyser().getWorldManager().setGameRule(session, gamerule.getJavaID(), value);
|
2020-10-29 23:58:17 +00:00
|
|
|
}
|
2020-08-08 22:41:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-10-29 23:58:17 +00:00
|
|
|
});
|
2020-08-08 22:41:12 +00:00
|
|
|
|
2020-10-29 23:58:17 +00:00
|
|
|
return builder.build();
|
2020-08-08 22:41:12 +00:00
|
|
|
}
|
2021-06-27 23:57:53 +00:00
|
|
|
|
|
|
|
private static String translateEntry(String key, String locale) {
|
2022-03-23 03:01:00 +00:00
|
|
|
if (key.startsWith("%")) {
|
|
|
|
// Bedrock will translate
|
|
|
|
return key;
|
|
|
|
}
|
2021-06-27 23:57:53 +00:00
|
|
|
if (key.startsWith("geyser.")) {
|
2021-11-20 23:29:46 +00:00
|
|
|
return GeyserLocale.getPlayerLocaleString(key, locale);
|
2021-06-27 23:57:53 +00:00
|
|
|
}
|
2021-11-20 23:29:46 +00:00
|
|
|
return MinecraftLocale.getLocaleString(key, locale);
|
2021-06-27 23:57:53 +00:00
|
|
|
}
|
2020-08-08 22:41:12 +00:00
|
|
|
}
|