Skip to content

Commit

Permalink
Merge pull request #81 from Foxikle/fix-sleeping
Browse files Browse the repository at this point in the history
Fix sleeping
  • Loading branch information
Foxikle authored Jun 2, 2024
2 parents 8805041 + fe22d7b commit 32b6343
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 10 deletions.
16 changes: 16 additions & 0 deletions api/src/main/java/dev/foxikle/customnpcs/api/NPC.java
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,22 @@ public void setEquipment(Equipment equipment) {
npc.setEquipment(equipment);
}

/**
* Gets the spawning location of the NPC. This may be different from {@link NPC#getLocation()} if the NPC has pathfinding (Coming soon).
* @return The spawn location of the NPC
*/
public Location getSpawnLocation() {
return npc.getSpawnLoc();
}

/**
* Gets te CURRENT location of the NPC. This may differ from {@link NPC#getSpawnLocation()} if the NPC has pathfinding
* @return the current location of the NPC
*/
public Location getLocation() {
return npc.getCurrentLocation();
}

/**
* Reloads the NPC's settings within the Settings and Equipment objects
*
Expand Down
11 changes: 8 additions & 3 deletions api/src/main/java/dev/foxikle/customnpcs/api/NPCApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import dev.foxikle.customnpcs.internal.CustomNPCs;
import org.bukkit.plugin.java.JavaPlugin;
import org.jetbrains.annotations.ApiStatus;

import java.util.UUID;

Expand All @@ -11,22 +12,26 @@
public class NPCApi {
/**
* A static instance of the plugin for API use.
* The `NPCApi#initialize()` method must be called before using it.
*/
protected static CustomNPCs plugin = null;
protected static CustomNPCs plugin = JavaPlugin.getPlugin(CustomNPCs.class);

/**
* Initiailizes the API
*
* @deprecated since it's no longer necessary
*/
@Deprecated
@ApiStatus.ScheduledForRemoval(inVersion = "1.8")
public static void initialize() {
plugin = JavaPlugin.getPlugin(CustomNPCs.class);
}

/**
* Gets the NPC object by ID.
*
* @param uuid the id of the NPC
* @return the NPC object associated with the NPC
* @throws NullPointerException if the specified UUID is null
* @throws NullPointerException if the specified UUID is null
* @throws IllegalArgumentException if an NPC doesn't exist by that UUID
*/
public static NPC getNPC(UUID uuid) throws NullPointerException, IllegalArgumentException {
Expand Down
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 32b6343

Please sign in to comment.