Skip to content
Permalink
Browse files

Drop mob equipment with non-default drop chance using non-Bukkit code.

Setting drop chances for armour and main/off hand items through the Bukkit API
results in NBT ArmorDropChances and HandDropChances that are negative numbers,
where changed. Consequently nothing drops. This is presumably a Spigot bug,
which this commit works around.
  • Loading branch information...
totemo committed Feb 14, 2019
1 parent 9571503 commit 98accef93c75bdc6a3efba84a71ea89639f87d28
Showing with 60 additions and 15 deletions.
  1. +1 −1 pom.xml
  2. +36 −1 src/nu/nerd/beastmaster/BeastMaster.java
  3. +10 −0 src/nu/nerd/beastmaster/Util.java
  4. +13 −13 src/nu/nerd/beastmaster/mobs/MobType.java
@@ -4,7 +4,7 @@
<groupId>nu.nerd</groupId>
<name>BeastMaster</name>
<artifactId>${project.name}</artifactId>
<version>2.9.0</version>
<version>2.9.1</version>
<packaging>jar</packaging>
<description>Handles custom mob spawning.</description>
<url>https://github.com/NerdNu/${project.name}</url>
@@ -368,9 +368,44 @@ protected void onEntityDeath(EntityDeathEvent event) {
}
}

// Drop head, armour and hand items with non-vanilla code if
// those drop percent properties are set. This is a work around for
// a presumed Spigot bug wherein NBT drop HandItemDropChances and
// ArmorDropChances values are negative despite the Bukkit API
// drop chances being sane. Bukkit API drop chances are therefore
// set 0 in MobType when the properties are set.
// If the entity has a MobType, it's a LivingEntity.
LivingEntity mob = (LivingEntity) entity;
Location loc = mob.getLocation();
World world = loc.getWorld();

Double helmetPercent = (Double) mobType.getDerivedProperty("helmet-drop-percent").getValue();
if (helmetPercent != null && Util.random() < helmetPercent / 100) {
world.dropItem(loc, mob.getEquipment().getHelmet());
}
Double chestPercent = (Double) mobType.getDerivedProperty("chest-plate-drop-percent").getValue();
if (chestPercent != null && Util.random() < chestPercent / 100) {
world.dropItem(loc, mob.getEquipment().getChestplate());
}
Double leggingsPercent = (Double) mobType.getDerivedProperty("leggings-drop-percent").getValue();
if (leggingsPercent != null && Util.random() < leggingsPercent / 100) {
world.dropItem(loc, mob.getEquipment().getLeggings());
}
Double bootsPercent = (Double) mobType.getDerivedProperty("boots-drop-percent").getValue();
if (bootsPercent != null && Util.random() < bootsPercent / 100) {
world.dropItem(loc, mob.getEquipment().getBoots());
}
Double mainHandPercent = (Double) mobType.getDerivedProperty("main-hand-drop-percent").getValue();
if (mainHandPercent != null && Util.random() < mainHandPercent / 100) {
world.dropItem(loc, mob.getEquipment().getItemInMainHand());
}
Double offHandPercent = (Double) mobType.getDerivedProperty("off-hand-drop-percent").getValue();
if (offHandPercent != null && Util.random() < offHandPercent / 100) {
world.dropItem(loc, mob.getEquipment().getItemInOffHand());
}

