Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
cn.nukkit.block.BlockSignPost Maven / Gradle / Ivy
package cn.nukkit.block;
import cn.nukkit.Player;
import cn.nukkit.api.PowerNukkitDifference;
import cn.nukkit.api.PowerNukkitOnly;
import cn.nukkit.api.Since;
import cn.nukkit.blockentity.BlockEntity;
import cn.nukkit.blockentity.BlockEntitySign;
import cn.nukkit.blockproperty.ArrayBlockProperty;
import cn.nukkit.blockproperty.BlockProperties;
import cn.nukkit.blockproperty.BlockProperty;
import cn.nukkit.blockstate.BlockState;
import cn.nukkit.event.block.SignColorChangeEvent;
import cn.nukkit.event.block.SignGlowEvent;
import cn.nukkit.item.Item;
import cn.nukkit.item.ItemGlowInkSac;
import cn.nukkit.item.ItemSign;
import cn.nukkit.item.ItemTool;
import cn.nukkit.level.Level;
import cn.nukkit.math.AxisAlignedBB;
import cn.nukkit.math.BlockFace;
import cn.nukkit.math.CompassRoseDirection;
import cn.nukkit.nbt.tag.CompoundTag;
import cn.nukkit.nbt.tag.Tag;
import cn.nukkit.network.protocol.LevelEventPacket;
import cn.nukkit.utils.BlockColor;
import cn.nukkit.utils.DyeColor;
import cn.nukkit.utils.Faceable;
import lombok.extern.log4j.Log4j2;
import org.jetbrains.annotations.NotNull;
import javax.annotation.Nullable;
import static cn.nukkit.blockproperty.CommonBlockProperties.FACING_DIRECTION;
import static cn.nukkit.math.CompassRoseDirection.*;
/**
* @author Nukkit Project Team
*/
@PowerNukkitDifference(since = "1.4.0.0-PN", info = "Implements BlockEntityHolder only in PowerNukkit")
@Log4j2
public class BlockSignPost extends BlockTransparentMeta implements Faceable, BlockEntityHolder {
@PowerNukkitOnly
@Since("1.4.0.0-PN")
public static final BlockProperty GROUND_SIGN_DIRECTION = new ArrayBlockProperty<>("ground_sign_direction", false, new CompassRoseDirection[]{
SOUTH, SOUTH_SOUTH_WEST, SOUTH_WEST, WEST_SOUTH_WEST,
WEST, WEST_NORTH_WEST, NORTH_WEST, NORTH_NORTH_WEST,
NORTH, NORTH_NORTH_EAST, NORTH_EAST, EAST_NORTH_EAST,
EAST, EAST_SOUTH_EAST, SOUTH_EAST, SOUTH_SOUTH_EAST
}).ordinal(true);
@PowerNukkitOnly
@Since("1.4.0.0-PN")
public static final BlockProperties PROPERTIES = new BlockProperties(GROUND_SIGN_DIRECTION);
public BlockSignPost() {
this(0);
}
public BlockSignPost(int meta) {
super(meta);
}
@Override
public int getId() {
return SIGN_POST;
}
@Since("1.4.0.0-PN")
@PowerNukkitOnly
@NotNull
@Override
public Class extends BlockEntitySign> getBlockEntityClass() {
return BlockEntitySign.class;
}
@PowerNukkitOnly
@Since("1.4.0.0-PN")
@NotNull
@Override
public String getBlockEntityType() {
return BlockEntity.SIGN;
}
@Since("1.4.0.0-PN")
@PowerNukkitOnly
@NotNull
@Override
public BlockProperties getProperties() {
return PROPERTIES;
}
@Override
public double getHardness() {
return 1;
}
@Override
public double getResistance() {
return 5;
}
@Override
public boolean isSolid() {
return false;
}
@Since("1.3.0.0-PN")
@PowerNukkitOnly
@Override
public boolean isSolid(BlockFace side) {
return false;
}
@Override
public String getName() {
return "Sign Post";
}
@Override
public AxisAlignedBB getBoundingBox() {
return null;
}
@PowerNukkitOnly
@Override
public int getWaterloggingLevel() {
return 1;
}
@PowerNukkitOnly
protected int getPostId() {
return getId();
}
@PowerNukkitOnly
public int getWallId() {
return WALL_SIGN;
}
@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) {
if (face == BlockFace.DOWN) {
return false;
}
Block layer0 = level.getBlock(this, 0);
Block layer1 = level.getBlock(this, 1);
CompoundTag nbt = new CompoundTag()
.putString("Text1", "")
.putString("Text2", "")
.putString("Text3", "")
.putString("Text4", "");
if (face == BlockFace.UP) {
CompassRoseDirection direction = GROUND_SIGN_DIRECTION.getValueForMeta(
(int) Math.floor((((player != null ? player.yaw : 0) + 180) * 16 / 360) + 0.5) & 0x0f
);
BlockState post = BlockState.of(getPostId()).withProperty(GROUND_SIGN_DIRECTION, direction);
getLevel().setBlock(block, post.getBlock(block), true);
} else {
BlockState wall = BlockState.of(getWallId()).withProperty(FACING_DIRECTION, face);
getLevel().setBlock(block, wall.getBlock(block), true);
}
if (player != null) {
nbt.putString("Creator", player.getUniqueId().toString());
}
if (item.hasCustomBlockData()) {
for (Tag aTag : item.getCustomBlockData().getAllTags()) {
nbt.put(aTag.getName(), aTag);
}
}
try {
createBlockEntity(nbt);
return true;
} catch (Exception e) {
log.warn("Failed to create block entity {} at {}", getBlockEntityType(), getLocation(), e);
level.setBlock(layer0, 0, layer0, true);
level.setBlock(layer1, 0, layer1, true);
return false;
}
}
@Override
public int onUpdate(int type) {
if (type == Level.BLOCK_UPDATE_NORMAL) {
if (down().getId() == Block.AIR) {
getLevel().useBreakOn(this);
return Level.BLOCK_UPDATE_NORMAL;
}
}
return 0;
}
@Override
public Item toItem() {
return new ItemSign();
}
@Override
public int getToolType() {
return ItemTool.TYPE_AXE;
}
@Override
public BlockColor getColor() {
return BlockColor.AIR_BLOCK_COLOR;
}
@PowerNukkitOnly
@Since("1.4.0.0-PN")
public CompassRoseDirection getSignDirection() {
return getPropertyValue(GROUND_SIGN_DIRECTION);
}
@PowerNukkitOnly
@Since("1.4.0.0-PN")
public void setSignDirection(CompassRoseDirection direction) {
setPropertyValue(GROUND_SIGN_DIRECTION, direction);
}
@PowerNukkitDifference(info = "Was returning the wrong face, it now return the closest face, or the left face if even", since = "1.4.0.0-PN")
@Override
public BlockFace getBlockFace() {
return getSignDirection().getClosestBlockFace();
}
@PowerNukkitOnly
@Since("1.3.0.0-PN")
@Override
public void setBlockFace(BlockFace face) {
setSignDirection(face.getCompassRoseDirection());
}
@Override
@PowerNukkitOnly
public boolean breaksWhenMoved() {
return true;
}
@Override
public boolean canBeActivated() {
return true;
}
@Override
public boolean onActivate(@NotNull Item item, Player player) {
BlockEntity blockEntity = this.level.getBlockEntity(this);
if (!(blockEntity instanceof BlockEntitySign sign)) {
return false;
}
if (item.getId() == Item.DYE) {
BlockColor color = DyeColor.getByDyeData(item.getDamage()).getSignColor();
if (color.equals(sign.getColor())) {
if (player != null) {
sign.spawnTo(player);
}
return false;
}
SignColorChangeEvent event = new SignColorChangeEvent(this, player, color);
this.level.getServer().getPluginManager().callEvent(event);
if (event.isCancelled()) {
if (player != null) {
sign.spawnTo(player);
}
return false;
}
sign.setColor(color);
sign.spawnToAll();
this.level.addLevelEvent(this, LevelEventPacket.EVENT_SOUND_DYE_USED);
if (player != null && (player.getGamemode() & 0x01) == 0) {
item.count--;
}
return true;
} else if (item instanceof ItemGlowInkSac) {
if (sign.isGlowing()) {
if (player != null) {
sign.spawnTo(player);
}
return false;
}
SignGlowEvent event = new SignGlowEvent(this, player, true);
this.level.getServer().getPluginManager().callEvent(event);
if (event.isCancelled()) {
if (player != null) {
sign.spawnTo(player);
}
return false;
}
sign.setGlowing(true);
sign.spawnToAll();
this.level.addLevelEvent(this, LevelEventPacket.EVENT_SOUND_INK_SACE_USED);
if (player != null && (player.getGamemode() & 0x01) == 0) {
item.count--;
}
return true;
}
return false;
}
}