From 3e3b8faf16ad09fa8aeb436cf1de53b1c90f82cd Mon Sep 17 00:00:00 2001
From: Konicai <71294714+Konicai@users.noreply.github.com>
Date: Fri, 14 May 2021 22:48:34 -0400
Subject: [PATCH] Add client setting for cooldown type, refactor
show-coordinates (#2193)
---
.../geysermc/connector/GeyserConnector.java | 2 +-
.../network/session/GeyserSession.java | 4 +-
.../session/cache/PreferencesCache.java | 71 +++++++++++++++++++
.../network/session/cache/WorldCache.java | 21 ------
.../connector/utils/CooldownUtils.java | 31 +++++---
.../connector/utils/SettingsUtils.java | 37 +++++++---
connector/src/main/resources/languages | 2 +-
7 files changed, 125 insertions(+), 43 deletions(-)
create mode 100644 connector/src/main/java/org/geysermc/connector/network/session/cache/PreferencesCache.java
diff --git a/connector/src/main/java/org/geysermc/connector/GeyserConnector.java b/connector/src/main/java/org/geysermc/connector/GeyserConnector.java
index 03bf7538c..d2caac216 100644
--- a/connector/src/main/java/org/geysermc/connector/GeyserConnector.java
+++ b/connector/src/main/java/org/geysermc/connector/GeyserConnector.java
@@ -188,7 +188,7 @@ public class GeyserConnector {
defaultAuthType = AuthType.getByName(config.getRemote().getAuthType());
- CooldownUtils.setShowCooldown(config.getShowCooldown());
+ CooldownUtils.setDefaultShowCooldown(config.getShowCooldown());
DimensionUtils.changeBedrockNetherId(config.isAboveBedrockNetherBuilding()); // Apply End dimension ID workaround to Nether
SkullBlockEntityTranslator.ALLOW_CUSTOM_SKULLS = config.isAllowCustomSkulls();
diff --git a/connector/src/main/java/org/geysermc/connector/network/session/GeyserSession.java b/connector/src/main/java/org/geysermc/connector/network/session/GeyserSession.java
index 28aff40b9..3748a3ba2 100644
--- a/connector/src/main/java/org/geysermc/connector/network/session/GeyserSession.java
+++ b/connector/src/main/java/org/geysermc/connector/network/session/GeyserSession.java
@@ -144,6 +144,7 @@ public class GeyserSession implements CommandSender {
private ChunkCache chunkCache;
private EntityCache entityCache;
private EntityEffectCache effectCache;
+ private final PreferencesCache preferencesCache;
private final TagCache tagCache;
private WorldCache worldCache;
private WindowCache windowCache;
@@ -451,6 +452,7 @@ public class GeyserSession implements CommandSender {
this.chunkCache = new ChunkCache(this);
this.entityCache = new EntityCache(this);
this.effectCache = new EntityEffectCache();
+ this.preferencesCache = new PreferencesCache(this);
this.tagCache = new TagCache();
this.worldCache = new WorldCache(this);
this.windowCache = new WindowCache(this);
@@ -1224,7 +1226,7 @@ public class GeyserSession implements CommandSender {
public void setReducedDebugInfo(boolean value) {
reducedDebugInfo = value;
// Set the showCoordinates data. This is done because updateShowCoordinates() uses this gamerule as a variable.
- getWorldCache().updateShowCoordinates();
+ preferencesCache.updateShowCoordinates();
}
/**
diff --git a/connector/src/main/java/org/geysermc/connector/network/session/cache/PreferencesCache.java b/connector/src/main/java/org/geysermc/connector/network/session/cache/PreferencesCache.java
new file mode 100644
index 000000000..d477066c2
--- /dev/null
+++ b/connector/src/main/java/org/geysermc/connector/network/session/cache/PreferencesCache.java
@@ -0,0 +1,71 @@
+/*
+ * Copyright (c) 2019-2021 GeyserMC. http://geysermc.org
+ *
+ * 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
+ */
+
+package org.geysermc.connector.network.session.cache;
+
+import lombok.Getter;
+import lombok.Setter;
+import org.geysermc.connector.configuration.GeyserConfiguration;
+import org.geysermc.connector.network.session.GeyserSession;
+import org.geysermc.connector.utils.CooldownUtils;
+
+@Getter
+public class PreferencesCache {
+ private final GeyserSession session;
+
+ /**
+ * True if the client prefers being shown their coordinates, regardless if they're being shown or not.
+ * This will be true everytime the client joins the server because neither the client nor server store the preference permanently.
+ */
+ @Setter
+ private boolean prefersShowCoordinates = true;
+ /**
+ * If the client's preference will be ignored, this will return false.
+ */
+ private boolean allowShowCoordinates;
+
+ /**
+ * Which CooldownType the client prefers. Initially set to {@link CooldownUtils#getDefaultShowCooldown()}.
+ */
+ @Setter
+ private CooldownUtils.CooldownType cooldownPreference = CooldownUtils.getDefaultShowCooldown();
+
+ public PreferencesCache(GeyserSession session) {
+ this.session = session;
+ }
+
+ /**
+ * Tell the client to hide or show the coordinates.
+ *
+ * If {@link #prefersShowCoordinates} is true, coordinates will be shown, unless either of the following conditions apply:
+ *
+ * {@link GeyserSession#reducedDebugInfo} is enabled
+ * {@link GeyserConfiguration#isShowCoordinates()} is disabled
+ */
+ public void updateShowCoordinates() {
+ allowShowCoordinates = !session.isReducedDebugInfo() && session.getConnector().getConfig().isShowCoordinates();
+ session.sendGameRule("showcoordinates", allowShowCoordinates && prefersShowCoordinates);
+ }
+}
diff --git a/connector/src/main/java/org/geysermc/connector/network/session/cache/WorldCache.java b/connector/src/main/java/org/geysermc/connector/network/session/cache/WorldCache.java
index 84678c211..4a2939621 100644
--- a/connector/src/main/java/org/geysermc/connector/network/session/cache/WorldCache.java
+++ b/connector/src/main/java/org/geysermc/connector/network/session/cache/WorldCache.java
@@ -28,7 +28,6 @@ package org.geysermc.connector.network.session.cache;
import com.github.steveice10.mc.protocol.data.game.setting.Difficulty;
import lombok.Getter;
import lombok.Setter;
-import org.geysermc.connector.configuration.GeyserConfiguration;
import org.geysermc.connector.network.session.GeyserSession;
import org.geysermc.connector.scoreboard.Objective;
import org.geysermc.connector.scoreboard.Scoreboard;
@@ -40,13 +39,6 @@ public class WorldCache {
@Setter
private Difficulty difficulty = Difficulty.EASY;
- /**
- * True if the client prefers being shown their coordinates, regardless if they're being shown or not.
- * This will be true everytime the client joins the server because neither the client nor server store the preference permanently.
- */
- @Setter
- private boolean prefersShowCoordinates = true;
-
private Scoreboard scoreboard;
private final ScoreboardUpdater scoreboardUpdater;
@@ -71,17 +63,4 @@ public class WorldCache {
int pps = scoreboardUpdater.getPacketsPerSecond();
return Math.max(pps, pendingPps);
}
-
- /**
- * Tell the client to hide or show the coordinates.
- *
- * If {@link #prefersShowCoordinates} is true, coordinates will be shown, unless either of the following conditions apply:
- *
- * {@link GeyserSession#reducedDebugInfo} is enabled
- * {@link GeyserConfiguration#isShowCoordinates()} is disabled
- */
- public void updateShowCoordinates() {
- boolean allowShowCoordinates = !session.isReducedDebugInfo() && session.getConnector().getConfig().isShowCoordinates();
- session.sendGameRule("showcoordinates", allowShowCoordinates && prefersShowCoordinates);
- }
}
\ No newline at end of file
diff --git a/connector/src/main/java/org/geysermc/connector/utils/CooldownUtils.java b/connector/src/main/java/org/geysermc/connector/utils/CooldownUtils.java
index 0b5c2bdd3..615143589 100644
--- a/connector/src/main/java/org/geysermc/connector/utils/CooldownUtils.java
+++ b/connector/src/main/java/org/geysermc/connector/utils/CooldownUtils.java
@@ -28,6 +28,7 @@ package org.geysermc.connector.utils;
import com.nukkitx.protocol.bedrock.packet.SetTitlePacket;
import lombok.Getter;
import org.geysermc.connector.network.session.GeyserSession;
+import org.geysermc.connector.network.session.cache.PreferencesCache;
import java.util.concurrent.TimeUnit;
@@ -36,18 +37,25 @@ import java.util.concurrent.TimeUnit;
* Much of the work here is from the wonderful folks from ViaRewind: https://github.com/ViaVersion/ViaRewind
*/
public class CooldownUtils {
- private static CooldownType SHOW_COOLDOWN;
+ private static CooldownType DEFAULT_SHOW_COOLDOWN;
- public static void setShowCooldown(String showCooldown) {
- SHOW_COOLDOWN = CooldownType.getByName(showCooldown);
+ public static void setDefaultShowCooldown(String showCooldown) {
+ DEFAULT_SHOW_COOLDOWN = CooldownType.getByName(showCooldown);
+ }
+
+ public static CooldownType getDefaultShowCooldown() {
+ return DEFAULT_SHOW_COOLDOWN;
}
/**
- * Starts sending the fake cooldown to the Bedrock client.
+ * Starts sending the fake cooldown to the Bedrock client. If the cooldown is not disabled, the sent type is {@link PreferencesCache#getCooldownPreference()}
* @param session GeyserSession
*/
public static void sendCooldown(GeyserSession session) {
- if (SHOW_COOLDOWN == CooldownType.DISABLED) return;
+ if (DEFAULT_SHOW_COOLDOWN == CooldownType.DISABLED) return;
+ CooldownType sessionPreference = session.getPreferencesCache().getCooldownPreference();
+ if (sessionPreference == CooldownType.DISABLED) return;
+
if (session.getAttackSpeed() == 0.0 || session.getAttackSpeed() > 20) return; // 0.0 usually happens on login and causes issues with visuals; anything above 20 means a plugin like OldCombatMechanics is being used
// Needs to be sent or no subtitle packet is recognized by the client
SetTitlePacket titlePacket = new SetTitlePacket();
@@ -56,19 +64,20 @@ public class CooldownUtils {
session.sendUpstreamPacket(titlePacket);
session.setLastHitTime(System.currentTimeMillis());
long lastHitTime = session.getLastHitTime(); // Used later to prevent multiple scheduled cooldown threads
- computeCooldown(session, lastHitTime);
+ computeCooldown(session, sessionPreference, lastHitTime);
}
/**
* Keeps updating the cooldown until the bar is complete.
* @param session GeyserSession
+ * @param sessionPreference The type of cooldown the client prefers
* @param lastHitTime The time of the last hit. Used to gauge how long the cooldown is taking.
*/
- private static void computeCooldown(GeyserSession session, long lastHitTime) {
+ private static void computeCooldown(GeyserSession session, CooldownType sessionPreference, long lastHitTime) {
if (session.isClosed()) return; // Don't run scheduled tasks if the client left
if (lastHitTime != session.getLastHitTime()) return; // Means another cooldown has started so there's no need to continue this one
SetTitlePacket titlePacket = new SetTitlePacket();
- if (SHOW_COOLDOWN == CooldownType.ACTIONBAR) {
+ if (sessionPreference == CooldownType.ACTIONBAR) {
titlePacket.setType(SetTitlePacket.Type.ACTIONBAR);
} else {
titlePacket.setType(SetTitlePacket.Type.SUBTITLE);
@@ -79,10 +88,10 @@ public class CooldownUtils {
titlePacket.setStayTime(2);
session.sendUpstreamPacket(titlePacket);
if (hasCooldown(session)) {
- session.getConnector().getGeneralThreadPool().schedule(() -> computeCooldown(session, lastHitTime), 50, TimeUnit.MILLISECONDS); // Updated per tick. 1000 divided by 20 ticks equals 50
+ session.getConnector().getGeneralThreadPool().schedule(() -> computeCooldown(session, sessionPreference, lastHitTime), 50, TimeUnit.MILLISECONDS); // Updated per tick. 1000 divided by 20 ticks equals 50
} else {
SetTitlePacket removeTitlePacket = new SetTitlePacket();
- if (SHOW_COOLDOWN == CooldownType.ACTIONBAR) {
+ if (sessionPreference == CooldownType.ACTIONBAR) {
removeTitlePacket.setType(SetTitlePacket.Type.ACTIONBAR);
} else {
removeTitlePacket.setType(SetTitlePacket.Type.SUBTITLE);
@@ -133,7 +142,7 @@ public class CooldownUtils {
public static final CooldownType[] VALUES = values();
/**
- * Convert the CooldownType string (from config) to the enum, TITLE on fail
+ * Convert the CooldownType string (from config) to the enum, DISABLED on fail
*
* @param name CooldownType string
*
diff --git a/connector/src/main/java/org/geysermc/connector/utils/SettingsUtils.java b/connector/src/main/java/org/geysermc/connector/utils/SettingsUtils.java
index 1d06c8a0f..d0cfce862 100644
--- a/connector/src/main/java/org/geysermc/connector/utils/SettingsUtils.java
+++ b/connector/src/main/java/org/geysermc/connector/utils/SettingsUtils.java
@@ -57,11 +57,24 @@ public class SettingsUtils {
CustomFormBuilder builder = new CustomFormBuilder(LanguageUtils.getPlayerLocaleString("geyser.settings.title.main", language));
builder.setIcon(new FormImage(FormImage.FormImageType.PATH, "textures/ui/settings_glyph_color_2x.png"));
- // Client can only see its coordinates if reducedDebugInfo is disabled and coordinates are enabled in geyser config.
- if (!session.isReducedDebugInfo() && session.getConnector().getConfig().isShowCoordinates()) {
+ // Only show the client title if any of the client settings are available
+ if (session.getPreferencesCache().isAllowShowCoordinates() || CooldownUtils.getDefaultShowCooldown() != CooldownUtils.CooldownType.DISABLED) {
builder.addComponent(new LabelComponent(LanguageUtils.getPlayerLocaleString("geyser.settings.title.client", language)));
- builder.addComponent(new ToggleComponent(LanguageUtils.getPlayerLocaleString("geyser.settings.option.coordinates", language), session.getWorldCache().isPrefersShowCoordinates()));
+ // Client can only see its coordinates if reducedDebugInfo is disabled and coordinates are enabled in geyser config.
+ if (session.getPreferencesCache().isAllowShowCoordinates()) {
+ builder.addComponent(new ToggleComponent(LanguageUtils.getPlayerLocaleString("geyser.settings.option.coordinates", language), session.getPreferencesCache().isPrefersShowCoordinates()));
+ }
+
+ if (CooldownUtils.getDefaultShowCooldown() != CooldownUtils.CooldownType.DISABLED) {
+ DropdownComponent cooldownDropdown = new DropdownComponent();
+ cooldownDropdown.setText(LocaleUtils.getLocaleString("options.attackIndicator", language));
+ cooldownDropdown.setOptions(new ArrayList<>());
+ cooldownDropdown.addOption(LocaleUtils.getLocaleString("options.attack.crosshair", language), session.getPreferencesCache().getCooldownPreference() == CooldownUtils.CooldownType.TITLE);
+ cooldownDropdown.addOption(LocaleUtils.getLocaleString("options.attack.hotbar", language), session.getPreferencesCache().getCooldownPreference() == CooldownUtils.CooldownType.ACTIONBAR);
+ cooldownDropdown.addOption(LocaleUtils.getLocaleString("options.off", language), session.getPreferencesCache().getCooldownPreference() == CooldownUtils.CooldownType.DISABLED);
+ builder.addComponent(cooldownDropdown);
+ }
}
@@ -121,13 +134,21 @@ public class SettingsUtils {
}
int offset = 0;
- // Client can only see its coordinates if reducedDebugInfo is disabled and coordinates are enabled in geyser config.
- if (!session.isReducedDebugInfo() && session.getConnector().getConfig().isShowCoordinates()) {
+ if (session.getPreferencesCache().isAllowShowCoordinates() || CooldownUtils.getDefaultShowCooldown() != CooldownUtils.CooldownType.DISABLED) {
offset++; // Client settings title
- session.getWorldCache().setPrefersShowCoordinates(settingsResponse.getToggleResponses().get(offset));
- session.getWorldCache().updateShowCoordinates();
- offset++;
+ // Client can only see its coordinates if reducedDebugInfo is disabled and coordinates are enabled in geyser config.
+ if (session.getPreferencesCache().isAllowShowCoordinates()) {
+ session.getPreferencesCache().setPrefersShowCoordinates(settingsResponse.getToggleResponses().get(offset));
+ session.getPreferencesCache().updateShowCoordinates();
+ offset++;
+ }
+
+ if (CooldownUtils.getDefaultShowCooldown() != CooldownUtils.CooldownType.DISABLED) {
+ CooldownUtils.CooldownType cooldownType = CooldownUtils.CooldownType.VALUES[settingsResponse.getDropdownResponses().get(offset).getElementID()];
+ session.getPreferencesCache().setCooldownPreference(cooldownType);
+ offset++;
+ }
}
if (session.getOpPermissionLevel() >= 2 || session.hasPermission("geyser.settings.server")) {
diff --git a/connector/src/main/resources/languages b/connector/src/main/resources/languages
index 96e7ed66c..9b08df518 160000
--- a/connector/src/main/resources/languages
+++ b/connector/src/main/resources/languages
@@ -1 +1 @@
-Subproject commit 96e7ed66ccdafea0cc991b8004566d448e8f6e6a
+Subproject commit 9b08df51898fd71ee24e7accdfbe56f164b5c539