Guest User

Wander Code Simplified

a guest
Dec 31st, 2025
8
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.60 KB | None | 0 0
  1. import de.bsommerfeld.pathetic.api.pathing.result.PathfinderResult;
  2. import de.bsommerfeld.pathetic.api.wrapper.PathPosition;
  3. import de.bsommerfeld.pathetic.bukkit.context.BukkitEnvironmentContext;
  4. import de.bsommerfeld.pathetic.bukkit.mapper.BukkitMapper;
  5. import net.citizensnpcs.api.ai.tree.BehaviorGoalAdapter;
  6. import net.citizensnpcs.api.ai.tree.BehaviorStatus;
  7. import net.citizensnpcs.api.trait.trait.Equipment;
  8. import net.citizensnpcs.trait.waypoint.Waypoints;
  9. import org.bukkit.*;
  10. import org.bukkit.entity.Entity;
  11. import org.bukkit.entity.EntityType;
  12. import org.bukkit.entity.LivingEntity;
  13. import org.bukkit.entity.Player;
  14. import org.bukkit.inventory.ItemStack;
  15. import org.bukkit.inventory.meta.ItemMeta;
  16.  
  17. import net.citizensnpcs.api.npc.NPC;
  18. import org.bukkit.util.Vector;
  19. import org.mcmonkey.sentinel.SentinelTrait;
  20.  
  21. import java.util.*;
  22. import java.util.concurrent.CompletionStage;
  23.  
  24. public class HandSwitchBehavior extends BehaviorGoalAdapter {
  25.  
  26.  
  27. /**
  28. * This behavior cycles through weapons
  29. * @param npcWanderer that switches weapons
  30. * @param g weapon 1
  31. * @param g2 weapon 2
  32. */
  33. public HandSwitchBehavior(NPC npcWanderer, ItemStack g, ItemStack g2) {
  34. this.npc = npcWanderer;
  35. // more fields initialized here
  36. }
  37.  
  38. @Override
  39. public void reset() {
  40. target = null;
  41. }
  42.  
  43. @Override
  44. public BehaviorStatus run() {
  45. Entity entity = npc.getEntity();
  46. if (entity == null || !entity.isValid() || entity.isDead()) {
  47. return BehaviorStatus.RUNNING; // stop pathfinding entirely
  48. }
  49. SentinelTrait st = npc.getOrAddTrait(SentinelTrait.class);
  50.  
  51. // SEARCH FOR TARGET
  52. if (target == null) {
  53. // Loop and find best candidate
  54. target = bestCandidate;
  55. }
  56.  
  57. // TARGET FOUND
  58. if (target instanceof Player player) {
  59. npc.getNavigator().setPaused(true);
  60. npc.getNavigator().getLocalParameters().speedModifier(1f);
  61. }
  62.  
  63. // TARGET INVALID
  64. if (target != null && target.isDead()) {
  65. target = null;
  66. npc.getNavigator().setPaused(false);
  67. npc.getNavigator().getLocalParameters().speedModifier(1f);
  68. }
  69.  
  70. // --- PATHFINDING TO TARGET ---
  71. if (target != null) {
  72. // Compute path here
  73. }
  74.  
  75. // --- Taking Control here ---
  76. if (!simplifiedPath.isEmpty() && pathIndex < simplifiedPath.size()) {
  77. Vector currentTarget = simplifiedPath.get(pathIndex);
  78. Location targetLoc = currentTarget.toLocation(npc.getEntity().getWorld());
  79.  
  80. // Update every tick with setMoveDestination()
  81. npc.getNavigator().getLocalParameters().speedModifier(1.95f);
  82. npc.getNavigator().getLocalParameters().range(0.5f);
  83.  
  84. npc.setMoveDestination(targetLoc);
  85.  
  86. pathIndex++;
  87.  
  88. }
  89.  
  90. // Back to wander mode (not working)
  91. if (target == null) {
  92. if (!wandering) {
  93. npc.getOrAddTrait(Waypoints.class).setWaypointProvider("wander");
  94. wandering = true;
  95. }
  96. return BehaviorStatus.RUNNING;
  97. }
  98.  
  99. // FACE TARGET ENTITY
  100. st.faceLocation(target.getLocation());
  101. if (target instanceof Player player) {
  102. if (st.attackHelper.tryAttack((LivingEntity) target)) {
  103. return BehaviorStatus.RUNNING;
  104. }
  105. return BehaviorStatus.RUNNING;
  106. }
  107.  
  108. return BehaviorStatus.RUNNING;
  109. }
  110.  
  111. @Override
  112. public boolean shouldExecute() {
  113. return true;
  114. }
  115.  
  116. }
Advertisement
Add Comment
Please, Sign In to add comment