Uploaded image for project: 'Spigot'
  1. Spigot
  2. SPIGOT-4586

Fix PotionEffectType#values to match Enum#values

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: Minor Minor
    • None
    • None
    • None
    • 1.13.2
    • Yes

      As I am on a few discord servers that focus on helping player coding spigot plugins, we ran into a quite strange issue when it comes to PotionEffectType#values.

      The task was to provide a list of all available PotionEffectTypes to a player and the code to achieve that was this: 

      Stream.of(PotionEffectType.values()).map(PotionEffectType::getName).forEach(player::sendMessage);
      

       

      One would expect that this prints a list of the 30 available PotionEffectTypes to the player, yet it causes a NullPointerException. 

      /**
       * Returns an array of all the registered {@link PotionEffectType}s.
       * This array is not necessarily in any particular order and may contain null.
       *
       * @return Array of types.
       */
      public static PotionEffectType[] values() {
          return byId.clone();
      }
      

      As this is the code to provide the array of PotionEffectTypes, it simply clones the array ordered by id. As the first PotionEffectType has the id 1 and is stored on index 1 instead of 0, the final array has a size of 31 values.

      The value at index 0 is therefore just null, which I find to be counterintuitive to what you would expect from the method. I would open a PR, but I would like confirmation that this is not intended. 

      The java docs state that there may be null values, but I still do not believe a method designed after the Enum#values method should. 

      The solution would be to simply use Arrays.copyOfRange(array , a , array.length) on the byID array

            Unassigned Unassigned
            LynxPlay Bjarne Koll
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: