All Downloads are FREE. Search and download functionalities are using the official Maven repository.

cn.nukkit.block.BlockSlab Maven / Gradle / Ivy

There is a newer version: 1.20.40-r1
Show newest version
package cn.nukkit.block;

import cn.nukkit.Player;
import cn.nukkit.api.PowerNukkitOnly;
import cn.nukkit.api.PowerNukkitXOnly;
import cn.nukkit.api.Since;
import cn.nukkit.blockproperty.BlockProperties;
import cn.nukkit.blockproperty.BooleanBlockProperty;
import cn.nukkit.item.Item;
import cn.nukkit.item.ItemTool;
import cn.nukkit.math.BlockFace;
import org.jetbrains.annotations.NotNull;

import javax.annotation.Nullable;

/**
 * @author MagicDroidX (Nukkit Project)
 */
public abstract class BlockSlab extends BlockTransparentMeta {
    @PowerNukkitOnly
    @Since("1.4.0.0-PN")
    public static final BooleanBlockProperty TOP_SLOT_PROPERTY = new BooleanBlockProperty("top_slot_bit", false);

    @PowerNukkitOnly
    @Since("1.4.0.0-PN")
    public static final BlockProperties SIMPLE_SLAB_PROPERTIES = new BlockProperties(TOP_SLOT_PROPERTY);

    protected final int doubleSlab;

    public BlockSlab(int meta, int doubleSlab) {
        super(meta);
        this.doubleSlab = doubleSlab;
    }

    @PowerNukkitXOnly
    @Since("1.6.0.0-PNX")
    public BlockSlab(int doubleSlab){
        this.doubleSlab = doubleSlab;
    }

    @PowerNukkitOnly
    @Since("1.4.0.0-PN")
    public abstract String getSlabName();

    @Override
    public String getName() {
        return (isOnTop()? "Upper " : "") + getSlabName() + " Slab";
    }

    @Override
    public double getMinY() {
        return isOnTop() ? this.y + 0.5 : this.y;
    }

    @Override
    public double getMaxY() {
        return isOnTop() ? this.y + 1 : this.y + 0.5;
    }

    @Override
    public double getHardness() {
        return 2;
    }

    @Override
    public double getResistance() {
        return getToolType() < ItemTool.TYPE_AXE ? 30 : 15;
    }

    @PowerNukkitOnly
    @Override
    public int getWaterloggingLevel() {
        return 1;
    }
    
    @PowerNukkitOnly
    @Since("1.4.0.0-PN")
    public boolean isOnTop() {
        return getBooleanValue(TOP_SLOT_PROPERTY);
    }

    @PowerNukkitOnly
    @Since("1.4.0.0-PN")
    public void setOnTop(boolean top) {
        setBooleanValue(TOP_SLOT_PROPERTY, top);
    }

    @PowerNukkitOnly
    @Since("1.4.0.0-PN")
    public abstract boolean isSameType(BlockSlab slab);

    @Since("1.3.0.0-PN")
    @PowerNukkitOnly
    @Override
    public boolean isSolid(BlockFace side) {
        return side == BlockFace.UP && isOnTop() || side == BlockFace.DOWN && !isOnTop();
    }

    @Override
    public boolean place(@NotNull Item item, @NotNull Block block, @NotNull Block target, @NotNull BlockFace face, double fx, double fy, double fz, @Nullable Player player) {
        setOnTop(false);
        if (face == BlockFace.DOWN) {
            if (target instanceof BlockSlab && target.getBooleanValue(TOP_SLOT_PROPERTY) && isSameType((BlockSlab) target)) {
                this.getLevel().setBlock(target, getCurrentState().withBlockId(doubleSlab).getBlock(target), true);

                return true;
            } else if (block instanceof BlockSlab && isSameType((BlockSlab) block)) {
                this.getLevel().setBlock(block, getCurrentState().withBlockId(doubleSlab).getBlock(target), true);

                return true;
            } else {
                setOnTop(true);
            }
        } else if (face == BlockFace.UP) {
            if (target instanceof BlockSlab && !target.getBooleanValue(TOP_SLOT_PROPERTY) && isSameType((BlockSlab) target)) {
                this.getLevel().setBlock(target, getCurrentState().withBlockId(doubleSlab).getBlock(target), true);

                return true;
            } else if (block instanceof BlockSlab && isSameType((BlockSlab) block)) {
                this.getLevel().setBlock(block, getCurrentState().withBlockId(doubleSlab).getBlock(target), true);

                return true;
            }
            //TODO: check for collision
        } else {
            if (block instanceof BlockSlab) {
                if (isSameType((BlockSlab) block)) {
                    this.getLevel().setBlock(block, getCurrentState().withBlockId(doubleSlab).getBlock(block), true);

                    return true;
                }

                return false;
            } else {
                if (fy > 0.5) {
                    setOnTop(true);
                }
            }
        }

        if (block instanceof BlockSlab && !isSameType((BlockSlab) block)) {
            return false;
        }
        this.getLevel().setBlock(block, this, true, true);

        return true;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy