Crossfire JXClient, Trunk  R20561
PoisonWatcher.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-2011 Andreas Kirschbaum.
20  */
21 
22 package com.realtime.crossfire.jxclient.stats;
23 
27 import java.awt.event.ActionListener;
28 import javax.swing.Timer;
29 import org.jetbrains.annotations.NotNull;
30 
37 public class PoisonWatcher {
38 
43  private static final int TIMEOUT_DE_ASSERT = 10000;
44 
48  @NotNull
49  private static final String ASSERT_MESSAGE = "You feel very sick...";
50 
54  @NotNull
55  private static final String DE_ASSERT_MESSAGE = "You feel much better now.";
56 
60  @NotNull
61  private static final String CURE_MESSAGE = "Your body feels cleansed";
62 
66  @NotNull
67  private final Object sync = new Object();
68 
72  @NotNull
73  private final Stats stats;
74 
78  private boolean active = true;
79 
83  @NotNull
84  @SuppressWarnings("FieldCanBeLocal")
85  private final CrossfireDrawinfoListener drawinfoListener = (text, type) -> check(text);
86 
90  @NotNull
91  @SuppressWarnings("FieldCanBeLocal")
93 
94  @Override
95  public void commandDrawextinfoReceived(final int color, final int type, final int subtype, @NotNull final String message) {
96  check(message);
97  }
98 
99  @Override
100  public void setDebugMode(final boolean printMessageTypes) {
101  // ignore
102  }
103 
104  };
105 
110  @NotNull
111  private final ActionListener timeoutEvent = e -> setActive(false);
112 
116  @NotNull
117  private final Timer timer = new Timer(TIMEOUT_DE_ASSERT, timeoutEvent);
118 
124  public PoisonWatcher(@NotNull final Stats stats, @NotNull final CrossfireServerConnection crossfireServerConnection) {
125  this.stats = stats;
126  timer.setRepeats(false);
127  crossfireServerConnection.addCrossfireDrawinfoListener(drawinfoListener);
128  crossfireServerConnection.addCrossfireDrawextinfoListener(drawextinfoListener);
129  setActive(false);
130  }
131 
136  private void check(@NotNull final String message) {
137  if (message.equals(ASSERT_MESSAGE)) {
138  setActive(true);
139  } else if (message.equals(DE_ASSERT_MESSAGE) || message.equals(CURE_MESSAGE)) {
140  setActive(false);
141  }
142  }
143 
148  private void setActive(final boolean active) {
149  synchronized (sync) {
150  if (active) {
151  timer.restart();
152  } else {
153  timer.stop();
154  }
155 
156  if (this.active == active) {
157  return;
158  }
159 
160  this.active = active;
161  stats.setStat(Stats.C_STAT_POISONED, active ? 1 : 0);
162  }
163  }
164 
165 }
final Stats stats
The stats instance to notify.
Interface for listeners interested in drawinfo messages received from the Crossfire server...
final Timer timer
The Timer for turning off the poison symbol.
Helper class to synthesize an "is poisoned" stat value.
void check(@NotNull final String message)
Examines a text message.
static final String ASSERT_MESSAGE
The text message the server sends in poisoned state.
static final String CURE_MESSAGE
The text message the server sends when the poison is cured via a spell.
static final String DE_ASSERT_MESSAGE
The text message the server sends when the poison wears off.
PoisonWatcher(@NotNull final Stats stats, @NotNull final CrossfireServerConnection crossfireServerConnection)
Creates a new instance.
static final int TIMEOUT_DE_ASSERT
Timeout after that the "poisoned" state is reset.
void setActive(final boolean active)
Sets the current poisoned state.
final ActionListener timeoutEvent
The timeout event used to turn off poisoning if the de-assert message was missed. ...
static final int C_STAT_POISONED
The "is poisoned" indicator.
Definition: Stats.java:440
Interface for listeners interested in drawextinfo messages received from the Crossfire server...
final Object sync
The object used for synchronization.
final CrossfireDrawinfoListener drawinfoListener
The drawinfo listener to receive drawinfo messages.
Adds encoding/decoding of crossfire protocol packets to a ServerConnection.
void setStat(final int statNo, final int value)
Sets the given statistic numerical value.
Definition: Stats.java:633
This is the representation of all the statistics of a player, like its speed or its experience...
Definition: Stats.java:43
final CrossfireDrawextinfoListener drawextinfoListener
The drawextinfo listener to receive drawextinfo messages.
boolean active
Whether poisoning is active.