From d2a5dbcaaa330a531570e8909efbf69d33c38bbf Mon Sep 17 00:00:00 2001 From: Apehum Date: Sun, 26 Mar 2023 00:11:46 +0800 Subject: [PATCH] fix: don't re-initialize WhisperActivation --- .../plo/voice/whisper/WhisperActivation.java | 26 +++++++++++-------- .../su/plo/voice/whisper/WhisperAddon.java | 8 +++--- 2 files changed, 18 insertions(+), 16 deletions(-) diff --git a/src/main/java/su/plo/voice/whisper/WhisperActivation.java b/src/main/java/su/plo/voice/whisper/WhisperActivation.java index 6fdf3a5..ccc1869 100644 --- a/src/main/java/su/plo/voice/whisper/WhisperActivation.java +++ b/src/main/java/su/plo/voice/whisper/WhisperActivation.java @@ -26,10 +26,11 @@ public final class WhisperActivation { + private static final String ACTIVATION_NAME = "whisper"; + private final PlasmoVoiceServer voiceServer; private final WhisperAddon addon; - private final WhisperConfig config; private final Set playerWhisperVisualized = Sets.newCopyOnWriteArraySet(); @@ -39,15 +40,16 @@ public WhisperActivation(@NotNull PlasmoVoiceServer voiceServer, @NotNull WhisperAddon addon) { this.voiceServer = voiceServer; this.addon = addon; - this.config = addon.getConfig(); } public void register() { unregister(); + WhisperConfig config = addon.getConfig(); + ServerActivation.Builder builder = voiceServer.getActivationManager().createBuilder( addon, - "whisper", + ACTIVATION_NAME, "pv.activation.whisper", "plasmovoice:textures/icons/microphone_whisper.png", "pv.activation.whisper", @@ -74,7 +76,7 @@ public boolean checkRequirements(@NotNull VoicePlayer player, @NotNull PlayerAud ServerSourceLine sourceLine = voiceServer.getSourceLineManager().createBuilder( addon, - "whisper", + ACTIVATION_NAME, "pv.activation.whisper", "plasmovoice:textures/icons/speaker_whisper.png", config.sourceLineWeight() @@ -82,9 +84,7 @@ public boolean checkRequirements(@NotNull VoicePlayer player, @NotNull PlayerAud activation.onPlayerActivationStart(this::onActivationStart); - if (proximityHelper != null) { - voiceServer.getEventBus().unregister(this, proximityHelper); - } + if (proximityHelper != null) voiceServer.getEventBus().unregister(addon, proximityHelper); this.proximityHelper = new ProximityServerActivationHelper( voiceServer, @@ -138,10 +138,11 @@ public void onClientDisconnect(@NotNull UdpClientDisconnectedEvent event) { } private void unregister() { + voiceServer.getActivationManager().unregister(ACTIVATION_NAME); + voiceServer.getSourceLineManager().unregister(ACTIVATION_NAME); + if (proximityHelper == null) return; - voiceServer.getActivationManager().unregister(proximityHelper.getActivation()); - voiceServer.getSourceLineManager().unregister(proximityHelper.getSourceLine()); voiceServer.getEventBus().unregister(addon, proximityHelper); this.proximityHelper = null; } @@ -149,7 +150,10 @@ private void unregister() { private void onActivationStart(@NotNull VoicePlayer player) { if (!playerWhisperVisualized.contains(player.getInstance().getUUID())) { playerWhisperVisualized.add(player.getInstance().getUUID()); - player.visualizeDistance(calculateWhisperDistance((VoiceServerPlayer) player), config.visualizeDistanceHexColor()); + player.visualizeDistance( + calculateWhisperDistance((VoiceServerPlayer) player), + addon.getConfig().visualizeDistanceHexColor() + ); } } @@ -165,7 +169,7 @@ private short calculateWhisperDistance(@NotNull VoiceServerPlayer player) { if (proximityDistance < 0) return -1; return (short) MathLib.clamp( - (int) ((proximityDistance / 100F) * config.proximityPercent()), + (int) ((proximityDistance / 100F) * addon.getConfig().proximityPercent()), 1, proximityActivation.get().getMaxDistance() ); diff --git a/src/main/java/su/plo/voice/whisper/WhisperAddon.java b/src/main/java/su/plo/voice/whisper/WhisperAddon.java index d7c258f..56e78ec 100644 --- a/src/main/java/su/plo/voice/whisper/WhisperAddon.java +++ b/src/main/java/su/plo/voice/whisper/WhisperAddon.java @@ -58,14 +58,12 @@ private void reloadConfig() { throw new IllegalStateException("Failed to load config", e); } - if (activation != null) { - voiceServer.getEventBus().unregister(this, activation); + if (activation == null) { + this.activation = new WhisperActivation(voiceServer, this); + voiceServer.getEventBus().register(this, activation); } - this.activation = new WhisperActivation(voiceServer, this); activation.register(); - - voiceServer.getEventBus().register(this, activation); } private InputStream getLanguageResource(@NotNull String resourcePath) throws IOException {