use of com.easterlyn.effect.event.IndirectBreakEvent in project Easterlyn by Easterlyn.
the class EffectTunnelBore method applyEffect.
@Override
public void applyEffect(@NotNull LivingEntity entity, int level, @Nullable Event event) {
if (!(event instanceof BlockBreakEvent) || event instanceof IndirectBreakEvent) {
return;
}
BlockBreakEvent breakEvent = (BlockBreakEvent) event;
Player player = breakEvent.getPlayer();
if (player.isSneaking()) {
// Sneak to mine single blocks
return;
}
Block block = breakEvent.getBlock();
if (!BlockUtil.isCorrectTool(player.getInventory().getItemInMainHand(), breakEvent.getBlock().getType())) {
// Require correct tool used
return;
}
RegisteredServiceProvider<EasterlynCore> registration = getPlugin().getServer().getServicesManager().getRegistration(EasterlynCore.class);
if (registration == null) {
return;
}
BlockUpdateManager budManager = registration.getProvider().getBlockUpdateManager();
/*
* Forgiveness of .25 hardness allows indirect breaking of most common related materials
* while not excessively increasing risk of accidents inside builds. Allows for mining gravel
* via sand and dirt, etc.
*/
float hardness = block.getType().getHardness() + 0.25F;
for (BlockFace yLevel : levels) {
if (block.getY() == 0 && yLevel == BlockFace.DOWN) {
continue;
}
Block relativeCenter = block.getRelative(yLevel);
if (yLevel != BlockFace.SELF) {
if (handleBlock(hardness, relativeCenter, player, budManager)) {
return;
}
}
for (BlockFace face : faces) {
if (handleBlock(hardness, relativeCenter.getRelative(face), player, budManager)) {
return;
}
}
}
player.updateInventory();
}
use of com.easterlyn.effect.event.IndirectBreakEvent in project Easterlyn by Easterlyn.
the class EffectPullNearbyItems method applyEffect.
@Override
public void applyEffect(@NotNull LivingEntity entity, int level, Event event) {
final double radius = level * 1.5;
final UUID uuid = entity.getUniqueId();
new BukkitRunnable() {
@Override
public void run() {
Player player = Bukkit.getPlayer(uuid);
if (player == null) {
// Logged out
return;
}
for (Entity near : player.getNearbyEntities(radius, radius, radius)) {
if (!(near instanceof Item)) {
continue;
}
near.getWorld().spawnParticle(Particle.ENCHANTMENT_TABLE, near.getLocation(), 1);
near.teleport(player);
if (!(event instanceof IndirectBreakEvent)) {
player.playSound(near.getLocation(), Sound.ENTITY_ENDER_DRAGON_FLAP, SoundCategory.BLOCKS, 0.1F, 1.5F);
}
Item item = (Item) near;
if (item.getPickupDelay() < 1000) {
item.setPickupDelay(0);
}
}
}
}.runTask(getPlugin());
}
use of com.easterlyn.effect.event.IndirectBreakEvent in project Easterlyn by Easterlyn.
the class EffectTunnelBore method handleBlock.
private boolean handleBlock(float requiredHardness, Block block, Player player, BlockUpdateManager budManager) {
float blockHardness = block.getType().getHardness();
if (blockHardness > requiredHardness || blockHardness < 0 || block.getState() instanceof InventoryHolder || !BlockUtil.isCorrectTool(player.getInventory().getItemInMainHand(), block.getType())) {
return false;
}
IndirectBreakEvent event = new IndirectBreakEvent(block, player);
Bukkit.getServer().getPluginManager().callEvent(event);
if (event.isCancelled() || block.isLiquid()) {
return false;
}
if (player.getGameMode() == GameMode.CREATIVE) {
block.setType(Material.AIR, false);
budManager.queueBlock(block);
return false;
}
// Item breaking blocks has to be in main hand
ItemStack hand = player.getInventory().getItemInMainHand();
Collection<ItemStack> drops = BlockUtil.getDrops(hand, block);
int exp = BlockUtil.getExp(player, hand, block);
block.setType(Material.AIR, false);
for (ItemStack is : drops) {
player.getWorld().dropItem(player.getLocation(), is).setPickupDelay(0);
}
if (exp > 0) {
ExperienceUtil.changeExp(player, exp);
}
budManager.queueBlock(block);
ItemMeta handMeta = hand.getItemMeta();
if (handMeta != null && !handMeta.isUnbreakable() && handMeta instanceof Damageable && (!hand.containsEnchantment(Enchantment.DURABILITY) || ThreadLocalRandom.current().nextDouble() < 1.0 / (hand.getEnchantmentLevel(Enchantment.DURABILITY) + 2))) {
Damageable damageable = (Damageable) handMeta;
damageable.setDamage(damageable.getDamage() + 1);
hand.setItemMeta(handMeta);
return damageable.getDamage() > hand.getType().getMaxDurability();
}
return false;
}
Aggregations