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);