Crossfire JXClient, Trunk
NewCharModel.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.gui.label;
24 
30 import java.util.EnumMap;
31 import java.util.Iterator;
32 import java.util.List;
33 import java.util.Map;
34 import java.util.Objects;
35 import java.util.TreeMap;
36 import org.jetbrains.annotations.NotNull;
37 import org.jetbrains.annotations.Nullable;
38 
43 public class NewCharModel {
44 
48  @NotNull
49  private static final String UNKNOWN = "unknown";
50 
54  public static final int PRIORITY_INVALID_STAT_STR = 1;
55 
59  public static final int PRIORITY_INVALID_STAT_DEX = 2;
60 
64  public static final int PRIORITY_INVALID_STAT_CON = 3;
65 
69  public static final int PRIORITY_INVALID_STAT_INT = 4;
70 
74  public static final int PRIORITY_INVALID_STAT_WIS = 5;
75 
79  public static final int PRIORITY_INVALID_STAT_POW = 6;
80 
84  public static final int PRIORITY_INVALID_STAT_CHA = 7;
85 
89  public static final int PRIORITY_UNUSED_POINTS = 8;
90 
94  public static final int PRIORITY_CHARACTER_NAME = 9;
95 
99  public static final int PRIORITY_SERVER_FAILURE = Integer.MAX_VALUE;
100 
104  @NotNull
106 
110  @NotNull
112 
116  @NotNull
117  private final Map<NewcharStat, Integer> values = new EnumMap<>(NewcharStat.class);
118 
122  @NotNull
123  private String race = UNKNOWN;
124 
128  @NotNull
129  private String class_ = UNKNOWN;
130 
134  @NotNull
135  private String startingMap = UNKNOWN;
136 
141  @Nullable
142  private Choice option;
143 
147  private int optionIndex;
148 
153  @NotNull
154  private final Map<Integer, String> errorTexts = new TreeMap<>();
155 
161  this.newCharacterInformation = newCharacterInformation;
162 
163  this.newCharacterInformation.addNewCharacterInformationListener(new NewCharacterInformationListener() {
164 
165  @Override
166  public void classListChanged() {
167  setClass(class_);
168  notifyListeners();
169  }
170 
171  @Override
172  public void classInfoChanged(@NotNull final String className) {
173  notifyListeners();
174  }
175 
176  @Override
177  public void raceListChanged() {
178  setRace(race);
179  notifyListeners();
180  }
181 
182  @Override
183  public void raceInfoChanged(@NotNull final String raceName) {
184  notifyListeners();
185  }
186 
187  @Override
188  public void startingMapListChanged() {
190  notifyListeners();
191  }
192 
193  @Override
194  public void startingMapInfoChanged(@NotNull final String startingMapName) {
195  notifyListeners();
196  }
197 
198  });
199  setClass(UNKNOWN);
200  setRace(UNKNOWN);
202  }
203 
208  public void addListener(@NotNull final NewCharModelListener listener) {
209  listeners.add(listener);
210  }
211 
216  public void removeListener(@NotNull final NewCharModelListener listener) {
217  listeners.remove(listener);
218  }
219 
225  @NotNull
228  }
229 
234  @NotNull
235  public String getRace() {
236  return race;
237  }
238 
243  public void setRace(@NotNull final String race) {
244  final List<String> list = newCharacterInformation.getRaceList();
245  final String newRace;
246  if (list.contains(race)) {
247  newRace = race;
248  } else if (!list.isEmpty()) {
249  newRace = list.get(0);
250  } else {
251  newRace = UNKNOWN;
252  }
253  if (this.race.equals(newRace)) {
254  return;
255  }
256 
257  this.race = newRace;
258  updateOption();
259  notifyListeners();
260  }
261 
266  @NotNull
267  public String getClass_() {
268  return class_;
269  }
270 
275  public void setClass(@NotNull final String class_) {
276  final List<String> list = newCharacterInformation.getClassesList();
277  final String newClass;
278  if (list.contains(class_)) {
279  newClass = class_;
280  } else if (!list.isEmpty()) {
281  newClass = list.get(0);
282  } else {
283  newClass = UNKNOWN;
284  }
285  if (this.class_.equals(newClass)) {
286  return;
287  }
288 
289  this.class_ = newClass;
290  updateOption();
291  notifyListeners();
292  }
293 
298  @NotNull
299  public String getStartingMap() {
300  return startingMap;
301  }
302 
307  public void setStartingMap(@NotNull final String startingMap) {
308  final List<String> list = newCharacterInformation.getStartingMapList();
309  final String newStartingMap;
310  if (list.contains(startingMap)) {
311  newStartingMap = startingMap;
312  } else if (!list.isEmpty()) {
313  newStartingMap = list.get(0);
314  } else {
315  newStartingMap = UNKNOWN;
316  }
317  if (this.startingMap.equals(newStartingMap)) {
318  return;
319  }
320 
321  this.startingMap = newStartingMap;
322  updateOption();
323  notifyListeners();
324  }
325 
331  public int getTotal(@NotNull final NewcharStat stat) {
332  return getValue(stat)+getRaceStatAdjustment(stat)+getClassStatAdjustment(stat);
333  }
334 
340  public void setValue(@NotNull final NewcharStat stat, final int value) {
341  final Integer prevValue = values.put(stat, value);
342  //noinspection EqualsReplaceableByObjectsCall
343  if (prevValue == null || !prevValue.equals(value)) {
344  notifyListeners();
345  }
346  }
347 
353  public int getValue(@NotNull final NewcharStat stat) {
354  final Integer tmp = values.get(stat);
355  return tmp == null ? 0 : tmp;
356  }
357 
363  public int getRaceStatAdjustment(@NotNull final NewcharStat stat) {
365  return info == null ? 0 : (int)info.getStatAdjustment(stat.getStat()); // XXX: long->int cast
366  }
367 
373  public int getClassStatAdjustment(@NotNull final NewcharStat stat) {
375  return info == null ? 0 : (int)info.getStatAdjustment(stat.getStat()); // XXX: long->int cast
376  }
377 
383  public void setErrorText(final int priority, @Nullable final String text) {
384  final String prevText = errorTexts.get(priority);
385  errorTexts.compute(priority, (p, s) -> text);
386  if (!Objects.equals(text, prevText)) {
387  notifyListeners();
388  }
389  }
390 
395  @NotNull
396  public String getErrorText() {
397  final Iterator<String> it = errorTexts.values().iterator();
398  return it.hasNext() ? it.next() : "";
399  }
400 
405  public boolean hasNonServerFailureErrorText() {
406  final Iterator<Integer> it = errorTexts.keySet().iterator();
407  return it.hasNext() && it.next() != PRIORITY_SERVER_FAILURE;
408  }
409 
415  private void notifyListeners() {
416  for (final NewCharModelListener listener : listeners) {
417  listener.changed();
418  }
419  }
420 
425  public int getUnusedPoints() {
427  for (int value : values.values()) {
428  result -= value;
429  }
430  return result;
431  }
432 
437  @Nullable
438  public Choice getOption() {
439  return option;
440  }
441 
446  public int getOptionIndex() {
447  return optionIndex;
448  }
449 
454  public void setOptionIndex(final int optionIndex) {
455  final int newOptionIndex = optionIndex < 0 || option == null || optionIndex >= option.getChoices().size() ? 0 : optionIndex;
456  if (this.optionIndex == newOptionIndex) {
457  return;
458  }
459  this.optionIndex = newOptionIndex;
460  notifyListeners();
461  }
462 
466  private void updateOption() {
467  @Nullable final Choice newOption;
469  if (raceInfo != null && !raceInfo.getChoices().isEmpty()) {
470  newOption = raceInfo.getChoices().get(0);
471  } else {
473  if (classInfo != null && !classInfo.getChoices().isEmpty()) {
474  newOption = classInfo.getChoices().get(0);
475  } else {
476  newOption = null;
477  }
478  }
479  if (!Objects.equals(option, newOption)) {
480  option = newOption;
481  optionIndex = 0;
482  }
483  }
484 
485 }
com.realtime.crossfire.jxclient.gui.label.NewCharModel.getClass_
String getClass_()
Definition: NewCharModel.java:267
com.realtime.crossfire.jxclient.gui.label.NewCharModel.PRIORITY_CHARACTER_NAME
static final int PRIORITY_CHARACTER_NAME
Definition: NewCharModel.java:94
com.realtime.crossfire.jxclient
com.realtime.crossfire.jxclient.gui.label.NewcharStat
Definition: NewcharStat.java:32
com.realtime.crossfire.jxclient.character.NewCharacterInformationListener
Definition: NewCharacterInformationListener.java:33
com.realtime.crossfire.jxclient.gui.label.NewCharModelListener
Definition: NewCharModelListener.java:30
com.realtime.crossfire.jxclient.gui.label.NewCharModel.UNKNOWN
static final String UNKNOWN
Definition: NewCharModel.java:49
com.realtime.crossfire.jxclient.character.NewCharacterInformation.getClassesList
List< String > getClassesList()
Definition: NewCharacterInformation.java:214
com.realtime.crossfire.jxclient.character.Choice
Definition: Choice.java:34
com.realtime.crossfire.jxclient.gui.label.NewCharModel.getErrorText
String getErrorText()
Definition: NewCharModel.java:396
com.realtime.crossfire.jxclient.gui.label.NewCharModel.PRIORITY_UNUSED_POINTS
static final int PRIORITY_UNUSED_POINTS
Definition: NewCharModel.java:89
com.realtime.crossfire.jxclient.gui.label.NewCharModel.addListener
void addListener(@NotNull final NewCharModelListener listener)
Definition: NewCharModel.java:208
com.realtime.crossfire.jxclient.util.EventListenerList2
Definition: EventListenerList2.java:37
com.realtime.crossfire.jxclient.gui.label.NewCharModel.class_
String class_
Definition: NewCharModel.java:129
com.realtime.crossfire.jxclient.character.NewCharacterInformation.getStartingMapList
List< String > getStartingMapList()
Definition: NewCharacterInformation.java:129
com.realtime.crossfire.jxclient.gui.label.NewCharModel.startingMap
String startingMap
Definition: NewCharModel.java:135
com.realtime.crossfire.jxclient.gui.label.NewCharModel.getNewCharacterInformation
NewCharacterInformation getNewCharacterInformation()
Definition: NewCharModel.java:226
com.realtime.crossfire.jxclient.character.ClassRaceInfo.getChoices
List< Choice > getChoices()
Definition: ClassRaceInfo.java:130
com.realtime.crossfire.jxclient.gui.label.NewCharModel.getStartingMap
String getStartingMap()
Definition: NewCharModel.java:299
com.realtime.crossfire.jxclient.gui.label.NewCharModel.setValue
void setValue(@NotNull final NewcharStat stat, final int value)
Definition: NewCharModel.java:340
com.realtime.crossfire.jxclient.character.NewCharacterInformation.getNewCharInfo
NewCharInfo getNewCharInfo()
Definition: NewCharacterInformation.java:253
com.realtime.crossfire.jxclient.gui.label.NewCharModel.PRIORITY_INVALID_STAT_DEX
static final int PRIORITY_INVALID_STAT_DEX
Definition: NewCharModel.java:59
com.realtime.crossfire.jxclient.gui.label.NewCharModel.PRIORITY_INVALID_STAT_INT
static final int PRIORITY_INVALID_STAT_INT
Definition: NewCharModel.java:69
com.realtime.crossfire.jxclient.gui.label.NewCharModel.option
Choice option
Definition: NewCharModel.java:142
com.realtime.crossfire.jxclient.character.NewCharacterInformation
Definition: NewCharacterInformation.java:39
com.realtime.crossfire.jxclient.character.NewCharacterInformation.getRaceInfo
ClassRaceInfo getRaceInfo(@NotNull final String race)
Definition: NewCharacterInformation.java:193
com.realtime.crossfire.jxclient.gui.label.NewCharModel.PRIORITY_INVALID_STAT_WIS
static final int PRIORITY_INVALID_STAT_WIS
Definition: NewCharModel.java:74
com.realtime.crossfire.jxclient.gui.label.NewCharModel.getValue
int getValue(@NotNull final NewcharStat stat)
Definition: NewCharModel.java:353
com.realtime.crossfire.jxclient.gui.label.NewCharModel.race
String race
Definition: NewCharModel.java:123
com.realtime.crossfire.jxclient.character.ClassRaceInfo
Definition: ClassRaceInfo.java:37
com.realtime.crossfire.jxclient.gui.label.NewCharModel.PRIORITY_INVALID_STAT_STR
static final int PRIORITY_INVALID_STAT_STR
Definition: NewCharModel.java:54
com.realtime.crossfire.jxclient.gui.label.NewCharModel.PRIORITY_INVALID_STAT_CON
static final int PRIORITY_INVALID_STAT_CON
Definition: NewCharModel.java:64
com.realtime.crossfire.jxclient.character.NewCharacterInformation.getRaceList
List< String > getRaceList()
Definition: NewCharacterInformation.java:172
com.realtime.crossfire.jxclient.character.NewCharInfo.getPoints
int getPoints()
Definition: NewCharInfo.java:97
com.realtime.crossfire.jxclient.gui.label.NewCharModel.PRIORITY_INVALID_STAT_POW
static final int PRIORITY_INVALID_STAT_POW
Definition: NewCharModel.java:79
com.realtime.crossfire.jxclient.gui.label.NewCharModel.setRace
void setRace(@NotNull final String race)
Definition: NewCharModel.java:243
com.realtime.crossfire.jxclient.character.ClassRaceInfo.getStatAdjustment
long getStatAdjustment(final int statNo)
Definition: ClassRaceInfo.java:117
com.realtime.crossfire.jxclient.gui.label.NewCharModel.PRIORITY_SERVER_FAILURE
static final int PRIORITY_SERVER_FAILURE
Definition: NewCharModel.java:99
com.realtime.crossfire.jxclient.util
Definition: Codec.java:23
com.realtime.crossfire.jxclient.gui.label.NewCharModel.getRace
String getRace()
Definition: NewCharModel.java:235
com.realtime.crossfire.jxclient.gui.label.NewCharModel.NewCharModel
NewCharModel(@NotNull final NewCharacterInformation newCharacterInformation)
Definition: NewCharModel.java:160
com.realtime.crossfire.jxclient.gui.label.NewCharModel.optionIndex
int optionIndex
Definition: NewCharModel.java:147
com.realtime.crossfire.jxclient.character.NewCharacterInformation.getClassInfo
ClassRaceInfo getClassInfo(@NotNull final String className)
Definition: NewCharacterInformation.java:236
com.realtime.crossfire.jxclient.gui.label.NewCharModel
Definition: NewCharModel.java:43
com.realtime.crossfire.jxclient.gui.label.NewCharModel.setOptionIndex
void setOptionIndex(final int optionIndex)
Definition: NewCharModel.java:454
com.realtime.crossfire.jxclient.gui.label.NewCharModel.listeners
final EventListenerList2< NewCharModelListener > listeners
Definition: NewCharModel.java:111
com.realtime.crossfire.jxclient.gui.label.NewCharModel.PRIORITY_INVALID_STAT_CHA
static final int PRIORITY_INVALID_STAT_CHA
Definition: NewCharModel.java:84
com.realtime.crossfire.jxclient.gui.label.NewCharModel.setStartingMap
void setStartingMap(@NotNull final String startingMap)
Definition: NewCharModel.java:307
com.realtime.crossfire
com.realtime.crossfire.jxclient.gui.label.NewCharModel.newCharacterInformation
final NewCharacterInformation newCharacterInformation
Definition: NewCharModel.java:105
com.realtime
com.realtime.crossfire.jxclient.gui.label.NewCharModel.setClass
void setClass(@NotNull final String class_)
Definition: NewCharModel.java:275
com
com.realtime.crossfire.jxclient.character.Choice.getChoices
Map< String, String > getChoices()
Definition: Choice.java:89
com.realtime.crossfire.jxclient.gui.label.NewCharModel.getTotal
int getTotal(@NotNull final NewcharStat stat)
Definition: NewCharModel.java:331
com.realtime.crossfire.jxclient.gui.label.NewCharModel.getRaceStatAdjustment
int getRaceStatAdjustment(@NotNull final NewcharStat stat)
Definition: NewCharModel.java:363
com.realtime.crossfire.jxclient.character
Definition: Choice.java:23
com.realtime.crossfire.jxclient.gui.label.NewCharModel.updateOption
void updateOption()
Definition: NewCharModel.java:466
com.realtime.crossfire.jxclient.gui.label.NewCharModel.notifyListeners
void notifyListeners()
Definition: NewCharModel.java:415
com.realtime.crossfire.jxclient.gui.label.NewCharModel.getOptionIndex
int getOptionIndex()
Definition: NewCharModel.java:446
com.realtime.crossfire.jxclient.gui.label.NewCharModel.getOption
Choice getOption()
Definition: NewCharModel.java:438
com.realtime.crossfire.jxclient.gui.label.NewCharModel.hasNonServerFailureErrorText
boolean hasNonServerFailureErrorText()
Definition: NewCharModel.java:405
com.realtime.crossfire.jxclient.character.NewCharacterInformation.addNewCharacterInformationListener
void addNewCharacterInformationListener(@NotNull final NewCharacterInformationListener newCharacterInformationListener)
Definition: NewCharacterInformation.java:94
com.realtime.crossfire.jxclient.gui.label.NewCharModel.removeListener
void removeListener(@NotNull final NewCharModelListener listener)
Definition: NewCharModel.java:216
com.realtime.crossfire.jxclient.gui.label.NewCharModel.errorTexts
final Map< Integer, String > errorTexts
Definition: NewCharModel.java:154
com.realtime.crossfire.jxclient.gui.label.NewCharModel.getUnusedPoints
int getUnusedPoints()
Definition: NewCharModel.java:425
com.realtime.crossfire.jxclient.gui.label.NewCharModel.getClassStatAdjustment
int getClassStatAdjustment(@NotNull final NewcharStat stat)
Definition: NewCharModel.java:373
com.realtime.crossfire.jxclient.gui.label.NewCharModel.setErrorText
void setErrorText(final int priority, @Nullable final String text)
Definition: NewCharModel.java:383
com.realtime.crossfire.jxclient.gui.label.NewCharModel.values
final Map< NewcharStat, Integer > values
Definition: NewCharModel.java:117