Crossfire JXClient, Trunk  R20561
ActiveSkillWatcher.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 org.jetbrains.annotations.NotNull;
28 
35 public class ActiveSkillWatcher {
36 
40  @NotNull
41  private static final String READIED_SKILLS = "Readied skill: ";
42 
46  @NotNull
47  private final Object sync = new Object();
48 
52  @NotNull
53  private final Stats stats;
54 
58  @NotNull
59  private String activeSkill = "";
60 
64  @NotNull
65  @SuppressWarnings("FieldCanBeLocal")
66  private final StatsListener statsListener = new StatsListener() {
67 
68  @Override
69  public void reset() {
70  // ignore
71  }
72 
73  @Override
74  public void statChanged(final int statNo, final int value) {
75  // ignore
76  }
77 
78  @Override
79  public void simpleWeaponSpeedChanged(final boolean simpleWeaponSpeed) {
80  // ignore
81  }
82 
83  @Override
84  public void titleChanged(@NotNull final String title) {
85  // ignore
86  }
87 
88  @Override
89  public void rangeChanged(@NotNull final String range) {
90  checkRange(range);
91  }
92 
93  @Override
94  public void activeSkillChanged(@NotNull final String activeSkill) {
95  // ignore
96  }
97 
98  @Override
99  public void experienceChanged(final long exp) {
100  // ignore
101  }
102 
103  @Override
104  public void experienceNextLevelChanged(final long expNextLevel) {
105  // ignore
106  }
107 
108  };
109 
113  @NotNull
114  @SuppressWarnings("FieldCanBeLocal")
115  private final CrossfireDrawinfoListener drawinfoListener = (text, type) -> checkMessage(text);
116 
120  @NotNull
121  @SuppressWarnings("FieldCanBeLocal")
123 
124  @Override
125  public void commandDrawextinfoReceived(final int color, final int type, final int subtype, @NotNull final String message) {
126  checkMessage(message);
127  }
128 
129  @Override
130  public void setDebugMode(final boolean printMessageTypes) {
131  // ignore
132  }
133 
134  };
135 
141  public ActiveSkillWatcher(@NotNull final Stats stats, @NotNull final CrossfireServerConnection crossfireServerConnection) {
142  this.stats = stats;
144  crossfireServerConnection.addCrossfireDrawinfoListener(drawinfoListener);
145  crossfireServerConnection.addCrossfireDrawextinfoListener(drawextinfoListener);
146  setActive("");
147  }
148 
153  private void checkRange(@NotNull final String range) {
154  if (range.startsWith("Skill: ")) {
155  setActive(range.substring(7));
156  }
157  }
158 
163  private void checkMessage(@NotNull final String message) {
164  if (message.startsWith(READIED_SKILLS)) {
165  final String tmp = message.substring(READIED_SKILLS.length());
166  setActive(tmp.endsWith(".") ? tmp.substring(0, tmp.length()-1) : tmp);
167  }
168  }
169 
174  @SuppressWarnings("IfMayBeConditional")
175  private void setActive(@NotNull final String activeSkill) {
176  // Normalize skill name: the Crossfire server sometimes sends "Skill:
177  // <skill item name>" rather than "Skill: <skill name>".
178  final String normalizedActiveSkill;
179  switch (activeSkill) {
180  case "lockpicks":
181  normalizedActiveSkill = "lockpicking";
182  break;
183 
184  case "writing pen":
185  normalizedActiveSkill = "inscription";
186  break;
187 
188  default:
189  normalizedActiveSkill = activeSkill;
190  break;
191  }
192 
193  synchronized (sync) {
194  if (this.activeSkill.equals(normalizedActiveSkill)) {
195  return;
196  }
197 
198  this.activeSkill = normalizedActiveSkill;
199  stats.setActiveSkill(this.activeSkill);
200  }
201  }
202 
203 }
Interface for listeners interested in drawinfo messages received from the Crossfire server...
final CrossfireDrawextinfoListener drawextinfoListener
The drawextinfo listener to receive drawextinfo messages.
static final String READIED_SKILLS
Prefix string when searching for the currently active skill.
void setActiveSkill(@NotNull final String activeSkill)
Sets the active skill name.
Definition: Stats.java:707
final Stats stats
The stats instance to notify.
String activeSkill
The last known active skill name.
void addCrossfireStatsListener(@NotNull final StatsListener statsListener)
Adds a StatsListener to be notified about stat changes.
Definition: Stats.java:770
void setActive(@NotNull final String activeSkill)
Sets the active skill name.
Helper class to synthesize an "active skill" stat value.
ActiveSkillWatcher(@NotNull final Stats stats, @NotNull final CrossfireServerConnection crossfireServerConnection)
Creates a new instance.
final Object sync
The object used for synchronization.
Interface for listeners interested in changes of Stats instances.
final StatsListener statsListener
The stats listener to detect the range stat.
Interface for listeners interested in drawextinfo messages received from the Crossfire server...
void checkMessage(@NotNull final String message)
Checks whether a drawinfo message is skill related.
Adds encoding/decoding of crossfire protocol packets to a ServerConnection.
final CrossfireDrawinfoListener drawinfoListener
The drawinfo listener to receive drawinfo messages.
This is the representation of all the statistics of a player, like its speed or its experience...
Definition: Stats.java:43
void checkRange(@NotNull final String range)
Checks whether the range attribute has changed.