Newer
Older
CactusServer / src / main / java / cactusServer / entities / Item.java
  1. package cactusServer.entities;
  2.  
  3. import net.arnx.jsonic.JSONHint;
  4.  
  5. public class Item extends Entity {
  6. private String name;
  7. private int amount;
  8.  
  9. @JSONHint(ignore = true)
  10. public static final int UNIQUE_ID_LENGTH = 12;
  11.  
  12. private Item() {
  13. // JSONDecode時の呼び出し用
  14. }
  15.  
  16. public Item(String name, int amount) {
  17. setName(name);
  18. setAmount(amount);
  19. }
  20.  
  21. public String getName() {
  22. return name;
  23. }
  24.  
  25. public int getAmount() {
  26. return amount;
  27. }
  28.  
  29. public boolean isEmpty() {
  30. return (amount == 0);
  31. }
  32.  
  33. public void setName(String name) {
  34. this.name = name;
  35. }
  36.  
  37. public void setAmount(int amount) {
  38. this.amount = amount;
  39. }
  40.  
  41. public Item changeAmount(int amountOfChange) {
  42. amount = Math.max(amount + amountOfChange, 0);
  43. return this;
  44. }
  45.  
  46. @Override
  47. public int hashCode() {
  48. int result = 17;
  49. result = result * 31 + name.hashCode();
  50. return result;
  51. }
  52.  
  53. @Override
  54. public boolean equals(java.lang.Object obj) {
  55. if (this == obj) {
  56. return true;
  57. }
  58. if (obj instanceof Item) {
  59. Item item = (Item) obj;
  60. if (this.name.equals(item.name)) {
  61. return true; // 同名のアイテムは等価
  62. }
  63. }
  64. return false;
  65. }
  66. }