-
-
Notifications
You must be signed in to change notification settings - Fork 96
Feat: Adding some Item Components #381
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
Merged
Merged
Changes from 6 commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
6e7d52c
added some item components
Chaosgh 88893db
added some item components
Chaosgh 22b5a40
fixed default mismatch
Chaosgh 9ef42b2
refactor ItemComponents Add InteractionContext for parity and added b…
Chaosgh 0add039
Better handeling of invalide namespaces in ItemPDCComponent (for exam…
Chaosgh c5dd35e
Merge branch 'develop' into develop
Chaosgh 57df20e
Merge branch 'develop' into develop
Chaosgh feedee3
Small fix in `ItemNameComponent`: `effectiveName()` is only available…
Chaosgh 5356714
Merge remote-tracking branch 'origin/develop' into develop
Chaosgh dbbb85f
Merge pull request #1 from gabber235/develop
Chaosgh 8bc8515
- Data types were added to 'pdc' and 'CustomModelDataType'.
Chaosgh 99077b6
- Data types were added to 'pdc' and 'CustomModelDataType'.
Chaosgh 8478c5c
small fixes to stay compatible with the typewriter panel
Chaosgh aa3f821
Merge pull request #2 from Chaosgh/components
Chaosgh 5c88045
- different unsupported message to make it clearer
Chaosgh 9518b06
small cleanups
Chaosgh a500a66
Merge branch 'develop' into develop
Chaosgh 6e2eef9
small cleanups like gabber told me
Chaosgh 0f61b85
small cleanups like gabber told me
Chaosgh 47d31fc
Merge branch 'develop' into develop
Chaosgh 38192a5
Merge remote-tracking branch 'origin/develop' into develop
Chaosgh e330496
Merge branch 'develop' into develop
Chaosgh dc1fef0
Merge branch 'develop' into develop
Chaosgh 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
47 changes: 47 additions & 0 deletions
47
...otlin/com/typewritermc/engine/paper/utils/item/components/ItemCustomModelDataComponent.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,47 @@ | ||
package com.typewritermc.engine.paper.utils.item.components | ||
|
||
import com.typewritermc.core.books.pages.Colors | ||
import com.typewritermc.core.extension.annotations.AlgebraicTypeInfo | ||
import com.typewritermc.core.extension.annotations.Default | ||
import com.typewritermc.core.extension.annotations.InnerMin | ||
import com.typewritermc.core.extension.annotations.Min | ||
import com.typewritermc.core.interaction.InteractionContext | ||
import com.typewritermc.engine.paper.entry.entries.ConstVar | ||
import com.typewritermc.engine.paper.entry.entries.Var | ||
import com.typewritermc.engine.paper.entry.entries.get | ||
import org.bukkit.entity.Player | ||
import org.bukkit.inventory.ItemStack | ||
|
||
@Suppress("UnstableApiUsage") | ||
@AlgebraicTypeInfo("custom_model_data", Colors.GREEN, "fa6-solid:shapes") | ||
class ItemCustomModelDataComponent( | ||
@InnerMin(Min(0)) | ||
@Default("0") | ||
|
||
val customModelData: Var<Int> = ConstVar(0), | ||
) : ItemComponent { | ||
override fun apply(player: Player?, interactionContext: InteractionContext?, item: ItemStack) { | ||
item.editMeta { meta -> | ||
val modelData = customModelData.get(player, interactionContext) ?: return@editMeta | ||
val expectedFloats = if (modelData == 0) emptyList() else listOf(modelData.toFloat()) | ||
val component = meta.customModelDataComponent | ||
if (component.floats == expectedFloats) return@editMeta | ||
component.floats = expectedFloats | ||
meta.setCustomModelDataComponent(component) | ||
} | ||
gabber235 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
} | ||
|
||
override fun matches(player: Player?, interactionContext: InteractionContext?, item: ItemStack): Boolean { | ||
val expectedModelData = customModelData.get(player, interactionContext) ?: return false | ||
val actualComponent = item.itemMeta?.customModelDataComponent | ||
val actualFloats = actualComponent?.floats ?: emptyList() | ||
|
||
val expectedFloats = if (expectedModelData == 0) { | ||
emptyList() | ||
} else { | ||
listOf(expectedModelData.toFloat()) | ||
} | ||
|
||
return actualFloats == expectedFloats | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
...otlin/com/typewritermc/engine/paper/utils/item/components/ItemJukeboxPlayableComponent.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,43 @@ | ||
package com.typewritermc.engine.paper.utils.item.components | ||
|
||
import com.typewritermc.core.books.pages.Colors | ||
import com.typewritermc.core.extension.annotations.AlgebraicTypeInfo | ||
import com.typewritermc.core.interaction.InteractionContext | ||
import com.typewritermc.engine.paper.entry.entries.ConstVar | ||
import com.typewritermc.engine.paper.entry.entries.Var | ||
import com.typewritermc.engine.paper.entry.entries.get | ||
import com.typewritermc.engine.paper.utils.Sound | ||
import org.bukkit.entity.Player | ||
import org.bukkit.inventory.ItemStack | ||
|
||
@Suppress("UnstableApiUsage") | ||
@AlgebraicTypeInfo("jukebox_playable", Colors.RED, "fa6-solid:music") | ||
class ItemJukeboxPlayableComponent( | ||
val sound: Var<Sound> = ConstVar(Sound.EMPTY) | ||
) : ItemComponent { | ||
override fun apply(player: Player?, interactionContext: InteractionContext?, item: ItemStack) { | ||
item.editMeta { meta -> | ||
val resolvedSound = sound.get(player, interactionContext) | ||
if (resolvedSound == null || resolvedSound == Sound.EMPTY) { | ||
meta.setJukeboxPlayable(null) | ||
return@editMeta | ||
} | ||
|
||
val soundKey = resolvedSound.soundId.namespacedKey ?: return@editMeta | ||
val jukeboxComponent = meta.jukeboxPlayable | ||
jukeboxComponent.songKey = soundKey | ||
meta.setJukeboxPlayable(jukeboxComponent) | ||
} | ||
} | ||
|
||
override fun matches(player: Player?, interactionContext: InteractionContext?, item: ItemStack): Boolean { | ||
val expectedSound = sound.get(player, interactionContext) | ||
val actualComponent = item.itemMeta?.jukeboxPlayable | ||
|
||
if (expectedSound == null || expectedSound == Sound.EMPTY) { | ||
return actualComponent == null | ||
} | ||
|
||
return actualComponent?.songKey == expectedSound.soundId.namespacedKey | ||
} | ||
} |
63 changes: 63 additions & 0 deletions
63
...r/src/main/kotlin/com/typewritermc/engine/paper/utils/item/components/ItemPDCComponent.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,63 @@ | ||
package com.typewritermc.engine.paper.utils.item.components | ||
|
||
import com.typewritermc.core.books.pages.Colors | ||
import com.typewritermc.core.extension.annotations.AlgebraicTypeInfo | ||
import com.typewritermc.core.extension.annotations.Default | ||
import com.typewritermc.core.interaction.InteractionContext | ||
import com.typewritermc.engine.paper.entry.entries.ConstVar | ||
import com.typewritermc.engine.paper.entry.entries.Var | ||
import com.typewritermc.engine.paper.entry.entries.get | ||
import org.bukkit.NamespacedKey | ||
import org.bukkit.entity.Player | ||
import org.bukkit.inventory.ItemStack | ||
import org.bukkit.persistence.PersistentDataType | ||
|
||
@AlgebraicTypeInfo("persistent_data_container", Colors.PURPLE, "fa6-solid:database") | ||
class ItemPDCComponent( | ||
@Default("\"typewriter\"") | ||
val namespace: Var<String> = ConstVar("typewriter"), | ||
@Default("\"custom_data\"") | ||
val key: Var<String> = ConstVar("custom_data"), | ||
@Default("\"\"") | ||
val value: Var<String> = ConstVar(""), | ||
) : ItemComponent { | ||
|
||
override fun apply(player: Player?, interactionContext: InteractionContext?, item: ItemStack) { | ||
item.editMeta { meta -> | ||
val ns = namespace.get(player, interactionContext)?.trim()?.takeIf { it.isNotEmpty() } ?: return@editMeta | ||
val k = key.get(player, interactionContext)?.trim()?.takeIf { it.isNotEmpty() } ?: return@editMeta | ||
val v = value.get(player, interactionContext)?.trim() ?: return@editMeta | ||
|
||
val namespacedKey = try { | ||
NamespacedKey(ns, k) | ||
} catch (_: IllegalArgumentException) { | ||
return@editMeta | ||
} | ||
|
||
if (v.isBlank()) { | ||
meta.persistentDataContainer.remove(namespacedKey) | ||
} else { | ||
meta.persistentDataContainer.set(namespacedKey, PersistentDataType.STRING, v) | ||
} | ||
} | ||
} | ||
|
||
override fun matches(player: Player?, interactionContext: InteractionContext?, item: ItemStack): Boolean { | ||
val ns = namespace.get(player, interactionContext)?.trim()?.takeIf { it.isNotEmpty() } ?: return false | ||
val k = key.get(player, interactionContext)?.trim()?.takeIf { it.isNotEmpty() } ?: return false | ||
val expected = value.get(player, interactionContext)?.trim() ?: return false | ||
|
||
val namespacedKey = try { | ||
NamespacedKey(ns, k) | ||
} catch (_: IllegalArgumentException) { | ||
return false | ||
} | ||
val actualValue = item.itemMeta?.persistentDataContainer?.get(namespacedKey, PersistentDataType.STRING) | ||
|
||
return if (expected.isBlank()) { | ||
actualValue.isNullOrBlank() | ||
} else { | ||
actualValue == expected | ||
} | ||
} | ||
} | ||
gabber235 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
39 changes: 39 additions & 0 deletions
39
...n/kotlin/com/typewritermc/engine/paper/utils/item/components/ItemTooltipStyleComponent.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,39 @@ | ||
package com.typewritermc.engine.paper.utils.item.components | ||
|
||
import com.typewritermc.core.books.pages.Colors | ||
import com.typewritermc.core.extension.annotations.AlgebraicTypeInfo | ||
import com.typewritermc.core.extension.annotations.Default | ||
import com.typewritermc.core.interaction.InteractionContext | ||
import com.typewritermc.engine.paper.entry.entries.ConstVar | ||
import com.typewritermc.engine.paper.entry.entries.Var | ||
import com.typewritermc.engine.paper.entry.entries.get | ||
import org.bukkit.NamespacedKey | ||
import org.bukkit.entity.Player | ||
import org.bukkit.inventory.ItemStack | ||
|
||
@AlgebraicTypeInfo("tooltip_style", Colors.CYAN, "fa6-solid:palette") | ||
class ItemTooltipStyleComponent( | ||
@Default("\"minecraft\"") | ||
val namespace: Var<String> = ConstVar("minecraft"), | ||
@Default("\"default\"") | ||
val key: Var<String> = ConstVar("default"), | ||
) : ItemComponent { | ||
override fun apply(player: Player?, interactionContext: InteractionContext?, item: ItemStack) { | ||
item.editMeta { meta -> | ||
val namespaceValue = namespace.get(player, interactionContext) ?: return@editMeta | ||
val keyValue = key.get(player, interactionContext) ?: return@editMeta | ||
val namespacedKey = NamespacedKey(namespaceValue, keyValue) | ||
if (meta.tooltipStyle == namespacedKey) return@editMeta | ||
meta.tooltipStyle = namespacedKey | ||
} | ||
} | ||
|
||
|
||
override fun matches(player: Player?, interactionContext: InteractionContext?, item: ItemStack): Boolean { | ||
val expectedNamespace = namespace.get(player, interactionContext) ?: return false | ||
val expectedKey = key.get(player, interactionContext) ?: return false | ||
val actualStyle = item.itemMeta?.tooltipStyle | ||
val expectedNamespacedKey = NamespacedKey(expectedNamespace, expectedKey) | ||
return actualStyle == expectedNamespacedKey | ||
Chaosgh marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
} | ||
} |
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.
Uh oh!
There was an error while loading. Please reload this page.