Crossfire JXClient, Trunk
SoundStatsWatcher.java
Go to the documentation of this file.
1 /*
2  * This file is part of JXClient, the Fullscreen Java Crossfire Client.
3  *
4  * JXClient is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * JXClient is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with JXClient; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17  *
18  * Copyright (C) 2005-2008 Yann Chachkoff
19  * Copyright (C) 2006-2017,2019-2023 Andreas Kirschbaum
20  * Copyright (C) 2010-2012,2014-2018,2020-2023 Nicolas Weeger
21  */
22 
23 package com.realtime.crossfire.jxclient.sound;
24 
32 import org.jetbrains.annotations.NotNull;
33 
38 public class SoundStatsWatcher {
39 
43  private static final long DELAY = 5000;
44 
48  @NotNull
49  private final SoundManager soundManager;
50 
54  private boolean active;
55 
59  private boolean poisoned;
60 
64  private int level;
65 
71  private long ignoreLevelChange = System.currentTimeMillis()+DELAY;
72 
76  @NotNull
77  @SuppressWarnings("FieldCanBeLocal")
78  private final StatsListener statsListener = new StatsListener() {
79 
80  @Override
81  public void resetBefore() {
82  ignoreLevelChange = System.currentTimeMillis()+DELAY;
83  }
84 
85  @Override
86  public void resetAfter() {
87  // ignore
88  }
89 
90  @Override
91  public void statChanged(final int statNo, final int value) {
92  checkStats(statNo, value);
93  }
94 
95  @Override
96  public void simpleWeaponSpeedChanged(final boolean simpleWeaponSpeed) {
97  // ignore
98  }
99 
100  @Override
101  public void titleChanged(@NotNull final String title) {
102  // ignore
103  }
104 
105  @Override
106  public void godNameChanged(@NotNull final String godName) {
107  // ignore
108  }
109 
110  @Override
111  public void rangeChanged(@NotNull final String range) {
112  // ignore
113  }
114 
115  @Override
116  public void activeSkillChanged(@NotNull final String activeSkill) {
117  // ignore
118  }
119 
120  @Override
121  public void experienceChanged(final long exp) {
122  // ignore
123  }
124 
125  @Override
126  public void experienceNextLevelChanged(final long expNextLevel) {
127  // ignore
128  }
129 
130  };
131 
135  @NotNull
136  @SuppressWarnings("FieldCanBeLocal")
137  private final RendererGuiStateListener rendererGuiStateListener = rendererGuiState -> {
138  active = rendererGuiState == RendererGuiState.PLAYING;
139  ignoreLevelChange = System.currentTimeMillis()+DELAY;
140  };
141 
145  @NotNull
146  @SuppressWarnings("FieldCanBeLocal")
148 
149  @Override
150  public void delinvReceived(final int tag) {
151  // ignore
152  }
153 
154  @Override
155  public void delitemReceived(final int @NotNull [] tags) {
156  // ignore
157  }
158 
159  @Override
160  public void addItemReceived(final int location, final int tag, final int flags, final int weight, final int faceNum, @NotNull final String name, @NotNull final String namePl, final int anim, final int animSpeed, final int nrof, final int type) {
161  // ignore
162  }
163 
164  @Override
165  public void playerReceived(final int tag, final int weight, final int faceNum, @NotNull final String name) {
166  ignoreLevelChange = System.currentTimeMillis()+DELAY;
167  }
168 
169  @Override
170  public void upditemReceived(final int flags, final int tag, final int valLocation, final int valFlags, final int valWeight, final int valFaceNum, @NotNull final String valName, @NotNull final String valNamePl, final int valAnim, final int valAnimSpeed, final int valNrof) {
171  // ignore
172  }
173 
174  };
175 
183  public SoundStatsWatcher(@NotNull final Stats stats, @NotNull final JXCWindowRenderer windowRenderer, @NotNull final CrossfireServerConnection server, @NotNull final SoundManager soundManager) {
184  this.soundManager = soundManager;
185  poisoned = stats.getStat(Stats.C_STAT_POISONED) != 0;
186  level = stats.getStat(Stats.CS_STAT_LEVEL);
187  stats.addCrossfireStatsListener(statsListener);
188  windowRenderer.addGuiStateListener(rendererGuiStateListener);
189  rendererGuiStateListener.guiStateChanged(windowRenderer.getGuiState());
190  server.addCrossfireUpdateItemListener(crossfireUpdateItemListener);
191  }
192 
198  private void checkStats(final int statNo, final int value) {
199  if (statNo == Stats.C_STAT_POISONED) {
200  final boolean newPoisoned = value != 0;
201  if (poisoned != newPoisoned) {
202  poisoned = newPoisoned;
203  if (active) {
204  playClip(newPoisoned ? Sounds.POISON_ON : Sounds.POISON_OFF);
205  }
206  }
207  } else if (statNo == Stats.CS_STAT_LEVEL) {
208  if (level != value) {
209  if (ignoreLevelChange != 0 && ignoreLevelChange <= System.currentTimeMillis()) {
210  ignoreLevelChange = 0;
211  }
212  if (ignoreLevelChange == 0 && level < value && active) {
214  }
215  level = value;
216  }
217  }
218  }
219 
224  private void playClip(@NotNull final CharSequence clip) {
225  if (active) {
227  }
228  }
229 
230 }
com.realtime.crossfire.jxclient
com.realtime.crossfire.jxclient.sound.Sounds
Definition: Sounds.java:31
com.realtime.crossfire.jxclient.sound.SoundStatsWatcher.SoundStatsWatcher
SoundStatsWatcher(@NotNull final Stats stats, @NotNull final JXCWindowRenderer windowRenderer, @NotNull final CrossfireServerConnection server, @NotNull final SoundManager soundManager)
Definition: SoundStatsWatcher.java:183
com.realtime.crossfire.jxclient.server
com.realtime.crossfire.jxclient.stats.Stats.CS_STAT_LEVEL
static final int CS_STAT_LEVEL
Definition: Stats.java:109
com.realtime.crossfire.jxclient.sound.SoundStatsWatcher.DELAY
static final long DELAY
Definition: SoundStatsWatcher.java:43
com.realtime.crossfire.jxclient.sound.SoundStatsWatcher.checkStats
void checkStats(final int statNo, final int value)
Definition: SoundStatsWatcher.java:198
com.realtime.crossfire.jxclient.sound.SoundStatsWatcher.active
boolean active
Definition: SoundStatsWatcher.java:54
com.realtime.crossfire.jxclient.gui.gui.RendererGuiStateListener.guiStateChanged
void guiStateChanged(@NotNull RendererGuiState rendererGuiState)
com.realtime.crossfire.jxclient.stats.Stats
Definition: Stats.java:44
com.realtime.crossfire.jxclient.stats.StatsListener
Definition: StatsListener.java:32
com.realtime.crossfire.jxclient.gui.misc.JXCWindowRenderer
Definition: JXCWindowRenderer.java:87
com.realtime.crossfire.jxclient.sound.SoundStatsWatcher.statsListener
final StatsListener statsListener
Definition: SoundStatsWatcher.java:78
com.realtime.crossfire.jxclient.sound.SoundStatsWatcher.poisoned
boolean poisoned
Definition: SoundStatsWatcher.java:59
com.realtime.crossfire.jxclient.sound.SoundStatsWatcher.ignoreLevelChange
long ignoreLevelChange
Definition: SoundStatsWatcher.java:71
com.realtime.crossfire.jxclient.sound.SoundStatsWatcher.playClip
void playClip(@NotNull final CharSequence clip)
Definition: SoundStatsWatcher.java:224
com.realtime.crossfire.jxclient.server.crossfire.CrossfireServerConnection
Definition: CrossfireServerConnection.java:37
com.realtime.crossfire.jxclient.gui.gui.RendererGuiState.PLAYING
PLAYING
Definition: RendererGuiState.java:61
com.realtime.crossfire.jxclient.gui
com.realtime.crossfire.jxclient.sound.SoundStatsWatcher.level
int level
Definition: SoundStatsWatcher.java:64
com.realtime.crossfire.jxclient.sound.SoundManager.playClip
void playClip(@NotNull final Sounds type, @NotNull final CharSequence action)
Definition: SoundManager.java:183
com.realtime.crossfire.jxclient.server.crossfire
Definition: AbstractCrossfireServerConnection.java:23
com.realtime.crossfire.jxclient.sound.SoundStatsWatcher
Definition: SoundStatsWatcher.java:38
com.realtime.crossfire.jxclient.gui.gui
Definition: AbstractGUIElement.java:23
com.realtime.crossfire.jxclient.sound.Sounds.POISON_OFF
static final String POISON_OFF
Definition: Sounds.java:48
com.realtime.crossfire
com.realtime
com.realtime.crossfire.jxclient.sound.SoundStatsWatcher.soundManager
final SoundManager soundManager
Definition: SoundStatsWatcher.java:49
com.realtime.crossfire.jxclient.sound.SoundStatsWatcher.crossfireUpdateItemListener
final CrossfireUpdateItemListener crossfireUpdateItemListener
Definition: SoundStatsWatcher.java:147
com.realtime.crossfire.jxclient.sound.Sounds.CHARACTER
CHARACTER
Definition: Sounds.java:36
com.realtime.crossfire.jxclient.sound.SoundStatsWatcher.rendererGuiStateListener
final RendererGuiStateListener rendererGuiStateListener
Definition: SoundStatsWatcher.java:137
com
com.realtime.crossfire.jxclient.server.crossfire.CrossfireUpdateItemListener
Definition: CrossfireUpdateItemListener.java:32
com.realtime.crossfire.jxclient.sound.Sounds.POISON_ON
static final String POISON_ON
Definition: Sounds.java:42
com.realtime.crossfire.jxclient.gui.gui.RendererGuiState
Definition: RendererGuiState.java:31
com.realtime.crossfire.jxclient.sound.SoundManager
Definition: SoundManager.java:40
com.realtime.crossfire.jxclient.gui.misc
Definition: GUICheckBox.java:23
com.realtime.crossfire.jxclient.gui.gui.RendererGuiStateListener
Definition: RendererGuiStateListener.java:32
com.realtime.crossfire.jxclient.stats.Stats.C_STAT_POISONED
static final int C_STAT_POISONED
Definition: Stats.java:462
com.realtime.crossfire.jxclient.sound.Sounds.LEVEL_UP
static final String LEVEL_UP
Definition: Sounds.java:54
com.realtime.crossfire.jxclient.stats
Definition: ActiveSkillWatcher.java:23