Skip to content

Commit

Permalink
Implementation of Pathfind
Browse files Browse the repository at this point in the history
Pathfinding check has been added. Likely to be the final implementation of the targetting.

Pathfinding is strictly checking for path within 1 block during damage events. If there exists no path between the player and entity, it is assumed the player cannot be attacked.

Created registerCommand() to get rid of the annoying IntelliJ warning wanting me to do Objects.requireNonNull() and no I am totally not angry.
  • Loading branch information
Peashooter101 committed Aug 9, 2022
1 parent 0ffe767 commit 24b3d14
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 4 deletions.
24 changes: 21 additions & 3 deletions src/main/java/adhdmc/nerffarms/MobDamageListener.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package adhdmc.nerffarms;

import com.destroystokyo.paper.entity.Pathfinder;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.NamespacedKey;
Expand All @@ -17,8 +18,8 @@
import java.util.logging.Logger;

public class MobDamageListener implements Listener {
NamespacedKey nerfMob = new NamespacedKey(NerfFarms.plugin, "nerfMob");
NamespacedKey environmentalDamage = new NamespacedKey(NerfFarms.plugin, "environmentalDamage");
public static final NamespacedKey nerfMob = new NamespacedKey(NerfFarms.plugin, "nerfMob");
public static final NamespacedKey environmentalDamage = new NamespacedKey(NerfFarms.plugin, "environmentalDamage");
byte f = 0;
byte t = 1;

Expand Down Expand Up @@ -101,6 +102,13 @@ public void onMobDamage(EntityDamageByEntityEvent damageEvent) {
mobPDC.set(nerfMob, PersistentDataType.BYTE, t);
return;
}
if (!hasPathToPlayer((Player) entityDamager, (Mob) damagedEntity)) {
if (d) {
l.info("Nerfing " + damagedEntity.getName() + " because they never could reach the player.");
}
mobPDC.set(nerfMob, PersistentDataType.BYTE, t);
return;
}
if (ConfigParser.getStandOnBlackList().contains(entityStandingOn)) {
if (d) {
l.info("Nerfing " + damagedEntity.getName() + " since they are standing on " + entityStandingOn);
Expand All @@ -116,7 +124,17 @@ public void onMobDamage(EntityDamageByEntityEvent damageEvent) {
}
}

private void addPDCDamage(PersistentDataContainer mobPDC, double damage) {
private boolean hasPathToPlayer(Player p, Mob m) {
Location playerLoc = p.getLocation();
Pathfinder.PathResult entityPath = m.getPathfinder().findPath(p);
if (entityPath == null) { return false; }
Location finalLoc = entityPath.getFinalPoint();
if (finalLoc == null) { return false; }
// TODO: Make configurable distance.
return playerLoc.distance(finalLoc) < 1;
}

private static void addPDCDamage(PersistentDataContainer mobPDC, double damage) {
double damageTotal = mobPDC.getOrDefault(environmentalDamage, PersistentDataType.DOUBLE, 0.0);
damageTotal += damage;
mobPDC.set(environmentalDamage, PersistentDataType.DOUBLE, damageTotal);
Expand Down
11 changes: 10 additions & 1 deletion src/main/java/adhdmc/nerffarms/NerfFarms.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

import adhdmc.nerffarms.Commands.CommandHandler;
import net.kyori.adventure.text.minimessage.MiniMessage;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.PluginCommand;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.plugin.java.JavaPlugin;

Expand All @@ -22,7 +25,13 @@ public void onEnable() {
this.saveDefaultConfig();
this.getServer().getPluginManager().registerEvents(new MobDeathListener(), this);
this.getServer().getPluginManager().registerEvents(new MobDamageListener(), this);
this.getCommand("nerffarms").setExecutor(new CommandHandler());
registerCommand(this.getCommand("nerffarms"), new CommandHandler());
}

private static void registerCommand(PluginCommand command, CommandExecutor executor) {
if (command != null) {
command.setExecutor(executor);
}
}

private void configDefaults() {
Expand Down

0 comments on commit 24b3d14

Please sign in to comment.