File tree Expand file tree Collapse file tree 1 file changed +23
-2
lines changed
rebar/src/main/kotlin/io/github/pylonmc/rebar/registry Expand file tree Collapse file tree 1 file changed +23
-2
lines changed Original file line number Diff line number Diff line change @@ -28,6 +28,8 @@ class RebarRegistry<T : Keyed>(val key: NamespacedKey) : Iterable<T> {
2828
2929 private val values: MutableMap <NamespacedKey , T > = LinkedHashMap ()
3030
31+ private val keyMapping = mutableMapOf<NamespacedKey , NamespacedKey >()
32+
3133 fun register (vararg values : T ) {
3234 for (value in values) {
3335 val key = value.key
@@ -63,15 +65,34 @@ class RebarRegistry<T : Keyed>(val key: NamespacedKey) : Iterable<T> {
6365 }
6466
6567 operator fun get (key : NamespacedKey ): T ? {
68+ var key = key
69+ while (key in keyMapping) {
70+ key = keyMapping[key]!!
71+ }
6672 return values[key]
6773 }
6874
6975 fun getOrThrow (key : NamespacedKey ): T {
70- return values[ key] ? : throw NoSuchElementException (" No value found for key $key in registry $this " )
76+ return get( key) ? : throw NoSuchElementException (" No value found for key $key in registry $this " )
7177 }
7278
7379 fun getOrCreate (key : NamespacedKey , creator : () -> T ): T {
74- return values.getOrPut(key) { creator().also { register(it) } }
80+ val value = get(key)
81+ if (value != null ) {
82+ return value
83+ }
84+ val newValue = creator()
85+ register(newValue)
86+ return newValue
87+ }
88+
89+ /* *
90+ * Maps a key to another key, allowing values to be looked up by either key. This is useful for updating
91+ * keys without breaking existing references. For example, if an item is renamed from "addon:old_item" to
92+ * "addon:new_item", you can map "addon:old_item" to "addon:new_item" so that both keys will return the same item.
93+ */
94+ fun mapKey (from : NamespacedKey , to : NamespacedKey ) {
95+ keyMapping[from] = to
7596 }
7697
7798 fun getKeys (): Set <NamespacedKey > {
You can’t perform that action at this time.
0 commit comments