Skip to content

Commit

Permalink
Experimental | 优化&更新
Browse files Browse the repository at this point in the history
Rename ......
  • Loading branch information
TheFloodDragon committed Jun 25, 2024
1 parent a3678fd commit 26d11ab
Show file tree
Hide file tree
Showing 22 changed files with 137 additions and 102 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ public Priority(@NotNull Byte priority, @NotNull T value) {
this.value = value;
}

public static final Byte DEFAULT_PRIORITY = 0;

@NotNull
private Byte priority;
@NotNull
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ package cn.fd.ratziel.core
import java.util.*

/**
* TheIdentifier
* IdentifierImpl
*
* @author TheFloodDragon
* @since 2024/6/24 13:47
*/
open class TheIdentifier(val unique: String) : Identifier {
open class IdentifierImpl(val unique: String) : Identifier {

constructor(uuid: UUID) : this(uuid.toString())

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,57 +18,57 @@ inline fun <T> FutureFactory(block: FutureFactory<T>.() -> Unit) = FutureFactory
* @author TheFloodDragon
* @since 2023/11/19 11:27
*/
open class FutureFactory<T>(
class FutureFactory<T>(
/**
* 任务列表
*/
protected open val tasks: MutableCollection<CompletableFuture<T>>
protected val tasks: MutableCollection<CompletableFuture<T>>
) : MutableCollection<CompletableFuture<T>> by tasks {

constructor() : this(ConcurrentLinkedQueue())

/**
* 获取 [FutureFactory] 保存的所有任务
*/
open fun getAllTasks(): Collection<CompletableFuture<T>> = tasks
fun getAllTasks(): Collection<CompletableFuture<T>> = tasks

/**
* 清空[FutureFactory] 保存的所有任务
*/
open fun clearAllTasks() = tasks.clear()
fun clearAllTasks() = tasks.clear()

/**
* 提交任务
*/
open fun submitTask(task: CompletableFuture<T>) = task.also { tasks += it }
fun submitTask(task: CompletableFuture<T>) = task.also { tasks += it }

/**
* 创建异步任务并提交
*/
open fun submitAsync(function: Supplier<T>) = submitTask(CompletableFuture.supplyAsync(function))
fun submitAsync(function: Supplier<T>) = submitTask(CompletableFuture.supplyAsync(function))

open fun submitAsync(executor: Executor, function: Supplier<T>) = submitTask(CompletableFuture.supplyAsync(function, executor))
fun submitAsync(executor: Executor, function: Supplier<T>) = submitTask(CompletableFuture.supplyAsync(function, executor))

/**
* 当所有任务完成时 (非阻塞)
* 并提供所有任务返回值的列表
* @param action 对返回值进行的操作
*/
open fun <R> thenApply(action: Function<List<T>, R>): CompletableFuture<R> = tasks.toTypedArray().let { futures ->
fun <R> thenApply(action: Function<List<T>, R>): CompletableFuture<R> = tasks.toTypedArray().let { futures ->
CompletableFuture.allOf(*futures).thenApply {
val results = futures.map { it.get() } // 获取异步结果
action.apply(results) // 处理结果
}
}

open fun thenAccept(action: Consumer<List<T>> = Consumer {}) = thenApply { action.accept(it) }
fun thenAccept(action: Consumer<List<T>> = Consumer {}) = thenApply { action.accept(it) }

open fun thenRun(action: Runnable = Runnable {}) = thenApply { action.run() }
fun thenRun(action: Runnable = Runnable {}) = thenApply { action.run() }

/**
* 等待所有任务完成 (阻塞)
*/
open fun waitAll() {
fun waitAll() {
val future = CompletableFuture.allOf(*tasks.toTypedArray())
future.join() // 阻塞等待完成
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,21 +47,25 @@ object ItemRegistry {
/**
* 物品组件注册表
*/
internal val registry: MutableMap<Class<*>, ItemTransformer<*>> = ConcurrentHashMap()
internal val registry: MutableMap<Class<*>, Priority<ItemTransformer<*>>> = ConcurrentHashMap()

override fun <T> register(type: Class<T>, transformer: ItemTransformer<out T>) {
registry[type] = transformer
override fun <T> register(type: Class<T>, transformer: ItemTransformer<out T>, priority: Byte) {
registry[type] = Priority(priority, transformer)
}

override fun unregister(type: Class<*>) {
registry.remove(type)
}

override fun <T> get(type: Class<T>): ItemTransformer<out T>? = uncheck(registry[type])
override fun <T> getPriority(type: Class<T>): Priority<ItemTransformer<out T>>? = uncheck(registry[type])

override fun isRegistered(type: Class<*>) = registry.containsKey(type)

override fun getMap(): Map<Class<*>, ItemTransformer<*>> = registry
override fun getRegistry(): Map<Class<*>, ItemTransformer<*>> = buildMap {
getRegistryPriority().forEach { (k, p) -> put(k, p.value) }
}

override fun getRegistryPriority(): Map<Class<*>, Priority<ItemTransformer<*>>> = registry

}

Expand Down Expand Up @@ -107,8 +111,6 @@ object ItemRegistry {
registry.add(Priority(priority, resolver))
}

fun register(resolver: ItemResolver) = register(resolver, 0)

override fun unregister(type: Class<out ItemResolver>) {
for (element in registry) {
if (element.value::class.java == type) registry.remove(element)
Expand All @@ -121,19 +123,17 @@ object ItemRegistry {
}
}

override fun <T : ItemResolver> get(type: Class<T>): T? = getWithPriority(type)?.value

fun <T : ItemResolver> getWithPriority(type: Class<T>): Priority<T>? = uncheck(registry.find { it::class.java == type })
override fun <T : ItemResolver> getPriority(type: Class<T>): Priority<T>? = uncheck(registry.find { it::class.java == type })

override fun isRegistered(type: Class<out ItemResolver>) = registry.find { it.value::class.java == type } != null

override fun isRegistered(resolver: ItemResolver) = registry.find { it.value == resolver } != null

override fun getResolvers() = registry.map { it.value }

fun getResolversSorted(): List<ItemResolver> {
return registry.sortPriority()
}
override fun getResolversPriority(): Collection<Priority<ItemResolver>> = registry

fun getResolversSorted(): List<ItemResolver> = registry.sortPriority()

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import cn.fd.ratziel.function.argument.DefaultArgumentFactory
* @author TheFloodDragon
* @since 2024/5/21 22:54
*/
interface ArgumentResolver<E, T> : Resolver<E, T> {
interface ArgumentResolver<E, T> {

/**
* 解析元素 (带参数)
Expand All @@ -19,6 +19,6 @@ interface ArgumentResolver<E, T> : Resolver<E, T> {
/**
* 解析元素 (不带参数)
*/
override fun resolve(element: E): T = resolve(element, DefaultArgumentFactory())
fun resolve(element: E): T = resolve(element, DefaultArgumentFactory())

}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package cn.fd.ratziel.module.item.impl
package cn.fd.ratziel.module.item.api.builder

import cn.fd.ratziel.core.exception.UnsupportedTypeException
import cn.fd.ratziel.module.item.api.builder.ItemSerializer
import kotlinx.serialization.KSerializer
import kotlinx.serialization.encoding.Decoder
import kotlinx.serialization.encoding.Encoder
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package cn.fd.ratziel.module.item.api.registry

import cn.fd.ratziel.core.Priority
import cn.fd.ratziel.module.item.api.ItemTransformer

/**
Expand All @@ -14,8 +15,16 @@ interface ComponentRegistry {
* 注册组件以及其转换器
* @param type 组件类型 (组件类)
* @param transformer 绑定到组件上的物品转换器
* @param priority 优先级
*/
fun <T> register(type: Class<T>, transformer: ItemTransformer<out T>)
fun <T> register(type: Class<T>, transformer: ItemTransformer<out T>, priority: Byte)

/**
* 注册组件以及其转换器
* @param type 组件类型 (组件类)
* @param transformer 绑定到组件上的物品转换器
*/
fun <T> register(type: Class<T>, transformer: ItemTransformer<out T>) = register(type, transformer, Priority.DEFAULT_PRIORITY)

/**
* 取消注册组件以及其转换器
Expand All @@ -28,7 +37,14 @@ interface ComponentRegistry {
* @param type 组件类型 (组件类)
* @return 对应组件类型的转化器, 当该组件未注册时, 返回空
*/
fun <T> get(type: Class<T>): ItemTransformer<out T>?
fun <T> get(type: Class<T>): ItemTransformer<out T>? = getPriority(type)?.value

/**
* 获取对应组件类型的转化器 - [Priority]
* @param type 组件类型 (组件类)
* @return 对应组件类型的转化器, 当该组件未注册时, 返回空
*/
fun <T> getPriority(type: Class<T>): Priority<ItemTransformer<out T>>?

/**
* 判断此组件是否被注册过
Expand All @@ -39,6 +55,11 @@ interface ComponentRegistry {
/**
* 获取注册表的 [Map] 形式
*/
fun getMap(): Map<Class<*>, ItemTransformer<*>>
fun getRegistry(): Map<Class<*>, ItemTransformer<*>>

/**
* 获取注册表的 [Map] 形式 - [Priority]
*/
fun getRegistryPriority(): Map<Class<*>, Priority<ItemTransformer<*>>>

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package cn.fd.ratziel.module.item.api.registry

import cn.fd.ratziel.core.Priority
import cn.fd.ratziel.module.item.api.builder.ItemResolver

/**
Expand All @@ -12,11 +13,17 @@ interface ResolverRegistry {

/**
* 注册物品解析器
* @param priority 优先级
* @param resolver 物品解析器
* @param priority 优先级
*/
fun register(resolver: ItemResolver, priority: Byte)

/**
* 注册物品解析器
* @param resolver 物品解析器
*/
fun register(resolver: ItemResolver) = register(resolver, Priority.DEFAULT_PRIORITY)

/**
* 取消注册指定类型的解析器
* @param type 解析器类型
Expand All @@ -32,7 +39,12 @@ interface ResolverRegistry {
/**
* 获取指定类型的物品解析器
*/
fun <T : ItemResolver> get(type: Class<T>): T?
fun <T : ItemResolver> get(type: Class<T>): T? = getPriority(type)?.value

/**
* 获取指定类型的物品解析器 - [Priority]
*/
fun <T : ItemResolver> getPriority(type: Class<T>): Priority<T>?

/**
* 判断指定类型的解析器是否被注册过
Expand All @@ -51,4 +63,9 @@ interface ResolverRegistry {
*/
fun getResolvers(): Collection<ItemResolver>

/**
* 获取所有注册的解析器 - [Priority]
*/
fun getResolversPriority(): Collection<Priority<ItemResolver>>

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ import cn.fd.ratziel.module.item.api.ItemMaterial
import cn.fd.ratziel.module.item.nbt.NBTCompound

/**
* TheItemData
* ItemDataImpl
*
* @author TheFloodDragon
* @since 2024/5/5 13:33
*/
data class TheItemData(
data class ItemDataImpl(
/**
* 物品材料
*/
Expand All @@ -27,7 +27,15 @@ data class TheItemData(

companion object {

val EMPTY = TheItemData()
val EMPTY = ItemDataImpl()

/**
* 将 [target] 合并到 [source] 中
*/
fun merge(source: ItemData, target: ItemData, replace: Boolean = true) {
mergeWithoutTag(source, target)
source.tag.merge(target.tag, replace)
}

/**
* 将 [target] 合并到 [source] 中 (不合并[tag])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ import taboolib.library.reflex.ReflexClass
import taboolib.library.xseries.XMaterial

/**
* TheItemMaterial
* ItemMaterialImpl
*
* 由于不可抗力的影响(我不会), 仅支持 [BukkitMaterial], 即仅支持原版物品
*
* @author TheFloodDragon
* @since 2024/4/5 13:26
*/
data class TheItemMaterial(override val name: String) : ItemMaterial {
data class ItemMaterialImpl(override val name: String) : ItemMaterial {

constructor(mat: XMaterial) : this(mat.name)

Expand Down Expand Up @@ -77,7 +77,7 @@ data class TheItemMaterial(override val name: String) : ItemMaterial {
*/
val materialsMap by lazy {
HashMap<String, ItemMaterial>().apply {
BukkitMaterial.entries.forEach { put(it.name, TheItemMaterial(it)) }
BukkitMaterial.entries.forEach { put(it.name, ItemMaterialImpl(it)) }
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package cn.fd.ratziel.module.item.impl

import cn.fd.ratziel.core.Identifier
import cn.fd.ratziel.core.TheIdentifier
import cn.fd.ratziel.core.IdentifierImpl
import cn.fd.ratziel.module.item.api.ItemData
import cn.fd.ratziel.module.item.api.NeoItem
import cn.fd.ratziel.module.item.impl.service.GlobalServiceManager
Expand All @@ -19,9 +19,9 @@ open class RatzielItem : NeoItem {
this.data = data
}

constructor(data: ItemData) : this(TheIdentifier(), data)
constructor(data: ItemData) : this(IdentifierImpl(), data)

constructor() : this(TheItemData())
constructor() : this(ItemDataImpl())

/**
* 物品唯一标识符
Expand Down
Loading

0 comments on commit 26d11ab

Please sign in to comment.