Resource [NMS] Get the color of any block!

Discussion in 'Spigot Plugin Development' started by Wasabi_Thumbs, Jul 15, 2021.

  1. I exposed a method in NMS that I don't see anyone talking about, and it was very handy for my camera implementation shown below.
    All of this required zero hard-coded mappings, because of this little snippet!

    Code (Java):
    import java.awt.Color;
    import org.bukkit.block.Block;
    import org.bukkit.craftbukkit.v1_16_R3.block.CraftBlock;
    // ...
    new Color(((CraftBlock) block).getNMS().getBlock().s().rgb);
    From what I understand, this gets the color that would be used on the normal map for a specific block. It also takes into account it's states. Let it be known that the ".s()" method here is an arbitrary name. Due to the nature of NMS, the name of the method is not guaranteed to stay the same between versions. So, just look for a method that takes in no arguments and produces a MaterialMapColor object.

    EDIT: It looks like the name of the ".rgb" member is also not guaranteed. As of 1.17.1, it's ".al". If you have IntelliJ IDEA or some other similar IDE, it should come with a decompiler that you can use to determine the name of the member you are looking for.
    #1 Wasabi_Thumbs, Jul 15, 2021
    Last edited: Jul 15, 2021
    • Useful x 4
    • Agree x 2
    • Like x 1
    • Winner x 1
    • Creative x 1
  2. Choco


    If you'd like, this is something that could have API be easily PR'd to Bukkit and CraftBukkit. The method could be added in the BlockData interface.
    • Winner Winner x 3
  3. Oh that would be sick! Gonna make a PR then. Thanks for the suggestion.
    • Like Like x 1
    • Agree Agree x 1
  4. Ah, I need my application to be accepted in order to be able to fork CraftBukkit. On JIRA or something. My proposed changes look like this:
    Code (Java):
    public Color getMapColor() {
         return new Color(state.getBlock().s().al);
    • Like Like x 1
  5. Choco


    They're reviewed every few days or so as far as I'm aware :) Looks good to me. Thank you for the future contribution! md_5 will likely have some comments.
  6. For example:
    - Add NotNull annotation
    - Add JavaDoc

    Did you look into your ItemType BlockType pr and found/implemented a well fit legacy support possibility? So that md is happy getting their legacy support.
    • Friendly Friendly x 1