How to check if block type is Bed in 1.13?

Discussion in 'Spigot Plugin Development' started by HeroCC, Aug 7, 2018.

  1. Hello everyone!

    I'm working on updating one of my plugins to 1.13 and I noticed that Material.BED_BLOCK / Material.BED no longer exist. I see lots of Material.COLOR_BED, but I'd rather not have to check each individual color.

    Without a helper function, is there a way to see if a block a player interacted with is a bed? Head or foot.

  2. Since the vanilla block id is differently, there are different Material types.
    You might check Block#getData instanceof
    • Agree Agree x 2
    • Useful Useful x 1
  3. Eventually I guess you could check for the string value of material and use .contains("BED") on it.
  4. Except that it includes bedrock.
    • Agree Agree x 2
  5. Code (Text):
    && item.getType != Material.BEDROCK
  6. Choco


    Unfortunately, Mojang didn't create as many tags as they should have -,- Glass (/ panes) and beds being some of the tags they should have added. Though with them not existing, you have two options:

    1. Make an unnecessarily long if statement to check all bed colours
    2. Create your own tag-like collection with an EnumSet listing all possible bed colours and check against it
    Code (Java):
    private static final Set<Material> BEDS = EnumSet.of(
        Material.BLACK_BED, Material.BLUE_BED, Material.BROWN_BED,
        Material.CYAN_BED, Material.GRAY_BED, Material.GREEN_BED,
        // So on and so forth... list all bed colours

    // Later in the code
    if (BEDS.contains(block.getType())) {
        // You have a bed!
    • Agree Agree x 2
    • Funny Funny x 2
  7. FrostedSnowman

    Resource Staff

    Code (Java):
    private static final Function<Block, Boolean> CHECK_BED = block -> block.getBlockData() instanceof Bed;

    if (CHECK_BED.apply(block)) {
         //block is a bed
  8. Choco


    I'm not sure that an instanceof check, especially on an unmodifiable object that gets created every time its method is invoked, is more performant than a #contains() call on an EnumSet.
    • Funny Funny x 1
  9. What about
    Code (Text):
    block instanceof org.bukkit.block.Bed
    block instanceof
    block instanceof org.bukkit.material.Bed
    They all seem to compile, do any work or is it inefficient?

  10. Any of these should work:
    Code (Java):
    block.getBlockData() instanceof // The one I used (1.13+ only)
    block.getBlockData().getMaterial() instanceof org.bukkit.material.Bed
    block instanceof org.bukkit.block.Bed // This one I haven't tested and is deprecated, you probably don't want it