Skip to content

Commit

Permalink
Fix npcs taking up sleeping spaces
Browse files Browse the repository at this point in the history
  • Loading branch information
Foxikle committed Jun 2, 2024
1 parent 14e5407 commit fe22d7b
Showing 1 changed file with 18 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,7 @@
import lombok.Setter;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Sound;
import org.bukkit.World;
import org.bukkit.*;
import org.bukkit.command.ConsoleCommandSender;
import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType;
Expand Down Expand Up @@ -372,7 +369,7 @@ public void onChat(AsyncPlayerChatEvent e) {
SCHEDULER.runTask(plugin, () -> core.getSkinMenu().open(player));
});
} catch (Exception ex) {
player.sendMessage(Utils.style("&cAn error occurred whilst parsing NPC skin. Is this URL valid?"));
player.sendMessage(Utils.style("&cAn error occurred whilst parsing NPC skin. Is this URL valid? ERR: " + ex.getMessage()));
}
} else if (plugin.hologramWaiting.contains(player)) {
if (cancel) {
Expand Down Expand Up @@ -419,7 +416,10 @@ public void onPlayerLogin(PlayerJoinEvent e) {
if (plugin.update && plugin.getConfig().getBoolean("AlertOnUpdate") && player.hasPermission("customnpcs.alert")) {
player.sendMessage(SHOULD_UPDATE_MESSAGE);
}
for (InternalNpc npc : plugin.getNPCs()) npc.injectPlayer(player);
for (InternalNpc npc : plugin.getNPCs()) {
npc.injectPlayer(player);
}
recalcSleepingPercentages();
}

/**
Expand Down Expand Up @@ -449,12 +449,13 @@ public void onTeleport(PlayerTeleportEvent e) {
World world = player.getWorld();
for (InternalNpc npc : plugin.npcs.values()) {
Location spawnLocation = npc.getSpawnLoc();
if (world != npc.getWorld()) return;
if (world != npc.getWorld()) continue;

double distanceSquared = location.distanceSquared(spawnLocation);
if (distanceSquared <= FIVE_BLOCKS && !npc.getSettings().isTunnelvision()) {
npc.lookAt(LookAtAnchor.HEAD, player);
}
recalcSleepingPercentages();
}
}

Expand All @@ -471,6 +472,8 @@ public void onDimensionChange(PlayerChangedWorldEvent e) {
World world = player.getWorld();
for (InternalNpc npc : plugin.npcs.values()) {
if (world != npc.getWorld()) continue;
npc.injectPlayer(player);
recalcSleepingPercentages();
}
}

Expand All @@ -494,6 +497,7 @@ public void onLeave(PlayerQuitEvent e) {
plugin.urlWaiting.remove(player);
plugin.playerWaiting.remove(player);
plugin.hologramWaiting.remove(player);
recalcSleepingPercentages();
}

@Getter
Expand All @@ -514,4 +518,11 @@ public MovementData copy() {
return new MovementData(uniqueId, lastLocation, distanceSquared);
}
}

private void recalcSleepingPercentages() {
Bukkit.getWorlds().forEach(world -> {
world.getGameRuleValue(GameRule.PLAYERS_SLEEPING_PERCENTAGE);
world.setGameRule(GameRule.PLAYERS_SLEEPING_PERCENTAGE, (int) (world.getPlayers().size() / (double) plugin.getNPCs().stream().filter(npc -> npc.getWorld() == world).toList().size()));
});
}
}

0 comments on commit fe22d7b

Please sign in to comment.