Skip to content

Commit

Permalink
Optimize hunt Challenges
Browse files Browse the repository at this point in the history
  • Loading branch information
MiraculixxT committed May 7, 2024
1 parent 1f567ca commit 4b3f965
Show file tree
Hide file tree
Showing 18 changed files with 207 additions and 416 deletions.
8 changes: 2 additions & 6 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ dependencies {
// MC Libraries
implementation("de.miraculixx:mc-commons:1.0.1")
implementation("de.miraculixx:kpaper-light:1.2.1")
implementation("dev.jorel:commandapi-bukkit-shade-mojang-mapped:9.4.0")
library("dev.jorel:commandapi-bukkit-shade-mojang-mapped:9.4.0")
library("dev.jorel:commandapi-bukkit-kotlin:9.4.0")

// Internal APIs
Expand All @@ -68,10 +68,9 @@ tasks {
shadowJar {
dependencies {
include {
it.moduleGroup == "de.miraculixx" || it.moduleGroup == "dev.jorel"
it.moduleGroup == "de.miraculixx"
}
}
relocate("dev.jorel.commandapi", "de.miraculixx.mchallenge.commandapi")
}
}

Expand All @@ -92,9 +91,6 @@ bukkit {
load = BukkitPluginDescription.PluginLoadOrder.STARTUP
depend = listOf()
softDepend = listOf("MTimer", "MWeb")
commands.create("mobhunt")
commands.create("itemhunt")
commands.create("deathhunt")
libraries = listOf(
"io.ktor:ktor-client-core-jvm:2.3.7",
"io.ktor:ktor-client-cio-jvm:2.3.7"
Expand Down
19 changes: 13 additions & 6 deletions data/challenges.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
[
{
"key": "DEATH_HUNT",
"version": 105,
"block": "totem_of_undying",
"tags": [
"FUN",
"FORCE"
],
"new": true
},
{
"key": "RHYTHM_CRAFT",
"version": -1,
Expand Down Expand Up @@ -32,8 +42,7 @@
"wrongDamage",
"randomOrder",
"visual"
],
"new": true
]
},
{
"key": "WORLD_DECAY",
Expand All @@ -45,8 +54,7 @@
"settings": [
"delay",
"steps"
],
"new": true
]
},
{
"key": "CHUNK_SYNC",
Expand All @@ -57,8 +65,7 @@
],
"settings": [
"env"
],
"new": true
]
},
{
"key": "LOW_VISION",
Expand Down
12 changes: 9 additions & 3 deletions data/language/mchallenge/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ command:
notLoggedIn: "Connect your MUtils Account to unlock Premium Features"
updateRule: "The rule <input-1> was updated to <input-2>"
noRule: "<red>Please select a valid rule to change!"
registered: "Added command <blue>/<input-1></blue>. Use to interact with the Challenge"
registered: "<grey>Challenge Command: <blue>/<input-1></blue>"


#-----------------------------------------#
Expand All @@ -95,12 +95,15 @@ event:
noDoubleKill: "<red><input-1> killed the same mob twice in a row!"
hitOrder: "<red><input-1> was not allowed do damage a mob"
crushedAnvils: "<red><input-1> was hit by an anvil!"
mobHunt:
mob_hunt:
collect: "<green><input-1> found <input-2>!"
success: "<green>Congratulations! You found all mobs"
itemHunt:
item_hunt:
collect: "<green><input-1> collected <input-2>!"
success: "<green>Congratulations! You found all <input-1> items"
death_hunt:
collect: "<green><input-1> died to <input-2>!"
success: "<green>Congratulations! You died to all <input-1> deaths"
itemDecay:
time: "Decay in"
paused: "Paused display (selected)"
Expand Down Expand Up @@ -406,6 +409,9 @@ items:
HP_DRAIN:
n: "Health Drainer"
l: "Every few minutes your max health will be<br>reduced by a given percentage.<br>Better be quick!"
DEATH_HUNT:
n: "Death Hunt"
l: "Die to all given deaths in a specific order<br>to finish!<br>Use /deathhunt to modify"
chS:
FLY:
power:
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
kotlin.code.style=official

# Project Settings - set name in settings.gradle.kts
version=115
version=116
group=de.miraculixx
name=MChallenge
description=MUtils Challenges - Play various Challenges that can modify the game slightly to completely against or in coop with your friends!
Expand Down
3 changes: 2 additions & 1 deletion src/main/kotlin/de/miraculixx/mchallenge/MChallenge.kt
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ class MChallenge : KPaper() {
ConfigManager.addConfigurable(BackpackCommand())

// Load configuration
prefix = cmp("MChallenge", cHighlight) + _prefixSeparator
ConfigManager.addConfigurable(ChallengeManager)
val settings = ConfigManager.settings
debug = settings.debug
Expand All @@ -120,7 +121,7 @@ class MChallenge : KPaper() {
Spectator.loadData()

// Connect Bridge
bridgeAPI = MUtilsBridge(MUtilsPlatform.PAPER, MUtilsModule.CHALLENGES, server.version, server.port, debug)
bridgeAPI = MUtilsBridge(MUtilsPlatform.PAPER, MUtilsModule.CHALLENGES, server.minecraftVersion, server.port, debug)
val version = bridgeAPI.versionCheck(description.version.toIntOrNull() ?: 0, File("plugins/update"))
//bridgeAPI.modrinthUpdate(File("plugins/update"))
// TODO Prompt with click to update
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import de.miraculixx.mchallenge.utils.Command
import de.miraculixx.mchallenge.utils.bc
import de.miraculixx.mcommons.text.prefix

interface ChallengeCommand {
interface CommandChallenge {
val command: Command

fun registerCommand() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
package de.miraculixx.mchallenge.modules.challenges.interfaces

import de.miraculixx.kpaper.event.listen
import de.miraculixx.kpaper.event.register
import de.miraculixx.kpaper.event.unregister
import de.miraculixx.kpaper.extensions.broadcast
import de.miraculixx.kpaper.extensions.onlinePlayers
import de.miraculixx.mchallenge.MChallenge
import de.miraculixx.mchallenge.modules.ChallengeManager
import de.miraculixx.mchallenge.utils.Command
import de.miraculixx.mchallenge.utils.command
import de.miraculixx.mchallenge.utils.config.loadConfig
import de.miraculixx.mchallenge.utils.config.saveConfig
import de.miraculixx.mchallenge.utils.serializer.Serializer
import de.miraculixx.mcommons.serializer.miniMessage
import de.miraculixx.mcommons.text.cmp
import de.miraculixx.mcommons.text.prefix
import dev.jorel.commandapi.kotlindsl.anyExecutor
import dev.jorel.commandapi.kotlindsl.literalArgument
import kotlinx.serialization.Serializable
import net.kyori.adventure.audience.Audience
import net.kyori.adventure.bossbar.BossBar
import net.kyori.adventure.key.Key
import net.kyori.adventure.sound.Sound
import org.bukkit.event.player.PlayerJoinEvent
import java.io.File

abstract class HuntChallenge<T>(name: String, val key: String) : CommandChallenge {
abstract val remainingEntries: MutableList<T>
abstract val allEntries: List<T>
abstract val typeName: String
abstract var currentTarget: T?
abstract val maxEntries: Int
abstract val serializer: Serializer<T>

private val dataFile = File("${MChallenge.configFolder.path}/data/$key.json")
private val bar = BossBar.bossBar(cmp("Waiting for server..."), 0f, BossBar.Color.BLUE, BossBar.Overlay.PROGRESS)

override val command: Command = command(name) {
literalArgument("skip") {
anyExecutor { sender, _ ->
nextEntry(sender.name, sender)
}
}
literalArgument("reset") {
anyExecutor { _, _ ->
remainingEntries.clear()
remainingEntries.addAll(allEntries)
calcBar()
}
}
}

abstract fun getTranslationKey(): String?

fun nextEntry(playerName: String, audience: Audience) {
broadcast(prefix, "event.$key.collect", listOf(playerName, getTranslationKey()?.let { "<lang:$it>" } ?: ""))
audience.playSound(Sound.sound(Key.key("entity.chicken.egg"), Sound.Source.MASTER, 1f, 1.2f))
currentTarget = if (remainingEntries.isEmpty()) {
broadcast(prefix, "event.$key.success")
ChallengeManager.stopChallenges()
null
} else remainingEntries.random()
remainingEntries.remove(currentTarget)
calcBar()
}

private fun calcBar() {
val collectedAmount = maxEntries - remainingEntries.size
val target = getTranslationKey()?.let { "<blue><b><lang:$it><blue><b>" } ?: "<green><b>Finished</b></green>"
bar.name(miniMessage.deserialize("<gray>$typeName:</gray> $target <dark_gray>(<gray><green>$collectedAmount</green>/<red>$maxEntries</red></gray>)</dark_gray>"))
}


//
// Common Events
//
private val onJoin = listen<PlayerJoinEvent> {
it.player.showBossBar(bar)
}


//
// Challenge Lifecycle
//
fun startHunt() {
registerCommand()
val preData = dataFile.loadConfig(HuntData())
val preTarget = preData.currentTarget
currentTarget = if (preTarget == null) {
remainingEntries.addAll(allEntries)
remainingEntries.random()
} else {
remainingEntries.addAll(preData.remainingEntries.map { serializer.toObject(it) })
serializer.toObject(preTarget)
}

remainingEntries.remove(currentTarget)
calcBar()
onlinePlayers.forEach { it.showBossBar(bar) }
onJoin.register()
}

fun stopHunt() {
unregisterCommand()
dataFile.saveConfig(HuntData(currentTarget?.let { serializer.toString(it) }, remainingEntries.map { serializer.toString(it) }))
onlinePlayers.forEach { it.hideBossBar(bar) }
onJoin.unregister()
}


//
// Data Holder
//
@Serializable
data class HuntData(
val currentTarget: String? = null,
val remainingEntries: List<String> = emptyList()
)
}
Loading

0 comments on commit 4b3f965

Please sign in to comment.