Skip to content

Commit

Permalink
Check visibility every second
Browse files Browse the repository at this point in the history
  • Loading branch information
OliverSchlueter committed Feb 17, 2024
1 parent 1c06258 commit f583b77
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 9 deletions.
8 changes: 5 additions & 3 deletions api/src/main/java/de/oliver/fancynpcs/api/Npc.java
Original file line number Diff line number Diff line change
Expand Up @@ -106,12 +106,14 @@ protected boolean shouldBeVisible(Player player) {

public void checkAndUpdateVisibility(Player player) {
FancyNpcsPlugin.get().getScheduler().runTaskAsynchronously(() -> {
boolean isVisible = shouldBeVisible(player);
boolean shouldBeVisible = shouldBeVisible(player);
boolean wasVisible = isVisibleForPlayer.getOrDefault(player.getUniqueId(), false);

if (isVisible && !wasVisible) {
if (shouldBeVisible && !wasVisible) {
spawn(player);
} else if (!isVisible && wasVisible) {
System.out.println("Spawned");
} else if (!shouldBeVisible && wasVisible) {
System.out.println("Removed");
remove(player);
}
});
Expand Down
6 changes: 4 additions & 2 deletions src/main/java/de/oliver/fancynpcs/FancyNpcs.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
import de.oliver.fancynpcs.commands.FancyNpcsCMD;
import de.oliver.fancynpcs.commands.npc.NpcCMD;
import de.oliver.fancynpcs.listeners.*;
import de.oliver.fancynpcs.tracker.NpcTracker;
import de.oliver.fancynpcs.tracker.TurnToPlayerTracker;
import de.oliver.fancynpcs.tracker.VisibilityTracker;
import de.oliver.fancynpcs.v1_19_4.Npc_1_19_4;
import de.oliver.fancynpcs.v1_19_4.PacketReader_1_19_4;
import de.oliver.fancynpcs.v1_20.PacketReader_1_20;
Expand Down Expand Up @@ -205,7 +206,8 @@ else if (mcVersion.equals("1.20"))
// load config
scheduler.runTaskLater(null, 20L * 5, () -> npcManager.loadNpcs());

scheduler.runTaskTimerAsynchronously(0, 1, new NpcTracker());
scheduler.runTaskTimerAsynchronously(0, 1, new TurnToPlayerTracker());
scheduler.runTaskTimerAsynchronously(0, 20, new VisibilityTracker());

int autosaveInterval = config.getAutoSaveInterval();
if (config.isEnableAutoSave() && config.getAutoSaveInterval() > 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,10 @@
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.entity.Player;
import org.bukkit.scheduler.BukkitRunnable;

import java.util.Collection;

public class NpcTracker extends BukkitRunnable {
public class TurnToPlayerTracker implements Runnable {

@Override
public void run() {
Expand All @@ -23,8 +22,6 @@ public void run() {
Location playerLocation = player.getLocation();

for (Npc npc : npcs) {
npc.checkAndUpdateVisibility(player);

NpcData npcData = npc.getData();
Location npcLocation = npcData.getLocation();

Expand Down
18 changes: 18 additions & 0 deletions src/main/java/de/oliver/fancynpcs/tracker/VisibilityTracker.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package de.oliver.fancynpcs.tracker;

import de.oliver.fancynpcs.FancyNpcs;
import de.oliver.fancynpcs.api.Npc;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;

public class VisibilityTracker implements Runnable {

@Override
public void run() {
for (Player player : Bukkit.getOnlinePlayers()) {
for (Npc npc : FancyNpcs.getInstance().getNpcManagerImpl().getAllNpcs()) {
npc.checkAndUpdateVisibility(player);
}
}
}
}

0 comments on commit f583b77

Please sign in to comment.