2015-04-04 80 views
-1

我做了一個插件,增加了表彰/staffwands。它會給一個棒,這是一個工作人員。這是一個魔杖插件。Bukkit員工魔杖插件

但是我想確保如果遊戲中的普通玩家只是拿到一根棍子,它不會是一根魔杖,只有當你做/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); 
       } 
      } 
     } 
    } 
} 
+0

我通過選擇代碼然後使用ctrl-K將代碼移入代碼塊中。還從代碼示例中刪除了一堆空行。 – 2015-04-04 14:20:17

+1

我沒有得到你的onComand():在'!sender.hasPermission()'裏面放置'sender.hasPermission()'。這是沒有意義的。 – thekiwi5000 2015-04-08 10:17:08

回答

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 
}