Skip to content

Commit

Permalink
fix: don't re-initialize WhisperActivation
Browse files Browse the repository at this point in the history
  • Loading branch information
Apehum committed Mar 25, 2023
1 parent 0ff74c9 commit d2a5dbc
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 16 deletions.
26 changes: 15 additions & 11 deletions src/main/java/su/plo/voice/whisper/WhisperActivation.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<UUID> playerWhisperVisualized = Sets.newCopyOnWriteArraySet();

Expand All @@ -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",
Expand All @@ -74,17 +76,15 @@ 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()
).build();

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,
Expand Down Expand Up @@ -138,18 +138,22 @@ 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;
}

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()
);
}
}

Expand All @@ -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()
);
Expand Down
8 changes: 3 additions & 5 deletions src/main/java/su/plo/voice/whisper/WhisperAddon.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit d2a5dbc

Please sign in to comment.