-
Notifications
You must be signed in to change notification settings - Fork 387
Rename GTRegistry.remap() to .alias(), create a proper Remap, and make Aliases not break 1:1 key-value pairing #4934
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
Open
DilithiumThoride
wants to merge
1
commit into
1.20.1
Choose a base branch
from
dt/registry-remap
base: 1.20.1
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+105
−11
Open
Changes from all commits
Commits
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -25,6 +25,7 @@ public abstract class GTRegistry<K, V> implements Iterable<V> { | |
|
|
||
| protected final Map<K, V> keyToValue; | ||
| protected final Map<V, K> valueToKey; | ||
| protected final Map<K, K> alias; | ||
| @Getter | ||
| protected final ResourceLocation registryName; | ||
| @Getter | ||
|
|
@@ -33,13 +34,14 @@ public abstract class GTRegistry<K, V> implements Iterable<V> { | |
| public GTRegistry(ResourceLocation registryName) { | ||
| this.keyToValue = new HashMap<>(); | ||
| this.valueToKey = new HashMap<>(); | ||
| this.alias = new HashMap<>(); | ||
| this.registryName = registryName; | ||
|
|
||
| REGISTERED.put(registryName, this); | ||
| } | ||
|
|
||
| public boolean containKey(K key) { | ||
| return keyToValue.containsKey(key); | ||
| return keyToValue.containsKey(key) || alias.containsKey(key); | ||
| } | ||
|
|
||
| public boolean containValue(V value) { | ||
|
|
@@ -87,21 +89,46 @@ public <T extends V> T register(K key, T value) { | |
| return registerOrOverride(key, value); | ||
| } | ||
|
|
||
| public void remap(K oldKey, K newKey) { | ||
| /** | ||
| * Creates an alias between two keys. Used for migration. | ||
| * | ||
| * @param alias The migrated-out key used as the alias | ||
| * @param key The proper key in the current registry | ||
| */ | ||
| public void alias(K alias, K key) { | ||
| if (frozen) { | ||
| throw new IllegalStateException("[register] registry %s has been frozen".formatted(registryName)); | ||
| } | ||
|
|
||
| if (keyToValue.containsKey(oldKey)) { | ||
| GTCEu.LOGGER.warn("[remap] cannot remap existing key {} in registry {}", oldKey, registryName); | ||
| if (keyToValue.containsKey(alias)) { | ||
| GTCEu.LOGGER.warn("[alias] cannot create alias using existing key {} in registry {}", alias, registryName); | ||
| return; | ||
| } | ||
| if (!keyToValue.containsKey(newKey)) { | ||
| GTCEu.LOGGER.warn("[remap] couldn't find value for key {} in registry {}", newKey, registryName); | ||
| if (!keyToValue.containsKey(key)) { | ||
| GTCEu.LOGGER.warn("[alias] couldn't find existing key {} in registry {}", key, registryName); | ||
| return; | ||
| } | ||
| V newValue = keyToValue.get(newKey); | ||
| keyToValue.put(oldKey, newValue); | ||
| this.alias.put(alias, key); | ||
| } | ||
|
|
||
| /** | ||
| * Remaps an existing value to a new key | ||
| */ | ||
| public <T extends V> T remap(K newKey, T value) { | ||
| if (frozen) { | ||
| throw new IllegalStateException("[register] registry %s has been frozen".formatted(registryName)); | ||
| } | ||
|
|
||
| if (!valueToKey.containsKey(value)) { | ||
| GTCEu.LOGGER.warn("[remap] couldn't find existing value {} in registry {}", value, registryName); | ||
| } else { | ||
| remove(getKey(value)); | ||
| } | ||
| if (keyToValue.containsKey(newKey)) { | ||
| GTCEu.LOGGER.warn("[remap] key {} already exists in registry {}", newKey, registryName); | ||
| remove(newKey); | ||
| } | ||
| return registerOrOverride(newKey, value); | ||
| } | ||
|
|
||
| @Nullable | ||
|
|
@@ -145,21 +172,28 @@ public <T extends V> T registerOrOverride(K key, T value) { | |
| return Collections.unmodifiableMap(keyToValue); | ||
| } | ||
|
|
||
| public @UnmodifiableView Map<K, K> aliases() { | ||
| return Collections.unmodifiableMap(alias); | ||
| } | ||
|
|
||
| public void clear() { | ||
| if (frozen) { | ||
| throw new IllegalArgumentException("Registry is frozen!"); | ||
| } | ||
| keyToValue.clear(); | ||
| valueToKey.clear(); | ||
| alias.clear(); | ||
| } | ||
|
|
||
| @Nullable | ||
| public V get(K key) { | ||
| return keyToValue.get(key); | ||
| return (keyToValue.containsKey(key) ? keyToValue.get(key) : keyToValue.get(alias.get(key))); | ||
| } | ||
|
|
||
| public V getOrDefault(K key, V defaultValue) { | ||
| return keyToValue.getOrDefault(key, defaultValue); | ||
| return (keyToValue.containsKey(key) ? | ||
| keyToValue.get(key) : | ||
| keyToValue.getOrDefault(alias.get(key), defaultValue)); | ||
| } | ||
|
|
||
| public K getKey(V value) { | ||
|
|
@@ -170,6 +204,10 @@ public K getOrDefaultKey(V value, K defaultKey) { | |
| return valueToKey.getOrDefault(value, defaultKey); | ||
| } | ||
|
|
||
| public K getAliasedKey(K aliasKey) { | ||
| return alias.get(aliasKey); | ||
| } | ||
|
|
||
| public abstract void writeBuf(V value, FriendlyByteBuf buf); | ||
|
|
||
| @Nullable | ||
|
|
@@ -180,15 +218,71 @@ public K getOrDefaultKey(V value, K defaultKey) { | |
| @Nullable | ||
| public abstract V loadFromNBT(Tag tag); | ||
|
|
||
| /** | ||
| * Removes a key-value pair from the registry. | ||
| * If the key to be removed was an alias, also removes the original key. | ||
| * To only remove an alias, use | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing reference? |
||
| * | ||
| * @return if the registry contained the key to be removed | ||
| */ | ||
| public boolean remove(K name) { | ||
| if (frozen) { | ||
| throw new IllegalArgumentException("Registry is frozen!"); | ||
| } | ||
| var value = keyToValue.remove(name); | ||
| if (value != null) { | ||
| valueToKey.remove(value); | ||
| if (alias.containsValue(name)) removeAliasesForKey(name); | ||
| return true; | ||
| } | ||
| if (alias.containsKey(name)) { | ||
| return remove(alias.remove(name)); | ||
| } | ||
|
|
||
| return false; | ||
| } | ||
|
|
||
| /** | ||
| * Removes an alias | ||
| * | ||
| * @return if the registry contained this alias | ||
| */ | ||
| public boolean removeAlias(K name) { | ||
| if (frozen) { | ||
| throw new IllegalArgumentException("Registry is frozen!"); | ||
| } | ||
| if (!alias.containsKey(name)) | ||
| return false; | ||
| alias.remove(name); | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| /** | ||
| * Removes all aliases for a registry key | ||
| * | ||
| * @param name the name to find and remove aliases to | ||
| * @return if any aliases existed for that name | ||
| */ | ||
| public boolean removeAliasesForKey(K name) { | ||
| if (frozen) { | ||
| throw new IllegalArgumentException("Registry is frozen!"); | ||
| } | ||
| if (!alias.containsValue(name)) | ||
| return false; | ||
|
|
||
| var aliasesOfName = new ArrayList<K>(); | ||
| for (K aliasOfName : alias.keySet()) { | ||
| if (alias.get(aliasOfName).equals(name)) | ||
| aliasesOfName.add(aliasOfName); | ||
| } | ||
| for (K aliasOfName : aliasesOfName) { | ||
| alias.remove(aliasOfName); | ||
| } | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| public abstract Codec<V> codec(); | ||
|
|
||
| // ************************ Built-in Registry ************************// | ||
|
|
||
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is not possible that reg.containKey(alias) == true but reg.keys().contains(alias) == false, reg.entries().map(getKey).contains(alias) == false, etc. Not sure if we want to fix this or not