Skip to content

Commit

Permalink
Update to BetterQuesting UUID API
Browse files Browse the repository at this point in the history
  • Loading branch information
D-Cysteine committed Apr 22, 2023
1 parent 204d4b4 commit fde800f
Show file tree
Hide file tree
Showing 11 changed files with 66 additions and 54 deletions.
4 changes: 2 additions & 2 deletions gradle.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
nesqlExporterVersion=0.4.6
nesqlExporterVersion=0.4.7

minecraftVersion=1.7.10
forgeVersion=10.13.4.1614
Expand Down Expand Up @@ -27,4 +27,4 @@ projectRedVersion=4.7.9-GTNH
thaumcraftVersion=4.2.3.5
thaumcraftNeiVersion=2241913

betterQuestingVersion=3.1.7-GTNH
betterQuestingVersion=3.3.16-GTNH
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
package com.github.dcysteine.nesql.exporter.plugin.quest;

import betterquesting.api.questing.IQuestLine;
import betterquesting.api2.storage.DBEntry;
import betterquesting.questing.QuestLineDatabase;
import com.github.dcysteine.nesql.exporter.main.Logger;
import com.github.dcysteine.nesql.exporter.plugin.PluginExporter;
import com.github.dcysteine.nesql.exporter.plugin.PluginHelper;
import com.github.dcysteine.nesql.exporter.plugin.quest.factory.QuestLineFactory;
import com.github.dcysteine.nesql.sql.quest.QuestLine;

import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;

