 |
Crossfire JXClient, Trunk
|
Go to the documentation of this file.
23 package com.realtime.crossfire.jxclient.items;
29 import java.util.Collection;
30 import java.util.Collections;
31 import java.util.HashMap;
32 import java.util.HashSet;
33 import java.util.List;
34 import java.util.ListIterator;
36 import java.util.concurrent.CopyOnWriteArrayList;
37 import org.jetbrains.annotations.NotNull;
38 import org.jetbrains.annotations.Nullable;
51 private final Map<Integer, CfItem>
allItems =
new HashMap<>();
57 private final Map<Integer, List<CfItem>>
items =
new HashMap<>();
64 private final Object
sync =
new Object();
133 synchronized (
sync) {
134 final List<CfItem> result =
items.get(location);
135 if (result ==
null) {
136 return Collections.emptyList();
138 return Collections.unmodifiableList(result);
149 synchronized (
sync) {
150 final Collection<CfItem> result =
items.get(location);
151 return result ==
null ? 0 : result.size();
163 synchronized (
sync) {
170 final List<CfItem> list =
items.get(where);
172 throw new AssertionError(
"cannot find item "+item.
getTag());
175 index = list.indexOf(item);
176 if (list.remove(index) ==
null) {
177 throw new AssertionError(
"cannot find item "+item.
getTag());
181 throw new AssertionError();
186 itemListener.inventoryRemoved(where, index);
190 itemListener.itemRemoved(tag);
201 for (
int tag : tags) {
203 System.err.println(
"removeItem3: item "+tag+
" does not exist");
216 synchronized (
sync) {
217 if (
allItems.put(item.getTag(), item) !=
null) {
218 throw new AssertionError(
"duplicate item "+item.getTag());
221 where = item.getLocation();
222 List<CfItem> list =
items.get(where);
224 list =
new CopyOnWriteArrayList<>();
225 if (
items.put(where, list) !=
null) {
226 throw new AssertionError();
231 index = list.size()-1;
235 itemListener.inventoryAdded(where, index, item);
246 synchronized (
sync) {
247 final List<CfItem> inventory =
items.get(tag);
248 if (inventory ==
null) {
249 return Collections.emptyList();
251 return Collections.unmodifiableList(inventory);
262 synchronized (
sync) {
273 synchronized (
sync) {
283 synchronized (
sync) {
284 if (this.player ==
player) {
292 listener.playerChanged(
player);
302 synchronized (
sync) {
314 synchronized (
sync) {
329 final ListIterator<CfItem> it = inventoryItems.listIterator(inventoryItems.size());
330 while (it.hasPrevious()) {
331 final CfItem item = it.previous();
350 public void updateItem(
final int flags,
final int tag,
final int valLocation,
final int valFlags,
final int valWeight, @NotNull
final Face valFace, @NotNull
final String valName, @NotNull
final String valNamePl,
final int valAnim,
final int valAnimSpeed,
final int valNrof) {
357 final boolean wasOpen;
358 final boolean clearMarked;
359 synchronized (
sync) {
363 item.
update(flags, valFlags, valWeight, valFace, valName, valNamePl, valAnim, valAnimSpeed, valNrof, clearMarked);
372 itemListener.itemChanged(tag);
378 }
else if (wasOpen) {
389 synchronized (
sync) {
392 if (playerTag != -1) {
395 final Iterable<CfItem> tmp;
396 synchronized (
sync) {
397 tmp =
new HashSet<>(
allItems.values());
411 synchronized (
sync) {
429 synchronized (
sync) {
444 return 0 <= index && index < inventoryItems.size() ? inventoryItems.get(index) :
null;
445 }
catch (
final ArrayIndexOutOfBoundsException ignored) {
457 final List<CfItem> inventory;
458 synchronized (
sync) {
464 if (inventory ==
null) {
469 for (
final CfItem item : inventory) {
470 item.setMarked(item.getTag() == markedItemTag);
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, final boolean clearMarked)
Processes an "upditem" command.
CfItem getInventoryItem(final int tag, final int index)
Returns a CfItem from the inventory of an item.
void addInventoryListener(final int tag, @NotNull final ItemListener listener)
Adds an ItemListener to be notified about changes.
int openContainerFloor
The currently opened container or.
List< CfItem > getItemsByLocation(final int location)
Returns a list of items in a given location.
void reset()
Resets the manager's state.
Model class maintaining the CfItems known to the player.
CfItem player
The current player object this client controls.
final Object sync
The synchronization object for allItems, items, player, or openContainerFloor.
CfItem getItemByTag(final int tag)
Returns an item by tag.
final Map< Integer, CfItem > allItems
Maps item tags to items.
Iterable< CfItem > getPlayerInventory()
Returns the player's inventory.
final HashedEventListenerList< ItemListener > itemListeners
The registered ItemListeners to be notified about changes.
void setPlayer(@Nullable final CfItem player)
Sets the player object this client controls.
Interface for listeners for changes of item locations.
Interface defining constants for the "upditem" Crossfire protocol message.
Manages image information ("faces") needed to display the map view, items, and spell icons.
int getOpenContainer()
Returns the currently opened container.
A list of event listeners.
void setOpenContainer(final int openContainerFloor)
Sets the currently opened container.
boolean isOpen()
Returns whether this item is an opened container.
void setMarkedItemTag(final int markedItemTag)
Sets the marked item.
The representation of a Crossfire Item, client-side.
int getTag()
Returns the tag.
int UPD_FLAGS
The update flags value for flags updates.
void cleanInventory(final int tag)
Clears the inventory of an item.
int UPD_LOCATION
The update flags value for location updates.
int getNumberOfItemsByLocation(final int location)
Returns the number of items in a given location.
final EventListenerList2< ItemSetListener > itemSetListeners
The list of ItemSetListeners to be notified about changes.
void addItemSetListener(@NotNull final ItemSetListener listener)
Adds an ItemSetListener to be notified about changes.
CfItem getPlayer()
Returns the player object this client controls.
CfItem getItemOrPlayer(final int tag)
Returns an item by tag.
void updateItem(final int flags, final int tag, final int valLocation, final int valFlags, final int valWeight, @NotNull final Face valFace, @NotNull final String valName, @NotNull final String valNamePl, final int valAnim, final int valAnimSpeed, final int valNrof)
Processes an "upditem" command.
int removeItemByTag(final int tag)
Removes a CfItem.
int getLocation()
Returns the location.
List< CfItem > getInventoryByTag(final int tag)
Returns the inventory of an item.
void setLocation(final int location)
Updates the location.
void addItem(@NotNull final CfItem item)
Adds an item.
Interface for listeners in ItemSet related events.
final Map< Integer, List< CfItem > > items
Maps location (=tag) to list of items in that location.
void removeItems(final int @NotNull[] tags)
Deletes items by tag.
void removeItemSetListener(@NotNull final ItemSetListener listener)
Removes an ItemSetListener to be notified about changes.
void removeInventoryListener(final int tag, @NotNull final ItemListener listener)
Removes an ItemListener to be notified about changes.