Bed not spawning

Discussion in 'Spigot Plugin Development' started by Swords123456, Aug 14, 2018.

  1. The code I have tried.
    Code (Java):
    Block b = player.getLocation().getWorld().getBlockAt(player.getLocation());
                        Block b1 = player.getLocation().getWorld().getBlockAt(player.getLocation().add(0, 0, -1));

                        b.setType(Material.BED_BLOCK);

                        ((Bed) b.getState().getData()).setHeadOfBed(true);
                        ((Bed) b.getState().getData()).setFacingDirection(BlockFace.NORTH);

                        b1.setType(Material.BED);
                        ((Bed) b1.getState().getData()).setHeadOfBed(false);
                        ((Bed) b.getState().getData()).setFacingDirection(BlockFace.SOUTH);

                        System.out.println(b.getType());
                        System.out.println(b1.getType());
    But it looks like this, also im on 1.13
    [​IMG]
     
    • Agree Agree x 2
  2. Update:
    Code (Java):
    Block b = player.getLocation().getWorld().getBlockAt(player.getLocation());
                        Block b1 = player.getLocation().getWorld().getBlockAt(player.getLocation().add(0, 0, -1));

                        b.setType(Material.BED_BLOCK);

                        ((Bed) b.getState().getData()).setHeadOfBed(true);
                        ((Bed) b.getState().getData()).setFacingDirection(BlockFace.NORTH);

                        b1.setType(Material.BED_BLOCK);

                        ((Bed) b1.getState().getData()).setHeadOfBed(false);
                        ((Bed) b.getState().getData()).setFacingDirection(BlockFace.SOUTH);
    that code is now like this
    [​IMG]
     
  3. I think you need to set the block data to the block, like itemstack and itemmeta.
    Btw you should use this:
    Code (Java):
    p.getLocation().getBlock();
     
    • Useful Useful x 1
  4. hmm. ill look at a complete bed block, and get data by maybe an event and try to find some more info online
     
    • Agree Agree x 1
  5. and
    Do nothing as getState returns a *snapshot* of the state which you then modify but never use to update the state of the block. You need to do:
    Code (Java):

    /* Get a (deep) copy of the state of the block */
    BlockState state = b.getState();

    /* Get a (copy or reference?) of the data */
    Bed bed = (Bed) state.getData();

    /* Change the settings */
    bed.setHeadOfBed([B]true[/B]);
    bed.setFacingDirection(BlockFace.NORTH);

    /* Set the data for the blockstate (not required if bed was just a reference, but it's safe either way) */
    state.setData(bed);
    /* Use the state we've changed to update the block in the world. */
    state.update();
     
    I also notice you set the facing direction of b twice, the second time it should be b1.
     
    • Winner Winner x 1