Implemented ItemMetaProxy with an ability to set/get trim from the 1.20 armor meta
Added an ArgParserTrim to add trims to armor via item lookup strings
This commit is contained in:
@@ -29,6 +29,7 @@ import com.willfp.eco.core.packet.Packet;
|
||||
import com.willfp.eco.core.placeholder.context.PlaceholderContext;
|
||||
import com.willfp.eco.core.proxy.ProxyFactory;
|
||||
import com.willfp.eco.core.scheduling.Scheduler;
|
||||
import com.willfp.eco.core.tuples.Pair;
|
||||
import net.kyori.adventure.platform.bukkit.BukkitAudiences;
|
||||
import org.apache.commons.lang.Validate;
|
||||
import org.bukkit.Location;
|
||||
@@ -38,6 +39,7 @@ import org.bukkit.entity.Entity;
|
||||
import org.bukkit.entity.Mob;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.inventory.meta.ItemMeta;
|
||||
import org.bukkit.inventory.meta.SkullMeta;
|
||||
import org.bukkit.persistence.PersistentDataContainer;
|
||||
import org.jetbrains.annotations.ApiStatus;
|
||||
@@ -587,6 +589,25 @@ public interface Eco {
|
||||
@NotNull String args,
|
||||
@NotNull PlaceholderContext context);
|
||||
|
||||
/**
|
||||
* Set the armor trim for the given ItemMeta
|
||||
*
|
||||
* @param meta The given ItemMeta.
|
||||
* @param trim A pair of strings, where the first string represents the trim material name, and the second
|
||||
* one represents the trim pattern name.
|
||||
*/
|
||||
void setArmorTrim(@NotNull ItemMeta meta, @NotNull Pair<String, String> trim);
|
||||
|
||||
/**
|
||||
* Get the armor trim for the given ItemMeta
|
||||
*
|
||||
* @param meta The given ItemMeta.
|
||||
* @return A pair of strings, where the first string represents the trim material name, and the second
|
||||
* one represents the trim pattern name.
|
||||
*/
|
||||
@Nullable
|
||||
Pair<String, String> getArmorTrim(@NotNull ItemMeta meta);
|
||||
|
||||
/**
|
||||
* Get the instance of eco; the bridge between the api frontend and the implementation backend.
|
||||
*
|
||||
|
||||
39
eco-api/src/main/java/com/willfp/eco/util/ItemMetaUtils.java
Normal file
39
eco-api/src/main/java/com/willfp/eco/util/ItemMetaUtils.java
Normal file
@@ -0,0 +1,39 @@
|
||||
package com.willfp.eco.util;
|
||||
|
||||
import com.willfp.eco.core.Eco;
|
||||
import com.willfp.eco.core.tuples.Pair;
|
||||
import org.bukkit.inventory.meta.ItemMeta;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
/**
|
||||
* Utilities / API methods for the server.
|
||||
*/
|
||||
public final class ItemMetaUtils {
|
||||
/**
|
||||
* Set the armor trim for the given ItemMeta
|
||||
*
|
||||
* @param meta The given ItemMeta.
|
||||
* @param trim A pair of strings, where the first string represents the trim material name, and the second
|
||||
* one represents the trim pattern name.
|
||||
*/
|
||||
public static void setArmorTrim(@NotNull ItemMeta meta, @NotNull Pair<String, String> trim) {
|
||||
Eco.get().setArmorTrim(meta, trim);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the armor trim for the given ItemMeta
|
||||
*
|
||||
* @param meta The given ItemMeta.
|
||||
* @return A pair of strings, where the first string represents the trim material name, and the second
|
||||
* one represents the trim pattern name.
|
||||
*/
|
||||
@Nullable
|
||||
public static Pair<String, String> getArmorTrim(@NotNull ItemMeta meta) {
|
||||
return Eco.get().getArmorTrim(meta);
|
||||
}
|
||||
|
||||
private ItemMetaUtils() {
|
||||
throw new UnsupportedOperationException("This is a utility class and cannot be instantiated");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
package com.willfp.eco.internal.items
|
||||
|
||||
import com.willfp.eco.core.items.args.LookupArgParser
|
||||
import com.willfp.eco.core.tuples.Pair
|
||||
import com.willfp.eco.util.ItemMetaUtils
|
||||
import org.bukkit.inventory.ItemStack
|
||||
import org.bukkit.inventory.meta.ItemMeta
|
||||
import java.util.function.Predicate
|
||||
|
||||
object ArgParserTrim : LookupArgParser {
|
||||
override fun parseArguments(args: Array<out String>, meta: ItemMeta): Predicate<ItemStack>? {
|
||||
var material: String? = null
|
||||
var pattern: String? = null
|
||||
|
||||
for (arg in args) {
|
||||
val argSplit = arg.split(":")
|
||||
if (!argSplit[0].equals("trim", ignoreCase = true)) {
|
||||
continue
|
||||
}
|
||||
if (argSplit.size < 3) {
|
||||
continue
|
||||
}
|
||||
material = argSplit[1]
|
||||
pattern = argSplit[2]
|
||||
}
|
||||
|
||||
material ?: pattern ?: return null
|
||||
|
||||
ItemMetaUtils.setArmorTrim(meta, Pair(material, pattern))
|
||||
|
||||
return Predicate {
|
||||
val testMeta = it.itemMeta ?: return@Predicate false
|
||||
val trim = ItemMetaUtils.getArmorTrim(testMeta) ?: return@Predicate false
|
||||
return@Predicate trim.first!!.equals(material, true)
|
||||
&& trim.second!!.equals(pattern, true)
|
||||
}
|
||||
}
|
||||
|
||||
override fun serializeBack(meta: ItemMeta): String? {
|
||||
val trim = ItemMetaUtils.getArmorTrim(meta) ?: return null
|
||||
|
||||
return "trim:${trim.first}:${trim.second}"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package com.willfp.eco.internal.spigot.proxy.v1_17_R1
|
||||
|
||||
import com.willfp.eco.core.tuples.Pair
|
||||
import com.willfp.eco.internal.spigot.proxy.ItemMetaProxy
|
||||
import org.bukkit.inventory.meta.ItemMeta
|
||||
|
||||
class ItemMeta : ItemMetaProxy {
|
||||
override fun getTrim(meta: ItemMeta): Pair<String, String>? {
|
||||
return null
|
||||
}
|
||||
|
||||
override fun setTrim(meta: ItemMeta, trim: Pair<String, String>) {}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package com.willfp.eco.internal.spigot.proxy.v1_18_R1
|
||||
|
||||
import com.willfp.eco.core.tuples.Pair
|
||||
import com.willfp.eco.internal.spigot.proxy.ItemMetaProxy
|
||||
import org.bukkit.inventory.meta.ItemMeta
|
||||
|
||||
class ItemMeta : ItemMetaProxy {
|
||||
override fun getTrim(meta: ItemMeta): Pair<String, String>? {
|
||||
return null
|
||||
}
|
||||
|
||||
override fun setTrim(meta: ItemMeta, trim: Pair<String, String>) {}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package com.willfp.eco.internal.spigot.proxy.v1_18_R2
|
||||
|
||||
import com.willfp.eco.core.tuples.Pair
|
||||
import com.willfp.eco.internal.spigot.proxy.ItemMetaProxy
|
||||
import org.bukkit.inventory.meta.ItemMeta
|
||||
|
||||
class ItemMeta : ItemMetaProxy {
|
||||
override fun getTrim(meta: ItemMeta): Pair<String, String>? {
|
||||
return null
|
||||
}
|
||||
|
||||
override fun setTrim(meta: ItemMeta, trim: Pair<String, String>) {}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package com.willfp.eco.internal.spigot.proxy.v1_19_R1
|
||||
|
||||
import com.willfp.eco.core.tuples.Pair
|
||||
import com.willfp.eco.internal.spigot.proxy.ItemMetaProxy
|
||||
import org.bukkit.inventory.meta.ItemMeta
|
||||
|
||||
class ItemMeta : ItemMetaProxy {
|
||||
override fun getTrim(meta: ItemMeta): Pair<String, String>? {
|
||||
return null
|
||||
}
|
||||
|
||||
override fun setTrim(meta: ItemMeta, trim: Pair<String, String>) {}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package com.willfp.eco.internal.spigot.proxy.v1_19_R2
|
||||
|
||||
import com.willfp.eco.core.tuples.Pair
|
||||
import com.willfp.eco.internal.spigot.proxy.ItemMetaProxy
|
||||
import org.bukkit.inventory.meta.ItemMeta
|
||||
|
||||
class ItemMeta : ItemMetaProxy {
|
||||
override fun getTrim(meta: ItemMeta): Pair<String, String>? {
|
||||
return null
|
||||
}
|
||||
|
||||
override fun setTrim(meta: ItemMeta, trim: Pair<String, String>) {}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package com.willfp.eco.internal.spigot.proxy.v1_19_R3
|
||||
|
||||
import com.willfp.eco.core.tuples.Pair
|
||||
import com.willfp.eco.internal.spigot.proxy.ItemMetaProxy
|
||||
import org.bukkit.inventory.meta.ItemMeta
|
||||
|
||||
class ItemMeta : ItemMetaProxy {
|
||||
override fun getTrim(meta: ItemMeta): Pair<String, String>? {
|
||||
return null
|
||||
}
|
||||
|
||||
override fun setTrim(meta: ItemMeta, trim: Pair<String, String>) {}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
package com.willfp.eco.internal.spigot.proxy.v1_20_R1
|
||||
|
||||
import com.willfp.eco.core.tuples.Pair
|
||||
import com.willfp.eco.internal.spigot.proxy.ItemMetaProxy
|
||||
import org.bukkit.NamespacedKey
|
||||
import org.bukkit.Registry
|
||||
import org.bukkit.inventory.meta.ArmorMeta
|
||||
import org.bukkit.inventory.meta.ItemMeta
|
||||
import org.bukkit.inventory.meta.trim.ArmorTrim
|
||||
|
||||
class ItemMeta : ItemMetaProxy {
|
||||
override fun getTrim(meta: ItemMeta): Pair<String, String>? {
|
||||
if (meta !is ArmorMeta) return null
|
||||
val trim = meta.trim ?: return null
|
||||
return Pair(
|
||||
trim.material.key.key,
|
||||
trim.pattern.key.key
|
||||
)
|
||||
}
|
||||
|
||||
override fun setTrim(meta: ItemMeta, trim: Pair<String, String>) {
|
||||
if (meta !is ArmorMeta) return
|
||||
val material = Registry.TRIM_MATERIAL.get(
|
||||
NamespacedKey.minecraft(trim.first!!.lowercase())
|
||||
) ?: return
|
||||
|
||||
val pattern = Registry.TRIM_PATTERN.get(
|
||||
NamespacedKey.minecraft(trim.second!!.lowercase())
|
||||
) ?: return
|
||||
|
||||
meta.trim = ArmorTrim(
|
||||
material,
|
||||
pattern
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -15,6 +15,7 @@ import com.willfp.eco.core.gui.slot.functional.SlotProvider
|
||||
import com.willfp.eco.core.items.Items
|
||||
import com.willfp.eco.core.packet.Packet
|
||||
import com.willfp.eco.core.placeholder.context.PlaceholderContext
|
||||
import com.willfp.eco.core.tuples.Pair
|
||||
import com.willfp.eco.internal.EcoPropsParser
|
||||
import com.willfp.eco.internal.command.EcoPluginCommand
|
||||
import com.willfp.eco.internal.command.EcoSubcommand
|
||||
@@ -49,17 +50,7 @@ import com.willfp.eco.internal.spigot.integrations.bstats.MetricHandler
|
||||
import com.willfp.eco.internal.spigot.math.DelegatedExpressionHandler
|
||||
import com.willfp.eco.internal.spigot.math.ImmediatePlaceholderTranslationExpressionHandler
|
||||
import com.willfp.eco.internal.spigot.math.LazyPlaceholderTranslationExpressionHandler
|
||||
import com.willfp.eco.internal.spigot.proxy.BukkitCommandsProxy
|
||||
import com.willfp.eco.internal.spigot.proxy.CommonsInitializerProxy
|
||||
import com.willfp.eco.internal.spigot.proxy.DummyEntityFactoryProxy
|
||||
import com.willfp.eco.internal.spigot.proxy.EntityControllerFactoryProxy
|
||||
import com.willfp.eco.internal.spigot.proxy.ExtendedPersistentDataContainerFactoryProxy
|
||||
import com.willfp.eco.internal.spigot.proxy.FastItemStackFactoryProxy
|
||||
import com.willfp.eco.internal.spigot.proxy.MiniMessageTranslatorProxy
|
||||
import com.willfp.eco.internal.spigot.proxy.PacketHandlerProxy
|
||||
import com.willfp.eco.internal.spigot.proxy.SNBTConverterProxy
|
||||
import com.willfp.eco.internal.spigot.proxy.SkullProxy
|
||||
import com.willfp.eco.internal.spigot.proxy.TPSProxy
|
||||
import com.willfp.eco.internal.spigot.proxy.*
|
||||
import org.bukkit.Location
|
||||
import org.bukkit.NamespacedKey
|
||||
import org.bukkit.configuration.ConfigurationSection
|
||||
@@ -67,6 +58,7 @@ import org.bukkit.entity.Entity
|
||||
import org.bukkit.entity.Mob
|
||||
import org.bukkit.entity.Player
|
||||
import org.bukkit.inventory.ItemStack
|
||||
import org.bukkit.inventory.meta.ItemMeta
|
||||
import org.bukkit.inventory.meta.SkullMeta
|
||||
import org.bukkit.persistence.PersistentDataContainer
|
||||
import java.net.URLClassLoader
|
||||
@@ -346,4 +338,12 @@ class EcoImpl : EcoSpigotPlugin(), Eco {
|
||||
|
||||
override fun getPlaceholderValue(plugin: EcoPlugin?, args: String, context: PlaceholderContext) =
|
||||
placeholderParser.getPlaceholderResult(plugin, args, context)
|
||||
|
||||
override fun setArmorTrim(meta: ItemMeta, trim: Pair<String, String>) {
|
||||
getProxy(ItemMetaProxy::class.java).setTrim(meta, trim)
|
||||
}
|
||||
|
||||
override fun getArmorTrim(meta: ItemMeta): Pair<String, String>? {
|
||||
return getProxy(ItemMetaProxy::class.java).getTrim(meta)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -41,14 +41,7 @@ import com.willfp.eco.internal.entities.EntityArgParserSilent
|
||||
import com.willfp.eco.internal.entities.EntityArgParserSize
|
||||
import com.willfp.eco.internal.entities.EntityArgParserSpawnReinforcements
|
||||
import com.willfp.eco.internal.entities.EntityArgParserSpeed
|
||||
import com.willfp.eco.internal.items.ArgParserColor
|
||||
import com.willfp.eco.internal.items.ArgParserCustomModelData
|
||||
import com.willfp.eco.internal.items.ArgParserEnchantment
|
||||
import com.willfp.eco.internal.items.ArgParserFlag
|
||||
import com.willfp.eco.internal.items.ArgParserHead
|
||||
import com.willfp.eco.internal.items.ArgParserName
|
||||
import com.willfp.eco.internal.items.ArgParserTexture
|
||||
import com.willfp.eco.internal.items.ArgParserUnbreakable
|
||||
import com.willfp.eco.internal.items.*
|
||||
import com.willfp.eco.internal.lookup.SegmentParserGroup
|
||||
import com.willfp.eco.internal.lookup.SegmentParserUseIfPresent
|
||||
import com.willfp.eco.internal.particle.ParticleFactoryRGB
|
||||
@@ -146,6 +139,7 @@ abstract class EcoSpigotPlugin : EcoPlugin() {
|
||||
init {
|
||||
Items.registerArgParser(ArgParserEnchantment)
|
||||
Items.registerArgParser(ArgParserColor)
|
||||
Items.registerArgParser(ArgParserTrim)
|
||||
Items.registerArgParser(ArgParserTexture)
|
||||
Items.registerArgParser(ArgParserCustomModelData)
|
||||
Items.registerArgParser(ArgParserFlag)
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
package com.willfp.eco.internal.spigot.proxy
|
||||
|
||||
import org.bukkit.inventory.meta.ItemMeta
|
||||
import com.willfp.eco.core.tuples.Pair
|
||||
|
||||
interface ItemMetaProxy {
|
||||
fun getTrim(meta: ItemMeta): Pair<String, String>?
|
||||
fun setTrim(meta: ItemMeta, trim: Pair<String, String>)
|
||||
}
|
||||
Reference in New Issue
Block a user