骷髅可以使用我的插件吗?

时间:2016-05-14 14:46:31

标签: java bukkit

改变的一切都在这里。请帮我闪电,不要在每个刻度线的箭头位置产卵

主档

package me.Pixel;

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

import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Arrow;
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.entity.EntityShootBowEvent;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
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();
    private Arrow arrow;
    public LightningShot LightningShot = new LightningShot(arrow);

    @Override
    public void onEnable() {    
        plugin = this;
        this.getServer().getPluginManager().registerEvents(this, this);
        spells.add("LightningShot");
    }

    @Override
    public boolean onCommand(CommandSender sender, Command command,
            String label, String[] args) {
        if(label.equalsIgnoreCase("bow")) {
            if(!(sender instanceof Player)) {
                sender.sendMessage(ChatColor.RED + "You need to be an in-game player to execute this command!");
            }else{
                Player p = (Player)sender;
                if(sender.hasPermission("bow.bow")) { 
                ItemStack stack = new ItemStack(Material.BOW);
                ItemMeta stackMeta = stack.getItemMeta();
                stackMeta.setDisplayName(ChatColor.AQUA + "Magic" + ChatColor.RED + "Bow");
                stack.setItemMeta(stackMeta);
                p.getInventory().addItem(stack);
                ChatUtilities.sendMessage(p, "You now have the powerfull Magic Bow");
                }else{
                    sender.sendMessage(ChatColor.RED + "No Permission!");
                }
            }
        }
        return false;
    }

    @SuppressWarnings("deprecation")
    @EventHandler
    public void onEntityShootBow(EntityShootBowEvent ev) {
        if (ev.getProjectile() instanceof Arrow) {
            Arrow a = (Arrow) ev.getProjectile();
            if (a.getShooter() instanceof Player) {
                ItemStack bow = ev.getBow();
                if (bow.hasItemMeta() && bow.getItemMeta().getDisplayName().equals(ChatColor.AQUA + "Magic" + ChatColor.RED + "Bow")) {
                new LightningShot(a);
                }
            }
        }
    }

    @EventHandler
    public void onClick(PlayerInteractEvent e) {
        if(e.getAction() == Action.RIGHT_CLICK_AIR || e.getAction() == Action.RIGHT_CLICK_BLOCK) {
            Player p = e.getPlayer();
            ItemStack stack = p.getItemInHand();
            if(stack != null && stack.getType() == Material.BOW && stack.hasItemMeta() && stack.getItemMeta().getDisplayName().equals(ChatColor.AQUA + "Magic" + ChatColor.RED + "Bow")) {
                int SpellSelected = stack.getDurability();
                if(SpellSelected == 1) {
                    this.LightningShot.run();
                }
            }
        }
    }
}

我的LightningShot文件

package me.Pixel;

import org.bukkit.entity.Arrow;
import org.bukkit.scheduler.BukkitRunnable;

public class LightningShot extends BukkitRunnable {
    private Arrow arrow;
    private int tick = 1;

    public LightningShot(Arrow arrow) {
        this.arrow = arrow;
    }

    @Override
    public void run() {
        if (arrow == null || arrow.isOnGround() || tick++ > 20 * 10) {
            this.cancel();
        } else {
            arrow.getWorld().strikeLightning(arrow.getLocation());
        }
    }
}

所以我希望你能帮助我让它发挥作用。 这是所有新代码。

1 个答案:

答案 0 :(得分:1)

如果你想检查Bow,箭头射出是否是“魔法弓”,你可以在ItemStackEntityShootBowEvent获得弓箭的event.getBow()。现在,您可以查看事件中弓的名称是否等于您在“/ bow”命令中给予玩家的弓的名称。

希望这可以帮助你:)抱歉我的英语不好; D

编辑: 喜欢这个

@EventHandler
public void onEntityShootBow(EntityShootBowEvent ev) {
    if (ev.getProjectile() instanceof Arrow) {
        Arrow a = (Arrow) ev.getProjectile();
        if (a.getShooter() instanceof Player) {
            ItemStack bow = ev.getBow();
            if (bow.hasItemMeta() && bow.getItemMeta().getDisplayName().equals(ChatColor.AQUA + "Magic" + ChatColor.RED + "Bow")) {
                System.out.println("Launching LightningShot...");
                new LightningShot(arrow).runTaskTimer(this, 0, 1);
            }
        }
    }
}

