cn.nukkit.item.ItemFireCharge Maven / Gradle / Ivy
package cn.nukkit.item;
import cn.nukkit.Player;
import cn.nukkit.block.Block;
import cn.nukkit.block.BlockFire;
import cn.nukkit.block.BlockID;
import cn.nukkit.event.block.BlockIgniteEvent;
import cn.nukkit.level.Level;
import cn.nukkit.math.BlockFace;
import cn.nukkit.network.protocol.LevelEventPacket;
import java.util.concurrent.ThreadLocalRandom;
/**
* @author PetteriM1
*/
public class ItemFireCharge extends Item {
public ItemFireCharge() {
this(0, 1);
}
public ItemFireCharge(Integer meta) {
this(meta, 1);
}
public ItemFireCharge(Integer meta, int count) {
super(FIRE_CHARGE, 0, count, "Fire Charge");
}
@Override
public boolean canBeActivated() {
return true;
}
@Override
public boolean onActivate(Level level, Player player, Block block, Block target, BlockFace face, double fx, double fy, double fz) {
if (player.isAdventure()) {
return false;
}
if (block.getId() == AIR && (target.isSolid() || target.getBurnChance() > 0)) {
if (target.getId() == OBSIDIAN) {
if (level.createPortal(target)) {
return true;
}
}
BlockFire fire = (BlockFire) Block.get(BlockID.FIRE);
fire.x = block.x;
fire.y = block.y;
fire.z = block.z;
fire.level = level;
if (fire.isBlockTopFacingSurfaceSolid(fire.down()) || fire.canNeighborBurn()) {
BlockIgniteEvent e = new BlockIgniteEvent(block, null, player, BlockIgniteEvent.BlockIgniteCause.FLINT_AND_STEEL);
block.getLevel().getServer().getPluginManager().callEvent(e);
if (!e.isCancelled()) {
level.setBlock(fire, fire, true);
level.addLevelEvent(block, LevelEventPacket.EVENT_SOUND_GHAST_SHOOT, 78642);
level.scheduleUpdate(fire, fire.tickRate() + ThreadLocalRandom.current().nextInt(10));
}
if (player.isSurvival()) {
Item item = player.getInventory().getItemInHand();
item.setCount(item.getCount() - 1);
player.getInventory().setItemInHand(item);
}
return true;
}
}
return false;
}
}