PlayerInventory.java 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. package cz.davza.studentadventure.objects;
  2. import cz.davza.studentadventure.base.ItemCollectionBase;
  3. import cz.davza.studentadventure.enums.CommandState;
  4. import cz.davza.studentadventure.interfaces.IItem;
  5. import cz.davza.studentadventure.interfaces.IObservable;
  6. import cz.davza.studentadventure.interfaces.IObserver;
  7. import java.util.HashMap;
  8. import java.util.HashSet;
  9. import java.util.Map;
  10. import java.util.Set;
  11. /**
  12. * The player's personal item inventory, with Observer support for change notifications.
  13. *
  14. * <p>Extends {@link ItemCollectionBase} with the full slot-based stacking logic,
  15. * and additionally implements {@link IObservable} so that external listeners
  16. * (such as a GUI panel) can react whenever the inventory contents change.</p>
  17. *
  18. * <p>Every call to {@link #Add} or {@link #Remove} automatically fires a
  19. * {@link CommandState#INVENTORY_CHANGE} notification to all registered observers
  20. * after delegating the operation to the superclass.</p>
  21. *
  22. * @see cz.davza.studentadventure.base.ItemCollectionBase
  23. * @see IObservable
  24. * @see IObserver
  25. */
  26. public class PlayerInventory extends ItemCollectionBase<IItem> implements IObservable {
  27. private final Map<CommandState, Set<IObserver>> observers = new HashMap<>();
  28. /**
  29. * Creates a new inventory with the given number of bulk-unit capacity.
  30. *
  31. * @param size the total bulk capacity of this inventory
  32. */
  33. public PlayerInventory(int size) {
  34. super(size);
  35. }
  36. /**
  37. * {@inheritDoc}
  38. *
  39. * <p>After the item is added (or partially added) by the superclass,
  40. * all observers registered for {@link CommandState#INVENTORY_CHANGE} are notified.</p>
  41. */
  42. @Override
  43. public int Add(IItem item, int count){
  44. int result = super.Add(item, count);
  45. notifyObservers(CommandState.INVENTORY_CHANGE);
  46. return result;
  47. }
  48. /**
  49. * {@inheritDoc}
  50. *
  51. * <p>After the item is removed by the superclass,
  52. * all observers registered for {@link CommandState#INVENTORY_CHANGE} are notified.</p>
  53. */
  54. @Override
  55. public void Remove(String itemCode, int count) {
  56. super.Remove(itemCode, count);
  57. notifyObservers(CommandState.INVENTORY_CHANGE);
  58. }
  59. /**
  60. * {@inheritDoc}
  61. */
  62. @Override
  63. public void addObserver(CommandState observedstate, IObserver o) {
  64. if (!observers.containsKey(observedstate)) observers.put(observedstate, new HashSet<>());
  65. observers.get(observedstate).add(o);
  66. }
  67. /**
  68. * {@inheritDoc}
  69. */
  70. @Override
  71. public void notifyObservers(CommandState notifiedState) {
  72. if (!observers.containsKey(notifiedState)) return;
  73. for (IObserver observer : observers.get(notifiedState)) {
  74. observer.update();
  75. }
  76. }
  77. }