Skip to content

Commit 1efd3f1

Browse files
committed
commit
1 parent 0385748 commit 1efd3f1

2 files changed

Lines changed: 43 additions & 4 deletions

File tree

src/main/java/adhdmc/nerffarms/listener/MobDamageListener.java

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,15 @@
2020
import org.bukkit.persistence.PersistentDataType;
2121

2222
import java.util.ArrayList;
23+
import java.util.List;
2324
import java.util.Objects;
2425
import java.util.Set;
2526

2627
public class MobDamageListener implements Listener {
2728
public static final NamespacedKey nerfMob = new NamespacedKey(NerfFarms.plugin, "nerf-mob");
2829
public static final NamespacedKey blacklistedDamage = new NamespacedKey(NerfFarms.plugin, "blacklisted-damage");
2930
private static final byte t = 1;
31+
private static final ArrayList<Material> air = new ArrayList<>(List.of(Material.AIR, Material.CAVE_AIR, Material.VOID_AIR));
3032

3133
@EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true)
3234
public void onMobDamage(EntityDamageEvent damageEvent) {
@@ -332,8 +334,24 @@ private boolean checkPath(EntityDamageByEntityEvent event, Entity entity, Persis
332334
NerfFarms.debugLvl2("Entity's path is null. Returning false");
333335
return false;
334336
}
335-
if (!(entityPath.getPoints().size() > 1) ) {
336-
NerfFarms.debugLvl2("Entity does not have a path to the player. Returning true");
337+
int pathLength = entityPath.getPoints().size();
338+
Location entityLoc = entity.getLocation();
339+
double distance = entityLoc.distance(targetLoc);
340+
List<Location> pathPoints = entityPath.getPoints();
341+
if (pathLength <= 1 ) {
342+
NerfFarms.debugLvl2("Entity does not have a path to the player (Path length less than or equal to 1). Returning true");
343+
addPDCDamage(event, mobPDC, hitDamage);
344+
checkDamageThreshold(mobPDC, entity);
345+
return true;
346+
}
347+
if (!(air.contains(CheckUtils.getBlockAbove(pathPoints.get(1), entity)))) {
348+
NerfFarms.debugLvl2("Entity does not have a path to the player (2nd path point is blocked). Returning true");
349+
addPDCDamage(event, mobPDC, hitDamage);
350+
checkDamageThreshold(mobPDC, entity);
351+
return true;
352+
}
353+
if (!(air.contains(CheckUtils.getBlockAbove(pathPoints.get(2), entity)))) {
354+
NerfFarms.debugLvl2("Entity does not have a path to the player (3rd path point is blocked). Returning true");
337355
addPDCDamage(event, mobPDC, hitDamage);
338356
checkDamageThreshold(mobPDC, entity);
339357
return true;
@@ -373,6 +391,14 @@ private boolean checkDistance(EntityDamageByEntityEvent event, Entity entity, Pe
373391
return false;
374392
}
375393

394+
/**
395+
* Checks the blocks around a mob's head, to make sure it is not blocked from moving
396+
* @param event EntityDamageEvent
397+
* @param entity Damaged Entity
398+
* @param mobPDC Mob's Persistent Data Container
399+
* @param hitDamage double Final Damage
400+
* @return boolean
401+
*/
376402

377403
private boolean checkSurroundings(EntityDamageByEntityEvent event, Entity entity, PersistentDataContainer mobPDC, double hitDamage){
378404
LivingEntity damager = CheckUtils.getRealDamager(event);

src/main/java/adhdmc/nerffarms/util/CheckUtils.java

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22

33
import adhdmc.nerffarms.NerfFarms;
44
import adhdmc.nerffarms.config.ConfigToggle;
5+
import org.bukkit.Location;
6+
import org.bukkit.Material;
57
import org.bukkit.entity.Entity;
68
import org.bukkit.entity.LivingEntity;
7-
import org.bukkit.entity.Player;
89
import org.bukkit.entity.Projectile;
910
import org.bukkit.event.entity.EntityDamageByEntityEvent;
10-
import org.bukkit.projectiles.ProjectileSource;
1111

1212
public class CheckUtils {
1313

@@ -38,4 +38,17 @@ public static LivingEntity getRealDamager(EntityDamageByEntityEvent event){
3838
NerfFarms.debugLvl2("getRealDamager check, shooter is a LivingEntity, returning player value of: " + shooter);
3939
return shooter;
4040
}
41+
42+
public static Material getBlockAbove(Location location, Entity entity){
43+
double entityHeight = entity.getHeight();
44+
if (entityHeight > 1) {
45+
location = location.add(0, (entityHeight - 1), 0);
46+
}
47+
NerfFarms.debugLvl3("getBlockAbove: MaterialType " + location.getBlock().getType() + "\nLocation: " + location);
48+
return location.getBlock().getType();
49+
}
50+
51+
public static Material getBlockBelow(Location location) {
52+
return location.getBlock().getType();
53+
}
4154
}

0 commit comments

Comments
 (0)