<commit_before>package net.glowstone.block.blocktype;

import net.glowstone.GlowWorld;
import net.glowstone.block.GlowBlock;
import net.glowstone.block.GlowBlockState;
import net.glowstone.entity.GlowPlayer;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.block.Biome;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
import org.bukkit.block.BlockState;
import org.bukkit.entity.Creature;
import org.bukkit.entity.Entity;
import org.bukkit.entity.LivingEntity;
import org.bukkit.inventory.ItemStack;
import org.bukkit.material.Bed;
import org.bukkit.material.MaterialData;
import org.bukkit.util.Vector;

public class BlockBed extends BlockType {

    public BlockBed() {
        setDrops(new ItemStack(Material.BED));
    }

    @Override
    public void placeBlock(GlowPlayer player, GlowBlockState state, BlockFace face, ItemStack holding, Vector clickedLoc) {
        BlockFace direction = getOppositeBlockFace(player.getLocation(), false).getOppositeFace();
        if (state.getBlock().getRelative(direction).getType() == Material.AIR && state.getBlock().getRelative(direction).getRelative(BlockFace.DOWN).getType().isSolid()) {
            super.placeBlock(player, state, face, holding, clickedLoc);
            final MaterialData data = state.getData();
            if (data instanceof Bed) {
                ((Bed) data).setFacingDirection(direction);
                state.setData(data);
            } else {
                warnMaterialData(Bed.class, data);
            }
        }
    }

    @Override
    public void afterPlace(GlowPlayer player, GlowBlock block, ItemStack holding) {
        if (block.getType() == Material.BED_BLOCK) {
            BlockFace direction = ((Bed) block.getState().getData()).getFacing();
            GlowBlock headBlock = block.getRelative(direction);
            headBlock.setType(Material.BED_BLOCK);
            GlowBlockState headBlockState = headBlock.getState();
            MaterialData data = headBlockState.getData();
            ((Bed) data).setHeadOfBed(true);
            ((Bed) data).setFacingDirection(direction);
            headBlockState.setData(data);
            headBlockState.update(true);
        }
    }

    @Override
    public boolean blockInteract(GlowPlayer player, GlowBlock block, BlockFace face, Vector clickedLoc) {
        GlowWorld world = player.getWorld();
        MaterialData data = block.getState().getData();
        if (!(data instanceof Bed)) {
            warnMaterialData(Bed.class, data);
            return false;
        }
        block = getHead(block);

        // Disallow sleeping in nether and end biomes
        Biome biome = block.getBiome();
        if (biome == Biome.HELL || biome == Biome.SKY) {
            // Set off an explosion at the bed slightly stronger than TNT
            world.createExplosion(block.getLocation(), 5F, true);
            return true;
        }

        // Sleeping is only possible during the night or a thunderstorm
        // Tick values for day/night time taken from the minecraft wiki
        if (world.getTime() < 12541 || world.getTime() > 23458 || world.isThundering()) {
            player.sendMessage("You can only sleep at night");
            return true;
        }

        if (((Bed) data).isOccupied()) {
            player.sendMessage("This bed is occupied.");
            return true;
        }

        if (!isWithinDistance(player, block, 3, 2, 3)) {
            return true; // Distance between player and bed is too great, fail silently
        }

        for (LivingEntity e : world.getLivingEntities()) {
            // Check for hostile mobs relative to the block below the head of the bed
            if (e instanceof Creature && isWithinDistance(e, block.getRelative(BlockFace.DOWN), 8, 5, 8)) {
                player.sendMessage("You may not rest now, there are monsters nearby");
                return true;
            }
        }

        player.enterBed(block);
        return true;
    }

    /**
     * Helper method for set whether the specified bed blocks are occupied.
     * @param head head of the bed
     * @param foot foot of the bed
     */
    public static void setOccupied(GlowBlock head, GlowBlock foot, boolean occupied) {
        BlockState headState = head.getState();
        BlockState footState = foot.getState();
        Bed headData = (Bed) headState.getData();
        Bed footData = (Bed) footState.getData();
        headData.setOccupied(occupied);
        footData.setOccupied(occupied);
        headState.setData(headData);
        footState.setData(footData);
        headState.update();
        footState.update();
    }

