cn.nukkit.block.BlockFlower 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.api.PowerNukkitDifference;
import cn.nukkit.api.PowerNukkitOnly;
import cn.nukkit.api.Since;
import cn.nukkit.item.Item;
import cn.nukkit.level.Level;
import cn.nukkit.level.particle.BoneMealParticle;
import cn.nukkit.math.BlockFace;
import cn.nukkit.math.Vector3;
import cn.nukkit.utils.BlockColor;
import javax.annotation.Nonnull;
import java.util.concurrent.ThreadLocalRandom;
/**
* @author xtypr
* @since 2015/11/23
*/
public class BlockFlower extends BlockFlowable {
public static final int TYPE_POPPY = 0;
public static final int TYPE_BLUE_ORCHID = 1;
public static final int TYPE_ALLIUM = 2;
public static final int TYPE_AZURE_BLUET = 3;
public static final int TYPE_RED_TULIP = 4;
public static final int TYPE_ORANGE_TULIP = 5;
public static final int TYPE_WHITE_TULIP = 6;
public static final int TYPE_PINK_TULIP = 7;
public static final int TYPE_OXEYE_DAISY = 8;
public BlockFlower() {
this(0);
}
public BlockFlower(int meta) {
super(meta);
}
@Override
public int getId() {
return FLOWER;
}
@Override
public String getName() {
String[] names = new String[]{
"Poppy",
"Blue Orchid",
"Allium",
"Azure Bluet",
"Red Tulip",
"Orange Tulip",
"White Tulip",
"Pink Tulip",
"Oxeye Daisy",
"Unknown",
"Unknown",
"Unknown",
"Unknown",
"Unknown",
"Unknown",
"Unknown"
};
return names[this.getDamage() & 0x0f];
}
@PowerNukkitOnly
@Since("1.4.0.0-PN")
public static boolean isSupportValid(Block block) {
switch (block.getId()) {
case GRASS:
case DIRT:
case FARMLAND:
case PODZOL:
return true;
default:
return false;
}
}
@PowerNukkitDifference(since = "1.4.0.0-PN", info = "Fixed support logic")
public boolean canPlantOn(Block block) {
return isSupportValid(block);
}
@Override
public boolean place(@Nonnull Item item, @Nonnull Block block, @Nonnull Block target, @Nonnull BlockFace face, double fx, double fy, double fz, Player player) {
Block down = this.down();
if (canPlantOn(down)) {
this.getLevel().setBlock(block, this, true);
return true;
}
return false;
}
@PowerNukkitDifference(since = "1.4.0.0-PN", info = "Will break on normal update if the supporting block is invalid")
@Override
public int onUpdate(int type) {
if (type == Level.BLOCK_UPDATE_NORMAL) {
if (!canPlantOn(down())) {
this.getLevel().useBreakOn(this);
return Level.BLOCK_UPDATE_NORMAL;
}
}
return 0;
}
@Override
public BlockColor getColor() {
return BlockColor.FOLIAGE_BLOCK_COLOR;
}
@Override
public boolean canBeActivated() {
return true;
}
@Override
public boolean onActivate(@Nonnull Item item, Player player) {
if (item.isFertilizer()) { //Bone meal
if (player != null && (player.gamemode & 0x01) == 0) {
item.count--;
}
this.level.addParticle(new BoneMealParticle(this));
for (int i = 0; i < 8; i++) {
Vector3 vec = this.add(
ThreadLocalRandom.current().nextInt(-3, 4),
ThreadLocalRandom.current().nextInt(-1, 2),
ThreadLocalRandom.current().nextInt(-3, 4));
if (level.getBlock(vec).getId() == AIR && level.getBlock(vec.down()).getId() == GRASS && vec.getY() >= 0 && vec.getY() < 256) {
if (ThreadLocalRandom.current().nextInt(10) == 0) {
this.level.setBlock(vec, this.getUncommonFlower(), true);
} else {
this.level.setBlock(vec, get(this.getId()), true);
}
}
}
return true;
}
return false;
}
protected Block getUncommonFlower() {
return get(DANDELION);
}
}