2015-04-17 224 views
1

所以我正在製作一個插件,並且一切工作到目前爲止。丟棄被清除,但仍然下降

我唯一的問題是胸板絕殺溼巾,物品有時會掉落兩次,其中一次是耐用性破壞和相同的統計數據。任何幫助表示讚賞

package me.impatheimpaler.mmo; 

import java.util.ArrayList; 
import java.util.List; 
import java.util.Random; 
import org.bukkit.ChatColor; 
import org.bukkit.Material; 
import org.bukkit.entity.Player; 
import org.bukkit.entity.Skeleton; 
import org.bukkit.event.EventHandler; 
import org.bukkit.event.Listener; 
import org.bukkit.event.entity.EntityDeathEvent; 
import org.bukkit.inventory.ItemStack; 
import org.bukkit.inventory.meta.ItemMeta; 
import org.bukkit.plugin.java.JavaPlugin; 

public class Mobdrops extends JavaPlugin implements Listener { 

public List<String> t1h = new ArrayList<String>(); 
public List<String> t1c = new ArrayList<String>(); 
public List<String> t1l = new ArrayList<String>(); 
public List<String> t1b = new ArrayList<String>(); 
public List<String> t1s = new ArrayList<String>(); 

public static me.impatheimpaler.mmo.Main plugin; 

public Mobdrops(Main main) { 
    plugin = main; 
} 

@EventHandler 
public void onDeath(EntityDeathEvent e) { 

    Skeleton s = (Skeleton) e.getEntity(); 

    if (!(e.getEntity() instanceof Player)) { 
     e.getDrops().clear(); 
      e.setDroppedExp(0); 
     } 

     if ((e.getEntity() instanceof Skeleton)) { 
     Skeleton sk = (Skeleton)e.getEntity(); 
     if (sk.getCustomName() == null) { 
      return; 
     } 
     } 

    Random random = new Random(); 
    int rarity = random.nextInt(3) + 1; 
    int chestdrop = random.nextInt(20) + 1; 
    int legsdrop = random.nextInt(17) + 1; 
    int helmdrop = random.nextInt(6) + 1; 
    int bootsdrop = random.nextInt(11) + 1; 
    int swordDrop = random.nextInt(15) + 1; 


    if (helmdrop == 3) { 
    ItemStack t1helm = new ItemStack(Material.LEATHER_HELMET); 
    ItemMeta t1helmMeta = t1helm.getItemMeta(); 
    t1helmMeta.setDisplayName(ChatColor.WHITE + "Renegade's Torn Helmet"); 
    if (rarity == 3) { 
    int hp = random.nextInt(20) + 33; 
    t1h.add(ChatColor.RED + "HP: +" + hp); 
    t1h.add(ChatColor.GOLD + "Legendary"); 
    } 
    if (rarity == 2) { 
    int hp = random.nextInt(10) + 20; 
    t1h.add(ChatColor.RED + "HP: +" + hp); 
    t1h.add(ChatColor.AQUA + "Normal"); 
    } 
    if (rarity == 1) { 
    int hp = random.nextInt(15) + 6; 
    t1h.add(ChatColor.RED + "HP: +" + hp); 
    t1h.add(ChatColor.GRAY + "Poor"); 
    } 
    t1helmMeta.setLore(t1h); 
    t1helm.setItemMeta(t1helmMeta); 
    s.getEquipment().setHelmet(t1helm); 
    e.getDrops().add(t1helm); 
    t1h.clear(); 
    } 

    if (chestdrop == 7) { 
    ItemStack t1chest = new ItemStack(Material.LEATHER_CHESTPLATE); 
    ItemMeta t1chestMeta = t1chest.getItemMeta(); 
    t1chestMeta.setDisplayName(ChatColor.WHITE + "Renegade's Torn Chestplate"); 
    if (rarity == 3) { 
    int hp = random.nextInt(20) + 72; 
    t1c.add(ChatColor.RED + "HP: +" + hp); 
    t1c.add(ChatColor.GOLD + "Legendary"); 
    } 
    if (rarity == 2) { 
    int hp = random.nextInt(30) + 33; 
    t1c.add(ChatColor.RED + "HP: +" + hp); 
    t1c.add(ChatColor.AQUA + "Normal"); 
    } 
    if (rarity == 1) { 
    int hp = random.nextInt(10) + 20; 
    t1c.add(ChatColor.RED + "HP: +" + hp); 
    t1c.add(ChatColor.GRAY + "Poor"); 
    } 
    t1chestMeta.setLore(t1c); 
    t1chest.setItemMeta(t1chestMeta); 
    s.getEquipment().setChestplate(t1chest); 
    e.getDrops().add(t1chest); 
    t1c.clear(); 
} 

    if (legsdrop == 2) { 
    ItemStack t1legs = new ItemStack(Material.LEATHER_LEGGINGS); 
    ItemMeta t1legsMeta = t1legs.getItemMeta(); 
    t1legsMeta.setDisplayName(ChatColor.WHITE + "Renegade's Torn Leggings"); 
    if (rarity == 3) { 
    int hp = random.nextInt(20) + 61; 
    t1l.add(ChatColor.RED + "HP: +" + hp); 
    t1l.add(ChatColor.YELLOW + "Legendary"); 
    } 
    if (rarity == 2) { 
    int hp = random.nextInt(20) + 33; 
    t1l.add(ChatColor.RED + "HP: +" + hp); 
    t1l.add(ChatColor.AQUA + "Normal"); 
    } 
    if (rarity == 1) { 
    int hp = random.nextInt(10) + 10; 
    t1l.add(ChatColor.RED + "HP: +" + hp); 
    t1l.add(ChatColor.GRAY + "Poor"); 
    } 
    t1legsMeta.setLore(t1l); 
    t1legs.setItemMeta(t1legsMeta); 
    s.getEquipment().setLeggings(t1legs); 
    e.getDrops().add(t1legs); 
    t1l.clear(); 
    } 

    if (bootsdrop == 1) { 
    ItemStack t1boots = new ItemStack(Material.LEATHER_BOOTS); 
    ItemMeta t1bootsMeta = t1boots.getItemMeta(); 
    t1bootsMeta.setDisplayName(ChatColor.WHITE + "Renegade's Torn Boots"); 
    if (rarity == 3) { 
    int hp = random.nextInt(20) + 23; 
    t1b.add(ChatColor.RED + "HP: +" + hp); 
    t1b.add(ChatColor.YELLOW + "Legendary"); 
    } 
    if (rarity == 2) { 
    int hp = random.nextInt(10) + 10; 
    t1b.add(ChatColor.RED + "HP: +" + hp); 
    t1b.add(ChatColor.AQUA + "Normal"); 
    } 
    if (rarity == 1) { 
    int hp = random.nextInt(5) + 6; 
    t1b.add(ChatColor.RED + "HP: +" + hp); 
    t1b.add(ChatColor.GRAY + "Poor"); 
    } 
    t1bootsMeta.setLore(t1b); 
    t1boots.setItemMeta(t1bootsMeta); 
    s.getEquipment().setBoots(t1boots); 
    e.getDrops().add(t1boots); 
    t1b.clear(); 
    } 

    if (swordDrop == 3) { 
    ItemStack t1sword = new ItemStack(Material.WOOD_SWORD); 
    ItemMeta t1swordMeta = t1sword.getItemMeta(); 
    if (rarity == 3) { 
     int min = random.nextInt(20) + 11; 
     int max = random.nextInt(20) + 21; 
     t1s.add(ChatColor.RED + "DMG: " + min + " - " + max); 
     t1s.add(ChatColor.YELLOW + "Legendary"); 
    } 
    if (rarity == 2) { 
     int max = random.nextInt(10) + 21; 
     int min = random.nextInt(10) + 11; 
     t1s.add(ChatColor.RED + "DMG: " + min + " - " + max); 
     t1s.add(ChatColor.AQUA + "Normal"); 
    } 
    if (rarity == 1) { 
     int min = random.nextInt(5) + 6; 
     int max = random.nextInt(10) + 11; 
     t1s.add(ChatColor.RED + "DMG: " + min + " - " + max); 
     t1s.add(ChatColor.GRAY + "Poor"); 
    } 
    t1swordMeta.setLore(t1s); 
    t1sword.setItemMeta(t1swordMeta); 
    s.getEquipment().setItemInHand(t1sword); 
    e.getDrops().add(t1sword); 
    t1s.clear(); 
     } 
} 
} 
+2

