Files
CustomNPC-Plus/src/main/java/noppes/npcs/controllers/data/PlayerQuestData.java
T

297 lines
11 KiB
Java
Raw Normal View History

package noppes.npcs.controllers.data;
2018-09-13 12:03:28 -04:00
2025-01-30 12:44:39 -05:00
import kamkeel.npcs.network.PacketHandler;
import kamkeel.npcs.network.packets.data.AchievementPacket;
import kamkeel.npcs.network.packets.data.ChatAlertPacket;
import kamkeel.npcs.network.packets.data.OverlayQuestTrackingPacket;
2018-09-13 12:03:28 -04:00
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import noppes.npcs.EventHooks;
import noppes.npcs.NoppesUtilPlayer;
2023-01-12 21:33:06 -05:00
import noppes.npcs.api.handler.IPlayerQuestData;
2024-03-15 15:43:29 -04:00
import noppes.npcs.api.handler.data.IParty;
2023-01-12 21:33:06 -05:00
import noppes.npcs.api.handler.data.IQuest;
2024-03-17 23:15:11 -04:00
import noppes.npcs.constants.EnumQuestCompletion;
import noppes.npcs.constants.EnumQuestType;
import noppes.npcs.controllers.PartyController;
2025-02-21 07:05:56 -05:00
import noppes.npcs.controllers.PlayerQuestController;
import noppes.npcs.controllers.QuestController;
2018-09-13 12:03:28 -04:00
import noppes.npcs.entity.EntityNPCInterface;
import noppes.npcs.quests.QuestInterface;
2022-06-13 04:08:36 -05:00
import noppes.npcs.quests.QuestItem;
2023-01-12 21:33:06 -05:00
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
2018-09-13 12:03:28 -04:00
2022-06-12 01:26:23 -05:00
public class PlayerQuestData implements IPlayerQuestData {
2025-03-13 17:14:45 -04:00
private final PlayerData parent;
private IQuest trackedQuest = null;
2025-03-13 17:14:45 -04:00
public HashMap<Integer, QuestData> activeQuests = new HashMap<Integer, QuestData>();
public HashMap<Integer, Long> finishedQuests = new HashMap<Integer, Long>();
2022-06-12 01:26:23 -05:00
2025-03-13 17:14:45 -04:00
public PlayerQuestData(PlayerData parent) {
this.parent = parent;
}
2022-06-12 01:26:23 -05:00
2025-03-13 17:14:45 -04:00
public void loadNBTData(NBTTagCompound mainCompound) {
if (mainCompound == null)
return;
NBTTagCompound compound = mainCompound.getCompoundTag("QuestData");
2018-09-13 12:03:28 -04:00
NBTTagList list = compound.getTagList("CompletedQuests", 10);
2025-03-13 17:14:45 -04:00
if (list != null) {
HashMap<Integer, Long> finishedQuests = new HashMap<Integer, Long>();
for (int i = 0; i < list.tagCount(); i++) {
2018-09-13 12:03:28 -04:00
NBTTagCompound nbttagcompound = list.getCompoundTagAt(i);
2025-03-13 17:14:45 -04:00
finishedQuests.put(nbttagcompound.getInteger("Quest"), nbttagcompound.getLong("Date"));
2018-09-13 12:03:28 -04:00
}
this.finishedQuests = finishedQuests;
}
2018-09-13 12:03:28 -04:00
NBTTagList list2 = compound.getTagList("ActiveQuests", 10);
2025-03-13 17:14:45 -04:00
if (list2 != null) {
HashMap<Integer, QuestData> activeQuests = new HashMap<Integer, QuestData>();
for (int i = 0; i < list2.tagCount(); i++) {
2018-09-13 12:03:28 -04:00
NBTTagCompound nbttagcompound = list2.getCompoundTagAt(i);
int id = nbttagcompound.getInteger("Quest");
2023-06-05 22:12:48 -04:00
Quest quest = QuestController.Instance.quests.get(id);
2025-03-13 17:14:45 -04:00
if (quest == null)
continue;
2018-09-13 12:03:28 -04:00
QuestData data = new QuestData(quest);
data.readEntityFromNBT(nbttagcompound);
2025-03-13 17:14:45 -04:00
activeQuests.put(id, data);
2018-09-13 12:03:28 -04:00
}
this.activeQuests = activeQuests;
}
2025-03-13 17:14:45 -04:00
this.trackedQuest = QuestController.Instance.get(mainCompound.getInteger("TrackedQuestID"));
}
public void saveNBTData(NBTTagCompound maincompound) {
NBTTagCompound compound = new NBTTagCompound();
NBTTagList list = new NBTTagList();
for (int quest : finishedQuests.keySet()) {
NBTTagCompound nbttagcompound = new NBTTagCompound();
nbttagcompound.setInteger("Quest", quest);
nbttagcompound.setLong("Date", finishedQuests.get(quest));
list.appendTag(nbttagcompound);
}
compound.setTag("CompletedQuests", list);
NBTTagList list2 = new NBTTagList();
for (int quest : activeQuests.keySet()) {
NBTTagCompound nbttagcompound = new NBTTagCompound();
nbttagcompound.setInteger("Quest", quest);
activeQuests.get(quest).writeEntityToNBT(nbttagcompound);
list2.appendTag(nbttagcompound);
}
compound.setTag("ActiveQuests", list2);
maincompound.setTag("QuestData", compound);
if (this.trackedQuest != null) {
maincompound.setInteger("TrackedQuestID", this.trackedQuest.getId());
} else {
maincompound.setInteger("TrackedQuestID", -1);
}
}
public QuestData getQuestCompletion(EntityPlayer player, EntityNPCInterface npc) {
PlayerData playerData = PlayerData.get(player);
2025-03-13 17:14:45 -04:00
for (QuestData data : activeQuests.values()) {
Quest quest = data.quest;
if (quest != null && quest.completion == EnumQuestCompletion.Npc && quest.completerNpc.equals(npc.getCommandSenderName()) && quest.questInterface.isCompleted(playerData)) {
return data;
}
}
return null;
}
public Party getPartyQuestCompletion(EntityPlayer player, EntityNPCInterface npc) {
PlayerData playerData = PlayerData.get(player);
2025-03-13 17:14:45 -04:00
if (playerData != null) {
2024-03-05 01:04:15 -05:00
Party party = playerData.getPlayerParty();
2025-03-13 17:14:45 -04:00
if (party != null) {
2024-03-05 01:04:15 -05:00
QuestData data = party.getQuestData();
2025-03-13 17:14:45 -04:00
if (data != null) {
2024-03-05 01:04:15 -05:00
Quest quest = data.quest;
2025-03-13 17:14:45 -04:00
if (quest != null && quest.completion == EnumQuestCompletion.Npc && quest.completerNpc.equals(npc.getCommandSenderName()) && quest.questInterface.isPartyCompleted(party)) {
2024-03-05 01:04:15 -05:00
return party;
}
}
}
}
return null;
}
2025-03-13 17:14:45 -04:00
public boolean checkQuestCompletion(PlayerData playerData, EnumQuestType type) {
boolean bo = false;
EntityPlayer player = playerData.player;
2022-07-20 16:26:09 -05:00
int partyQuestID = -1;
2025-03-13 17:14:45 -04:00
if (playerData.partyUUID != null) {
Party party = PartyController.Instance().getParty(playerData.partyUUID);
2025-03-13 17:14:45 -04:00
if (party != null) {
IQuest quest = party.getQuest();
2025-03-13 17:14:45 -04:00
if (quest != null) {
partyQuestID = quest.getId();
}
}
}
2025-03-13 17:14:45 -04:00
ArrayList<QuestData> activeQuestValues = new ArrayList<>(this.activeQuests.values());
for (QuestData data : activeQuestValues) {
if (data.quest.type != type && type != null)
continue;
// Check if Active Quest = Party Quest
2025-03-13 17:14:45 -04:00
if (partyQuestID != -1 && data.quest.id == partyQuestID && data.quest.partyOptions.isAllowParty()) {
continue;
}
2025-03-13 17:14:45 -04:00
if (data.quest.partyOptions.allowParty && data.quest.partyOptions.onlyParty)
continue;
2025-03-13 17:14:45 -04:00
QuestInterface inter = data.quest.questInterface;
if (inter.isCompleted(playerData)) {
if ((!data.isCompleted && data.quest.completion == EnumQuestCompletion.Npc) || data.quest.instantComplete(player, data)) {
data.isCompleted = true;
if (data.quest.completion == EnumQuestCompletion.Npc) {
EventHooks.onQuestFinished(player, data.quest);
}
bo = true;
}
} else {
data.isCompleted = false;
}
if (this.trackedQuest != null && data.quest.getId() == this.trackedQuest.getId()) {
NoppesUtilPlayer.sendTrackedQuestData((EntityPlayerMP) player);
}
}
QuestItem.pickedUp = null;
return bo;
}
public void trackQuest(IQuest quest) {
if (this.trackedQuest == null || quest.getId() != this.trackedQuest.getId()) {
this.trackedQuest = quest;
NoppesUtilPlayer.sendTrackedQuestData((EntityPlayerMP) this.parent.player);
}
}
2024-03-15 15:43:29 -04:00
public void trackParty(IParty party) {
2025-03-13 17:14:45 -04:00
if (party == null)
2024-03-15 15:43:29 -04:00
return;
IQuest quest = party.getQuest();
if (this.trackedQuest == null || quest.getId() != this.trackedQuest.getId()) {
this.trackedQuest = quest;
NoppesUtilPlayer.sendPartyTrackedQuestData((EntityPlayerMP) this.parent.player, (Party) party);
}
}
2025-03-13 17:14:45 -04:00
public void untrackQuest() {
if (this.trackedQuest != null) {
this.trackedQuest = null;
2025-01-30 12:44:39 -05:00
PacketHandler.Instance.sendToPlayer(new OverlayQuestTrackingPacket(new NBTTagCompound()), (EntityPlayerMP) this.parent.player);
}
2025-03-13 17:14:45 -04:00
}
2025-03-13 17:14:45 -04:00
public IQuest getTrackedQuest() {
return this.trackedQuest;
}
2022-06-12 01:26:23 -05:00
2025-03-13 17:14:45 -04:00
public void startQuest(int id) {
Quest quest = QuestController.Instance.quests.get(id);
if (quest == null)
return;
if (activeQuests.containsKey(id))
return;
QuestData questdata = new QuestData(quest);
activeQuests.put(id, questdata);
AchievementPacket.sendAchievement((EntityPlayerMP) parent.player, false, "quest.newquest", quest.title);
ChatAlertPacket.sendChatAlert((EntityPlayerMP) parent.player, "quest.newquest", ": ", quest.title);
parent.updateClient = true;
2025-03-13 17:14:45 -04:00
}
2022-06-12 01:26:23 -05:00
2025-03-13 17:14:45 -04:00
public void finishQuest(int id) {
Quest quest = QuestController.Instance.quests.get(id);
if (quest == null)
return;
2025-02-21 07:05:56 -05:00
PlayerQuestController.setQuestFinished(quest, parent.player);
parent.updateClient = true;
2025-03-13 17:14:45 -04:00
}
2022-06-12 01:26:23 -05:00
2025-03-13 17:14:45 -04:00
public void stopQuest(int id) {
Quest quest = QuestController.Instance.quests.get(id);
if (quest == null)
return;
activeQuests.remove(id);
parent.updateClient = true;
2025-03-13 17:14:45 -04:00
}
public void removeQuest(int id) {
Quest quest = QuestController.Instance.quests.get(id);
if (quest == null)
return;
activeQuests.remove(id);
finishedQuests.remove(id);
parent.updateClient = true;
2025-03-13 17:14:45 -04:00
}
public boolean hasFinishedQuest(int id) {
return finishedQuests.containsKey(id);
}
public boolean hasActiveQuest(int id) {
return activeQuests.containsKey(id);
}
public IQuest[] getActiveQuests() {
List<IQuest> quests = new ArrayList<>();
for (int id : activeQuests.keySet()) {
IQuest quest = (IQuest) QuestController.Instance.quests.get(id);
if (quest != null) {
quests.add(quest);
}
}
return (IQuest[]) quests.toArray(new IQuest[0]);
}
public IQuest[] getFinishedQuests() {
List<IQuest> quests = new ArrayList<>();
for (int id : finishedQuests.keySet()) {
IQuest quest = (IQuest) QuestController.Instance.quests.get(id);
if (quest != null) {
quests.add(quest);
}
}
return (IQuest[]) quests.toArray(new IQuest[0]);
}
public long getLastCompletedTime(int id) {
if (this.hasFinishedQuest(id)) {
return this.finishedQuests.get(id);
}
return 0;
}
public void setLastCompletedTime(int id, long time) {
Quest quest = QuestController.Instance.quests.get(id);
if (quest == null)
return;
this.finishedQuests.put(id, time);
}
2018-09-13 12:03:28 -04:00
}