Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class RegistryCommandArgument<T : Keyed>(private val registry: RebarRegistry<T>)

@Suppress("PrivatePropertyName")
private val ERROR_UNKNOWN = DynamicCommandExceptionType {
MessageComponentSerializer.message().serialize(Component.text("Unknown key in ${registry.key}: $it"))
MessageComponentSerializer.message().serialize(Component.text("Unknown key: $it"))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will say this is an unfortunate loss

}

override fun convert(nativeType: NamespacedKey): T {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import io.github.pylonmc.rebar.item.ItemTypeWrapper
import io.github.pylonmc.rebar.item.RebarItemSchema
import io.github.pylonmc.rebar.item.research.Research
import io.github.pylonmc.rebar.recipe.RecipeType
import io.github.pylonmc.rebar.util.rebarKey
import org.bukkit.Keyed
import org.bukkit.NamespacedKey
import org.bukkit.Tag
Expand All @@ -22,9 +21,8 @@ import java.util.stream.Stream
* This class is not thread safe and any concurrent access must be synchronized externally.
*
* @param T the type of the registered values
* @property key the key of this registry
*/
class RebarRegistry<T : Keyed>(val key: NamespacedKey) : Iterable<T> {
class RebarRegistry<T : Keyed> : Iterable<T> {

private val values: MutableMap<NamespacedKey, T> = LinkedHashMap()

Expand Down Expand Up @@ -121,53 +119,15 @@ class RebarRegistry<T : Keyed>(val key: NamespacedKey) : Iterable<T> {

fun stream(): Stream<T> = values.values.stream()

override fun equals(other: Any?): Boolean = other is RebarRegistry<*> && key == other.key
override fun hashCode(): Int = key.hashCode()
override fun toString(): String = key.toString()

companion object {
private val registries: MutableMap<NamespacedKey, RebarRegistry<*>> = mutableMapOf()

// @formatter:off
@JvmField val ITEMS_KEY = rebarKey("items")
@JvmField val BLOCKS_KEY = rebarKey("blocks")
@JvmField val ENTITIES_KEY = rebarKey("entities")
@JvmField val FLUIDS_KEY = rebarKey("fluids")
@JvmField val GAMETESTS_KEY = rebarKey("gametests")
@JvmField val ADDONS_KEY = rebarKey("addons")
@JvmField val RECIPE_TYPES_KEY = rebarKey("recipe_types")
@JvmField val RESEARCHES_KEY = rebarKey("researches")
@JvmField val ITEM_TAGS_KEY = rebarKey("tags")
// @formatter:on

// @formatter:off
@JvmField val ITEMS = RebarRegistry<RebarItemSchema>(ITEMS_KEY).also(::addRegistry)
@JvmField val BLOCKS = RebarRegistry<RebarBlockSchema>(BLOCKS_KEY).also(::addRegistry)
@JvmField val ENTITIES = RebarRegistry<RebarEntitySchema>(ENTITIES_KEY).also(::addRegistry)
@JvmField val FLUIDS = RebarRegistry<RebarFluid>(FLUIDS_KEY).also(::addRegistry)
@JvmField val ADDONS = RebarRegistry<RebarAddon>(ADDONS_KEY).also(::addRegistry)
@JvmField val GAMETESTS = RebarRegistry<GameTestConfig>(GAMETESTS_KEY).also(::addRegistry)
@JvmField val RECIPE_TYPES = RebarRegistry<RecipeType<*>>(RECIPE_TYPES_KEY).also(::addRegistry)
@JvmField val RESEARCHES = RebarRegistry<Research>(RESEARCHES_KEY).also(::addRegistry)
@JvmField val ITEM_TAGS = RebarRegistry<Tag<ItemTypeWrapper>>(ITEM_TAGS_KEY).also(::addRegistry)
// @formatter:on

@JvmStatic
fun <T : Keyed> getRegistry(key: NamespacedKey): RebarRegistry<T> {
return getRegistryOrNull(key) ?: throw IllegalArgumentException("Registry $key not found")
}

@JvmStatic
fun <T : Keyed> getRegistryOrNull(key: NamespacedKey): RebarRegistry<T>? {
@Suppress("UNCHECKED_CAST")
return registries[key] as? RebarRegistry<T>
}

@JvmStatic
fun addRegistry(registry: RebarRegistry<*>) {
val key = registry.key
check(key !in registries) { "Registry $key is already registered" }
registries[key] = registry
}
@JvmField val ITEMS = RebarRegistry<RebarItemSchema>()
@JvmField val BLOCKS = RebarRegistry<RebarBlockSchema>()
@JvmField val ENTITIES = RebarRegistry<RebarEntitySchema>()
@JvmField val FLUIDS = RebarRegistry<RebarFluid>()
@JvmField val ADDONS = RebarRegistry<RebarAddon>()
@JvmField val GAMETESTS = RebarRegistry<GameTestConfig>()
@JvmField val RECIPE_TYPES = RebarRegistry<RecipeType<*>>()
@JvmField val RESEARCHES = RebarRegistry<Research>()
@JvmField val ITEM_TAGS = RebarRegistry<Tag<ItemTypeWrapper>>()
}
}
Loading