您可以創建一個[MCVE](http://stackoverflow.com/help/mcve)嗎? –

+1

不知道代碼的意圖是什麼,不可能提供有用的建議來解決「修復」問題。通過您的代碼格式化和評論。運行一個__debugger__。如果你不知道那是什麼,請閱讀__book__。 – Unihedron

+0

這裏有很多問題(假設你想讓特殊物品從骨架上掉落下來)你的代碼顯示「關於任何實體死亡,丟棄物品」(包括玩家),只有在沒有自定義名稱的骨架時纔會被取消。你應該在'if(稀有==#)'之間使用一些「else if」,因爲沒有必要浪費資源來再次檢查。你也有't1helmMeta.setLore(th1)'這樣的東西,其中'th1'只是** **甚至如果其中一個if語句是真的,你應該'返回' 我真的不能看到問題與其他許多人一樣,我很抱歉..也許清理你的意圖? – zfb

回答

0

有相當多的問題在這裏,但我認爲你看到重複項的原因是因爲你將它添加到兩個下拉列表以及一個裝備槽。當一個暴民裝備了一件物品時,它有一個掉落該物品的可能性很小。所以基本上你是兩次添加這個項目,偶爾會有一個額外的下降。此外,如果您想要擴展此代碼,則代碼格式和可讀性非常高。

我通常不這樣做,但我想幫助你。我在記事本中這樣做,所以請帶上任何小錯誤。如果有任何小錯誤讓我知道,所以我可以相應地更新代碼。

package me.impatheimpaler.mmo; 

import java.util.ArrayList; 
import java.util.List; 
import java.util.Random; 
import org.bukkit.ChatColor; 
import org.bukkit.Material; 
import org.bukkit.entity.Player; 
import org.bukkit.entity.Skeleton; 
import org.bukkit.event.EventHandler; 
import org.bukkit.event.Listener; 
import org.bukkit.event.entity.EntityDeathEvent; 
import org.bukkit.inventory.ItemStack; 
import org.bukkit.inventory.meta.ItemMeta; 
import org.bukkit.plugin.java.JavaPlugin; 

public class Mobdrops extends JavaPlugin implements Listener { 

    public static me.impatheimpaler.mmo.Main plugin; 

    public Mobdrops(Main main) { 
     plugin = main; 
    } 

    private ItemStack AddArmorToDropList(EntityDeathEvent e, Material oMaterial, String strItemName, int intRarity, int T3Random, int T3Bonus, int T2Random, int T2Bonus, int T1Random, int T1Bonus) { 
     ItemStack oItemStack = new ItemStack(oMaterial); 
     ItemMeta oItemMeta = oItemStack.getItemMeta(); 
     oItemMeta.setDisplayName(strItemName); 
     List<String> arrItemLore = new ArrayList<String>(); //Always use descriptive variable names 

     Random random = new Random(); 
     if (intRarity == 3) { 
      arrItemLore.add(ChatColor.RED + "HP: +" + (random.nextInt(T3Random) + T3Bonus)); 
      arrItemLore.add(ChatColor.GOLD + "Legendary"); 
     } 
     else if (intRarity == 2) { 
      arrItemLore.add(ChatColor.RED + "HP: +" +()random.nextInt(T2Random) + T2Bonus); 
      arrItemLore.add(ChatColor.AQUA + "Normal"); 
     } 
     else { //Always use a final else to catch all exceptions 
      arrItemLore.add(ChatColor.RED + "HP: +" + (random.nextInt(T1Random) + T1Bonus)); 
      arrItemLore.add(ChatColor.GRAY + "Poor"); 
     } 
     oItemMeta.setLore(arrItemLore); 
     oItemStack.setItemMeta(oItemMeta); 
     e.getDrops().add(oItemStack); 
    } 
    private void AddWeaponToDropList(EntityDeathEvent e, Material oMaterial, String strItemName, int intRarity, int T3MinDmg, int T3MaxDmg, int T2MinDmg, int T2MaxDmg, int T1MinDmg, int T1MaxDmg) { 
     ItemStack oItemStack = new ItemStack(oMaterial); 
     ItemMeta oItemMeta = oItemStack.getItemMeta(); 
     oItemMeta.setDisplayName(strItemName); 
     List<String> arrItemLore = new ArrayList<String>(); //Always use descriptive variable names 

     Random random = new Random(); 
     if (intRarity == 3) { 
      arrItemLore.add(ChatColor.RED + "DMG: " + T3MinDmg + " - " + T3MaxDmg); 
      arrItemLore.add(ChatColor.GOLD + "Legendary"); 
     } 
     else if (intRarity == 2) { 
      arrItemLore.add(ChatColor.RED + "DMG: " + T2MinDmg + " - " + T2MaxDmg); 
      arrItemLore.add(ChatColor.AQUA + "Normal"); 
     } 
     else { //Always use a final else to catch all exceptions 
      arrItemLore.add(ChatColor.RED + "DMG: " + T1MinDmg + " - " + T1MaxDmg); 
      arrItemLore.add(ChatColor.GRAY + "Poor"); 
     } 
     oItemMeta.setLore(arrItemLore); 
     oItemStack.setItemMeta(oItemMeta); 
     e.getDrops().add(oItemStack); 
    } 

    @EventHandler 
    public void onDeath(EntityDeathEvent e) { 

     Skeleton oSkeleton = null; //Never do a direct cast without being sure that the entity is what you think it is 

     if (!(e.getEntity() instanceof Player)) { //Clear items and exp dropped 
      e.getDrops().clear(); 
      e.setDroppedExp(0); 
     } 
     if (e.getEntity() instanceof Skeleton) { 
      if (oSkeleton.getCustomName() != null) { 
       oSkeleton = (Skeleton) e.getEntity(); //Set oSkeleton value 
      } 
     } 

     if (oSkeleton != null) { 
      Random random = new Random(); 
      int intRarity = random.nextInt(3) + 1; 

      if ((random.nextInt(6) + 1) == 3) { 
       AddArmorToDropList(e, Material.LEATHER_HELMET, ChatColor.WHITE + "Renegade's Torn Helmet", intRarity, 20, 33, 10, 20, 15, 6); 
      } 

      if ((random.nextInt(20) + 1) == 7) { 
       AddArmorToDropList(e, Material.LEATHER_CHESTPLATE, ChatColor.WHITE + "Renegade's Torn Chestplate", intRarity, 20, 72, 30, 33, 10, 20); 
      } 

      if ((random.nextInt(17) + 1) == 2) { 
       AddArmorToDropList(e, Material.LEATHER_LEGGINGS, ChatColor.WHITE + "Renegade's Torn Leggings", intRarity, 20, 61, 20, 33, 10, 10); 
      } 

      if ((random.nextInt(11) + 1) == 1) { 
       AddArmorToDropList(e, Material.LEATHER_BOOTS, ChatColor.WHITE + "Renegade's Torn Boots", intRarity, 20, 23, 10, 10, 5, 6); 
      } 

      if ((random.nextInt(15) + 1) == 3) { 
       int intT3Min = random.nextInt(20) + 11; 
       int intT3Max = random.nextInt(20) + 21; 
       int intT2Min = random.nextInt(10) + 11; 
       int intT2Max = random.nextInt(10) + 21; 
       int intT1Min = random.nextInt(5) + 6; 
       int intT1Max = random.nextInt(10) + 11; 

       AddWeaponToDropList(e, Material.WOOD_SWORD, ChatColor.WHITE + "RGAMinecraft's Sword", intRarity, intT3Min, intT2Max, intT2Min, intT3Max, intT1Min, intT1Max); 
      } 
     } 
    } 
} 
相關問題