Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add simple /npc pausepathfinding command
- Loading branch information
Showing
5 changed files
with
107 additions
and
0 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
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
66 changes: 66 additions & 0 deletions
66
main/src/main/java/net/citizensnpcs/trait/PausePathfindingTrait.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,66 @@ | ||
package net.citizensnpcs.trait; | ||
|
||
import org.bukkit.Bukkit; | ||
import org.bukkit.event.EventHandler; | ||
|
||
import net.citizensnpcs.api.CitizensAPI; | ||
import net.citizensnpcs.api.event.NPCRightClickEvent; | ||
import net.citizensnpcs.api.persistence.Persist; | ||
import net.citizensnpcs.api.trait.Trait; | ||
import net.citizensnpcs.api.trait.TraitName; | ||
|
||
@TraitName("pausepathfinding") | ||
public class PausePathfindingTrait extends Trait { | ||
@Persist("pauseticks") | ||
private int pauseTicks; | ||
@Persist("playerrange") | ||
private double playerRange = -1; | ||
@Persist("rightclick") | ||
private boolean rightclick; | ||
private int unpauseTaskId = -1; | ||
|
||
public PausePathfindingTrait() { | ||
super("pausepathfinding"); | ||
} | ||
|
||
@EventHandler(ignoreCancelled = true) | ||
public void onInteract(NPCRightClickEvent event) { | ||
if (!rightclick || event.getNPC() != npc) | ||
return; | ||
pause(); | ||
} | ||
|
||
private void pause() { | ||
if (unpauseTaskId != -1) { | ||
Bukkit.getScheduler().cancelTask(unpauseTaskId); | ||
} | ||
npc.getNavigator().cancelNavigation(); | ||
npc.getNavigator().setPaused(true); | ||
unpauseTaskId = Bukkit.getScheduler().scheduleSyncDelayedTask(CitizensAPI.getPlugin(), | ||
() -> npc.getNavigator().setPaused(false), pauseTicks <= 0 ? 20 : pauseTicks); | ||
} | ||
|
||
@Override | ||
public void run() { | ||
if (playerRange == -1 || !npc.isSpawned()) | ||
return; | ||
if (unpauseTaskId == -1 && !npc.getNavigator().isNavigating()) | ||
return; | ||
if (CitizensAPI.getLocationLookup().getNearbyPlayers(npc.getStoredLocation(), playerRange).iterator() | ||
.hasNext()) { | ||
pause(); | ||
} | ||
} | ||
|
||
public void setPauseTicks(int pauseTicks) { | ||
this.pauseTicks = pauseTicks; | ||
} | ||
|
||
public void setPlayerRangeBlocks(double range) { | ||
this.playerRange = range; | ||
} | ||
|
||
public void setRightClick(boolean rightclick) { | ||
this.rightclick = rightclick; | ||
} | ||
} |
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