-
-
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 17 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
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
26 changes: 26 additions & 0 deletions
26
...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,26 @@ | ||
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.item.components.customModelDataTypes.CustomModelDataType | ||
import com.typewritermc.engine.paper.utils.item.components.customModelDataTypes.LegacyCustomModelData | ||
import org.bukkit.entity.Player | ||
import org.bukkit.inventory.ItemStack | ||
|
||
@AlgebraicTypeInfo("custom_model_data", Colors.GREEN, "fa6-solid:shapes") | ||
class ItemCustomModelDataComponent( | ||
val customModelData: Var<CustomModelDataType> = ConstVar(LegacyCustomModelData(0)), | ||
) : ItemComponent { | ||
override fun apply(player: Player?, interactionContext: InteractionContext?, item: ItemStack) { | ||
customModelData.get(player, interactionContext)?.apply(player, interactionContext, item) | ||
} | ||
|
||
override fun matches(player: Player?, interactionContext: InteractionContext?, item: ItemStack): Boolean { | ||
val modelData = customModelData.get(player, interactionContext) ?: return false | ||
return modelData.matches(player, interactionContext, item) | ||
} | ||
} |
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 | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
...src/main/kotlin/com/typewritermc/engine/paper/utils/item/components/ItemModelComponent.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.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("item_model", Colors.BLUE, "fa6-solid:layer-group") | ||
class ItemModelComponent( | ||
val modelKey: Var<String> = ConstVar("") | ||
) : ItemComponent { | ||
|
||
override fun apply(player: Player?, interactionContext: InteractionContext?, item: ItemStack) { | ||
item.editMeta { meta -> | ||
val raw = modelKey.get(player, interactionContext)?.trim().orEmpty() | ||
if (raw.isEmpty()) { | ||
meta.itemModel = null | ||
return@editMeta | ||
} | ||
|
||
val key = NamespacedKey.fromString(raw) | ||
if (key == null) { | ||
meta.itemModel = null | ||
return@editMeta | ||
} | ||
|
||
meta.itemModel = key | ||
} | ||
} | ||
|
||
override fun matches(player: Player?, interactionContext: InteractionContext?, item: ItemStack): Boolean { | ||
val expectedRaw = modelKey.get(player, interactionContext)?.trim().orEmpty() | ||
val actual = item.itemMeta?.itemModel | ||
|
||
if (expectedRaw.isEmpty()) { | ||
return actual == null | ||
} | ||
|
||
val expected = NamespacedKey.fromString(expectedRaw) ?: return actual == null | ||
return actual == expected | ||
} | ||
} | ||
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
38 changes: 38 additions & 0 deletions
38
...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,38 @@ | ||
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.item.components.pdcTypes.PdcDataType | ||
import com.typewritermc.engine.paper.utils.item.components.pdcTypes.StringPdcData | ||
import org.bukkit.NamespacedKey | ||
import org.bukkit.entity.Player | ||
import org.bukkit.inventory.ItemStack | ||
|
||
@AlgebraicTypeInfo("persistent_data_container", Colors.PURPLE, "fa6-solid:database") | ||
class ItemPDCComponent( | ||
|
||
Chaosgh marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
val dataKey: Var<String> = ConstVar("typewriter:custom_data"), | ||
val data: Var<PdcDataType> = ConstVar(StringPdcData("")), | ||
) : ItemComponent { | ||
|
||
override fun apply(player: Player?, interactionContext: InteractionContext?, item: ItemStack) { | ||
val raw = dataKey.get(player, interactionContext)?.trim().orEmpty() | ||
if (raw.isEmpty()) return | ||
|
||
val namespacedKey = NamespacedKey.fromString(raw) ?: return | ||
data.get(player, interactionContext)?.apply(player, interactionContext, item, namespacedKey) | ||
} | ||
|
||
override fun matches(player: Player?, interactionContext: InteractionContext?, item: ItemStack): Boolean { | ||
val raw = dataKey.get(player, interactionContext)?.trim().orEmpty() | ||
if (raw.isEmpty()) return false | ||
|
||
val namespacedKey = NamespacedKey.fromString(raw) ?: return false | ||
val pdcData = data.get(player, interactionContext) ?: return false | ||
return pdcData.matches(player, interactionContext, item, namespacedKey) | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
...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,50 @@ | ||
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 org.bukkit.NamespacedKey | ||
import org.bukkit.entity.Player | ||
import org.bukkit.inventory.ItemStack | ||
|
||
@AlgebraicTypeInfo("tooltip_style", Colors.CYAN, "fa6-solid:palette") | ||
class ItemTooltipStyleComponent( | ||
|
||
Chaosgh marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
val styleKey: Var<String> = ConstVar("minecraft:diamond"), | ||
) : ItemComponent { | ||
|
||
override fun apply(player: Player?, interactionContext: InteractionContext?, item: ItemStack) { | ||
item.editMeta { meta -> | ||
val raw = styleKey.get(player, interactionContext)?.trim().orEmpty() | ||
if (raw.isEmpty()) { | ||
meta.tooltipStyle = null | ||
return@editMeta | ||
} | ||
|
||
val key = NamespacedKey.fromString(raw) | ||
if (key == null) { | ||
meta.tooltipStyle = null | ||
return@editMeta | ||
} | ||
|
||
if (meta.tooltipStyle != key) { | ||
meta.tooltipStyle = key | ||
} | ||
} | ||
} | ||
|
||
override fun matches(player: Player?, interactionContext: InteractionContext?, item: ItemStack): Boolean { | ||
val raw = styleKey.get(player, interactionContext)?.trim().orEmpty() | ||
val actual = item.itemMeta?.tooltipStyle | ||
|
||
if (raw.isEmpty()) { | ||
return actual == null | ||
} | ||
|
||
val expected = NamespacedKey.fromString(raw) ?: return actual == null | ||
return actual == expected | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
...itermc/engine/paper/utils/item/components/customModelDataTypes/BooleansCustomModelData.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,38 @@ | ||
package com.typewritermc.engine.paper.utils.item.components.customModelDataTypes | ||
|
||
import com.github.retrooper.packetevents.manager.server.ServerVersion | ||
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.logger | ||
import com.typewritermc.engine.paper.utils.serverVersion | ||
import org.bukkit.entity.Player | ||
import org.bukkit.inventory.ItemStack | ||
|
||
@AlgebraicTypeInfo("boolean_array", Colors.GREEN, "fa6-solid:shapes") | ||
data class BooleansCustomModelData( | ||
val value: List<Boolean> = emptyList() | ||
|
||
) : CustomModelDataType { | ||
override fun apply(player: Player?, interactionContext: InteractionContext?, item: ItemStack) { | ||
if (!serverVersion.isNewerThanOrEquals(ServerVersion.V_1_21_4)) { | ||
logger.warning("${this::class.simpleName} is only supported in versions 1.21.4 and above") | ||
return | ||
} | ||
item.editMeta { meta -> | ||
val component = meta.customModelDataComponent | ||
if (component.flags == value) return@editMeta | ||
component.flags = value | ||
meta.setCustomModelDataComponent(component) | ||
} | ||
} | ||
|
||
override fun matches(player: Player?, interactionContext: InteractionContext?, item: ItemStack): Boolean { | ||
if (!serverVersion.isNewerThanOrEquals(ServerVersion.V_1_21_4)) { | ||
logger.warning("${this::class.simpleName} is only supported in versions 1.21.4 and above") | ||
return false | ||
} | ||
val meta = item.itemMeta ?: return false | ||
return meta.customModelDataComponent.flags == value | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
...writermc/engine/paper/utils/item/components/customModelDataTypes/ColorsCustomModelData.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,41 @@ | ||
package com.typewritermc.engine.paper.utils.item.components.customModelDataTypes | ||
|
||
import com.github.retrooper.packetevents.manager.server.ServerVersion | ||
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.logger | ||
import com.typewritermc.engine.paper.utils.Color | ||
import com.typewritermc.engine.paper.utils.serverVersion | ||
import org.bukkit.entity.Player | ||
import org.bukkit.inventory.ItemStack | ||
|
||
@AlgebraicTypeInfo("color_array", Colors.GREEN, "fa6-solid:shapes") | ||
data class ColorsCustomModelData( | ||
val value: List<Color> = emptyList() | ||
) : CustomModelDataType { | ||
override fun apply(player: Player?, interactionContext: InteractionContext?, item: ItemStack) { | ||
if (!serverVersion.isNewerThanOrEquals(ServerVersion.V_1_21_4)) { | ||
logger.warning("${this::class.simpleName} is only supported in versions 1.21.4 and above") | ||
return | ||
} | ||
item.editMeta { meta -> | ||
val component = meta.customModelDataComponent | ||
val bukkitColors = value.map { it.toBukkitColor() } | ||
|
||
if (component.colors == bukkitColors) return@editMeta | ||
component.colors = bukkitColors | ||
meta.setCustomModelDataComponent(component) | ||
} | ||
} | ||
|
||
override fun matches(player: Player?, interactionContext: InteractionContext?, item: ItemStack): Boolean { | ||
if (!serverVersion.isNewerThanOrEquals(ServerVersion.V_1_21_4)) { | ||
logger.warning("${this::class.simpleName} is only supported in versions 1.21.4 and above") | ||
return false | ||
} | ||
val meta = item.itemMeta ?: return false | ||
val bukkitColors = value.map { it.toBukkitColor() } | ||
return meta.customModelDataComponent.colors == bukkitColors | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
...pewritermc/engine/paper/utils/item/components/customModelDataTypes/CustomModelDataType.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,10 @@ | ||
package com.typewritermc.engine.paper.utils.item.components.customModelDataTypes | ||
|
||
import com.typewritermc.core.interaction.InteractionContext | ||
import org.bukkit.entity.Player | ||
import org.bukkit.inventory.ItemStack | ||
|
||
sealed interface CustomModelDataType { | ||
fun apply(player: Player?, interactionContext: InteractionContext?, item: ItemStack) | ||
fun matches(player: Player?, interactionContext: InteractionContext?, item: ItemStack): Boolean | ||
} |
37 changes: 37 additions & 0 deletions
37
...writermc/engine/paper/utils/item/components/customModelDataTypes/FloatsCustomModelData.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,37 @@ | ||
package com.typewritermc.engine.paper.utils.item.components.customModelDataTypes | ||
|
||
import com.github.retrooper.packetevents.manager.server.ServerVersion | ||
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.logger | ||
import com.typewritermc.engine.paper.utils.serverVersion | ||
import org.bukkit.entity.Player | ||
import org.bukkit.inventory.ItemStack | ||
|
||
@AlgebraicTypeInfo("float_array", Colors.GREEN, "fa6-solid:shapes") | ||
data class FloatsCustomModelData( | ||
val value: List<Float> = emptyList() | ||
) : CustomModelDataType { | ||
override fun apply(player: Player?, interactionContext: InteractionContext?, item: ItemStack) { | ||
if (!serverVersion.isNewerThanOrEquals(ServerVersion.V_1_21_4)) { | ||
logger.warning("${this::class.simpleName} is only supported in versions 1.21.4 and above") | ||
return | ||
} | ||
item.editMeta { meta -> | ||
val component = meta.customModelDataComponent | ||
if (component.floats == value) return@editMeta | ||
component.floats = value | ||
meta.setCustomModelDataComponent(component) | ||
} | ||
} | ||
|
||
override fun matches(player: Player?, interactionContext: InteractionContext?, item: ItemStack): Boolean { | ||
if (!serverVersion.isNewerThanOrEquals(ServerVersion.V_1_21_4)) { | ||
logger.warning("${this::class.simpleName} is only supported in versions 1.21.4 and above") | ||
return false | ||
} | ||
val meta = item.itemMeta ?: return false | ||
return meta.customModelDataComponent.floats == value | ||
} | ||
} |
Oops, something went wrong.
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.