Skip to content

Commit

Permalink
Fix support mob spawning and add debug logging thereof.
Browse files Browse the repository at this point in the history
  • Loading branch information
totemo committed Jan 24, 2023
1 parent 1b01623 commit 2eff787
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 11 deletions.
1 change: 1 addition & 0 deletions config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ debug:
blockstore: false
disguises: false
equipment-drops: false
support-mobs: false

chance:
wither-skeleton: 0.8
Expand Down
61 changes: 50 additions & 11 deletions src/nu/nerd/beastmaster/BeastMaster.java
Original file line number Diff line number Diff line change
Expand Up @@ -541,10 +541,19 @@ protected void onProjectileHit(ProjectileHitEvent event) {
}

// ------------------------------------------------------------------------
@SuppressWarnings("deprecation")
/**
* When a mob is damaged, play the `projectile-hurt-sound`, or the
* `melee-hurt-sound` for all other damage types.
* Handle damage to a mob.
*
* Processing:
* <ul>
* <li>Summon support mobs per the `support-*` properties.</li>
* <li>Process projectile immunity and corresponding sound.</li>
* <li>Play the `projectile-hurt-sound`, or the `melee-hurt-sound` for all
* other damage types.</li>
* <li>Apply `hurt-potions`.</li>
* <li>Teleport when hurt.</li>
* </ul>
* Note: onEntityDamange() is called after onEntityDamageByEntity().
*/
@EventHandler(ignoreCancelled = true, priority = EventPriority.HIGHEST)
Expand All @@ -567,19 +576,45 @@ protected void onEntityDamage(EntityDamageEvent event) {
Location mobLocation = entity.getLocation();

// Support mobs.
String supportId = (String) mobType.getProperty("support-mobs").getValue();
String supportId = (String) mobType.getDerivedProperty("support-mobs").getValue();
if (supportId != null) {
Double healthThreshold = (Double) mobType.getProperty("support-health").getValue();
Double healthThreshold = (Double) mobType.getDerivedProperty("support-health").getValue();
boolean healthLow = (healthThreshold == null || finalHealth <= healthThreshold);
Double prevHealth = (Double) EntityMeta.api().get(entity, this, "support-health");
Double healthStep = (Double) mobType.getProperty("support-health-step").getValue();
Double supportPercent = (Double) mobType.getProperty("support-percent").getValue();
Double healthStep = (Double) mobType.getDerivedProperty("support-health-step").getValue();
Double supportPercent = (Double) mobType.getDerivedProperty("support-percent").getValue();

if (healthLow && (prevHealth == null ||
healthStep == null ||
prevHealth - finalHealth >= healthStep)
&& (supportPercent == null ||
Math.random() * 100 < supportPercent)) {
if (healthThreshold == null) {
healthThreshold = damagedLiving.getMaxHealth();
}

// If support mobs have not previously spawned, require health
// to drop by at least healthStep.
if (prevHealth == null) {
prevHealth = damagedLiving.getMaxHealth();
}

if (healthStep == null) {
healthStep = 0.0;
}

if (supportPercent == null) {
supportPercent = 100.0;
}

if (CONFIG.DEBUG_SUPPORT_MOBS) {
getLogger().info("Support mobs for: " + mobType.getId());
getLogger().info(" Spawn when health <= " + healthThreshold);
getLogger().info(" Current health = " + finalHealth);
getLogger().info(" Previous health = " + prevHealth);
getLogger().info(" Change in health = " + (prevHealth - finalHealth));
getLogger().info(" Spawn when change >= " + healthStep);
getLogger().info(" % chance of spawn = " + supportPercent);
}

if (healthLow
&& (prevHealth - finalHealth >= healthStep)
&& Math.random() * 100 <= supportPercent) {

// TODO: spawning needs to do better at looking for a
// spawnable location. Really need to do spawn conditions
Expand All @@ -590,6 +625,10 @@ protected void onEntityDamage(EntityDamageEvent event) {
DropResults results = new DropResults();
List<LivingEntity> supportMobs = spawnMultipleMobs(supportLocation, supportId, false, results,
mobType.getId() + " support-mobs");
if (CONFIG.DEBUG_SUPPORT_MOBS) {
getLogger().info(" Spawned number of mobs = " + supportMobs.size());
}

if (damagedLiving instanceof Mob) {
for (LivingEntity mob : supportMobs) {
if (mob instanceof Mob) {
Expand Down
8 changes: 8 additions & 0 deletions src/nu/nerd/beastmaster/Configuration.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ public class Configuration {
*/
public boolean DEBUG_EQUIPMENT_DROPS;

/**
* If true, log debug messages about the spawning of support mobs to show
* why that did or didn't happen.
*/
public boolean DEBUG_SUPPORT_MOBS;

/**
* Probability, in the range [0.0,1.0] that a plains biome skeleton spawn in
* the nether environment will be replaced by a wither skeleton.
Expand All @@ -62,6 +68,7 @@ public void reload(boolean log) {
DEBUG_BLOCKSTORE = config.getBoolean("debug.blockstore");
DEBUG_DISGUISES = config.getBoolean("debug.disguises");
DEBUG_EQUIPMENT_DROPS = config.getBoolean("debug.equipment-drops");
DEBUG_SUPPORT_MOBS = config.getBoolean("debug.support-mobs");
CHANCE_WITHER_SKELETON = config.getDouble("chance.wither-skeleton");

EXCLUDED_ENTITY_TYPES.clear();
Expand All @@ -86,6 +93,7 @@ public void reload(boolean log) {
logger.info("DEBUG_BLOCKSTORE: " + DEBUG_BLOCKSTORE);
logger.info("DEBUG_DISGUISES: " + DEBUG_DISGUISES);
logger.info("DEBUG_EQUIPMENT_DROPS: " + DEBUG_EQUIPMENT_DROPS);
logger.info("DEBUG_SUPPORT_MOBS: " + DEBUG_SUPPORT_MOBS);
logger.info("CHANCE_WITHER_SKELETON: " + CHANCE_WITHER_SKELETON);

logger.info("EXCLUDED_ENTITY_TYPES: " + EXCLUDED_ENTITY_TYPES.stream()
Expand Down

0 comments on commit 2eff787

Please sign in to comment.