Long damageTime = getPlayerDamageTime(entity);
if (damageTime != null) {
Location loc = entity.getLocation();
if (loc.getWorld().getFullTime() - damageTime < PLAYER_DAMAGE_TICKS) {
MobProperty experience = mobType.getDerivedProperty("experience");
if (experience.getValue() != null) {
@@ -200,6 +200,16 @@ public static double random(double min, double max) {
return min + _random.nextDouble() * (max - min);
}

// ------------------------------------------------------------------------
/**
* Return a random double in the range [0.0,1.0).
*
* @return a random double in the range [0.0,1.0).
*/
public static double random() {
return _random.nextDouble();
}

// ------------------------------------------------------------------------
/**
* Random number generator.
@@ -286,7 +286,7 @@ public void configureMob(LivingEntity mob) {
EntityMeta.api().set(mob, BeastMaster.PLUGIN, "mob-type", getId());

for (String propertyId : getAllPropertyIds()) {
getDerivedProperty(propertyId).configureMob(mob, null);
getDerivedProperty(propertyId).configureMob(mob, BeastMaster.PLUGIN.getLogger());
}
}

@@ -426,8 +426,8 @@ protected void addProperties() {
}));
addProperty(new MobProperty("helmet-drop-percent", DataType.DOUBLE,
(mob, logger) -> {
double percent = (Double) getDerivedProperty("helmet-drop-percent").getValue();
mob.getEquipment().setHelmetDropChance((float) percent / 100);
// Outside vanilla drop mechanism.
mob.getEquipment().setHelmetDropChance(0);
}));
addProperty(new MobProperty("chest-plate", DataType.STRING,
(mob, logger) -> {
@@ -439,8 +439,8 @@ protected void addProperties() {
}));
addProperty(new MobProperty("chest-plate-drop-percent", DataType.DOUBLE,
(mob, logger) -> {
double percent = (Double) getDerivedProperty("chest-plate-drop-percent").getValue();
mob.getEquipment().setChestplateDropChance((float) percent / 100);
// Outside vanilla drop mechanism.
mob.getEquipment().setChestplateDropChance(0);
}));
addProperty(new MobProperty("leggings", DataType.STRING,
(mob, logger) -> {
@@ -452,8 +452,8 @@ protected void addProperties() {
}));
addProperty(new MobProperty("leggings-drop-percent", DataType.DOUBLE,
(mob, logger) -> {
double percent = (Double) getDerivedProperty("leggings-drop-percent").getValue();
mob.getEquipment().setLeggingsDropChance((float) percent / 100);
// Outside vanilla drop mechanism.
mob.getEquipment().setLeggingsDropChance(0);
}));
addProperty(new MobProperty("boots", DataType.STRING,
(mob, logger) -> {
@@ -465,8 +465,8 @@ protected void addProperties() {
}));
addProperty(new MobProperty("boots-drop-percent", DataType.DOUBLE,
(mob, logger) -> {
double percent = (Double) getDerivedProperty("boots-drop-percent").getValue();
mob.getEquipment().setBootsDropChance((float) percent / 100);
// Outside vanilla drop mechanism.
mob.getEquipment().setBootsDropChance(0);
}));
addProperty(new MobProperty("main-hand", DataType.STRING,
(mob, logger) -> {
@@ -478,8 +478,8 @@ protected void addProperties() {
}));
addProperty(new MobProperty("main-hand-drop-percent", DataType.DOUBLE,
(mob, logger) -> {
double percent = (Double) getDerivedProperty("main-hand-drop-percent").getValue();
mob.getEquipment().setItemInMainHandDropChance((float) percent / 100);
// Outside vanilla drop mechanism.
mob.getEquipment().setItemInMainHandDropChance(0);
}));
addProperty(new MobProperty("off-hand", DataType.STRING,
(mob, logger) -> {
@@ -491,8 +491,8 @@ protected void addProperties() {
}));
addProperty(new MobProperty("off-hand-drop-percent", DataType.DOUBLE,
(mob, logger) -> {
double percent = (Double) getDerivedProperty("off-hand-drop-percent").getValue();
mob.getEquipment().setItemInOffHandDropChance((float) percent / 100);
// Outside vanilla drop mechanism.
mob.getEquipment().setItemInOffHandDropChance(0);
}));
// Added after custom name => will clear PersistenceRequired NBT.
addProperty(new MobProperty("can-despawn", DataType.BOOLEAN,

0 comments on commit 98accef

Please sign in to comment.
You can’t perform that action at this time.