java - 能射出雷击的弓

标签 java bukkit

所以我目前正忙于开发一个插件,当你在《我的世界》中看到它们来自天空时,它会发射雷击,但与你的弓水平,设置半径为 100 或其他

package me.Pixel;

import org.bukkit.FireworkEffect;
import org.bukkit.World;
import org.bukkit.block.Block;
import org.bukkit.entity.Entity;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
import org.bukkit.event.Listener;
import org.bukkit.event.weather.LightningStrikeEvent;
import org.bukkit.util.BlockIterator;

public class LightningShot implements Listener {

    public Main plugin;

    public LightningShot(Main instance) {
        this.plugin = instance;
    }

    public void onCast(Player p) {
        final BlockIterator blockNext = new BlockIterator(p);
        new Runnable() {
            public int timer = 0;

            public void run() {
                if(this.timer++ > 50) {
                    cancel();
                   }
                if(blockNext.hasNext()) {
                    cancel();
                }
                Block next = blockNext.next();
                try{
                    for(Entity e : LightningShot.this.plugin.getTargets.getTargetList(p, next.getLocation(), 3)) {
                        if(e instanceof LivingEntity) {
                    }
                    FireworkEffectPlayer.playToLocation(next.getLocation(), null);
                }
            }
        }
    }
}

这就是我现在所拥有的,但上面写着“FireworkEffectPlayer.playToLocation(next.getLocation(), null);”我被卡住了,我需要用一个代码替换它,使闪电在发射时从弓中射出。

这是文件

package me.Pixel;

import java.util.ArrayList;
import java.util.List;

import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.Action;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.plugin.java.JavaPlugin;

public class Main extends JavaPlugin implements Listener {

    public Main plugin;
    public List<String> spells = new ArrayList<String>();
    public getTargets getTargets = new getTargets();

    @Override
    public void onEnable() {
        plugin = this;
        getCommand("bow").setExecutor(new BowCommand());
    }

    @EventHandler
    public void onClick(PlayerInteractEvent e) {
        if(e.getAction() == Action.LEFT_CLICK_AIR || e.getAction() == Action.LEFT_CLICK_BLOCK) {
            Player p = e.getPlayer();
            ItemStack stack = p.getItemInHand();
            if(stack != null &&stack.getType() == Material.BOW && stack.hasItemMeta() && stack.getItemMeta().getDisplayName().equals(ChatColor.RED + "Bow")) {

            }
        }
    }
}

类似这样的:http://youtu.be/riPIzITVp0c但从 中射出的不是雪球,而是箭头。 希望大家能够帮助我!

最佳答案

要使箭头在每个滴答声(或更少频率)的位置产生雷击,您可以创建自己的 BukkitRunnable 来跟踪 Arrow 实体如果箭头落地、消失或任务花费太长时间,则会自行取消。

这是一个 BukkitRunnable 的示例:

public class LightningArrowTask extends BukkitRunnable {

    private Arrow arrow; // The arrow to spawn lightning at
    private int tick = 0; // The number of times the run() method has been invoked

    // The constructor that asks for an arrow entity
    public LightningArrowTask(Arrow arrow) {
        this.arrow = arrow;
    }

    @Override
    public void run() {
        // If the arrow no longer exists, has landed, or this task has been running for more than ~10 seconds (200 ticks)
        if (arrow == null || arrow.isOnGround() || tick++ > 20 * 10) {
            this.cancel(); // Cancel the task
        } else {
            arrow.getWorld().strikeLightning(arrow.getLocation()); // Otherwise, make lightning strike at the arrow's location
        }
    }
}

在您的 EntityShootBowEvent 方法中,您可以像这样使用此类:

@EventHandler
public void onEntityShootBow(EntityShootBowEvent event) {
    if (event.getProjectile() instanceof Arrow) { // If the projectile shot is actually an arrow
        // Add your own conditions here ... such as the name of the bow etc.
        Arrow arrow = (Arrow) event.getProjectile(); // Cast
        // Create the LightningArrowTask BukkitRunnable with the arrow object, run it repeatedly every tick (with a 0 tick delay)
        new LightningArrowTask(arrow).runTaskTimer(this, 0, 1);
        // If you'd like the lightning to only be spawned every 2, 3, 5 etc. ticks, just change that last argument of the runTaskTimer method
    }
}

关于java - 能射出雷击的弓,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36890429/

相关文章:

java - 操作从java Map转换而来的JSON对象

java - 如何通过类路径用另一个 jar 修补一个 jar(替换类文件)

单独的 JAR 文件中的 Java API

java - 使用命令更改标志的内容

java - 使用 String.replaceAll 函数匹配正则表达式并从 Java 中的映射中获取替换值

java - 大型 XML 的高效解析器

java - Node.js:如何写入 java-childprocess-stdin

java - 基于级别的变量修饰符

java - 如何从 .jar 文件引用 .dll

java - Android中的传感器精确服务