/** Must run after {@link QuestProcessor}. */
public class QuestLineProcessor extends PluginHelper {
Expand All @@ -18,15 +19,15 @@ public QuestLineProcessor(PluginExporter exporter) {
}

public void process() {
List<DBEntry<IQuestLine>> questLineEntries = QuestLineDatabase.INSTANCE.getEntries();
Set<Map.Entry<UUID, IQuestLine>> questLineEntries = QuestLineDatabase.INSTANCE.entrySet();
int total = questLineEntries.size();
logger.info("Processing {} quest lines...", total);

QuestLineFactory questLineFactory = new QuestLineFactory(exporter);
int count = 0;
for (DBEntry<IQuestLine> entry : questLineEntries) {
for (Map.Entry<UUID, IQuestLine> entry : questLineEntries) {
count++;
QuestLine questLine = questLineFactory.get(entry.getID(), entry.getValue());
QuestLine questLine = questLineFactory.get(entry.getKey(), entry.getValue());

if (Logger.intermittentLog(count)) {
logger.info("Processed quest line {} of {}", count, total);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,32 +1,33 @@
package com.github.dcysteine.nesql.exporter.plugin.quest;

import betterquesting.api.questing.IQuest;
import betterquesting.api2.storage.DBEntry;
import betterquesting.questing.QuestDatabase;
import com.github.dcysteine.nesql.exporter.main.Logger;
import com.github.dcysteine.nesql.exporter.plugin.PluginExporter;
import com.github.dcysteine.nesql.exporter.plugin.PluginHelper;
import com.github.dcysteine.nesql.exporter.plugin.quest.factory.QuestFactory;
import com.github.dcysteine.nesql.sql.quest.Quest;

import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;

public class QuestPostProcessor extends PluginHelper {
public QuestPostProcessor(PluginExporter exporter) {
super(exporter);
}

public void postProcess() {
List<DBEntry<IQuest>> questEntries = QuestDatabase.INSTANCE.getEntries();
Set<Map.Entry<UUID, IQuest>> questEntries = QuestDatabase.INSTANCE.entrySet();
int total = questEntries.size();
logger.info("Post-processing {} quests...", total);

QuestFactory questFactory = new QuestFactory(exporter);
int count = 0;
for (DBEntry<IQuest> entry : questEntries) {
for (Map.Entry<UUID, IQuest> entry : questEntries) {
count++;

Quest quest = questFactory.findQuest(entry.getID());
Quest quest = questFactory.findQuest(entry.getKey());
questFactory.setRequiredQuests(quest, entry.getValue().getRequirements());

if (Logger.intermittentLog(count)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,31 +1,32 @@
package com.github.dcysteine.nesql.exporter.plugin.quest;

import betterquesting.api.questing.IQuest;
import betterquesting.api2.storage.DBEntry;
import betterquesting.questing.QuestDatabase;
import com.github.dcysteine.nesql.exporter.main.Logger;
import com.github.dcysteine.nesql.exporter.plugin.PluginExporter;
import com.github.dcysteine.nesql.exporter.plugin.PluginHelper;
import com.github.dcysteine.nesql.exporter.plugin.quest.factory.QuestFactory;
import com.github.dcysteine.nesql.sql.quest.Quest;

import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;

public class QuestProcessor extends PluginHelper {
public QuestProcessor(PluginExporter exporter) {
super(exporter);
}

public void process() {
List<DBEntry<IQuest>> questEntries = QuestDatabase.INSTANCE.getEntries();
Set<Map.Entry<UUID, IQuest>> questEntries = QuestDatabase.INSTANCE.entrySet();
int total = questEntries.size();
logger.info("Processing {} quests...", total);

QuestFactory questFactory = new QuestFactory(exporter);
int count = 0;
for (DBEntry<IQuest> entry : questEntries) {
for (Map.Entry<UUID, IQuest> entry : questEntries) {
count++;
Quest quest = questFactory.get(entry.getID(), entry.getValue());
Quest quest = questFactory.get(entry.getKey(), entry.getValue());

if (Logger.intermittentLog(count)) {
logger.info("Processed quest {} of {}", count, total);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import betterquesting.api.questing.IQuest;
import betterquesting.api.questing.rewards.IReward;
import betterquesting.api.questing.tasks.ITask;
import betterquesting.api.utils.UuidConverter;
import betterquesting.api2.storage.DBEntry;
import betterquesting.api2.utils.QuestTranslation;
import com.github.dcysteine.nesql.exporter.plugin.EntityFactory;
Expand All @@ -17,10 +18,11 @@
import com.github.dcysteine.nesql.sql.quest.Task;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.UUID;
import java.util.stream.Collectors;

public class QuestFactory extends EntityFactory<Quest, String> {
Expand All @@ -35,8 +37,9 @@ public QuestFactory(PluginExporter exporter) {
rewardFactory = new RewardFactory(exporter);
}

public Quest get(int questId, IQuest quest) {
String id = IdPrefixUtil.QUEST.applyPrefix(Integer.toString(questId));
public Quest get(UUID questId, IQuest quest) {
String encodedQuestId = UuidConverter.encodeUuid(questId);
String id = IdPrefixUtil.QUEST.applyPrefix(encodedQuestId);
Item icon = itemFactory.get(quest.getProperty(NativeProps.ICON).getBaseStack());

String name =
Expand All @@ -55,35 +58,36 @@ public Quest get(int questId, IQuest quest) {
int taskIndex = 0;
List<Task> tasks = new ArrayList<>();
for (DBEntry<ITask> entry : quest.getTasks().getEntries()) {
tasks.add(taskFactory.get(questId, taskIndex++, entry.getValue()));
tasks.add(taskFactory.get(encodedQuestId, taskIndex++, entry.getValue()));
}

int rewardIndex = 0;
List<Reward> rewards = new ArrayList<>();
for (DBEntry<IReward> entry : quest.getRewards().getEntries()) {
rewards.add(rewardFactory.get(questId, rewardIndex++, entry.getValue()));
rewards.add(rewardFactory.get(encodedQuestId, rewardIndex++, entry.getValue()));
}

Quest questEntity =
new Quest(
id, questId, icon, name, description, visibility, repeatTime,
id, encodedQuestId, icon, name, description, visibility, repeatTime,
questLogic, taskLogic, tasks, rewards);
return findOrPersist(Quest.class, questEntity);
}

public Quest findQuest(int questId) {
String id = IdPrefixUtil.QUEST.applyPrefix(Integer.toString(questId));
public Quest findQuest(UUID questId) {
String encodedQuestId = UuidConverter.encodeUuid(questId);
String id = IdPrefixUtil.QUEST.applyPrefix(encodedQuestId);
Quest quest = entityManager.find(Quest.class, id);
if (quest == null) {
throw new IllegalStateException("Could not find quest: " + questId);
}
return quest;
}

public void setRequiredQuests(Quest quest, int[] requiredQuestIds) {
public void setRequiredQuests(Quest quest, Collection<UUID> requiredQuestIds) {
Set<Quest> requiredQuests =
Arrays.stream(requiredQuestIds)
.mapToObj(this::findQuest)
requiredQuestIds.stream()
.map(this::findQuest)
.collect(Collectors.toCollection(HashSet::new));

quest.setRequiredQuests(requiredQuests);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import betterquesting.api.properties.NativeProps;
import betterquesting.api.questing.IQuestLine;
import betterquesting.api.utils.UuidConverter;
import betterquesting.api2.utils.QuestTranslation;
import com.github.dcysteine.nesql.exporter.plugin.EntityFactory;
import com.github.dcysteine.nesql.exporter.plugin.PluginExporter;
Expand All @@ -14,6 +15,7 @@

import java.util.HashSet;
import java.util.Set;
import java.util.UUID;
import java.util.stream.Collectors;

public class QuestLineFactory extends EntityFactory<QuestLine, String> {
Expand All @@ -26,8 +28,9 @@ public QuestLineFactory(PluginExporter exporter) {
questFactory = new QuestFactory(exporter);
}

public QuestLine get(int questLineId, IQuestLine questLine) {
String id = IdPrefixUtil.QUEST_LINE.applyPrefix(Integer.toString(questLineId));
public QuestLine get(UUID questLineId, IQuestLine questLine) {
String encodedQuestLineId = UuidConverter.encodeUuid(questLineId);
String id = IdPrefixUtil.QUEST_LINE.applyPrefix(encodedQuestLineId);
Item icon = itemFactory.get(questLine.getProperty(NativeProps.ICON).getBaseStack());

String name =
Expand All @@ -39,12 +42,12 @@ public QuestLine get(int questLineId, IQuestLine questLine) {
String visibility = questLine.getProperty(NativeProps.VISIBILITY).name();

Set<Quest> quests =
questLine.getEntries().stream()
.map(entry -> questFactory.findQuest(entry.getID()))
questLine.keySet().stream()
.map(questFactory::findQuest)
.collect(Collectors.toCollection(HashSet::new));

QuestLine questLineEntity =
new QuestLine(id, questLineId, icon, name, description, visibility, quests);
new QuestLine(id, encodedQuestLineId, icon, name, description, visibility, quests);
return findOrPersist(QuestLine.class, questLineEntity);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.github.dcysteine.nesql.exporter.plugin.quest.factory;

import betterquesting.api.questing.rewards.IReward;
import betterquesting.api.utils.UuidConverter;
import betterquesting.api2.utils.QuestTranslation;
import bq_standard.rewards.RewardChoice;
import bq_standard.rewards.RewardCommand;
Expand All @@ -27,49 +28,48 @@ public RewardFactory(PluginExporter exporter) {
itemGroupFactory = new ItemGroupFactory(exporter);
}

public Reward get(int questId, int index, IReward reward) {
public Reward get(String encodedQuestId, int index, IReward reward) {
String id =
IdPrefixUtil.QUEST_REWARD.applyPrefix(
Integer.toString(questId), Integer.toString(index));
IdPrefixUtil.QUEST_REWARD.applyPrefix(encodedQuestId, Integer.toString(index));
String name = QuestTranslation.translate(reward.getUnlocalisedName());

Reward rewardEntity;
if (reward instanceof RewardItem) {
RewardItem typedReward = (RewardItem) reward;
List<ItemGroup> items = QuestUtil.buildItems(itemGroupFactory, typedReward.items);
rewardEntity = new Reward(id, name, RewardType.ITEM, items, "", 0, false, 0);
rewardEntity = new Reward(id, name, RewardType.ITEM, items, "", 0, false, "");

} else if (reward instanceof RewardChoice) {
RewardChoice typedReward = (RewardChoice) reward;
List<ItemGroup> items = QuestUtil.buildItems(itemGroupFactory, typedReward.choices);
rewardEntity = new Reward(id, name, RewardType.CHOICE, items, "", 0, false, 0);
rewardEntity = new Reward(id, name, RewardType.CHOICE, items, "", 0, false, "");

} else if (reward instanceof RewardCommand) {
RewardCommand typedReward = (RewardCommand) reward;
rewardEntity =
new Reward(
id, name, RewardType.COMMAND, new ArrayList<>(),
typedReward.command, 0, false, 0);
typedReward.command, 0, false, "");

} else if (reward instanceof RewardXP) {
RewardXP typedReward = (RewardXP) reward;
rewardEntity =
new Reward(
id, name, RewardType.XP, new ArrayList<>(),
"", typedReward.amount, typedReward.levels, 0);
"", typedReward.amount, typedReward.levels, "");

} else if (reward instanceof RewardQuestCompletion) {
RewardQuestCompletion typedReward = (RewardQuestCompletion) reward;
rewardEntity =
new Reward(
id, name, RewardType.COMPLETE_QUEST, new ArrayList<>(),
"", 0, false, typedReward.questNum);
"", 0, false, UuidConverter.encodeUuid(typedReward.questNum));

} else {
// TODO add any additional reward types that we need to handle here.
logger.warn("Unhandled reward type: " + reward);
rewardEntity =
new Reward(id, name, RewardType.UNHANDLED, new ArrayList<>(), "", 0, false, 0);
new Reward(id, name, RewardType.UNHANDLED, new ArrayList<>(), "", 0, false, "");
}

return findOrPersist(Reward.class, rewardEntity);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,9 @@ public TaskFactory(PluginExporter exporter) {
fluidFactory = new FluidFactory(exporter);
}

public Task get(int questId, int index, ITask task) {
public Task get(String encodedQuestId, int index, ITask task) {
String id =
IdPrefixUtil.QUEST_TASK.applyPrefix(
Integer.toString(questId), Integer.toString(index));
IdPrefixUtil.QUEST_TASK.applyPrefix(encodedQuestId, Integer.toString(index));
String name = QuestTranslation.translate(task.getUnlocalisedName());

Task taskEntity;
Expand Down
7 changes: 4 additions & 3 deletions src/main/java/com/github/dcysteine/nesql/sql/quest/Quest.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ public class Quest implements Identifiable<String> {
private String id;

/** The quest ID in the BetterQuesting database. */
private int questId;
@Column(nullable = false)
private String questId;

@ManyToOne
private Item icon;
Expand Down Expand Up @@ -80,7 +81,7 @@ protected Quest() {}
* after all quests have been committed to the DB.
*/
public Quest(
String id, int questId, Item icon, String name, String description,
String id, String questId, Item icon, String name, String description,
String visibility, int repeatTime,
String questLogic, String taskLogic, List<Task> tasks, List<Reward> rewards) {
this.id = id;
Expand All @@ -102,7 +103,7 @@ public String getId() {
return id;
}

public int getQuestId() {
public String getQuestId() {
return questId;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ public class QuestLine implements Identifiable<String> {
private String id;

/** The quest line ID in the BetterQuesting database. */
private int questLineId;
@Column(nullable = false)
private String questLineId;

@ManyToOne
private Item icon;
Expand All @@ -46,7 +47,7 @@ public class QuestLine implements Identifiable<String> {
protected QuestLine() {}

public QuestLine(
String id, int questLineId, Item icon, String name, String description,
String id, String questLineId, Item icon, String name, String description,
String visibility, Set<Quest> quests) {
this.id = id;
this.questLineId = questLineId;
Expand All @@ -62,7 +63,7 @@ public String getId() {
return id;
}

public int getQuestLineId() {
public String getQuestLineId() {
return questLineId;
}

Expand Down
Loading

0 comments on commit fde800f

Please sign in to comment.