cn.nukkit.block.BlockCactus Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of powernukkit Show documentation
Show all versions of powernukkit Show documentation
A Minecraft Bedrock Edition server software implementation made in Java from scratch which supports all new features.
package cn.nukkit.block;
import cn.nukkit.Player;
import cn.nukkit.Server;
import cn.nukkit.api.PowerNukkitOnly;
import cn.nukkit.entity.Entity;
import cn.nukkit.event.block.BlockGrowEvent;
import cn.nukkit.event.entity.EntityDamageByBlockEvent;
import cn.nukkit.event.entity.EntityDamageEvent.DamageCause;
import cn.nukkit.item.Item;
import cn.nukkit.level.Level;
import cn.nukkit.math.AxisAlignedBB;
import cn.nukkit.math.BlockFace;
import cn.nukkit.math.SimpleAxisAlignedBB;
import cn.nukkit.math.Vector3;
import cn.nukkit.utils.BlockColor;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
/**
* @author Nukkit Project Team
*/
public class BlockCactus extends BlockTransparentMeta {
public BlockCactus(int meta) {
super(meta);
}
public BlockCactus() {
this(0);
}
@Override
public int getId() {
return CACTUS;
}
@Override
public double getHardness() {
return 0.4;
}
@Override
public double getResistance() {
return 2;
}
@PowerNukkitOnly
@Override
public int getWaterloggingLevel() {
return 1;
}
@Override
public boolean hasEntityCollision() {
return true;
}
@Override
public double getMinX() {
return this.x + 0.0625;
}
@Override
public double getMinY() {
return this.y;
}
@Override
public double getMinZ() {
return this.z + 0.0625;
}
@Override
public double getMaxX() {
return this.x + 0.9375;
}
@Override
public double getMaxY() {
return this.y + 0.9375;
}
@Override
public double getMaxZ() {
return this.z + 0.9375;
}
@Override
protected AxisAlignedBB recalculateCollisionBoundingBox() {
return new SimpleAxisAlignedBB(this.x, this.y, this.z, this.x + 1, this.y + 1, this.z + 1);
}
@Override
public void onEntityCollide(Entity entity) {
entity.attack(new EntityDamageByBlockEvent(this, entity, DamageCause.CONTACT, 1));
}
@Override
public int onUpdate(int type) {
if (type == Level.BLOCK_UPDATE_NORMAL) {
Block down = down();
if (down.getId() != SAND && down.getId() != CACTUS) {
this.getLevel().useBreakOn(this);
} else {
for (int side = 2; side <= 5; ++side) {
Block block = getSide(BlockFace.fromIndex(side));
if (!block.canBeFlowedInto()) {
this.getLevel().useBreakOn(this);
}
}
}
} else if (type == Level.BLOCK_UPDATE_RANDOM) {
if (down().getId() != CACTUS) {
if (this.getDamage() == 0x0F) {
for (int y = 1; y < 3; ++y) {
Block b = this.getLevel().getBlock(new Vector3(this.x, this.y + y, this.z));
if (b.getId() == AIR) {
BlockGrowEvent event = new BlockGrowEvent(b, Block.get(BlockID.CACTUS));
Server.getInstance().getPluginManager().callEvent(event);
if (!event.isCancelled()) {
this.getLevel().setBlock(b, event.getNewState(), true);
}
}
}
this.setDamage(0);
this.getLevel().setBlock(this, this);
} else {
this.setDamage(this.getDamage() + 1);
this.getLevel().setBlock(this, this);
}
}
}
return 0;
}
@Override
public boolean place(@Nonnull Item item, @Nonnull Block block, @Nonnull Block target, @Nonnull BlockFace face, double fx, double fy, double fz, @Nullable Player player) {
Block down = this.down();
if (down.getId() == SAND || down.getId() == CACTUS) {
Block block0 = north();
Block block1 = south();
Block block2 = west();
Block block3 = east();
if (block0.canBeFlowedInto() && block1.canBeFlowedInto() && block2.canBeFlowedInto() && block3.canBeFlowedInto()) {
this.getLevel().setBlock(this, this, true);
return true;
}
}
return false;
}
@Override
public String getName() {
return "Cactus";
}
@Override
public BlockColor getColor() {
return BlockColor.FOLIAGE_BLOCK_COLOR;
}
@Override
public Item[] getDrops(Item item) {
return new Item[]{
Item.getBlock(BlockID.CACTUS, 0, 1)
};
}
@Override
public boolean breaksWhenMoved() {
return true;
}
@Override
public boolean sticksToPiston() {
return false;
}
}