00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 package com.realtime.crossfire.jxclient.items;
00023
00024 import com.realtime.crossfire.jxclient.faces.Face;
00025 import com.realtime.crossfire.jxclient.server.crossfire.messages.UpdItem;
00026 import com.realtime.crossfire.jxclient.util.EventListenerList2;
00027 import org.jetbrains.annotations.NotNull;
00028
00036 public class CfItem {
00037
00041 private final int tag;
00042
00046 private int flags;
00047
00051 private int weight;
00052
00056 @NotNull
00057 private Face face;
00058
00062 @NotNull
00063 private String name;
00064
00068 @NotNull
00069 private String namePl;
00070
00074 private int anim;
00075
00079 private int animSpeed;
00080
00084 private int nrof;
00085
00089 private int location;
00090
00094 private final int type;
00095
00100 private boolean modified = true;
00101
00105 public static final int F_APPLIED = 0x000F;
00106
00110 public static final int F_UNIDENTIFIED = 0x0010;
00111
00115 public static final int F_BLESSED = 0x0100;
00116
00120 public static final int F_UNPAID = 0x0200;
00121
00125 public static final int F_MAGIC = 0x0400;
00126
00130 public static final int F_CURSED = 0x0800;
00131
00135 public static final int F_DAMNED = 0x1000;
00136
00140 public static final int F_OPEN = 0x2000;
00141
00145 public static final int F_NOPICK = 0x4000;
00146
00150 public static final int F_LOCKED = 0x8000;
00151
00155 private final EventListenerList2<CfItemListener> listeners = new EventListenerList2<CfItemListener>(CfItemListener.class);
00156
00171 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) {
00172 this.location = location;
00173 this.tag = tag;
00174 this.flags = flags;
00175 this.weight = weight;
00176 this.face = face;
00177 this.name = name;
00178 this.namePl = namePl;
00179 this.anim = anim;
00180 this.animSpeed = animSpeed;
00181 this.nrof = nrof;
00182 this.type = type;
00183 }
00184
00189 private void setFlags(final int flags) {
00190 if (this.flags != flags) {
00191 this.flags = flags;
00192 modified = true;
00193 }
00194 }
00195
00200 private void setWeight(final int weight) {
00201 if (this.weight != weight) {
00202 this.weight = weight;
00203 modified = true;
00204 }
00205 }
00206
00211 private void setFace(@NotNull final Face face) {
00212 if (this.face != face) {
00213 this.face = face;
00214 modified = true;
00215 }
00216 }
00217
00223 private void setName(@NotNull final String name, @NotNull final String namePl) {
00224 if (!this.name.equals(name) || !this.namePl.equals(namePl)) {
00225 this.name = name;
00226 this.namePl = namePl;
00227 modified = true;
00228 }
00229 }
00230
00235 private void setAnim(final int anim) {
00236 if (this.anim != anim) {
00237 this.anim = anim;
00238 modified = true;
00239 }
00240 }
00241
00246 private void setAnimSpeed(final int animSpeed) {
00247 if (this.animSpeed != animSpeed) {
00248 this.animSpeed = animSpeed;
00249 modified = true;
00250 }
00251 }
00252
00257 private void setNrOf(final int nrof) {
00258 if (this.nrof != nrof) {
00259 this.nrof = nrof;
00260 modified = true;
00261 }
00262 }
00263
00268 public void setLocation(final int location) {
00269 this.location = location;
00270 }
00271
00276 public int getTag() {
00277 return tag;
00278 }
00279
00284 public int getWeight() {
00285 return weight;
00286 }
00287
00292 @NotNull
00293 public Face getFace() {
00294 return face;
00295 }
00296
00301 @NotNull
00302 public String getName() {
00303 return nrof > 1 ? namePl : name;
00304 }
00305
00310 public int getNrOf() {
00311 return nrof;
00312 }
00313
00318 public boolean isApplied() {
00319 return (flags&F_APPLIED) != 0;
00320 }
00321
00326 public boolean isUnidentified() {
00327 return (flags&F_UNIDENTIFIED) != 0;
00328 }
00329
00334 public boolean isBlessed() {
00335 return (flags&F_BLESSED) != 0;
00336 }
00337
00342 public boolean isUnpaid() {
00343 return (flags&F_UNPAID) != 0;
00344 }
00345
00350 public boolean isMagic() {
00351 return (flags&F_MAGIC) != 0;
00352 }
00353
00358 public boolean isCursed() {
00359 return (flags&F_CURSED) != 0;
00360 }
00361
00366 public boolean isDamned() {
00367 return (flags&F_DAMNED) != 0;
00368 }
00369
00374 public boolean isOpen() {
00375 return (flags&F_OPEN) != 0;
00376 }
00377
00382 public boolean isNoPick() {
00383 return (flags&F_NOPICK) != 0;
00384 }
00385
00390 public boolean isLocked() {
00391 return (flags&F_LOCKED) != 0;
00392 }
00393
00398 public int getLocation() {
00399 return location;
00400 }
00401
00406 public int getType() {
00407 return type;
00408 }
00409
00422 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) {
00423 if ((updateFlags&UpdItem.UPD_FLAGS) != 0) {
00424 setFlags(flags);
00425 }
00426 if ((updateFlags&UpdItem.UPD_WEIGHT) != 0) {
00427 setWeight(weight);
00428 }
00429 if ((updateFlags&UpdItem.UPD_FACE) != 0) {
00430 setFace(face);
00431 }
00432 if ((updateFlags&UpdItem.UPD_NAME) != 0) {
00433 setName(name, namePl);
00434 }
00435 if ((updateFlags&UpdItem.UPD_ANIM) != 0) {
00436 setAnim(anim);
00437 }
00438 if ((updateFlags&UpdItem.UPD_ANIMSPEED) != 0) {
00439 setAnimSpeed(animSpeed);
00440 }
00441 if ((updateFlags&UpdItem.UPD_NROF) != 0) {
00442 setNrOf(nrof);
00443 }
00444 fireModified();
00445 }
00446
00450 private void fireModified() {
00451 if (!modified) {
00452 return;
00453 }
00454 modified = false;
00455
00456 for (final CfItemListener listener : listeners.getListeners()) {
00457 listener.itemModified();
00458 }
00459 }
00460
00466 public void addCfItemModifiedListener(@NotNull final CfItemListener listener) {
00467 listeners.add(listener);
00468 }
00469
00474 public void removeCfItemModifiedListener(@NotNull final CfItemListener listener) {
00475 listeners.remove(listener);
00476 }
00477
00482 @NotNull
00483 public String getTooltipText() {
00484 final String tooltipText1 = getTooltipText1();
00485 final String tooltipText2 = getTooltipText2();
00486 final String tooltipText3 = getTooltipText3();
00487 final StringBuilder sb = new StringBuilder(tooltipText1);
00488 if (tooltipText2.length() > 0) {
00489 sb.append("<br>");
00490 sb.append(tooltipText2);
00491 }
00492 if (tooltipText3.length() > 0) {
00493 sb.append("<br>");
00494 sb.append(tooltipText3);
00495 }
00496 return sb.toString();
00497 }
00498
00503 @NotNull
00504 public String getTooltipText1() {
00505 return nrof > 1 ? nrof+" "+namePl : name;
00506 }
00507
00512 @NotNull
00513 public String getTooltipText2() {
00514 final int totalWeight = nrof > 0 ? weight*nrof : weight;
00515 if (totalWeight <= 0) {
00516 return "";
00517 }
00518 if (totalWeight < 1000) {
00519 return totalWeight+" g";
00520 }
00521 if (totalWeight < 10000) {
00522 final int tmp = (totalWeight+50)/100;
00523 return tmp/10+"."+tmp%10+" kg";
00524 }
00525 final int tmp = (totalWeight+500)/1000;
00526 return tmp+" kg";
00527 }
00528
00533 @NotNull
00534 public String getTooltipText3() {
00535 final StringBuilder sb = new StringBuilder();
00536 appendFlag(sb, F_UNIDENTIFIED, "unidentified");
00537 appendFlag(sb, F_BLESSED, "blessed");
00538 appendFlag(sb, F_MAGIC, "magic");
00539 appendFlag(sb, F_CURSED, "cursed");
00540 appendFlag(sb, F_DAMNED, "damned");
00541 appendFlag(sb, F_UNPAID, "unpaid");
00542 return sb.toString();
00543 }
00544
00552 private void appendFlag(@NotNull final StringBuilder sb, final int flag, @NotNull final String ident) {
00553 if ((flags&flag) != 0) {
00554 sb.append('(');
00555 sb.append(ident);
00556 sb.append(')');
00557 }
00558 }
00559
00565 public boolean isItemGroupButton() {
00566 return flags == 0 && weight == -1 && nrof == 0 && type == 0 && name.startsWith("Click here to see ");
00567 }
00568
00569 }