-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #17 from iGabyTM/add-custom-requirements
- Loading branch information
Showing
30 changed files
with
726 additions
and
27 deletions.
There are no files selected for viewing
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
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
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
14 changes: 14 additions & 0 deletions
14
...in/me/gabytm/minecraft/arcanevouchers/io/serializers/adventure/TextComponentSerializer.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 me.gabytm.minecraft.arcanevouchers.io.serializers.adventure | ||
|
||
import com.google.gson.* | ||
import net.kyori.adventure.text.TextComponent | ||
import net.kyori.adventure.text.minimessage.MiniMessage | ||
import java.lang.reflect.Type | ||
|
||
class TextComponentSerializer : JsonSerializer<TextComponent> { | ||
|
||
override fun serialize(src: TextComponent?, typeOfSrc: Type?, context: JsonSerializationContext?): JsonElement { | ||
return if (src == null) JsonNull.INSTANCE else JsonPrimitive(MiniMessage.miniMessage().serialize(src)) | ||
} | ||
|
||
} |
28 changes: 28 additions & 0 deletions
28
...ain/kotlin/me/gabytm/minecraft/arcanevouchers/io/serializers/bukkit/LocationSerializer.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,28 @@ | ||
package me.gabytm.minecraft.arcanevouchers.io.serializers.bukkit | ||
|
||
import com.google.gson.* | ||
import org.bukkit.Location | ||
import java.lang.reflect.Type | ||
|
||
class LocationSerializer : JsonSerializer<Location> { | ||
|
||
override fun serialize(src: Location?, typeOfSrc: Type?, context: JsonSerializationContext?): JsonElement { | ||
if (src == null) { | ||
return JsonNull.INSTANCE | ||
} | ||
|
||
val element = JsonObject() | ||
element.addProperty("world", src.world?.name) | ||
element.addProperty("x", src.x) | ||
element.addProperty("y", src.y) | ||
element.addProperty("z", src.z) | ||
return element | ||
} | ||
|
||
companion object { | ||
|
||
val INSTANCE = LocationSerializer() | ||
|
||
} | ||
|
||
} |
16 changes: 16 additions & 0 deletions
16
...c/main/kotlin/me/gabytm/minecraft/arcanevouchers/io/serializers/java/PatternSerializer.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 me.gabytm.minecraft.arcanevouchers.io.serializers.java | ||
|
||
import com.google.gson.JsonElement | ||
import com.google.gson.JsonPrimitive | ||
import com.google.gson.JsonSerializationContext | ||
import com.google.gson.JsonSerializer | ||
import java.lang.reflect.Type | ||
import java.util.regex.Pattern | ||
|
||
class PatternSerializer : JsonSerializer<Pattern> { | ||
|
||
override fun serialize(src: Pattern?, typeOfSrc: Type?, context: JsonSerializationContext?): JsonElement { | ||
return JsonPrimitive(src?.pattern()) | ||
} | ||
|
||
} |
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
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
27 changes: 27 additions & 0 deletions
27
.../main/kotlin/me/gabytm/minecraft/arcanevouchers/voucher/requirements/ArcaneRequirement.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,27 @@ | ||
package me.gabytm.minecraft.arcanevouchers.voucher.requirements | ||
|
||
import me.gabytm.minecraft.arcanevouchers.actions.ArcaneAction | ||
import me.gabytm.minecraft.arcanevouchers.actions.ArcaneActionManager | ||
import me.gabytm.minecraft.util.requirements.Requirement | ||
import org.bukkit.entity.Player | ||
|
||
abstract class ArcaneRequirement( | ||
name: String, | ||
optional: Boolean, | ||
negated: Boolean, | ||
@Transient private val failActions: List<ArcaneAction>, | ||
@Transient private val actionManager: ArcaneActionManager | ||
// A requirement can not be 'optional' and 'required' at the same time | ||
// So we consider a requirement 'required' if it is not 'optional' | ||
) : Requirement<Player>(name, !optional, optional, negated) { | ||
|
||
@Suppress("unused") | ||
protected val requirementType: String = this::class.java.simpleName | ||
|
||
override fun onFail(player: Player?) { | ||
if (player != null && failActions.isNotEmpty()) { | ||
actionManager.executeActions(player, failActions) | ||
} | ||
} | ||
|
||
} |
13 changes: 13 additions & 0 deletions
13
...otlin/me/gabytm/minecraft/arcanevouchers/voucher/requirements/ArcaneRequirementFactory.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,13 @@ | ||
package me.gabytm.minecraft.arcanevouchers.voucher.requirements | ||
|
||
import me.gabytm.minecraft.arcanevouchers.actions.ArcaneActionManager | ||
import me.gabytm.minecraft.util.requirements.RequirementFactory | ||
import org.bukkit.configuration.ConfigurationSection | ||
|
||
abstract class ArcaneRequirementFactory<R : ArcaneRequirement> : RequirementFactory<ConfigurationSection, R> { | ||
|
||
override fun create(source: ConfigurationSection): R? = null | ||
|
||
abstract fun create(source: ConfigurationSection, actionManager: ArcaneActionManager): R? | ||
|
||
} |
50 changes: 50 additions & 0 deletions
50
...lin/me/gabytm/minecraft/arcanevouchers/voucher/requirements/ArcaneRequirementProcessor.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,50 @@ | ||
package me.gabytm.minecraft.arcanevouchers.voucher.requirements | ||
|
||
import me.gabytm.minecraft.arcanevouchers.actions.ArcaneActionManager | ||
import me.gabytm.minecraft.arcanevouchers.voucher.requirements.implementations.location.DistanceRequirementFactory | ||
import me.gabytm.minecraft.arcanevouchers.voucher.requirements.implementations.number.NumberRequirementFactory | ||
import me.gabytm.minecraft.arcanevouchers.voucher.requirements.implementations.string.StringRequirementFactory | ||
import me.gabytm.minecraft.util.requirements.RequirementsList | ||
import me.gabytm.minecraft.util.requirements.bukkit.BukkitRequirementProcessor | ||
import org.bukkit.configuration.ConfigurationSection | ||
import org.bukkit.entity.Player | ||
|
||
class ArcaneRequirementProcessor(private val actionManager: ArcaneActionManager) : BukkitRequirementProcessor<Player>() { | ||
|
||
private val requirementFactories = mutableSetOf<ArcaneRequirementFactory<out ArcaneRequirement>>() | ||
|
||
init { | ||
// Location | ||
registerFactory(DistanceRequirementFactory()) | ||
|
||
registerFactory(NumberRequirementFactory()) | ||
registerFactory(StringRequirementFactory()) | ||
} | ||
|
||
private fun createRequirement(source: ConfigurationSection): ArcaneRequirement? { | ||
return requirementFactories.firstOrNull { it.matches(source) }?.create(source, actionManager) | ||
} | ||
|
||
fun registerFactory(factory: ArcaneRequirementFactory<out ArcaneRequirement>) { | ||
requirementFactories.add(factory) | ||
} | ||
|
||
fun processRequirements(root: ConfigurationSection?): RequirementsList<ArcaneRequirement, Player> { | ||
if (root == null) { | ||
return RequirementsList(emptyList()) | ||
} | ||
|
||
val minimumRequirements = root.getInt("minimumRequirements", RequirementsList.ALL_REQUIREMENTS) | ||
val requirementsSection = root.getConfigurationSection("list") ?: return RequirementsList(emptyList()) | ||
val requirements = mutableListOf<ArcaneRequirement>() | ||
|
||
for (name in requirementsSection.getKeys(false)) { | ||
val section = requirementsSection.getConfigurationSection(name) ?: continue | ||
val requirement = createRequirement(section) ?: continue | ||
requirements.add(requirement) | ||
} | ||
|
||
return RequirementsList(requirements, minimumRequirements) | ||
} | ||
|
||
} |
Oops, something went wrong.