-
Notifications
You must be signed in to change notification settings - Fork 9
PylonItemStackBuilder #325
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
134 changes: 134 additions & 0 deletions
134
...n-core/src/main/kotlin/io/github/pylonmc/pylon/core/item/builder/PylonItemStackBuilder.kt
This file contains hidden or 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,134 @@ | ||
| package io.github.pylonmc.pylon.core.item.builder | ||
|
|
||
| import io.github.pylonmc.pylon.core.config.Settings | ||
| import io.github.pylonmc.pylon.core.config.adapter.ConfigAdapter | ||
| import io.papermc.paper.datacomponent.DataComponentTypes | ||
| import io.papermc.paper.datacomponent.item.ItemAttributeModifiers | ||
| import io.papermc.paper.datacomponent.item.Tool | ||
| import io.papermc.paper.datacomponent.item.UseCooldown | ||
| import io.papermc.paper.datacomponent.item.Weapon | ||
| import io.papermc.paper.registry.keys.tags.BlockTypeTagKeys | ||
| import io.papermc.paper.registry.set.RegistryKeySet | ||
| import net.kyori.adventure.util.TriState | ||
| import org.bukkit.NamespacedKey | ||
| import org.bukkit.Registry | ||
| import org.bukkit.attribute.Attribute | ||
| import org.bukkit.attribute.AttributeModifier | ||
| import org.bukkit.block.BlockType | ||
| import org.bukkit.inventory.EquipmentSlotGroup | ||
| import org.bukkit.inventory.ItemStack | ||
|
|
||
| @Suppress("UnstableApiUsage") | ||
| class PylonItemStackBuilder : ItemStackBuilder { | ||
| private val itemKey: NamespacedKey | ||
|
|
||
| internal constructor(stack: ItemStack, itemKey: NamespacedKey) : super(stack) { | ||
| this.itemKey = itemKey | ||
| } | ||
|
|
||
| @JvmOverloads | ||
| fun helmet( | ||
| armor: Double = Settings.get(itemKey).getOrThrow("armor", ConfigAdapter.DOUBLE), | ||
| armorToughness: Double = Settings.get(itemKey).getOrThrow("armor-toughness", ConfigAdapter.DOUBLE) | ||
| ) = armor(EquipmentSlotGroup.HEAD, armor, armorToughness) | ||
|
|
||
| @JvmOverloads | ||
| fun chestPlate( | ||
| armor: Double = Settings.get(itemKey).getOrThrow("armor", ConfigAdapter.DOUBLE), | ||
| armorToughness: Double = Settings.get(itemKey).getOrThrow("armor-toughness", ConfigAdapter.DOUBLE) | ||
| ) = armor(EquipmentSlotGroup.CHEST, armor, armorToughness) | ||
|
|
||
| @JvmOverloads | ||
| fun leggings( | ||
| armor: Double = Settings.get(itemKey).getOrThrow("armor", ConfigAdapter.DOUBLE), | ||
| armorToughness: Double = Settings.get(itemKey).getOrThrow("armor-toughness", ConfigAdapter.DOUBLE) | ||
| ) = armor(EquipmentSlotGroup.LEGS, armor, armorToughness) | ||
|
|
||
| @JvmOverloads | ||
| fun boots( | ||
| armor: Double = Settings.get(itemKey).getOrThrow("armor", ConfigAdapter.DOUBLE), | ||
| armorToughness: Double = Settings.get(itemKey).getOrThrow("armor-toughness", ConfigAdapter.DOUBLE) | ||
| ) = armor(EquipmentSlotGroup.FEET, armor, armorToughness) | ||
|
|
||
| @JvmOverloads | ||
| fun armor( | ||
| slot: EquipmentSlotGroup, | ||
| armor: Double = Settings.get(itemKey).getOrThrow("armor", ConfigAdapter.DOUBLE), | ||
| armorToughness: Double = Settings.get(itemKey).getOrThrow("armor-toughness", ConfigAdapter.DOUBLE) | ||
| ) = apply { | ||
| editDataOrSet(DataComponentTypes.ATTRIBUTE_MODIFIERS) { modifiers -> | ||
| val copying = modifiers?.modifiers()?.filter { it.modifier().key.namespace != "minecraft" || !it.modifier().key.key.contains("armor.") } | ||
| ItemAttributeModifiers.itemAttributes().copy(copying) | ||
| .addModifier(Attribute.ARMOR, AttributeModifier(itemKey, armor, AttributeModifier.Operation.ADD_NUMBER, slot)) | ||
| .addModifier(Attribute.ARMOR_TOUGHNESS, AttributeModifier(itemKey, armorToughness, AttributeModifier.Operation.ADD_NUMBER, slot)) | ||
| .build() | ||
| } | ||
| } | ||
|
|
||
| @JvmOverloads | ||
| fun axe( | ||
| miningSpeed: Float = Settings.get(itemKey).getOrThrow("mining-speed", ConfigAdapter.FLOAT), | ||
| miningDurabilityDamage: Int = Settings.get(itemKey).getOrThrow("mining-durability-damage", ConfigAdapter.INT) | ||
| ) = tool(Registry.BLOCK.getTag(BlockTypeTagKeys.MINEABLE_AXE), miningSpeed, miningDurabilityDamage) | ||
|
|
||
| @JvmOverloads | ||
| fun pickaxe( | ||
| miningSpeed: Float = Settings.get(itemKey).getOrThrow("mining-speed", ConfigAdapter.FLOAT), | ||
| miningDurabilityDamage: Int = Settings.get(itemKey).getOrThrow("mining-durability-damage", ConfigAdapter.INT) | ||
| ) = tool(Registry.BLOCK.getTag(BlockTypeTagKeys.MINEABLE_PICKAXE), miningSpeed, miningDurabilityDamage) | ||
|
|
||
| @JvmOverloads | ||
| fun shovel( | ||
| miningSpeed: Float = Settings.get(itemKey).getOrThrow("mining-speed", ConfigAdapter.FLOAT), | ||
| miningDurabilityDamage: Int = Settings.get(itemKey).getOrThrow("mining-durability-damage", ConfigAdapter.INT) | ||
| ) = tool(Registry.BLOCK.getTag(BlockTypeTagKeys.MINEABLE_SHOVEL), miningSpeed, miningDurabilityDamage) | ||
|
|
||
| @JvmOverloads | ||
| fun hoe( | ||
| miningSpeed: Float = Settings.get(itemKey).getOrThrow("mining-speed", ConfigAdapter.FLOAT), | ||
| miningDurabilityDamage: Int = Settings.get(itemKey).getOrThrow("mining-durability-damage", ConfigAdapter.INT) | ||
| ) = tool(Registry.BLOCK.getTag(BlockTypeTagKeys.MINEABLE_HOE), miningSpeed, miningDurabilityDamage) | ||
|
|
||
| @JvmOverloads | ||
| fun tool( | ||
| blocks: RegistryKeySet<BlockType>, | ||
| miningSpeed: Float = Settings.get(itemKey).getOrThrow("mining-speed", ConfigAdapter.FLOAT), | ||
| miningDurabilityDamage: Int = Settings.get(itemKey).getOrThrow("mining-durability-damage", ConfigAdapter.INT) | ||
| ) = apply { | ||
| set(DataComponentTypes.TOOL, Tool.tool() | ||
| .defaultMiningSpeed(miningSpeed) | ||
| .damagePerBlock(miningDurabilityDamage) | ||
| .addRule(Tool.rule(blocks, miningSpeed, TriState.TRUE))) | ||
| } | ||
|
|
||
| fun noTool() = unset(DataComponentTypes.TOOL) as PylonItemStackBuilder | ||
|
|
||
| @JvmOverloads | ||
| fun weapon( | ||
| disablesShield: Boolean = false, | ||
| attackDamage: Double = Settings.get(itemKey).getOrThrow("attack-damage", ConfigAdapter.DOUBLE), | ||
| attackSpeed: Double = Settings.get(itemKey).getOrThrow("attack-speed", ConfigAdapter.DOUBLE), | ||
| attackDurabilityDamage: Int = Settings.get(itemKey).getOrThrow("attack-durability-damage", ConfigAdapter.INT), | ||
| disableShieldSeconds: Float? = null | ||
| ) = apply { | ||
| addAttributeModifier(Attribute.ATTACK_DAMAGE, AttributeModifier(baseAttackDamage, -1.0 + attackDamage, AttributeModifier.Operation.ADD_NUMBER, EquipmentSlotGroup.MAINHAND)) | ||
| addAttributeModifier(Attribute.ATTACK_SPEED, AttributeModifier(baseAttackSpeed, -4.0 + attackSpeed, AttributeModifier.Operation.ADD_NUMBER, EquipmentSlotGroup.MAINHAND)) | ||
| set(DataComponentTypes.WEAPON, Weapon.weapon() | ||
| .itemDamagePerAttack(attackDurabilityDamage) | ||
| .disableBlockingForSeconds(if (disablesShield) disableShieldSeconds ?: Settings.get(itemKey).getOrThrow("disable-shield-seconds", ConfigAdapter.FLOAT) else 0f)) | ||
| } | ||
|
|
||
| @JvmOverloads | ||
| fun attackKnockback(knockback: Double = Settings.get(itemKey).getOrThrow("attack-knockback", ConfigAdapter.DOUBLE)) = | ||
| addAttributeModifier(Attribute.ATTACK_KNOCKBACK, AttributeModifier(itemKey, knockback, AttributeModifier.Operation.ADD_NUMBER, EquipmentSlotGroup.MAINHAND)) as PylonItemStackBuilder | ||
|
|
||
| @JvmOverloads | ||
| fun durability( | ||
| durability: Int = Settings.get(itemKey).getOrThrow("durability", ConfigAdapter.INT) | ||
| ) = set(DataComponentTypes.MAX_DAMAGE, durability) as PylonItemStackBuilder | ||
|
|
||
| @JvmOverloads | ||
| fun useCooldown( | ||
| cooldownTicks: Int = Settings.get(itemKey).getOrThrow("cooldown-ticks", ConfigAdapter.INT) | ||
| ) = set(DataComponentTypes.USE_COOLDOWN, UseCooldown.useCooldown(cooldownTicks / 20.0f).cooldownGroup(itemKey)) as PylonItemStackBuilder | ||
| } | ||
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ToolItemStackBuilder would be better, no reason to call it Pylon
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It does way more than just tools, requires a pylon item key, and uses pylon item settings, why would it not be pylon?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Tbh this does feel more like it should be either part of the ItemStackBuilder, or possibly a set of utility methods that create the components. I can see there potentially being a lot of confusion over 'why are there 2 item stack builders' and 'why is the PylonItemStackBuilder not needed to create pylon items'. Is there any reason these can't just be methods on the original ItemStackBuilder?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I initially did that but then thought it may bring confusion, if someone is making an item stack and wants to give it a weapon, or custom durability or something, and doesn't read the docs, it would throw an exception because there are no settings for a non pylon item.
I included the overloads for people that don't want to have values configurable but I suppose it also would allow for non-pylon items to use the methods assuming they provide a value for every field and don't use any defaults.
I'd also then have to add a field to ItemStackBuilder for a pylon item key. Which isn't necessarily good or bad it's just a change.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What if we move all the Pylon-specific stuff (like
pylonItem) intoPylonItemStackBuilderand have the normal one be just for vanilla/ui?