-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
311 additions
and
142 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
30 changes: 30 additions & 0 deletions
30
src/main/kotlin/com/nanabell/sponge/nico/module/quest/data/quest/Quest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package com.nanabell.sponge.nico.module.quest.data.quest | ||
|
||
import com.nanabell.sponge.nico.module.quest.interfaces.IQuest | ||
import ninja.leaping.configurate.ConfigurationNode | ||
import ninja.leaping.configurate.objectmapping.Setting | ||
import ninja.leaping.configurate.objectmapping.serialize.ConfigSerializable | ||
import java.util.* | ||
|
||
@ConfigSerializable | ||
abstract class Quest( | ||
|
||
@Setting("quest-id") | ||
override val id: UUID, | ||
|
||
@Setting("name") | ||
override val name: String, | ||
|
||
@Setting("description") | ||
override val description: String?, | ||
|
||
@Setting("tasks") | ||
override val tasks: List<UUID>, | ||
|
||
@Setting("rewards") | ||
override val rewards: List<UUID>, | ||
|
||
@Setting("dependencies") | ||
override val dependencies: List<UUID> | ||
|
||
) : IQuest |
14 changes: 14 additions & 0 deletions
14
src/main/kotlin/com/nanabell/sponge/nico/module/quest/data/quest/QuestProgress.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package com.nanabell.sponge.nico.module.quest.data.quest | ||
|
||
import com.nanabell.sponge.nico.module.quest.quest.QuestStatus | ||
import java.util.* | ||
|
||
class QuestProgress( | ||
val userId: UUID, | ||
val questId: UUID, | ||
val status: QuestStatus, | ||
val tasks: List<UUID>, | ||
val dependencies: List<UUID> | ||
) { | ||
|
||
} |
16 changes: 16 additions & 0 deletions
16
src/main/kotlin/com/nanabell/sponge/nico/module/quest/data/quest/SimpleQuest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package com.nanabell.sponge.nico.module.quest.data.quest | ||
|
||
import ninja.leaping.configurate.objectmapping.serialize.ConfigSerializable | ||
import java.util.* | ||
|
||
@ConfigSerializable | ||
class SimpleQuest( | ||
id: UUID, | ||
name: String, | ||
description: String, | ||
tasks: List<UUID>, | ||
rewards: List<UUID>, | ||
dependencies: List<UUID> | ||
) : Quest(id, name, description, tasks, rewards, dependencies) { | ||
|
||
} |
9 changes: 9 additions & 0 deletions
9
src/main/kotlin/com/nanabell/sponge/nico/module/quest/data/task/TaskProgress.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package com.nanabell.sponge.nico.module.quest.data.task | ||
|
||
import java.util.* | ||
|
||
class TaskProgress( | ||
val taskId: UUID, | ||
val userId: UUID | ||
) { | ||
} |
14 changes: 14 additions & 0 deletions
14
src/main/kotlin/com/nanabell/sponge/nico/module/quest/interfaces/IQuest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package com.nanabell.sponge.nico.module.quest.interfaces | ||
|
||
import java.util.* | ||
|
||
interface IQuest { | ||
|
||
val id: UUID | ||
val name: String | ||
val description: String? | ||
val tasks: List<UUID> | ||
val rewards: List<UUID> | ||
val dependencies: List<UUID> | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
184 changes: 50 additions & 134 deletions
184
src/main/kotlin/com/nanabell/sponge/nico/module/quest/service/QuestRegistry.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,171 +1,87 @@ | ||
package com.nanabell.sponge.nico.module.quest.service | ||
|
||
import com.google.common.reflect.TypeToken | ||
import com.nanabell.sponge.nico.NicoYazawa | ||
import com.nanabell.sponge.nico.internal.annotation.service.RegisterService | ||
import com.nanabell.sponge.nico.internal.serializer.CurrencySerializer | ||
import com.nanabell.sponge.nico.internal.serializer.DurationSerializer | ||
import com.nanabell.sponge.nico.internal.service.AbstractService | ||
import com.nanabell.sponge.nico.module.quest.QuestModule | ||
import com.nanabell.sponge.nico.module.quest.quest.DailyQuest | ||
import com.nanabell.sponge.nico.module.quest.quest.Quest | ||
import com.nanabell.sponge.nico.module.quest.quest.SimpleQuest | ||
import com.nanabell.sponge.nico.module.quest.quest.WeeklyQuest | ||
import com.nanabell.sponge.nico.module.quest.reward.MoneyReward | ||
import com.nanabell.sponge.nico.module.quest.task.KillTask | ||
import com.nanabell.sponge.nico.module.quest.data.quest.Quest | ||
import com.nanabell.sponge.nico.module.quest.data.quest.SimpleQuest | ||
import com.nanabell.sponge.nico.module.quest.interfaces.IQuest | ||
import ninja.leaping.configurate.ConfigurationNode | ||
import ninja.leaping.configurate.ConfigurationOptions | ||
import ninja.leaping.configurate.hocon.HoconConfigurationLoader | ||
import ninja.leaping.configurate.objectmapping.serialize.TypeSerializers | ||
import org.spongepowered.api.Sponge | ||
import org.spongepowered.api.service.economy.Currency | ||
import java.time.Duration | ||
import java.util.* | ||
import kotlin.collections.ArrayList | ||
|
||
@Suppress("UnstableApiUsage") | ||
@RegisterService | ||
@Suppress("UnstableApiUsage") | ||
class QuestRegistry : AbstractService<QuestModule>() { | ||
|
||
private val token = object : TypeToken<List<Quest>>() {} | ||
private val serializers = TypeSerializers.getDefaultSerializers().newChild() | ||
.registerType(TypeToken.of(Currency::class.java), CurrencySerializer()) | ||
.registerType(TypeToken.of(Duration::class.java), DurationSerializer()) | ||
|
||
private lateinit var defaultLoader: HoconConfigurationLoader | ||
private lateinit var defaultRootNode: ConfigurationNode | ||
private val token = object : TypeToken<List<IQuest>>() {} | ||
private val quests: MutableList<IQuest> = ArrayList() | ||
|
||
private lateinit var loader: HoconConfigurationLoader | ||
private lateinit var rootNode: ConfigurationNode | ||
private lateinit var node: ConfigurationNode | ||
|
||
override fun onPreEnable() { | ||
val path = Sponge.getConfigManager().getPluginConfig(NicoYazawa.getPlugin()).directory.resolve("quests.conf") | ||
loader = HoconConfigurationLoader.builder() | ||
.setDefaultOptions(ConfigurationOptions.defaults().setSerializers(serializers)) | ||
.setPath(path) | ||
.build() | ||
val questPath = Sponge.getConfigManager().getPluginConfig(plugin).directory.resolve("quest/quests.conf") | ||
loader = HoconConfigurationLoader.builder().setPath(questPath).build() | ||
node = loader.load() | ||
|
||
val defaultPath = Sponge.getConfigManager().getPluginConfig(NicoYazawa.getPlugin()).directory.resolve("default-quests.conf") | ||
defaultLoader = HoconConfigurationLoader.builder() | ||
.setDefaultOptions(ConfigurationOptions.defaults().setSerializers(serializers)) | ||
.setPath(defaultPath) | ||
.build() | ||
|
||
rootNode = loader.load() | ||
defaultRootNode = defaultLoader.load() | ||
loadQuests() | ||
} | ||
|
||
override fun onEnable() { | ||
loadDefaults() | ||
} | ||
|
||
fun load(uniqueId: UUID): List<Quest> { | ||
val playerNode = rootNode.getNode(uniqueId.toString()) | ||
if (playerNode.isVirtual) { | ||
save(uniqueId, loadDefaults()) | ||
fun loadQuests() { | ||
val questsNode = node.getNode("quests") | ||
if (questsNode.isVirtual) { | ||
saveQuests() | ||
} | ||
|
||
val quests = playerNode.getValue(token) | ||
if (quests == null) { | ||
save(uniqueId, loadDefaults()) | ||
return load(uniqueId) | ||
val quests = questsNode.getValue(token) | ||
if (quests == null) { | ||
saveQuests() | ||
return | ||
} | ||
|
||
val total = quests.toMutableList() | ||
loadDefaults().forEach { quest -> | ||
if (total.none { it.uniqueId == quest.uniqueId }) { | ||
total.add(quest) | ||
} | ||
} | ||
|
||
save(uniqueId, total) | ||
return quests | ||
this.quests.addAll(quests) | ||
} | ||
|
||
fun save(uniqueId: UUID, quests: List<Quest>) { | ||
var root: ConfigurationNode = rootNode.getNode(uniqueId.toString()) | ||
root.setValue(token, quests) | ||
fun saveQuests() { | ||
val toSave = quests.plus(defaults()) | ||
var root: ConfigurationNode = node.getNode("quests") | ||
root.setValue(token, toSave) | ||
|
||
if (root.parent != null) | ||
root = root.parent!! | ||
|
||
rootNode.mergeValuesFrom(root) | ||
loader.save(rootNode) | ||
node.mergeValuesFrom(root) | ||
loader.save(node) | ||
} | ||
|
||
fun loadDefaults(): List<Quest> { | ||
val questNode = defaultRootNode.getNode("quests") | ||
if (questNode.isVirtual) { | ||
saveDefaults(defaults()) | ||
} | ||
|
||
val quests = questNode.getValue(token) | ||
if (quests == null) { | ||
saveDefaults(defaults()) | ||
return loadDefaults() | ||
} | ||
|
||
return quests | ||
} | ||
|
||
fun saveDefaults(quests: List<Quest>) { | ||
val root: ConfigurationNode = defaultRootNode.getNode("quests") | ||
root.setValue(token, quests) | ||
|
||
defaultRootNode.mergeValuesFrom(root) | ||
defaultLoader.save(defaultRootNode) | ||
} | ||
|
||
// TODO: Move to Default serialized file once serialization stands | ||
fun defaults(): List<Quest> { | ||
private fun defaults(): List<Quest> { | ||
return listOf( | ||
SimpleQuest.builder() | ||
.setId(UUID.fromString("2e27a2b7-cea4-44fb-ae88-617a97d177fa")) | ||
.setDescription("Kill a single hostile mob") | ||
.addTask(KillTask.builder() | ||
.setAmount(1) | ||
.build()) | ||
.addReward(MoneyReward.builder() | ||
.setAmount(100) | ||
.build()) | ||
.build("Kill 1 Hostile Mob"), | ||
|
||
SimpleQuest.builder() | ||
.setId(UUID.fromString("a82f48d5-a4fd-4a5b-b55d-8635477456f4")) | ||
.setDescription("Kill 2 hostile mobs of any type") | ||
.addTask(KillTask.builder() | ||
.setAmount(2) | ||
.build()) | ||
.addReward(MoneyReward.builder() | ||
.setAmount(200) | ||
.build()) | ||
.addRequirement(UUID.fromString("2e27a2b7-cea4-44fb-ae88-617a97d177fa")) | ||
.build("Kill 2 Hostile Mobs"), | ||
|
||
DailyQuest.builder() | ||
.setId(UUID.fromString("6073df2d-cb4d-4663-a75d-aaf09e159479")) | ||
.setDescription("Kill 5 hostile mobs of any type") | ||
.addTask(KillTask.builder() | ||
.setAmount(5) | ||
.build()) | ||
.addReward(MoneyReward.builder() | ||
.setAmount(300) | ||
.build()) | ||
.addRequirement(UUID.fromString("a82f48d5-a4fd-4a5b-b55d-8635477456f4")) | ||
.build("Kill 5 Hostile Mobs"), | ||
|
||
WeeklyQuest.builder() | ||
.setId(UUID.fromString("a46eae73-28f1-4260-aea1-1d551bf28e72")) | ||
.setDescription("Kill 50 hostile mobs of any type") | ||
.addTask(KillTask.builder() | ||
.setAmount(50) | ||
.build()) | ||
.addReward(MoneyReward.builder() | ||
.setAmount(1000) | ||
.build()) | ||
.addRequirement(UUID.fromString("6073df2d-cb4d-4663-a75d-aaf09e159479")) | ||
.build("Kill 50 Hostile Mobs") | ||
|
||
|
||
SimpleQuest( | ||
UUID.randomUUID(), | ||
"Sample Quest", | ||
"This is a Sample Quest", | ||
listOf(UUID.randomUUID()), | ||
listOf(UUID.randomUUID(), UUID.randomUUID()), | ||
listOf()), | ||
SimpleQuest( | ||
UUID.randomUUID(), | ||
"Sample Quest 2", | ||
"This is a Sample Quest 2", | ||
listOf(UUID.randomUUID()), | ||
listOf(UUID.randomUUID(), UUID.randomUUID()), | ||
listOf()), | ||
SimpleQuest( | ||
UUID.randomUUID(), | ||
"Sample Quest 3", | ||
"This is a Sample Quest 3", | ||
listOf(UUID.randomUUID()), | ||
listOf(UUID.randomUUID(), UUID.randomUUID()), | ||
listOf(UUID.randomUUID(), UUID.randomUUID(), UUID.randomUUID())) | ||
) | ||
} | ||
} | ||
} |
Oops, something went wrong.