编辑#2:

LightningShot.java:

package me.Pixel;

import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.entity.Arrow;
import org.bukkit.scheduler.BukkitTask;

public class LightningShot {
    private static int interval = 5;

    Arrow arrow;
    int tick = interval;

    public LightningShot(Arrow a) {
        arrow = a;

        BukkitTask[] task = new BukkitTask[1];
        task[0] = Bukkit.getScheduler().runTaskTimerAsynchronously(Main.plugin, new Runnable() {
            @Override
            public void run() {
                if (arrow == null || arrow.isOnGround() || arrow.isDead() || tick >= 200) {
                    System.out.println("Stopping...");
                    task[0].cancel();
                } else {
                    tick += interval;
                    System.out.println("Launching Lightning...");
                    Location loc = arrow.getLocation();
                    System.out.println("At " + loc.getWorld().getName() + " " + loc.getX() + "/"+ loc.getY() + "/" + loc.getZ());
                    loc.getWorld().strikeLightning(arrow.getLocation());
                }
            }
        }, interval, interval);
    }
}

EntityShootBowEvent:

@EventHandler
public void onEntityShootBow(EntityShootBowEvent ev) {
    if (ev.getProjectile() instanceof Arrow) {
        Arrow a = (Arrow) ev.getProjectile();
        if (a.getShooter() instanceof Player) {
            System.out.println("Launching LightningShot...");
            new LightningShot(a);
        }
    }
}

您可以更改间隔以更改闪电产生的频率。

编辑#3:

Main.java

package me.Pixel;

import java.util.HashSet;
import java.util.Set;

import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Arrow;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.EntityShootBowEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.plugin.java.JavaPlugin;

public class Main extends JavaPlugin implements Listener {
    public static Main plugin;
    public Set<String> spells = new HashSet<String>();

    @Override
    public void onEnable() {
        plugin = this;
        this.getServer().getPluginManager().registerEvents(this, this);
        spells.add("LightningShot");
    }

    @Override
    public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
        if (label.equalsIgnoreCase("bow")) {
            if (!(sender instanceof Player)) {
                sender.sendMessage(ChatColor.RED + "You need to be an in-game player to execute this command!");
            } else {
                Player p = (Player) sender;
                if (sender.hasPermission("bow.bow")) {
                    ItemStack stack = new ItemStack(Material.BOW);
                    ItemMeta stackMeta = stack.getItemMeta();
                    stackMeta.setDisplayName(ChatColor.AQUA + "Magic" + ChatColor.RED + "Bow");
                    stack.setItemMeta(stackMeta);
                    p.getInventory().addItem(stack);
                    p.sendMessage("You now have the powerfull Magic Bow");
                } else {
                    sender.sendMessage(ChatColor.RED + "No Permission!");
                }
            }
        }
        return false;
    }

    @EventHandler
    public void onEntityShootBow(EntityShootBowEvent ev) {
        if (ev.getProjectile() instanceof Arrow) {
            Arrow a = (Arrow) ev.getProjectile();
            if (a.getShooter() instanceof Player) {
                ItemStack bow = ev.getBow();
                if (bow.hasItemMeta() && bow.getItemMeta().getDisplayName()
                        .equals(ChatColor.AQUA + "Magic" + ChatColor.RED + "Bow")) {
                    new LightningShot(a);
                }
            }
        }
    }
}

LightningShot.java

package me.Pixel;

import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.entity.Arrow;
import org.bukkit.scheduler.BukkitTask;

public class LightningShot {

    Arrow arrow;
    int tick = 3;

    public LightningShot(Arrow a) {
        arrow = a;

        BukkitTask[] task = new BukkitTask[1];
        task[0] = Bukkit.getScheduler().runTaskTimerAsynchronously(Main.plugin, new Runnable() {
            @Override
            public void run() {
                if (arrow == null || arrow.isOnGround() || arrow.isDead() || tick >= 200) {
                    task[0].cancel();
                } else {
                    tick += 1;
                    Location loc = arrow.getLocation();
                    loc.getWorld().strikeLightning(arrow.getLocation());
                }
            }
        }, 3, 1);
    }
}

两者都是我使用过的完整课程,并且有效...