Skip to content

Commit

Permalink
Added 1.21 item arg parsers
Browse files Browse the repository at this point in the history
  • Loading branch information
WillFP committed Jun 23, 2024
1 parent de04833 commit 1bada83
Show file tree
Hide file tree
Showing 18 changed files with 283 additions and 65 deletions.
14 changes: 13 additions & 1 deletion eco-core/core-backend/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,22 @@ dependencies {
implementation("org.reflections:reflections:0.9.12")
implementation("org.objenesis:objenesis:3.2")

compileOnly("io.papermc.paper:paper-api:1.20.2-R0.1-SNAPSHOT")
compileOnly("io.papermc.paper:paper-api:1.21-R0.1-SNAPSHOT")
compileOnly("me.clip:placeholderapi:2.11.4")
compileOnly("net.kyori:adventure-text-minimessage:4.10.0")
compileOnly("net.kyori:adventure-platform-bukkit:4.1.0")
compileOnly("org.yaml:snakeyaml:1.33")
compileOnly("com.moandjiezana.toml:toml4j:0.7.2")
}

tasks {
compileJava {
options.release = 21
}

compileKotlin {
kotlinOptions {
jvmTarget = "21"
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,15 @@ object EntityArgParserJumpStrength : EntityArgParser {
return@EntityArgParseResult false
}

val inst = it.getAttribute(Attribute.HORSE_JUMP_STRENGTH) ?: return@EntityArgParseResult false
val inst = it.getAttribute(Attribute.GENERIC_JUMP_STRENGTH) ?: return@EntityArgParseResult false
inst.value >= attributeValue
},
{
if (it !is LivingEntity) {
return@EntityArgParseResult
}

it.getAttribute(Attribute.HORSE_JUMP_STRENGTH)?.baseValue = attributeValue
it.getAttribute(Attribute.GENERIC_JUMP_STRENGTH)?.baseValue = attributeValue
}
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,4 @@ object ArgParserColor : LookupArgParser {

return "color:#${Integer.toHexString(meta.color.asRGB())}"
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ object ArgParserEnchantment : LookupArgParser {
continue
}

@Suppress("DEPRECATION")
val enchant = Enchantment.getByKey(NamespacedKeyUtils.create("minecraft", argSplit[0]))
val level = argSplit[1].toIntOrNull()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,4 @@ object ArgParserEntity : LookupArgParser {

return state.spawnedType?.let { "entity:${state.spawnedType!!.name}" } ?: return null
}
}
}
Original file line number Diff line number Diff line change
@@ -1,44 +1,30 @@
package com.willfp.eco.internal.items

import com.willfp.eco.core.items.args.LookupArgParser
import com.willfp.eco.internal.items.templates.ValueArgParser
import com.willfp.eco.util.StringUtils
import org.bukkit.inventory.ItemStack
import org.bukkit.inventory.meta.ItemMeta
import java.util.function.Predicate

object ArgParserName : LookupArgParser {
override fun parseArguments(args: Array<out String>, meta: ItemMeta): Predicate<ItemStack>? {
var name: String? = null

for (arg in args) {
if (!arg.lowercase().startsWith("name:")) {
continue
}
name = arg.substring(5, arg.length)
}

name ?: return null
object ArgParserName : ValueArgParser<String>("name") {
override fun parse(arg: String): String {
return arg
}

val formatted = StringUtils.format(name)
override fun apply(meta: ItemMeta, value: String) {
val formatted = StringUtils.format(value)

// I don't know why it says it's redundant, the compiler yells at me
@Suppress("UsePropertyAccessSyntax", "RedundantSuppression", "DEPRECATION")
meta.setDisplayName(formatted)

return Predicate {
val testMeta = it.itemMeta ?: return@Predicate false

@Suppress("DEPRECATION")
testMeta.displayName == formatted
}
}

override fun serializeBack(meta: ItemMeta): String? {
override fun test(meta: ItemMeta): String? {
if (!meta.hasDisplayName()) {
return null
}

@Suppress("DEPRECATION")
return "name:\"${meta.displayName}\""
return meta.displayName
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,4 @@ object ArgParserTexture : LookupArgParser {

return "texture:${SkullUtils.getSkullTexture(meta)}"
}
}
}
Original file line number Diff line number Diff line change
@@ -1,38 +1,14 @@
package com.willfp.eco.internal.items

import com.willfp.eco.core.items.args.LookupArgParser
import org.bukkit.inventory.ItemStack
import com.willfp.eco.internal.items.templates.FlagArgParser
import org.bukkit.inventory.meta.ItemMeta
import java.util.function.Predicate

object ArgParserUnbreakable : LookupArgParser {
override fun parseArguments(args: Array<out String>, meta: ItemMeta): Predicate<ItemStack>? {
var unbreakable = false

for (arg in args) {
if (arg.equals("unbreakable", true)) {
unbreakable = true
}
}

if (!unbreakable) {
return null
}

object ArgParserUnbreakable : FlagArgParser("unbreakable") {
override fun apply(meta: ItemMeta) {
meta.isUnbreakable = true

return Predicate {
val testMeta = it.itemMeta ?: return@Predicate false

testMeta.isUnbreakable
}
}

override fun serializeBack(meta: ItemMeta): String? {
if (!meta.isUnbreakable) {
return null
}

return "unbreakable"
override fun test(meta: ItemMeta): Boolean {
return meta.isUnbreakable
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.willfp.eco.internal.items.modern

import com.willfp.eco.internal.items.templates.FlagArgParser
import org.bukkit.inventory.meta.ItemMeta

object ArgParserFireResistant : FlagArgParser("fire_resistant") {
override fun apply(meta: ItemMeta) {
meta.isFireResistant = true
}

override fun test(meta: ItemMeta): Boolean {
return meta.isFireResistant
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.willfp.eco.internal.items.modern

import com.willfp.eco.internal.items.templates.FlagArgParser
import org.bukkit.inventory.meta.ItemMeta

object ArgParserGlint : FlagArgParser("glint") {
override fun apply(meta: ItemMeta) {
meta.setEnchantmentGlintOverride(true)
}

override fun test(meta: ItemMeta): Boolean {
return meta.hasEnchantmentGlintOverride()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.willfp.eco.internal.items.modern

import com.willfp.eco.internal.items.templates.ValueArgParser
import com.willfp.eco.util.StringUtils
import net.kyori.adventure.text.Component
import net.kyori.adventure.text.minimessage.MiniMessage
import org.bukkit.inventory.meta.ItemMeta

object ArgParserItemName : ValueArgParser<Component>("item_name") {
override fun parse(arg: String): Component {
return StringUtils.formatToComponent(arg)
}

override fun apply(meta: ItemMeta, value: Component) {
meta.itemName(value)
}

override fun test(meta: ItemMeta): String? {
if (!meta.hasItemName()) {
return null
}

val name = MiniMessage.miniMessage().serialize(meta.itemName())

return name
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.willfp.eco.internal.items.modern

import com.willfp.eco.internal.items.templates.ValueArgParser
import com.willfp.eco.util.StringUtils
import net.kyori.adventure.text.Component
import net.kyori.adventure.text.minimessage.MiniMessage
import org.bukkit.inventory.meta.Damageable
import org.bukkit.inventory.meta.ItemMeta
import org.checkerframework.checker.units.qual.m

object ArgParserMaxDamage : ValueArgParser<Int>("max_damage") {
override fun parse(arg: String): Int? {
return arg.toIntOrNull()
}

override fun apply(meta: ItemMeta, value: Int) {
if (meta !is Damageable) {
return
}

meta.setMaxDamage(value)
}

override fun test(meta: ItemMeta): String? {
if (meta !is Damageable) {
return null
}

if (!meta.hasMaxDamage()) {
return null
}

return meta.maxDamage.toString()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.willfp.eco.internal.items.modern

import com.willfp.eco.internal.items.templates.ValueArgParser
import com.willfp.eco.util.StringUtils
import net.kyori.adventure.text.Component
import net.kyori.adventure.text.minimessage.MiniMessage
import org.bukkit.inventory.meta.Damageable
import org.bukkit.inventory.meta.ItemMeta
import org.checkerframework.checker.units.qual.m

object ArgParserMaxStackSize : ValueArgParser<Int>("max_stack_size") {
override fun parse(arg: String): Int? {
return arg.toIntOrNull()
}

override fun apply(meta: ItemMeta, value: Int) {
meta.setMaxStackSize(value)
}

override fun test(meta: ItemMeta): String? {
if (!meta.hasMaxStackSize()) {
return null
}

return meta.maxStackSize.toString()
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.willfp.eco.internal.items
package com.willfp.eco.internal.items.modern

import com.willfp.eco.core.items.args.LookupArgParser
import org.bukkit.NamespacedKey
Expand All @@ -23,7 +23,11 @@ object ArgParserTrim : LookupArgParser {
if (!argSplit[0].equals("trim", ignoreCase = true)) {
continue
}

@Suppress("DEPRECATION")
material = Registry.TRIM_MATERIAL.get(NamespacedKey.minecraft(argSplit.getOrElse(1) {""}))

@Suppress("DEPRECATION")
pattern = Registry.TRIM_PATTERN.get(NamespacedKey.minecraft(argSplit.getOrElse(2) {""}))
}

Expand All @@ -43,6 +47,11 @@ object ArgParserTrim : LookupArgParser {
override fun serializeBack(meta: ItemMeta): String? {
val trim = (meta as? ArmorMeta)?.trim ?: return null

return "trim:${trim.material.key.key.lowercase()}:${trim.pattern.key.key.lowercase()}"
@Suppress("DEPRECATION")
val materialKey = Registry.TRIM_MATERIAL.getKey(trim.material) ?: return null
@Suppress("DEPRECATION")
val patternKey = Registry.TRIM_PATTERN.getKey(trim.pattern) ?: return null

return "trim:${materialKey.key.lowercase()}:${patternKey.key.lowercase()}"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.willfp.eco.internal.items.templates

import com.willfp.eco.core.items.args.LookupArgParser
import org.bukkit.inventory.ItemStack
import org.bukkit.inventory.meta.ItemMeta
import java.util.function.Predicate

abstract class FlagArgParser(
protected val flag: String
) : LookupArgParser {
abstract fun apply(meta: ItemMeta)

abstract fun test(meta: ItemMeta): Boolean

override fun parseArguments(args: Array<out String>, meta: ItemMeta): Predicate<ItemStack>? {
var has = false

for (arg in args) {
if (arg.equals(flag, true)) {
has = true
}
}

if (!has) {
return null
}

apply(meta)

return Predicate {
val testMeta = it.itemMeta ?: return@Predicate false

test(testMeta)
}
}

override fun serializeBack(meta: ItemMeta): String? {
if (!test(meta)) {
return null
}

return flag
}
}
Loading

0 comments on commit 1bada83

Please sign in to comment.