From fb18a6493a0f5935176b85611081374503e1b825 Mon Sep 17 00:00:00 2001 From: ImDaBigBoss <67973871+ImDaBigBoss@users.noreply.github.com> Date: Thu, 1 Apr 2021 06:06:01 +0200 Subject: [PATCH] Add an option for actionbar hit cooldown (#2006) Co-authored-by: Camotoy <20743703+Camotoy@users.noreply.github.com> --- .../geysermc/connector/GeyserConnector.java | 2 +- .../configuration/GeyserConfiguration.java | 2 +- .../GeyserJacksonConfiguration.java | 2 +- .../connector/utils/CooldownUtils.java | 50 ++++++++++++++++--- connector/src/main/resources/config.yml | 3 +- connector/src/main/resources/mappings | 2 +- 6 files changed, 50 insertions(+), 11 deletions(-) diff --git a/connector/src/main/java/org/geysermc/connector/GeyserConnector.java b/connector/src/main/java/org/geysermc/connector/GeyserConnector.java index 26b07583b..ae6dbbe1d 100644 --- a/connector/src/main/java/org/geysermc/connector/GeyserConnector.java +++ b/connector/src/main/java/org/geysermc/connector/GeyserConnector.java @@ -187,7 +187,7 @@ public class GeyserConnector { defaultAuthType = AuthType.getByName(config.getRemote().getAuthType()); - CooldownUtils.setShowCooldown(config.isShowCooldown()); + CooldownUtils.setShowCooldown(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/configuration/GeyserConfiguration.java b/connector/src/main/java/org/geysermc/connector/configuration/GeyserConfiguration.java index 0d879c401..652ec1339 100644 --- a/connector/src/main/java/org/geysermc/connector/configuration/GeyserConfiguration.java +++ b/connector/src/main/java/org/geysermc/connector/configuration/GeyserConfiguration.java @@ -73,7 +73,7 @@ public interface GeyserConfiguration { boolean isAllowThirdPartyEars(); - boolean isShowCooldown(); + String getShowCooldown(); boolean isShowCoordinates(); diff --git a/connector/src/main/java/org/geysermc/connector/configuration/GeyserJacksonConfiguration.java b/connector/src/main/java/org/geysermc/connector/configuration/GeyserJacksonConfiguration.java index cdc0ad239..e9adfe12b 100644 --- a/connector/src/main/java/org/geysermc/connector/configuration/GeyserJacksonConfiguration.java +++ b/connector/src/main/java/org/geysermc/connector/configuration/GeyserJacksonConfiguration.java @@ -95,7 +95,7 @@ public abstract class GeyserJacksonConfiguration implements GeyserConfiguration private boolean allowThirdPartyCapes = true; @JsonProperty("show-cooldown") - private boolean showCooldown = true; + private String showCooldown = "title"; @JsonProperty("show-coordinates") private boolean showCoordinates = true; 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 816b718aa..0b5c2bdd3 100644 --- a/connector/src/main/java/org/geysermc/connector/utils/CooldownUtils.java +++ b/connector/src/main/java/org/geysermc/connector/utils/CooldownUtils.java @@ -26,6 +26,7 @@ package org.geysermc.connector.utils; import com.nukkitx.protocol.bedrock.packet.SetTitlePacket; +import lombok.Getter; import org.geysermc.connector.network.session.GeyserSession; import java.util.concurrent.TimeUnit; @@ -35,10 +36,10 @@ 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 boolean SHOW_COOLDOWN; + private static CooldownType SHOW_COOLDOWN; - public static void setShowCooldown(boolean showCooldown) { - SHOW_COOLDOWN = showCooldown; + public static void setShowCooldown(String showCooldown) { + SHOW_COOLDOWN = CooldownType.getByName(showCooldown); } /** @@ -46,7 +47,7 @@ public class CooldownUtils { * @param session GeyserSession */ public static void sendCooldown(GeyserSession session) { - if (!SHOW_COOLDOWN) return; + if (SHOW_COOLDOWN == 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(); @@ -67,7 +68,11 @@ public class CooldownUtils { 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(); - titlePacket.setType(SetTitlePacket.Type.SUBTITLE); + if (SHOW_COOLDOWN == CooldownType.ACTIONBAR) { + titlePacket.setType(SetTitlePacket.Type.ACTIONBAR); + } else { + titlePacket.setType(SetTitlePacket.Type.SUBTITLE); + } titlePacket.setText(getTitle(session)); titlePacket.setFadeInTime(0); titlePacket.setFadeOutTime(5); @@ -77,7 +82,11 @@ public class CooldownUtils { session.getConnector().getGeneralThreadPool().schedule(() -> computeCooldown(session, lastHitTime), 50, TimeUnit.MILLISECONDS); // Updated per tick. 1000 divided by 20 ticks equals 50 } else { SetTitlePacket removeTitlePacket = new SetTitlePacket(); - removeTitlePacket.setType(SetTitlePacket.Type.SUBTITLE); + if (SHOW_COOLDOWN == CooldownType.ACTIONBAR) { + removeTitlePacket.setType(SetTitlePacket.Type.ACTIONBAR); + } else { + removeTitlePacket.setType(SetTitlePacket.Type.SUBTITLE); + } removeTitlePacket.setText(" "); session.sendUpstreamPacket(removeTitlePacket); } @@ -114,4 +123,33 @@ public class CooldownUtils { } return builder.toString(); } + + @Getter + public enum CooldownType { + TITLE, + ACTIONBAR, + DISABLED; + + public static final CooldownType[] VALUES = values(); + + /** + * Convert the CooldownType string (from config) to the enum, TITLE on fail + * + * @param name CooldownType string + * + * @return The converted CooldownType + */ + public static CooldownType getByName(String name) { + if (name.equalsIgnoreCase("true")) { // Backwards config compatibility + return CooldownType.TITLE; + } + + for (CooldownType type : VALUES) { + if (type.name().equalsIgnoreCase(name)) { + return type; + } + } + return DISABLED; + } + } } diff --git a/connector/src/main/resources/config.yml b/connector/src/main/resources/config.yml index 847eded1a..6ccddd401 100644 --- a/connector/src/main/resources/config.yml +++ b/connector/src/main/resources/config.yml @@ -112,7 +112,8 @@ allow-third-party-capes: true allow-third-party-ears: false # Allow a fake cooldown indicator to be sent. Bedrock players do not see a cooldown as they still use 1.8 combat -show-cooldown: true +# Can be title, actionbar or false +show-cooldown: title # Controls if coordinates are shown to players. show-coordinates: true diff --git a/connector/src/main/resources/mappings b/connector/src/main/resources/mappings index 216e90086..2ce794e21 160000 --- a/connector/src/main/resources/mappings +++ b/connector/src/main/resources/mappings @@ -1 +1 @@ -Subproject commit 216e9008678a761b3885808f4f3d43000404381b +Subproject commit 2ce794e21a0212865059e7551db893c28843620a