Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
206 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
src/main/java/net/citizensnpcs/api/ai/goals/MoveToGoal.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package net.citizensnpcs.api.ai.goals; | ||
|
||
import javax.annotation.Nullable; | ||
|
||
import net.citizensnpcs.api.ai.event.CancelReason; | ||
import net.citizensnpcs.api.ai.event.NavigatorCallback; | ||
import net.citizensnpcs.api.ai.tree.BehaviorGoalAdapter; | ||
import net.citizensnpcs.api.ai.tree.BehaviorStatus; | ||
import net.citizensnpcs.api.npc.NPC; | ||
|
||
import org.bukkit.Location; | ||
|
||
public class MoveToGoal extends BehaviorGoalAdapter { | ||
private boolean finished; | ||
private final NPC npc; | ||
private CancelReason reason; | ||
private final Location target; | ||
|
||
public MoveToGoal(NPC npc, Location target) { | ||
this.npc = npc; | ||
this.target = target; | ||
} | ||
|
||
@Override | ||
public void reset() { | ||
npc.getNavigator().cancelNavigation(); | ||
reason = null; | ||
finished = false; | ||
} | ||
|
||
@Override | ||
public BehaviorStatus run() { | ||
if (finished) { | ||
return reason == null ? BehaviorStatus.SUCCESS : BehaviorStatus.FAILURE; | ||
} | ||
return BehaviorStatus.RUNNING; | ||
} | ||
|
||
@Override | ||
public boolean shouldExecute() { | ||
boolean executing = !npc.getNavigator().isNavigating() && target != null; | ||
if (executing) { | ||
npc.getNavigator().setTarget(target); | ||
npc.getNavigator().getLocalParameters().addSingleUseCallback(new NavigatorCallback() { | ||
@Override | ||
public void onCompletion(@Nullable CancelReason cancelReason) { | ||
finished = true; | ||
reason = cancelReason; | ||
} | ||
}); | ||
} | ||
return executing; | ||
} | ||
} |
109 changes: 109 additions & 0 deletions
109
src/main/java/net/citizensnpcs/api/ai/goals/TargetNearbyEntityGoal.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
package net.citizensnpcs.api.ai.goals; | ||
|
||
import java.util.Collection; | ||
import java.util.EnumSet; | ||
import java.util.Set; | ||
|
||
import javax.annotation.Nullable; | ||
|
||
import net.citizensnpcs.api.ai.event.CancelReason; | ||
import net.citizensnpcs.api.ai.event.NavigatorCallback; | ||
import net.citizensnpcs.api.ai.tree.BehaviorGoalAdapter; | ||
import net.citizensnpcs.api.ai.tree.BehaviorStatus; | ||
import net.citizensnpcs.api.npc.NPC; | ||
|
||
import org.bukkit.entity.Entity; | ||
import org.bukkit.entity.EntityType; | ||
|
||
public class TargetNearbyEntityGoal extends BehaviorGoalAdapter { | ||
private final boolean aggressive; | ||
private boolean finished; | ||
private final NPC npc; | ||
private final double radius; | ||
private CancelReason reason; | ||
private Entity target; | ||
private final Set<EntityType> targets; | ||
|
||
private TargetNearbyEntityGoal(NPC npc, Set<EntityType> targets, boolean aggressive, double radius) { | ||
this.npc = npc; | ||
this.targets = targets; | ||
this.aggressive = aggressive; | ||
this.radius = radius; | ||
} | ||
|
||
@Override | ||
public void reset() { | ||
npc.getNavigator().cancelNavigation(); | ||
target = null; | ||
finished = false; | ||
reason = null; | ||
} | ||
|
||
@Override | ||
public BehaviorStatus run() { | ||
if (finished) { | ||
return reason == null ? BehaviorStatus.SUCCESS : BehaviorStatus.FAILURE; | ||
} | ||
return BehaviorStatus.RUNNING; | ||
} | ||
|
||
@Override | ||
public boolean shouldExecute() { | ||
if (targets.size() == 0 || !npc.isSpawned()) | ||
return false; | ||
Collection<Entity> nearby = npc.getBukkitEntity().getNearbyEntities(radius, radius, radius); | ||
this.target = null; | ||
for (Entity entity : nearby) { | ||
if (targets.contains(entity.getType())) { | ||
target = entity; | ||
break; | ||
} | ||
} | ||
if (target != null) { | ||
npc.getNavigator().setTarget(target, aggressive); | ||
npc.getNavigator().getLocalParameters().addSingleUseCallback(new NavigatorCallback() { | ||
@Override | ||
public void onCompletion(@Nullable CancelReason cancelReason) { | ||
reason = cancelReason; | ||
finished = true; | ||
} | ||
}); | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
public static class Builder { | ||
private boolean aggressive; | ||
private final NPC npc; | ||
private double radius = 10D; | ||
private Set<EntityType> targetTypes = EnumSet.noneOf(EntityType.class); | ||
|
||
public Builder(NPC npc) { | ||
this.npc = npc; | ||
} | ||
|
||
public Builder aggressive(boolean aggressive) { | ||
this.aggressive = aggressive; | ||
return this; | ||
} | ||
|
||
public TargetNearbyEntityGoal build() { | ||
return new TargetNearbyEntityGoal(npc, targetTypes, aggressive, radius); | ||
} | ||
|
||
public Builder radius(double radius) { | ||
this.radius = radius; | ||
return this; | ||
} | ||
|
||
public Builder targets(Set<EntityType> targetTypes) { | ||
this.targetTypes = targetTypes; | ||
return this; | ||
} | ||
} | ||
|
||
public static Builder builder(NPC npc) { | ||
return new Builder(npc); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package net.citizensnpcs.api.util; | ||
|
||
import java.io.File; | ||
|
||
public interface FileStorage extends Storage { | ||
File getFile(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters