Permalink
Browse files

Added required items to CustomDrops. Also fixed silk touch for entities.

  • Loading branch information...
me4502 committed Mar 8, 2017
1 parent 39daf74 commit d3f53f74bf31414901e924b426832c6f1d146d40
@@ -22,6 +22,7 @@
// WorldGuard Integration
private List<String> regions;
+ private List<ItemStack> items;
public CustomDropDefinition(String name, List<DropItemStack> drops, List<DropReward> extraRewards, TernaryState silkTouch) {
this.drops = drops.toArray(new DropItemStack[drops.size()]);
@@ -50,6 +51,17 @@ public void setRegions(List<String> regions) {
}
}
+ public List<ItemStack> getItems() {
+ return this.items;
+ }
+
+ public void setItems(List<ItemStack> items) {
+ this.items = items;
+ if (this.items.isEmpty()) {
+ this.items = null;
+ }
+ }
+
public DropReward[] getRewards() {
if(extraRewards == null)
extraRewards = EMPTY_DROP_REWARDS;
@@ -63,6 +63,7 @@ public boolean load() {
boolean append = config.getBoolean("custom-drops." + key + ".append", false);
TernaryState silkTouch = TernaryState.getFromString(config.getString("custom-drops." + key + ".silk-touch", "none"));
List<String> regions = config.getStringList("custom-drops." + key + ".regions", null);
+ List<String> requiredItems = config.getStringList("custom-drops." + key + ".required-items", null);
List<DropItemStack> drops = new ArrayList<DropItemStack>();
@@ -123,6 +124,13 @@ public boolean load() {
if (regions != null) {
def.setRegions(regions);
}
+ if (requiredItems != null) {
+ List<ItemStack> items = new ArrayList<ItemStack>();
+ for (String requiredItem : requiredItems) {
+ items.add(ItemSyntax.getItem(requiredItem));
+ }
+ def.setItems(items);
+ }
definitions.add(def);
}
}
@@ -138,6 +146,13 @@ public void save() {
config.setProperty("custom-drops." + def.getName() + ".silk-touch", def.getSilkTouch().toString());
if (def.getRegions() != null)
config.setProperty("custom-drops." + def.getName() + ".regions", def.getRegions());
+ if (def.getItems() != null) {
+ List<String> itemsList = new ArrayList<String>();
+ for (ItemStack itemStack : def.getItems()) {
+ itemsList.add(ItemSyntax.getStringFromItem(itemStack));
+ }
+ config.setProperty("custom-drops." + def.getName() + ".required-items", itemsList);
+ }
int i = 0;
for(DropItemStack stack : def.getDrops()) {
@@ -208,6 +223,21 @@ public void onBlockBreak(BlockBreakEvent event) {
return;
}
+ if (def.getItems() != null) {
+ boolean found = false;
+
+ for (ItemStack item : def.getItems()) {
+ if (ItemUtil.areItemsIdentical(event.getPlayer().getItemInHand(), item)) {
+ found = true;
+ break;
+ }
+ }
+
+ if (!found) {
+ return;
+ }
+ }
+
boolean isSilkTouch = event.getPlayer().getInventory().getItemInMainHand() != null && event.getPlayer().getInventory().getItemInMainHand().getEnchantmentLevel(Enchantment.SILK_TOUCH) > 0;
if(!def.getSilkTouch().doesPass(isSilkTouch))
continue;
@@ -256,7 +286,26 @@ public void onEntityDeath(EntityDeathEvent event) {
return;
}
- boolean isSilkTouch = event.getEntity() instanceof Player && ((Player) event.getEntity()).getInventory().getItemInMainHand() != null && ((Player) event.getEntity()).getInventory().getItemInMainHand().getEnchantmentLevel(Enchantment.SILK_TOUCH) > 0;
+ Player killer = event.getEntity().getKiller();
+
+ if (def.getItems() != null) {
+ boolean found = false;
+
+ if (killer != null) {
+ for (ItemStack item : def.getItems()) {
+ if (ItemUtil.areItemsIdentical(killer.getInventory().getItemInMainHand(), item)) {
+ found = true;
+ break;
+ }
+ }
+ }
+
+ if (!found) {
+ return;
+ }
+ }
+
+ boolean isSilkTouch = killer != null && killer.getInventory().getItemInMainHand() != null && killer.getInventory().getItemInMainHand().getEnchantmentLevel(Enchantment.SILK_TOUCH) > 0;
if(!def.getSilkTouch().doesPass(isSilkTouch))
continue;
@@ -270,8 +319,6 @@ public void onEntityDeath(EntityDeathEvent event) {
event.getEntity().getWorld().dropItemNaturally(event.getEntity().getLocation(), stack);
}
- Player killer = event.getEntity().getKiller();
-
for(DropReward reward : def.getRewards()) {
if(killer == null && reward.doesRequirePlayer()) continue;
reward.giveReward(killer);

0 comments on commit d3f53f7

Please sign in to comment.