-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4a2e5cb
commit 4fbd600
Showing
5 changed files
with
125 additions
and
83 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
60 changes: 60 additions & 0 deletions
60
project/module-item/src/main/kotlin/cn/fd/ratziel/module/item/impl/builder/ComponentUtil.kt
This file contains 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,60 @@ | ||
package cn.fd.ratziel.module.item.impl.builder | ||
|
||
import cn.fd.ratziel.module.item.api.ItemData | ||
import cn.fd.ratziel.module.item.api.ItemNode | ||
import cn.fd.ratziel.module.item.api.ItemTransformer | ||
import cn.fd.ratziel.module.item.impl.TheItemData | ||
import cn.fd.ratziel.module.item.nbt.NBTCompound | ||
import cn.fd.ratziel.module.item.nbt.NBTData | ||
|
||
/** | ||
* ComponentUtil | ||
* | ||
* @author TheFloodDragon | ||
* @since 2024/6/24 17:15 | ||
*/ | ||
object ComponentUtil { | ||
|
||
/** | ||
* 通过 [transformer] 将 [component] 转化成 [ItemData] | ||
*/ | ||
fun <T> toData(component: T, transformer: ItemTransformer<T>): ItemData { | ||
val data = transformer.transform(component) // 获取底层数据 | ||
val newTag = NBTCompound() // 创建新NBT | ||
setByNode(newTag, transformer.node, data.tag) // 设置新NBT | ||
return TheItemData(data.material, newTag, data.amount) | ||
} | ||
|
||
/** | ||
* 通过 [transformer] 将 [data] 转化成 物品组件 | ||
*/ | ||
fun <T> toComponent(data: ItemData, transformer: ItemTransformer<T>): T { | ||
val find = findByNode(data.tag, transformer.node) | ||
return transformer.detransform(TheItemData(data.material, find, data.amount)) | ||
} | ||
|
||
fun findByNode(source: NBTCompound, tailNode: ItemNode) = findByNode(source, fold(tailNode)) | ||
|
||
fun findByNode(source: NBTCompound, nodes: Iterable<ItemNode>): NBTCompound { | ||
var find = source | ||
for (node in nodes) { | ||
find = find.computeIfAbsent(node.name) { NBTCompound() } as NBTCompound | ||
} | ||
return find | ||
} | ||
|
||
fun setByNode(source: NBTCompound, tailNode: ItemNode, data: NBTData) { | ||
val node = tailNode.parent ?: return // 去掉最后一层节点, 若为尾节点顶级节点, 则直接返回 | ||
val find = findByNode(source, node) // 寻找节点 | ||
find[tailNode.name] = data // 设置最后一层 | ||
} | ||
|
||
fun fold(tailNode: ItemNode) = buildList { | ||
var node: ItemNode = tailNode | ||
while (node.parent != null) { | ||
add(node) | ||
node = node.parent ?: break | ||
} | ||
}.reversed() | ||
|
||
} |
This file contains 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
69 changes: 11 additions & 58 deletions
69
project/module-item/src/main/kotlin/cn/fd/ratziel/module/item/util/TransformUtil.kt
This file contains 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 |
---|---|---|
@@ -1,72 +1,25 @@ | ||
@file:Suppress("NOTHING_TO_INLINE") | ||
|
||
package cn.fd.ratziel.module.item.util | ||
|
||
import cn.fd.ratziel.function.util.uncheck | ||
import cn.fd.ratziel.module.item.api.ItemData | ||
import cn.fd.ratziel.module.item.api.ItemNode | ||
import cn.fd.ratziel.module.item.api.ItemTransformer | ||
import cn.fd.ratziel.module.item.impl.TheItemData | ||
import cn.fd.ratziel.module.item.nbt.NBTCompound | ||
import cn.fd.ratziel.module.item.impl.builder.ComponentUtil | ||
import cn.fd.ratziel.module.item.nbt.NBTData | ||
import java.util.function.Consumer | ||
|
||
fun <T> ItemTransformer<T>.toApexData(component: T): ItemData = ComponentUtil.toData(component, this) | ||
inline fun <T> ItemTransformer<T>.toApexData(component: T): ItemData = ComponentUtil.toData(component, this) | ||
|
||
fun <T> ItemTransformer<T>.toApexComponent(data: ItemData): T = ComponentUtil.toComponent(data, this) | ||
inline fun <T> ItemTransformer<T>.toApexComponent(data: ItemData): T = ComponentUtil.toComponent(data, this) | ||
|
||
/** | ||
* 转换[NBTData], 若成功转换(不为空), 则执行 [action] | ||
*/ | ||
inline fun <reified T : NBTData> NBTData?.castThen(action: Consumer<T>) = (this as? T)?.let { action.accept(it) } | ||
inline fun ItemTransformer<*>.toApexDataUncheck(component: Any): ItemData = ComponentUtil.toData(component, uncheck(this)) | ||
|
||
inline fun <reified T : NBTData> ItemData?.castThen(node: String, action: Consumer<T>) = this?.tag?.get(node)?.castThen<T>(action) | ||
inline fun ItemTransformer<*>.toApexComponentUncheck(data: ItemData): Any = ComponentUtil.toComponent(data, uncheck(this)) | ||
|
||
/** | ||
* ComponentUtil | ||
* | ||
* @author TheFloodDragon | ||
* @since 2024/5/18 08:15 | ||
* 转换[NBTData], 若成功转换(不为空), 则执行 [action] | ||
*/ | ||
object ComponentUtil { | ||
|
||
/** | ||
* 通过 [transformer] 将 [component] 转化成 [ItemData] | ||
*/ | ||
fun <T> toData(component: T, transformer: ItemTransformer<T>): ItemData { | ||
val data = transformer.transform(component) // 获取底层数据 | ||
val newTag = NBTCompound() // 创建新NBT | ||
setByNode(newTag, transformer.node, data.tag) // 设置新NBT | ||
return TheItemData(data.material, newTag, data.amount) | ||
} | ||
|
||
/** | ||
* 通过 [transformer] 将 [data] 转化成 物品组件 | ||
*/ | ||
fun <T> toComponent(data: ItemData, transformer: ItemTransformer<T>): T { | ||
val find = findByNode(data.tag, transformer.node) | ||
return transformer.detransform(TheItemData(data.material, find, data.amount)) | ||
} | ||
|
||
fun findByNode(source: NBTCompound, tailNode: ItemNode) = findByNode(source, fold(tailNode)) | ||
|
||
fun findByNode(source: NBTCompound, nodes: Iterable<ItemNode>): NBTCompound { | ||
var find = source | ||
for (node in nodes) { | ||
find = find.computeIfAbsent(node.name) { NBTCompound() } as NBTCompound | ||
} | ||
return find | ||
} | ||
|
||
fun setByNode(source: NBTCompound, tailNode: ItemNode, data: NBTData) { | ||
val node = tailNode.parent ?: return // 去掉最后一层节点, 若为尾节点顶级节点, 则直接返回 | ||
val find = findByNode(source, node) // 寻找节点 | ||
find[tailNode.name] = data // 设置最后一层 | ||
} | ||
|
||
fun fold(tailNode: ItemNode) = buildList { | ||
var node: ItemNode = tailNode | ||
while (node.parent != null) { | ||
add(node) | ||
node = node.parent ?: break | ||
} | ||
}.reversed() | ||
inline fun <reified T : NBTData> NBTData?.castThen(action: Consumer<T>) = (this as? T)?.let { action.accept(it) } | ||
|
||
} | ||
inline fun <reified T : NBTData> ItemData?.castThen(node: String, action: Consumer<T>) = this?.tag?.get(node)?.castThen<T>(action) |