Files
src
build.gradle
settings.gradle
Code
5 256
src/main/java/com/kodari/sculkbeam/SculkBeamPlugin.java
package com.kodari.sculkbeam;
import com.cryptomorin.xseries.XEnchantment;
import com.cryptomorin.xseries.XMaterial;
import com.cryptomorin.xseries.XSound;
import com.cryptomorin.xseries.particles.XParticle;
import org.bukkit.ChatColor;
import org.bukkit.Location;
import org.bukkit.NamespacedKey;
import org.bukkit.World;
import org.bukkit.block.Block;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Damageable;
import org.bukkit.entity.Entity;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.EntityShootBowEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.CrossbowMeta;
import org.bukkit.persistence.PersistentDataType;
import org.bukkit.util.RayTraceResult;
import org.bukkit.util.Vector;
import org.bukkit.plugin.java.JavaPlugin;
import java.util.Arrays;
import java.util.List;
public final class SculkBeamPlugin extends JavaPlugin implements Listener, CommandExecutor {
private NamespacedKey itemKey;
@Override
public void onEnable() {
saveDefaultConfig();
itemKey = new NamespacedKey(this, "sculk_crossbow");
getServer().getPluginManager().registerEvents(this, this);
getCommand("sculkcrossbow").setExecutor(this);
}
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
public void onCrossbowShoot(EntityShootBowEvent event) {
if (!isSculkCrossbow(event.getBow())) {
return;
}
event.setCancelled(true);
Entity projectile = event.getProjectile();
Location start = projectile.getLocation().clone();
Vector direction = projectile.getVelocity().clone();
projectile.remove();
if (direction.lengthSquared() < 1.0E-8) {
return;
}
fireBeam(event.getEntity(), start, direction.normalize());
}
@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
Player recipient;
if (args.length == 0 && sender instanceof Player player) {
recipient = player;
} else if (args.length == 1) {
recipient = getServer().getPlayerExact(args[0]);
if (recipient == null) {
sender.sendMessage(ChatColor.RED + "Игрок не найден или не в сети.");
return true;
}
} else {
sender.sendMessage(ChatColor.RED + "Использование: /sculkcrossbow [игрок]");
return true;
}
recipient.getInventory().addItem(createSculkCrossbow());
recipient.sendMessage(ChatColor.DARK_AQUA + "Вы получили Скалковый арбалет.");
return true;
}
private ItemStack createSculkCrossbow() {
ItemStack item = XMaterial.matchXMaterial("CROSSBOW")
.map(XMaterial::parseItem)
.orElseThrow(() -> new IllegalStateException("Crossbow material is unavailable"));
CrossbowMeta meta = (CrossbowMeta) item.getItemMeta();
meta.setDisplayName(ChatColor.DARK_AQUA + "Скалковый арбалет");
meta.setLore(Arrays.asList(
ChatColor.GRAY + "Выстреливает мгновенным звуковым лучом.",
ChatColor.GRAY + "Каждый заряд Multishot создаёт отдельный луч."
));
meta.setUnbreakable(true);
meta.getPersistentDataContainer().set(itemKey, PersistentDataType.BYTE, (byte) 1);
XEnchantment.matchXEnchantment("MULTISHOT")
.ifPresent(enchantment -> meta.addEnchant(enchantment.getEnchant(), 1, true));
item.setItemMeta(meta);
return item;
}
private boolean isSculkCrossbow(ItemStack item) {
if (item == null || !item.hasItemMeta() || !(item.getItemMeta() instanceof CrossbowMeta meta)) {
return false;
}
Byte marker = meta.getPersistentDataContainer().get(itemKey, PersistentDataType.BYTE);
return Byte.valueOf((byte) 1).equals(marker);
}
private void fireBeam(LivingEntity shooter, Location start, Vector direction) {
World world = start.getWorld();
double range = clamp(getConfig().getDouble("beam.range", 40.0), 1.0, 128.0);
double rayRadius = clamp(getConfig().getDouble("beam.entity-ray-radius", 0.3), 0.0, 4.0);
RayTraceResult blockHit = world.rayTraceBlocks(start, direction, range, org.bukkit.FluidCollisionMode.NEVER, true);
RayTraceResult entityHit = world.rayTraceEntities(start, direction, range, rayRadius,
entity -> entity != shooter && entity.isValid());
double distance = range;
Block hitBlock = null;
Entity hitEntity = null;
if (blockHit != null) {
distance = start.toVector().distance(blockHit.getHitPosition());
hitBlock = blockHit.getHitBlock();
}
if (entityHit != null) {
double entityDistance = start.toVector().distance(entityHit.getHitPosition());
if (entityDistance < distance) {
distance = entityDistance;
hitEntity = entityHit.getHitEntity();
hitBlock = null;
}
}
Location impact = start.clone().add(direction.clone().multiply(distance));
spawnBeamParticles(world, start, direction, distance);
XSound.matchXSound("ENTITY_WARDEN_SONIC_BOOM")
.ifPresent(sound -> world.playSound(impact, sound.get(), 1.0f, 1.0f));
if (hitBlock != null && getConfig().getBoolean("block-destruction.enabled", true)) {
breakBlocks(hitBlock);
}
damageImpact(shooter, hitEntity, impact);
}
private void spawnBeamParticles(World world, Location start, Vector direction, double distance) {
double step = clamp(getConfig().getDouble("beam.particle-step", 0.75), 0.2, 4.0);
XParticle.of("SONIC_BOOM").ifPresent(particle -> {
for (double traveled = 0.0; traveled < distance; traveled += step) {
Location point = start.clone().add(direction.clone().multiply(traveled));
world.spawnParticle(particle.get(), point, 1, 0.0, 0.0, 0.0, 0.0);
}
world.spawnParticle(particle.get(), start.clone().add(direction.clone().multiply(distance)),
1, 0.0, 0.0, 0.0, 0.0);
});
}
private void damageImpact(LivingEntity shooter, Entity directHit, Location impact) {
double damage = Math.max(0.0, getConfig().getDouble("beam.damage", 12.0));
if (directHit instanceof Damageable damageable) {
damageable.damage(damage, shooter);
}
double radius = clamp(getConfig().getDouble("beam.blast-radius", 2.5), 0.0, 16.0);
if (radius == 0.0) {
return;
}
double radiusSquared = radius * radius;
for (Entity entity : impact.getWorld().getNearbyEntities(impact, radius, radius, radius)) {
if (entity == shooter || entity == directHit || !(entity instanceof Damageable damageable)) {
continue;
}
double distanceSquared = entity.getLocation().distanceSquared(impact);
if (distanceSquared >= radiusSquared) {
continue;
}
double falloff = 1.0 - Math.sqrt(distanceSquared) / radius;
damageable.damage(damage * falloff, shooter);
}
}
private void breakBlocks(Block hitBlock) {
int radius = Math.max(0, Math.min(4, getConfig().getInt("block-destruction.radius", 1)));
int maxBlocks = Math.max(1, Math.min(256, getConfig().getInt("block-destruction.max-blocks", 24)));
int broken = 0;
int centerX = hitBlock.getX();
int centerY = hitBlock.getY();
int centerZ = hitBlock.getZ();
int radiusSquared = radius * radius;
for (int x = -radius; x <= radius && broken < maxBlocks; x++) {
for (int y = -radius; y <= radius && broken < maxBlocks; y++) {
for (int z = -radius; z <= radius && broken < maxBlocks; z++) {
if (x * x + y * y + z * z > radiusSquared) {
continue;
}
Block block = hitBlock.getWorld().getBlockAt(centerX + x, centerY + y, centerZ + z);
if (block.getType().isAir()) {
continue;
}
block.breakNaturally();
broken++;