[英]Drops being cleared, but still dropping
所以我正在制作一個插件,到目前為止一切正常。
我唯一的問題是擦拭胸甲絕殺物品,有時物品掉落兩次,一次破損且性能不變。 任何幫助表示贊賞
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();
}
}
}
這里有很多問題,但是我認為您看到重復的項目的原因是因為您將其添加到放置列表以及設備插槽中。 當暴民裝備了一個物品時,掉落該物品的幾率很小。 因此,基本上,您要添加兩次該項目,偶爾還會有一個額外的項目掉線。 另外,如果您想擴展此代碼格式和可讀性,則非常有用。
我通常不這樣做,但我想幫助您。 我正在記事本中執行此操作,因此請盡量避免出現任何小錯誤。 如果有任何小錯誤,請告訴我,以便我可以相應地更新代碼。
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);
}
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.