Crossfire JXClient, Trunk  R20561
CfItem.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.items;
23 
27 import org.jetbrains.annotations.NotNull;
28 
36 public class CfItem {
37 
41  private final int tag;
42 
46  private int flags;
47 
51  private int weight;
52 
56  @NotNull
57  private Face face;
58 
62  @NotNull
63  private String name;
64 
68  @NotNull
69  private String namePl;
70 
74  private int anim;
75 
79  private int animSpeed;
80 
84  private int nrof;
85 
89  private int location;
90 
94  private final int type;
95 
100  private boolean modified = true;
101 
105  public static final int F_APPLIED = 0x000F;
106 
110  public static final int F_UNIDENTIFIED = 0x0010;
111 
115  public static final int F_BLESSED = 0x0100;
116 
120  public static final int F_UNPAID = 0x0200;
121 
125  public static final int F_MAGIC = 0x0400;
126 
130  public static final int F_CURSED = 0x0800;
131 
135  public static final int F_DAMNED = 0x1000;
136 
140  public static final int F_OPEN = 0x2000;
141 
145  public static final int F_NOPICK = 0x4000;
146 
150  public static final int F_LOCKED = 0x8000;
151 
156 
171  public CfItem(final int location, final int tag, final int flags, final int weight, @NotNull final Face face, @NotNull final String name, @NotNull final String namePl, final int anim, final int animSpeed, final int nrof, final int type) {
172  this.location = location;
173  this.tag = tag;
174  this.flags = flags;
175  this.weight = weight;
176  this.face = face;
177  this.name = name;
178  this.namePl = namePl;
179  this.anim = anim;
180  this.animSpeed = animSpeed;
181  this.nrof = nrof;
182  this.type = type;
183  }
184 
189  private void setFlags(final int flags) {
190  if (this.flags != flags) {
191  this.flags = flags;
192  modified = true;
193  }
194  }
195 
200  private void setWeight(final int weight) {
201  if (this.weight != weight) {
202  this.weight = weight;
203  modified = true;
204  }
205  }
206 
211  private void setFace(@NotNull final Face face) {
212  if (this.face != face) {
213  this.face = face;
214  modified = true;
215  }
216  }
217 
223  private void setName(@NotNull final String name, @NotNull final String namePl) {
224  if (!this.name.equals(name) || !this.namePl.equals(namePl)) {
225  this.name = name;
226  this.namePl = namePl;
227  modified = true;
228  }
229  }
230 
235  private void setAnim(final int anim) {
236  if (this.anim != anim) {
237  this.anim = anim;
238  modified = true;
239  }
240  }
241 
246  private void setAnimSpeed(final int animSpeed) {
247  if (this.animSpeed != animSpeed) {
248  this.animSpeed = animSpeed;
249  modified = true;
250  }
251  }
252 
257  private void setNrOf(final int nrof) {
258  if (this.nrof != nrof) {
259  this.nrof = nrof;
260  modified = true;
261  }
262  }
263 
268  public void setLocation(final int location) {
269  this.location = location;
270  }
271 
276  public int getTag() {
277  return tag;
278  }
279 
284  public int getWeight() {
285  return weight;
286  }
287 
292  @NotNull
293  public Face getFace() {
294  return face;
295  }
296 
301  @NotNull
302  public String getName() {
303  return nrof > 1 ? namePl : name;
304  }
305 
310  public int getNrOf() {
311  return nrof;
312  }
313 
318  public boolean isApplied() {
319  return (flags&F_APPLIED) != 0;
320  }
321 
326  public boolean isUnidentified() {
327  return (flags&F_UNIDENTIFIED) != 0;
328  }
329 
334  public boolean isBlessed() {
335  return (flags&F_BLESSED) != 0;
336  }
337 
342  public boolean isUnpaid() {
343  return (flags&F_UNPAID) != 0;
344  }
345 
350  public boolean isMagic() {
351  return (flags&F_MAGIC) != 0;
352  }
353 
358  public boolean isCursed() {
359  return (flags&F_CURSED) != 0;
360  }
361 
366  public boolean isDamned() {
367  return (flags&F_DAMNED) != 0;
368  }
369 
374  public boolean isOpen() {
375  return (flags&F_OPEN) != 0;
376  }
377 
382  public boolean isNoPick() {
383  return (flags&F_NOPICK) != 0;
384  }
385 
390  public boolean isLocked() {
391  return (flags&F_LOCKED) != 0;
392  }
393 
398  public int getLocation() {
399  return location;
400  }
401 
406  public int getType() {
407  return type;
408  }
409 
422  public void update(final int updateFlags, final int flags, final int weight, @NotNull final Face face, @NotNull final String name, @NotNull final String namePl, final int anim, final int animSpeed, final int nrof) {
423  if ((updateFlags&UpdItem.UPD_FLAGS) != 0) {
424  setFlags(flags);
425  }
426  if ((updateFlags&UpdItem.UPD_WEIGHT) != 0) {
427  setWeight(weight);
428  }
429  if ((updateFlags&UpdItem.UPD_FACE) != 0) {
430  setFace(face);
431  }
432  if ((updateFlags&UpdItem.UPD_NAME) != 0) {
433  setName(name, namePl);
434  }
435  if ((updateFlags&UpdItem.UPD_ANIM) != 0) {
436  setAnim(anim);
437  }
438  if ((updateFlags&UpdItem.UPD_ANIMSPEED) != 0) {
439  setAnimSpeed(animSpeed);
440  }
441  if ((updateFlags&UpdItem.UPD_NROF) != 0) {
442  setNrOf(nrof);
443  }
444  fireModified();
445  }
446 
450  private void fireModified() {
451  if (!modified) {
452  return;
453  }
454  modified = false;
455 
456  for (final CfItemListener listener : listeners) {
457  listener.itemModified();
458  }
459  }
460 
466  public void addCfItemModifiedListener(@NotNull final CfItemListener listener) {
467  listeners.add(listener);
468  }
469 
474  public void removeCfItemModifiedListener(@NotNull final CfItemListener listener) {
475  listeners.remove(listener);
476  }
477 
482  @NotNull
483  public String getTooltipText() {
484  final String tooltipText1 = getTooltipText1();
485  final String tooltipText2 = getTooltipText2();
486  final String tooltipText3 = getTooltipText3();
487  final StringBuilder sb = new StringBuilder(tooltipText1);
488  if (!tooltipText2.isEmpty()) {
489  sb.append("<br>");
490  sb.append(tooltipText2);
491  }
492  if (!tooltipText3.isEmpty()) {
493  sb.append("<br>");
494  sb.append(tooltipText3);
495  }
496  return sb.toString();
497  }
498 
503  @NotNull
504  public String getTooltipText1() {
505  return nrof > 1 ? nrof+" "+namePl : name;
506  }
507 
512  @NotNull
513  public String getTooltipText2() {
514  final int totalWeight = nrof > 0 ? weight*nrof : weight;
515  if (totalWeight <= 0) {
516  return "";
517  }
518  if (totalWeight < 1000) {
519  return totalWeight+" g";
520  }
521  if (totalWeight < 10000) {
522  final int tmp = (totalWeight+50)/100;
523  return tmp/10+"."+tmp%10+" kg";
524  }
525  final int tmp = (totalWeight+500)/1000;
526  return tmp+" kg";
527  }
528 
533  @NotNull
534  public String getTooltipText3() {
535  final StringBuilder sb = new StringBuilder();
536  appendFlag(sb, F_UNIDENTIFIED, "unidentified");
537  appendFlag(sb, F_BLESSED, "blessed");
538  appendFlag(sb, F_MAGIC, "magic");
539  appendFlag(sb, F_CURSED, "cursed");
540  appendFlag(sb, F_DAMNED, "damned");
541  appendFlag(sb, F_UNPAID, "unpaid");
542  return sb.toString();
543  }
544 
551  private void appendFlag(@NotNull final StringBuilder sb, final int flag, @NotNull final String ident) {
552  if ((flags&flag) != 0) {
553  sb.append('(');
554  sb.append(ident);
555  sb.append(')');
556  }
557  }
558 
564  public boolean isItemGroupButton() {
565  return flags == 0 && weight == -1 && nrof == 0 && type == 0 && name.startsWith("Click here to see ");
566  }
567 
568 }
void setFlags(final int flags)
Updates the flags.
Definition: CfItem.java:189
boolean isUnpaid()
Returns whether this item is unpaid.
Definition: CfItem.java:342
String getTooltipText1()
Returns the first line of the tooltip text.
Definition: CfItem.java:504
boolean isLocked()
Returns whether this item is locked.
Definition: CfItem.java:390
boolean isMagic()
Returns whether this item is magical.
Definition: CfItem.java:350
String getTooltipText()
Returns a description suitable for a tooltip text.
Definition: CfItem.java:483
static final int F_UNPAID
The flags value for unpaid items.
Definition: CfItem.java:120
void setFace(@NotNull final Face face)
Updates the face.
Definition: CfItem.java:211
CfItem(final int location, final int tag, final int flags, final int weight, @NotNull final Face face, @NotNull final String name, @NotNull final String namePl, final int anim, final int animSpeed, final int nrof, final int type)
Creates a new instance.
Definition: CfItem.java:171
int UPD_FACE
The update flags value for face updates.
Definition: UpdItem.java:48
String getTooltipText3()
Returns the third line of the tooltip text.
Definition: CfItem.java:534
static final int F_NOPICK
The flags value for non-pickable items.
Definition: CfItem.java:145
int UPD_ANIMSPEED
The update flags value for animation speed updates.
Definition: UpdItem.java:63
void setName(@NotNull final String name, @NotNull final String namePl)
Updates the name.
Definition: CfItem.java:223
static final int F_OPEN
The flags value for opened items.
Definition: CfItem.java:140
boolean isCursed()
Returns whether this item is cursed.
Definition: CfItem.java:358
int nrof
The number of objects in the stack.
Definition: CfItem.java:84
int UPD_FLAGS
The update flags value for flags updates.
Definition: UpdItem.java:38
void addCfItemModifiedListener(@NotNull final CfItemListener listener)
Add a.
Definition: CfItem.java:466
Manages image information ("faces") needed to display the map view, items, and spell icons...
int getNrOf()
Returns the number of objects in this item stack.
Definition: CfItem.java:310
int getLocation()
Returns the location.
Definition: CfItem.java:398
boolean isNoPick()
Returns whether this item cannot be picked up.
Definition: CfItem.java:382
int UPD_NAME
The update flags value for name updates.
Definition: UpdItem.java:53
boolean isDamned()
Returns whether this item is damned.
Definition: CfItem.java:366
static final int F_BLESSED
The flags value for blessed items.
Definition: CfItem.java:115
void setAnim(final int anim)
Updates the animation.
Definition: CfItem.java:235
boolean isUnidentified()
Returns whether this item is unidentified.
Definition: CfItem.java:326
void removeCfItemModifiedListener(@NotNull final CfItemListener listener)
Remove a.
Definition: CfItem.java:474
void setNrOf(final int nrof)
Updates the number of objects in the stack.
Definition: CfItem.java:257
String getName()
Returns the name.
Definition: CfItem.java:302
static final int F_UNIDENTIFIED
The flags value for unidentified items.
Definition: CfItem.java:110
int animSpeed
The animation speed.
Definition: CfItem.java:79
final EventListenerList2< CfItemListener > listeners
The listeners to be notified.
Definition: CfItem.java:155
boolean isItemGroupButton()
Returns whether this object is a fake object for selecting object groups in the ground view...
Definition: CfItem.java:564
void update(final int updateFlags, final int flags, final int weight, @NotNull final Face face, @NotNull final String name, @NotNull final String namePl, final int anim, final int animSpeed, final int nrof)
Processes an "upditem" command.
Definition: CfItem.java:422
void setWeight(final int weight)
Updates the weight.
Definition: CfItem.java:200
void add(@NotNull final T listener)
Adds a listener.
String name
The singular name.
Definition: CfItem.java:63
String getTooltipText2()
Returns the second line of the tooltip text.
Definition: CfItem.java:513
int getWeight()
Returns the weight.
Definition: CfItem.java:284
Interface for listeners for attribute changes of CfItems.
void appendFlag(@NotNull final StringBuilder sb, final int flag, @NotNull final String ident)
Appends "(&lt;ident&gt;)" if this item has the flag.
Definition: CfItem.java:551
void setLocation(final int location)
Updates the location.
Definition: CfItem.java:268
static final int F_APPLIED
The flags mask for applied states.
Definition: CfItem.java:105
Interface defining constants for the "upditem" Crossfire protocol message.
Definition: UpdItem.java:28
boolean isBlessed()
Returns whether this item is blessed.
Definition: CfItem.java:334
int UPD_ANIM
The update flags value for animation updates.
Definition: UpdItem.java:58
int UPD_NROF
The update flags value for nrof updates.
Definition: UpdItem.java:68
void setAnimSpeed(final int animSpeed)
Updates the animation speed.
Definition: CfItem.java:246
int UPD_WEIGHT
The update flags value for weight updates.
Definition: UpdItem.java:43
static final int F_MAGIC
The flags value for magical items.
Definition: CfItem.java:125
The representation of a Crossfire Item, client-side.
Definition: CfItem.java:36
void remove(@NotNull final T listener)
Removes a listener.
boolean modified
Set if any attribute has changed since the last time listeners were notified.
Definition: CfItem.java:100
static final int F_LOCKED
The flags value for locked items.
Definition: CfItem.java:150
static final int F_CURSED
The flags value for cursed items.
Definition: CfItem.java:130
boolean isApplied()
Returns whether this item is applied.
Definition: CfItem.java:318
void fireModified()
Notify all listener.
Definition: CfItem.java:450
static final int F_DAMNED
The flags value for damned items.
Definition: CfItem.java:135
boolean isOpen()
Returns whether this item is an opened container.
Definition: CfItem.java:374