Bukkit员工的魔杖插件

时间:2015-04-04 03:26:57

标签: java minecraft bukkit

我制作了一个插件,添加了推荐/staffwands。它会给一个棍子,这是一个工作人员。这是一个魔杖插件。

但是我想确保如果游戏中的普通玩家只是得到一根棍子就不会成为魔杖,只有你/staffwands它才会成为魔杖。我已经创建了一个具有权限和魔杖的课程,我只需要帮助。以下是代码,如果您可以提供任何建议,那就很棒:

package me.capz.stick;
import org.bukkit.Bukkit;    
import org.bukkit.ChatColor;    
import org.bukkit.EntityEffect;
import org.bukkit.Material;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Egg;
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.EntityDamageByEntityEvent;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.plugin.java.JavaPlugin;

public class stick extends JavaPlugin implements Listener {

    public void onEnable() {
        Bukkit.getServer().getPluginManager().registerEvents(this, this);
    }
    public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
        if(cmd.getName().equalsIgnoreCase("StaffWand")) {
            if(!sender.hasPermission("StaffWand.staff")) {
                sender.sendMessage(ChatColor.RED + "Only Staff can use this!");
                if(sender.hasPermission("StaffWand.staff")) {
                    sender.sendMessage(ChatColor.GREEN + "Here is your staff wand!");
                    ItemStack item = new ItemStack(Material.STICK);
                    ((Player)sender).getInventory().addItem(item); return true;
                }
            }
        }
        return false;               
    }

    @EventHandler
    public void onPlayerInteract(PlayerInteractEvent e) {
        Player player = e.getPlayer();
        if (!(e.getAction() == Action.RIGHT_CLICK_AIR)) return;

        if (!(e.getItem().getType() == Material.STICK)) return;

        Egg egg = e.getPlayer().launchProjectile(Egg.class);
        egg.setFireTicks(20);

        if(!(player.getInventory().contains(Material.MAGMA_CREAM)))return;
        player.getInventory().removeItem(new ItemStack(Material.MAGMA_CREAM));
        if(!player.getInventory().contains(Material.MAGMA_CREAM))return;
        player.sendMessage("Magma_Cream needed!");
        egg.setBounce(true);
        egg.playEffect(EntityEffect.HURT);
    }

    @EventHandler
    public void onEntityDamage(EntityDamageByEntityEvent e) {
        if (e.getDamager() instanceof Egg) {
            Egg egg = (Egg) e.getDamager();
            if (egg.getShooter() instanceof Player) {
                Player shooter = (Player) egg.getShooter();
                if (shooter.getItemInHand().getType() == Material.STICK) {
                    e.setDamage(10.0);  
                }   
            }   
        }
    }
}

1 个答案:

答案 0 :(得分:0)

您必须向ItemStack添加某种类型的元数据。我建议添加传说,因为无法改变:

ItemStack wand = new ItemStack(Material.STICK); //create the ItemStack
ItemMeta meta = wand.getItemMeta(); //get the ItemMeta

List<String> lore = new ArrayList<String>(); //create a List<String> for the lore
lore.add(ChatColor.GRAY + "Staff Wand"); //add "§7Staff Wand" to the lore

meta.setLore(lore); //set the ItemMeta's lore to the List<String>
wand.setItemMeta(meta); //set the ItemStack's meta.

然后,您可以通过检查传说是否等于上述传说来检查棒是否是员工棒

public boolean isStaffWand(ItemStack item){
  //make sure the item is not null, it is a stick, it has item meta, and it has lore
  //to avoid a NullPointerException
  if(item != null && item.getType().equals(Material.STICK) && item.hasItemMeta() && item.getItemMeta().hasLore()){

    List<String> lore = new ArrayList<String>();//create the lore List<String>
    lore.add(ChatColor.GRAY + "Staff Wand");//add "§7Staff Wand" to the lore
    //this lore is now the same as the lore created in the above code block

    //check if the item's lore equals the lore above
    if(item.getItemMeta().getLore().equals(lore)){
      return true; //"item" is a Staff Wand
    }
  }
  return false; //"item" is not a Staff Wand
}