    /**
     * Checks whether the entity is within the specified distance from the block.
     * @param entity the entity
     * @param block the block
     * @param x maximum distance on x axis
     * @param y maximum distance on y axis
     * @param z maximum distance on z axis
     * @return Whether the entity is within distance<commit_msg>Move set/getOccupied to Glowstone instead of Glowkit<commit_after>package net.glowstone.block.blocktype;

import net.glowstone.GlowWorld;
import net.glowstone.block.GlowBlock;
import net.glowstone.block.GlowBlockState;
import net.glowstone.entity.GlowPlayer;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.block.Biome;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
import org.bukkit.entity.Creature;
import org.bukkit.entity.Entity;
import org.bukkit.entity.LivingEntity;
import org.bukkit.inventory.ItemStack;
import org.bukkit.material.Bed;
import org.bukkit.material.MaterialData;
import org.bukkit.util.Vector;

public class BlockBed extends BlockType {

    public BlockBed() {
        setDrops(new ItemStack(Material.BED));
    }

    @Override
    public void placeBlock(GlowPlayer player, GlowBlockState state, BlockFace face, ItemStack holding, Vector clickedLoc) {
        BlockFace direction = getOppositeBlockFace(player.getLocation(), false).getOppositeFace();
        if (state.getBlock().getRelative(direction).getType() == Material.AIR && state.getBlock().getRelative(direction).getRelative(BlockFace.DOWN).getType().isSolid()) {
            super.placeBlock(player, state, face, holding, clickedLoc);
            final MaterialData data = state.getData();
            if (data instanceof Bed) {
                ((Bed) data).setFacingDirection(direction);
                state.setData(data);
            } else {
                warnMaterialData(Bed.class, data);
            }
        }
    }

    @Override
    public void afterPlace(GlowPlayer player, GlowBlock block, ItemStack holding) {
        if (block.getType() == Material.BED_BLOCK) {
            BlockFace direction = ((Bed) block.getState().getData()).getFacing();
            GlowBlock headBlock = block.getRelative(direction);
            headBlock.setType(Material.BED_BLOCK);
            GlowBlockState headBlockState = headBlock.getState();
            MaterialData data = headBlockState.getData();
            ((Bed) data).setHeadOfBed(true);
            ((Bed) data).setFacingDirection(direction);
            headBlockState.setData(data);
            headBlockState.update(true);
        }
    }

    @Override
    public boolean blockInteract(GlowPlayer player, GlowBlock block, BlockFace face, Vector clickedLoc) {
        GlowWorld world = player.getWorld();
        MaterialData data = block.getState().getData();
        if (!(data instanceof Bed)) {
            warnMaterialData(Bed.class, data);
            return false;
        }
        block = getHead(block);

        // Disallow sleeping in nether and end biomes
        Biome biome = block.getBiome();
        if (biome == Biome.HELL || biome == Biome.SKY) {
            // Set off an explosion at the bed slightly stronger than TNT
            world.createExplosion(block.getLocation(), 5F, true);
            return true;
        }

        // Sleeping is only possible during the night or a thunderstorm
        // Tick values for day/night time taken from the minecraft wiki
        if (world.getTime() < 12541 || world.getTime() > 23458 || world.isThundering()) {
            player.sendMessage("You can only sleep at night");
            return true;
        }

        if (isOccupied(block)) {
            player.sendMessage("This bed is occupied.");
            return true;
        }

        if (!isWithinDistance(player, block, 3, 2, 3)) {
            return true; // Distance between player and bed is too great, fail silently
        }

        for (LivingEntity e : world.getLivingEntities()) {
            // Check for hostile mobs relative to the block below the head of the bed
            if (e instanceof Creature && isWithinDistance(e, block.getRelative(BlockFace.DOWN), 8, 5, 8)) {
                player.sendMessage("You may not rest now, there are monsters nearby");
                return true;
            }
        }

        player.enterBed(block);
        return true;
    }

    /**
     * Helper method for set whether the specified bed blocks are occupied.
     * @param head head of the bed
     * @param foot foot of the bed
     */
    public static void setOccupied(GlowBlock head, GlowBlock foot, boolean occupied) {
        byte headData = head.getData();
        byte footData = foot.getData();
        head.setData((byte) (occupied ? (headData | 0x4) : (headData & ~0x4)));
        foot.setData((byte) (occupied ? (footData | 0x4) : (footData & ~0x4)));
    }

    /**
     * Return whether the specified bed block is occupied.
     * @param block part of the bed
     * @return true if this bed is occupied, false if it is not
     */
    public static boolean isOccupied(GlowBlock block) {
        return (block.getData() & 0x4) == 0x4;
    }

    /**
     * Checks whether the entity is within the specified distance from the block.
     * @param entity the entity
     * @param block the block
     * @param x maximum distance on x axis
     * @param y maximum distance on y axis
     * @param z maximum distance on z axis
     * @return Whether the entity is within distance
{'fraction_non_alphanumeric': 0.10088932806324111, 'fraction_numerical': 0.004545454545454545, 'mean_word_length': 2.5264808362369338, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6717563', 'n_tokens_mistral': 2885, 'n_tokens_neox': 2748, 'n_words': 789}