丢弃被清除,但仍在下降

时间:2015-04-17 13:13:14

标签: java minecraft bukkit

所以我正在制作一个插件,到目前为止一切正常。

我唯一的问题是胸部绝杀擦拭,物品有时会下降两次,一次具有破损的耐久性和相同的统计数据。任何帮助表示赞赏

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();
      }
}
}

1 个答案:

答案 0 :(得分: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);
            }
        }
    }
}