Preliminary support for ZNPCs, part 2

This commit is contained in:
PikaMug 2023-01-10 20:21:28 -05:00
parent 387913e173
commit aec6219f21
3 changed files with 118 additions and 55 deletions

View File

@ -38,6 +38,8 @@ import org.bukkit.entity.Player;
import org.bukkit.event.HandlerList;
import org.bukkit.plugin.RegisteredListener;
import org.bukkit.plugin.RegisteredServiceProvider;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import ro.nicuch.citizensbooks.CitizensBooksAPI;
import ro.nicuch.citizensbooks.CitizensBooksPlugin;
@ -279,7 +281,7 @@ public class Dependencies implements IDependencies {
return plugin.getDenizenTrigger().runDenizenScript(scriptName, quester, uuid);
}
public Location getNPCLocation(final UUID uuid) {
public @Nullable Location getNPCLocation(final UUID uuid) {
if (citizens != null && citizens.getNPCRegistry().getByUniqueId(uuid) != null) {
return citizens.getNPCRegistry().getByUniqueId(uuid).getStoredLocation();
} else if (getZnpcsUuids().contains(uuid)) {
@ -291,7 +293,19 @@ public class Dependencies implements IDependencies {
return null;
}
public String getNPCName(final UUID uuid) {
public @Nullable Entity getNPCEntity(final UUID uuid) {
if (citizens != null && citizens.getNPCRegistry().getByUniqueId(uuid) != null) {
return citizens.getNPCRegistry().getByUniqueId(uuid).getEntity();
} else if (getZnpcsUuids().contains(uuid)) {
final Optional<NPC> opt = NPC.all().stream().filter(npc1 -> npc1.getUUID().equals(uuid)).findAny();
if (opt.isPresent()) {
return (Entity) opt.get().getBukkitEntity();
}
}
return null;
}
public @NotNull String getNPCName(final UUID uuid) {
Entity npc = null;
if (citizens != null && citizens.getNPCRegistry().getByUniqueId(uuid) != null) {
return citizens.getNPCRegistry().getByUniqueId(uuid).getName();
@ -308,6 +322,19 @@ public class Dependencies implements IDependencies {
}
return "NPC";
}
/**
* Checks whether an Entity is a supported NPC
*
* @param entity the Entity to check
* @return true if a supported NPC
*/
public boolean isSupportedNPC(Entity entity) {
if (citizens != null && citizens.getNPCRegistry().isNPC(entity)) {
return true;
}
return getZnpcsUuids().contains(entity.getUniqueId());
}
public int getMcmmoSkillLevel(final SkillType st, final String player) {
final McMMOPlayer mPlayer = UserManager.getPlayer(player);

View File

@ -4506,69 +4506,109 @@ public class Quests extends JavaPlugin implements QuestsAPI {
}
return null;
}
/**
* Checks whether an NPC has a quest that the player may accept
*
* @param npc the giver NPC UUID to check
* @param quester The player to check
* @return true if at least one available quest has not yet been completed
*/
public boolean hasQuest(final UUID npc, final IQuester quester) {
for (final IQuest q : quests) {
if (q.getNpcStart() != null && !quester.getCompletedQuestsTemp().contains(q)) {
if (q.getNpcStart().equals(npc)) {
final boolean ignoreLockedQuests = settings.canIgnoreLockedQuests();
if (!ignoreLockedQuests || q.testRequirements(quester)) {
return true;
}
}
}
}
return false;
}
// Unused internally, left for external use
/**
* Checks whether an NPC has a quest that the player has already completed
*
* @param npc The giver NPC UUID to check
* @param quester The player to check
* @return true if at least one available quest has been completed
*/
public boolean hasCompletedQuest(final UUID npc, final IQuester quester) {
for (final IQuest q : quests) {
if (q.getNpcStart() != null && quester.getCompletedQuestsTemp().contains(q)) {
if (q.getNpcStart().equals(npc)) {
final boolean ignoreLockedQuests = settings.canIgnoreLockedQuests();
if (!ignoreLockedQuests || q.testRequirements(quester)) {
return true;
}
}
}
}
return false;
}
/**
* Checks whether an NPC has a repeatable quest that the player has already completed
*
* @param npc The giver NPC UUID to check
* @param quester The player to check
* @return true if at least one available, redoable quest has been completed
*/
public boolean hasCompletedRedoableQuest(final UUID npc, final IQuester quester) {
for (final IQuest q : quests) {
if (q.getNpcStart() != null && quester.getCompletedQuestsTemp().contains(q)
&& q.getPlanner().getCooldown() > -1) {
if (q.getNpcStart().equals(npc)) {
final boolean ignoreLockedQuests = settings.canIgnoreLockedQuests();
if (!ignoreLockedQuests || q.testRequirements(quester)) {
return true;
}
}
}
}
return false;
}
/**
* Checks whether a NPC has a quest that the player may accept
* Checks whether an NPC has a quest that the player may accept
*
* @param npc The giver NPC to check
* @param quester The player to check
* @return true if at least one available quest has not yet been completed
* @deprecated Use {@link #hasQuest(UUID, IQuester)}
*/
@Deprecated
public boolean hasQuest(final NPC npc, final IQuester quester) {
for (final IQuest q : quests) {
if (q.getNpcStart() != null && !quester.getCompletedQuestsTemp().contains(q)) {
if (q.getNpcStart().equals(npc.getUniqueId())) {
final boolean ignoreLockedQuests = settings.canIgnoreLockedQuests();
if (!ignoreLockedQuests || q.testRequirements(quester)) {
return true;
}
}
}
}
return false;
return hasQuest(npc.getUniqueId(), quester);
}
// Unused internally, left for external use
/**
* Checks whether a NPC has a quest that the player has already completed
* Checks whether an NPC has a quest that the player has already completed
*
* @param npc The giver NPC to check
* @param quester The player to check
* @return true if at least one available quest has been completed
* @deprecated Use {@link #hasCompletedQuest(UUID, IQuester)}
*/
@Deprecated
public boolean hasCompletedQuest(final NPC npc, final IQuester quester) {
for (final IQuest q : quests) {
if (q.getNpcStart() != null && quester.getCompletedQuestsTemp().contains(q)) {
if (q.getNpcStart().equals(npc.getUniqueId())) {
final boolean ignoreLockedQuests = settings.canIgnoreLockedQuests();
if (!ignoreLockedQuests || q.testRequirements(quester)) {
return true;
}
}
}
}
return false;
return hasCompletedQuest(npc.getUniqueId(), quester);
}
/**
* Checks whether a NPC has a repeatable quest that the player has already completed
* Checks whether an NPC has a repeatable quest that the player has already completed
*
* @param npc The giver NPC to check
* @param quester The player to check
* @return true if at least one available, redoable quest has been completed
* @deprecated Use {@link #hasCompletedRedoableQuest(UUID, IQuester)}
*/
@Deprecated
public boolean hasCompletedRedoableQuest(final NPC npc, final IQuester quester) {
for (final IQuest q : quests) {
if (q.getNpcStart() != null && quester.getCompletedQuestsTemp().contains(q)
&& q.getPlanner().getCooldown() > -1) {
if (q.getNpcStart().equals(npc.getUniqueId())) {
final boolean ignoreLockedQuests = settings.canIgnoreLockedQuests();
if (!ignoreLockedQuests || q.testRequirements(quester)) {
return true;
}
}
}
}
return false;
return hasCompletedRedoableQuest(npc.getUniqueId(), quester);
}
}

View File

@ -38,24 +38,20 @@ public class NpcEffectThread implements Runnable {
if (!nearby.isEmpty()) {
final Quester quester = plugin.getQuester(player.getUniqueId());
for (final Entity entity : nearby) {
if (plugin.getDependencies().getCitizens() != null
&& plugin.getDependencies().getCitizens().getNPCRegistry() != null) {
if (plugin.getDependencies().getCitizens().getNPCRegistry().isNPC(entity)) {
final NPC npc = plugin.getDependencies().getCitizens().getNPCRegistry().getNPC(entity);
final QuesterPostViewEffectEvent event;
if (plugin.hasQuest(npc, quester)) {
showEffect(player, npc.getEntity(), plugin.getSettings().getEffect());
if (plugin.getDependencies().isSupportedNPC(entity)) {
final QuesterPostViewEffectEvent event;
if (plugin.hasQuest(entity.getUniqueId(), quester)) {
showEffect(player, entity, plugin.getSettings().getEffect());
event = new QuesterPostViewEffectEvent(quester, entity,
plugin.getSettings().getEffect(), false);
plugin.getServer().getPluginManager().callEvent(event);
} else if (plugin.hasCompletedRedoableQuest(npc, quester)) {
showEffect(player, npc.getEntity(), plugin.getSettings().getRedoEffect());
event = new QuesterPostViewEffectEvent(quester, entity,
plugin.getSettings().getEffect(), false);
plugin.getServer().getPluginManager().callEvent(event);
} else if (plugin.hasCompletedRedoableQuest(entity.getUniqueId(), quester)) {
showEffect(player, entity, plugin.getSettings().getRedoEffect());
event = new QuesterPostViewEffectEvent(quester, entity,
plugin.getSettings().getEffect(), true);
plugin.getServer().getPluginManager().callEvent(event);
}
event = new QuesterPostViewEffectEvent(quester, entity,
plugin.getSettings().getEffect(), true);
plugin.getServer().getPluginManager().callEvent(event);
}
}
}