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
|
|
|
|
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
|
|
|
|
2023-12-04 00:44:06 +00:00
|
|
|
// Let's store these to avoid issues
|
|
|
|
boolean showCoordinates = session.getPreferencesCache().isAllowShowCoordinates();
|
|
|
|
boolean cooldownShown = CooldownUtils.getDefaultShowCooldown() != CooldownUtils.CooldownType.DISABLED;
|
|
|
|
boolean customSkulls = session.getGeyser().getConfig().isAllowCustomSkulls();
|
|
|
|
|
2021-05-15 02:48:34 +00:00
|
|
|
// Only show the client title if any of the client settings are available
|
2023-12-04 00:44:06 +00:00
|
|
|
boolean showClientSettings = showCoordinates || cooldownShown || customSkulls;
|
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.
|
2023-12-04 00:44:06 +00:00
|
|
|
if (showCoordinates) {
|
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
|
|
|
|
2023-12-04 00:44:06 +00:00
|
|
|
if (cooldownShown) {
|
2021-05-26 00:17:33 +00:00
|
|
|
DropdownComponent.Builder cooldownDropdown = DropdownComponent.builder("options.attackIndicator");
|
2023-12-04 00:44:06 +00:00
|
|
|
CooldownUtils.CooldownType currentCooldownType = session.getPreferencesCache().getCooldownPreference();
|
|
|
|
cooldownDropdown.option("options.attack.crosshair", currentCooldownType == CooldownUtils.CooldownType.TITLE);
|
|
|
|
cooldownDropdown.option("options.attack.hotbar", currentCooldownType == CooldownUtils.CooldownType.ACTIONBAR);
|
|
|
|
cooldownDropdown.option("options.off", currentCooldownType == CooldownUtils.CooldownType.DISABLED);
|
2021-05-26 00:17:33 +00:00
|
|
|
builder.dropdown(cooldownDropdown);
|
2021-05-15 02:48:34 +00:00
|
|
|
}
|
2021-10-31 03:22:04 +00:00
|
|
|
|
2023-12-04 00:44:06 +00:00
|
|
|
if (customSkulls) {
|
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 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();
|
2022-12-02 19:11:56 +00:00
|
|
|
for (GameRule gamerule : GameRule.VALUES) {
|
2020-08-08 22:41:12 +00:00
|
|
|
// 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.
|
2023-12-04 00:44:06 +00:00
|
|
|
if (showCoordinates) {
|
|
|
|
// In theory, a server could update the gamerule while the client is in the settings menu.
|
|
|
|
// We need to still read the response to update the client's preference, but we don't want to update the gamerule.
|
|
|
|
if (session.getPreferencesCache().isAllowShowCoordinates()) {
|
|
|
|
session.getPreferencesCache().setPrefersShowCoordinates(response.next());
|
|
|
|
session.getPreferencesCache().updateShowCoordinates();
|
|
|
|
}
|
2021-05-26 00:17:33 +00:00
|
|
|
}
|
2020-08-08 22:41:12 +00:00
|
|
|
|
2023-12-04 00:44:06 +00:00
|
|
|
if (cooldownShown) {
|
2021-05-26 00:17:33 +00:00
|
|
|
CooldownUtils.CooldownType cooldownType = CooldownUtils.CooldownType.VALUES[(int) response.next()];
|
|
|
|
session.getPreferencesCache().setCooldownPreference(cooldownType);
|
|
|
|
}
|
2021-10-31 03:22:04 +00:00
|
|
|
|
2023-12-04 00:44:06 +00:00
|
|
|
if (customSkulls) {
|
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 (showGamerules) {
|
|
|
|
for (GameRule gamerule : GameRule.VALUES) {
|
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
|
|
|
}
|