fiveriverflow

Consumable

Oct 11th, 2019
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.22 KB | None | 0 0
  1. public enum Consumable {
  2.  
  3.     ATTACK_POTION(Type.COMBAT_POTION, Items.ATTACK_POTION_4, Items.ATTACK_POTION_3, Items.ATTACK_POTION_2, Items.ATTACK_POTION_1) {
  4.         @Override
  5.         public void onConsumption() {
  6.  
  7.         }
  8.     },
  9.  
  10.     ;
  11.  
  12.     private Type type;
  13.     private int[] ids;
  14.  
  15.     Consumable(Type type, int... ids) {
  16.         this.ids = ids;
  17.         this.type = type;
  18.     }
  19.  
  20.     public abstract void onConsumption();
  21.  
  22.     public Item replacement(int id) {
  23.         if (isConsumablePotion(id)) {
  24.             int index = getIds().indexOf(id);
  25.             if (index == getIds().size())
  26.                 return new Item(229);
  27.             else
  28.                 return new Item(index + 1);
  29.         }
  30.         return new Item(-1);
  31.     }
  32.  
  33.     public List<Integer> getIds() {
  34.         return Arrays.stream(ids).boxed().collect(Collectors.toList());
  35.     }
  36.  
  37.     public static Optional<Consumable> get(int id) {
  38.         return Arrays.stream(values()).filter(consumable -> consumable.getIds().contains(id)).findFirst();
  39.     }
  40.  
  41.     private boolean isConsumablePotion(int id) {
  42.         return Arrays.asList(Type.COMBAT_POTION, Type.RECOVERY_POTION, Type.CURES_OR_ANTI_POTION, Type.SKILL_POTION, Type.WEAPON_POISON_POTION).contains(id);
  43.     }
  44.  
  45.     public enum Type {
  46.         UNFINISHED_POTION, COMBAT_POTION, RECOVERY_POTION, CURES_OR_ANTI_POTION, SKILL_POTION, WEAPON_POISON_POTION,
  47.     }
  48.  
  49. }
Add Comment
Please, Sign In to add comment