Skip to content

Commit

Permalink
Add Folia support
Browse files Browse the repository at this point in the history
  • Loading branch information
CJCrafter committed Oct 13, 2024
1 parent 025a8bf commit bc1479f
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 38 deletions.
17 changes: 10 additions & 7 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,27 +1,29 @@
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

group = "com.cjcrafter"
version = "3.0.5"
version = "3.1.0"

plugins {
`java-library`
`maven-publish`
id("com.github.johnrengelman.shadow") version "8.1.1"
id("net.minecrell.plugin-yml.bukkit") version "0.5.2"
kotlin("jvm") version "1.9.21"
`maven-publish`
id("com.gradleup.shadow") version "8.3.3"
id("net.minecrell.plugin-yml.bukkit") version "0.6.0"
}

// See https://github.com/Minecrell/plugin-yml
bukkit {
main = "com.cjcrafter.armormechanics.ArmorMechanics"
apiVersion = "1.13"
foliaSupported = true

authors = listOf("CJCrafter")
depend = listOf("MechanicsCore")
softDepend = listOf("WeaponMechanics")
}

repositories {
mavenLocal()
mavenCentral()

maven(url = "https://hub.spigotmc.org/nexus/content/repositories/snapshots/") // Spigot
Expand All @@ -34,9 +36,10 @@ dependencies {
implementation("com.jeff_media:SpigotUpdateChecker:3.0.3")

compileOnly("org.spigotmc:spigot-api:1.20.6-R0.1-SNAPSHOT")
compileOnly("io.lumine:Mythic-Dist:5.3.5")
compileOnly("com.cjcrafter:mechanicscore:3.4.1")
compileOnly("com.cjcrafter:weaponmechanics:3.4.1")
compileOnly("io.lumine:Mythic-Dist:5.7.2")
compileOnly("com.cjcrafter:foliascheduler:0.6.0")
compileOnly("com.cjcrafter:mechanicscore:3.4.13-FOLIA")
compileOnly("com.cjcrafter:weaponmechanics:3.4.14-FOLIA")
}

tasks.shadowJar {
Expand Down
30 changes: 17 additions & 13 deletions src/main/java/com/cjcrafter/armormechanics/ArmorMechanics.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ package com.cjcrafter.armormechanics

import com.cjcrafter.armormechanics.commands.Command
import com.cjcrafter.armormechanics.listeners.*
import com.cjcrafter.foliascheduler.FoliaCompatibility
import com.cjcrafter.foliascheduler.ServerImplementation
import com.cjcrafter.foliascheduler.TaskImplementation
import com.jeff_media.updatechecker.UpdateCheckSource
import com.jeff_media.updatechecker.UpdateChecker
import com.jeff_media.updatechecker.UserAgentBuilder
Expand All @@ -10,12 +13,10 @@ import me.deecaad.core.events.QueueSerializerEvent
import me.deecaad.core.file.BukkitConfig
import me.deecaad.core.file.SerializeData
import me.deecaad.core.file.SerializerException
import me.deecaad.core.file.TaskChain
import me.deecaad.core.utils.Debugger
import me.deecaad.core.utils.FileUtil
import me.deecaad.core.utils.LogLevel
import me.deecaad.core.utils.MinecraftVersions
import me.deecaad.core.utils.ReflectionUtil
import org.bstats.bukkit.Metrics
import org.bstats.charts.SimplePie
import org.bukkit.configuration.file.FileConfiguration
Expand All @@ -26,10 +27,12 @@ import org.bukkit.inventory.ItemStack
import org.bukkit.plugin.java.JavaPlugin
import java.io.File
import java.util.concurrent.Callable
import java.util.concurrent.CompletableFuture

class ArmorMechanics : JavaPlugin() {

lateinit var debug: Debugger
lateinit var scheduler: ServerImplementation
private var metrics: Metrics? = null
val effects: MutableMap<String, BonusEffect> = HashMap()
val armors: MutableMap<String, ItemStack> = HashMap()
Expand All @@ -40,6 +43,7 @@ class ArmorMechanics : JavaPlugin() {
val level = getConfig().getInt("Debug_Level", 2)
val printTraces = getConfig().getBoolean("Print_Traces", false)
debug = Debugger(logger, level, printTraces)
scheduler = FoliaCompatibility(this).serverImplementation
if (!MinecraftVersions.UPDATE_AQUATIC.isAtLeast()) {
debug.error(
" !!!!! ERROR !!!!!",
Expand Down Expand Up @@ -89,16 +93,15 @@ class ArmorMechanics : JavaPlugin() {
}
}

fun reload(): TaskChain {
return TaskChain(this)
.thenRunAsync(Runnable {
// Write config from jar to datafolder
if (!dataFolder.exists() || (dataFolder.listFiles()?.size ?: 0) == 0) {
debug.info("Copying files from jar (This process may take up to 30 seconds during the first load!)")
FileUtil.copyResourcesTo(classLoader.getResource("ArmorMechanics"), dataFolder.toPath())
}
})
.thenRunSync(Runnable {
fun reload(): CompletableFuture<TaskImplementation<Void>> {
return scheduler.async().runNow(Runnable {
// Write config from jar to datafolder
if (!dataFolder.exists() || (dataFolder.listFiles()?.size ?: 0) == 0) {
debug.info("Copying files from jar (This process may take up to 30 seconds during the first load!)")
FileUtil.copyResourcesTo(classLoader.getResource("ArmorMechanics"), dataFolder.toPath())
}
}).asFuture().thenCompose {
scheduler.global().run(Runnable {
reloadConfig()

// Clear old data
Expand Down Expand Up @@ -136,7 +139,8 @@ class ArmorMechanics : JavaPlugin() {
e.log(debug)
}
}
})
}).asFuture()
}
}

private fun registerBStats() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,9 @@ object Command {
permission("armormechanics.commands.reload")
description("Reloads ArmorMechanics configuration")
executeAny { sender, args ->
ArmorMechanics.INSTANCE.reload()
.thenRunSync(Runnable { sender.sendMessage("${ChatColor.GREEN}Reloaded ArmorMechanics") })
ArmorMechanics.INSTANCE.reload().thenRun {
sender.sendMessage("${ChatColor.GREEN}Reloaded ArmorMechanics")
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import me.deecaad.core.events.EntityEquipmentEvent
import org.bukkit.entity.LivingEntity
import org.bukkit.event.EventHandler
import org.bukkit.event.Listener
import org.bukkit.scheduler.BukkitRunnable

class ArmorUpdateListener : Listener {

Expand All @@ -20,23 +19,21 @@ class ArmorUpdateListener : Listener {
// event since it contains a COPY of the armor. So, we need to check 1
// tick after the event and check to update it. If no armor is in that
// slot anymore, we can just assume the player has already removed it.
object : BukkitRunnable() {
override fun run() {
val equipment = entity.equipment
val item = ArmorMechanicsAPI.getItem(equipment!!, event.slot)
ArmorMechanics.INSTANCE.scheduler.entity(entity).runDelayed(Runnable {
val equipment = entity.equipment
val item = ArmorMechanicsAPI.getItem(equipment!!, event.slot)

// Either not ArmorMechanics armor, or just not any armor at
// all. Either way, we don't need to update it.
if (ArmorMechanicsAPI.getArmorTitle(item) == null) return
// Either not ArmorMechanics armor, or just not any armor at
// all. Either way, we don't need to update it.
if (ArmorMechanicsAPI.getArmorTitle(item) == null) return@Runnable

// Dupe protection, it is theoretically possible for a client
// to swap out the item with a different armor. Not a very useful
// dupe since it replaces the old armor, but still a potential bug
if (event.equipped != item) return
// Dupe protection, it is theoretically possible for a client
// to swap out the item with a different armor. Not a very useful
// dupe since it replaces the old armor, but still a potential bug
if (event.equipped != item) return@Runnable

ArmorMechanicsAPI.update(entity, item!!)
ArmorMechanicsAPI.setItem(equipment, event.slot, item)
}
}.runTask(ArmorMechanics.INSTANCE)
ArmorMechanicsAPI.update(entity, item!!)
ArmorMechanicsAPI.setItem(equipment, event.slot, item)
}, 1L)
}
}

0 comments on commit bc1479f

Please sign in to comment.