forked from QuiltMC/quilt-standard-libraries
-
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.
Add system for Dynamic Registry Flags (QuiltMC#330)
* Implement Dynamic Registry Flags. - Currently, only one flag, `OPTIONAL`, exists. - Modified DynamicRegistrySyncMixin in order to implement these flags. Due to the Redirect mixin header used to implement the flag filtering being taken directly from FAPI, the header of DynamicRegistrySyncMixin was changed to mention FabricMC copyright, in accordance with QSL contribution rules in CONTRIBUTING.md * Implement Dynamic Registry Flags p2 - Modify DynamicRegistryFlagManager to use Identifiers over RegistryKeys internally, in order to avoid generic-related jank. - Fix mixin ported from FAPI by correcting method target string to use Quilt Mappings. - Implement very basic tests. * Expanded upon OPTIONAL flag javadocs to elaborate use-case * Expanded further on OPTIONAL javadocs * Implemented some changes suggested by Pyrofab - Added protected helper method DynamicMetaRegistryImpl#isFrozen for use in DynamicRegistryFlagManager - Added DynamicMetaRegistryImpl#LOGGER with the name "quilt_dynamic_registries" as a logger for catching exceptions related to the setting of flags - Removed the ability to disable dynamic registry flags, changed the signature of the related flag-enabling methods from enableFlag -> setFlag (on both DynamicRegistryFlag and DynamicRegistryFlagManager) - DynamicRegistryFlagManager#setFlag now throws an IllegalStateException if a mod attempts to enable a flag when dynamic registries are frozen. This is caught and printed to the logger mentioned prior in DynamicRegistryFlag calls - Added varargs for setting DynamicRegistryFlag values on dynamic registry creation - Improved DynamicRegistryFlag#OPTIONAL javadocs slightly * Commit most style changes One of them needs a more specific wording change, so it's going to be done in another commit since that's easier for us Co-authored-by: Ennui Langeweile <[email protected]> * Make documentation for DynamicRegistryFlag more accurate * Improve the javadocs on DynamicRegistryFlag AGAIN accidentally dropped a word guh * BUT WAIT, THERE'S MORE! Hopefully the final style change moment Co-authored-by: Ennui Langeweile <[email protected]> * Implement style change in DynamicRegistryFlagManager Co-authored-by: LambdAurora <[email protected]> * Fix binary incompatibility hopefully? - Added back methods to DynamicMetaRegistry with the same signature as the old ones to avoid binary incompatibility. These are marked as deprecated and should be removed in a breaking/major QSL release. * Remove deprecation annotations on compat. method overloads in DynamicMetaRegistry --------- Co-authored-by: Ennui Langeweile <[email protected]> Co-authored-by: LambdAurora <[email protected]>
- Loading branch information
1 parent
63d39fd
commit b892ad0
Showing
6 changed files
with
229 additions
and
4 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
78 changes: 78 additions & 0 deletions
78
...core/registry/src/main/java/org/quiltmc/qsl/registry/api/dynamic/DynamicRegistryFlag.java
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,78 @@ | ||
/* | ||
* Copyright 2023 The Quilt Project | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.quiltmc.qsl.registry.api.dynamic; | ||
|
||
import net.minecraft.registry.RegistryKey; | ||
import net.minecraft.util.Identifier; | ||
|
||
import org.quiltmc.qsl.registry.impl.dynamic.DynamicMetaRegistryImpl; | ||
import org.quiltmc.qsl.registry.impl.dynamic.DynamicRegistryFlagManager; | ||
|
||
/** | ||
* Flags that can be set on dynamic registries to define their behavior. | ||
* <p> | ||
* All flags are off by default, and can be enabled using static methods in this class or on dynamic registry creation via varargs. | ||
* | ||
* @see org.quiltmc.qsl.registry.api.sync.RegistrySynchronization org.quiltmc.qsl.registry.api.sync.RegistrySynchronization, | ||
* which contains similar flag setters/getters for static registries | ||
*/ | ||
public enum DynamicRegistryFlag { | ||
/** | ||
* Indicates that this registry (and the entries within) do not necessarily need to be sent | ||
* to (logical) clients for synchronization in multiplayer contexts. | ||
* <p> | ||
* <b>Note:</b> This flag is intended only for synchronized dynamic registries. On non-synced dynamic registries, this flag does nothing. | ||
* <p> | ||
* One use-case for this flag is for creating mods that are entirely compatible with vanilla, and thus do not | ||
* require the dynamic registry to exist clientside to connect to the server. | ||
* This allows for both vanilla clients/clients without the mod to connect <i>and</i> for clients with the mod | ||
* supplying the registry to connect, with the latter being able to see the contents of the registry and possibly | ||
* enable extra clientside features accordingly. | ||
*/ | ||
OPTIONAL; | ||
|
||
/** | ||
* Enables a specific flag on a dynamic registry. | ||
* | ||
* @param registryId the value id ({@link RegistryKey#getValue()}) of the target dynamic registry | ||
* @param flag the flag value to enable on the dynamic registry | ||
*/ | ||
public static void setFlag(Identifier registryId, DynamicRegistryFlag flag) { | ||
try { | ||
DynamicRegistryFlagManager.setFlag(registryId, flag); | ||
} catch (Exception e) { | ||
logFlagModifyException(registryId, flag, e); | ||
} | ||
} | ||
|
||
/** | ||
* Checks if a dynamic registry has the {@link DynamicRegistryFlag#OPTIONAL} flag enabled on it. | ||
* | ||
* @param registryId the value id ({@link RegistryKey#getValue()}) of the dynamic registry to check | ||
* @return whether the checked dynamic registry has the {@link DynamicRegistryFlag#OPTIONAL} flag enabled | ||
*/ | ||
public static boolean isOptional(Identifier registryId) { | ||
return DynamicRegistryFlagManager.isOptional(registryId); | ||
} | ||
|
||
/** | ||
* Helper method for logging exceptions to avoid code duplication. | ||
*/ | ||
private static void logFlagModifyException(Identifier registryId, DynamicRegistryFlag flag, Exception e) { | ||
DynamicMetaRegistryImpl.LOGGER.error("Caught exception while attempting to enable flag {} on registry id {}: {}", flag.toString(), registryId, e.toString()); | ||
} | ||
} |
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
49 changes: 49 additions & 0 deletions
49
...istry/src/main/java/org/quiltmc/qsl/registry/impl/dynamic/DynamicRegistryFlagManager.java
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,49 @@ | ||
/* | ||
* Copyright 2023 The Quilt Project | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.quiltmc.qsl.registry.impl.dynamic; | ||
|
||
import org.jetbrains.annotations.ApiStatus; | ||
import com.google.common.collect.Multimap; | ||
import com.google.common.collect.MultimapBuilder; | ||
|
||
import net.minecraft.util.Identifier; | ||
|
||
import org.quiltmc.qsl.registry.api.dynamic.DynamicRegistryFlag; | ||
|
||
@ApiStatus.Internal | ||
public final class DynamicRegistryFlagManager { | ||
private static final Multimap<Identifier, DynamicRegistryFlag> DYNAMIC_REGISTRY_FLAGS = | ||
MultimapBuilder.hashKeys().enumSetValues(DynamicRegistryFlag.class).build(); | ||
|
||
public static void setFlag(Identifier registryId, DynamicRegistryFlag flag) throws IllegalStateException { | ||
if (DynamicMetaRegistryImpl.isFrozen()) { | ||
throw new IllegalStateException("Dynamic registries are frozen, and thus flags cannot be changed!"); | ||
} | ||
|
||
if (!DynamicMetaRegistryImpl.isModdedRegistryId(registryId)) return; | ||
|
||
DYNAMIC_REGISTRY_FLAGS.put(registryId, flag); | ||
} | ||
|
||
public static boolean isOptional(Identifier registryId) { | ||
if (DYNAMIC_REGISTRY_FLAGS.containsKey(registryId)) { | ||
return DYNAMIC_REGISTRY_FLAGS.get(registryId).contains(DynamicRegistryFlag.OPTIONAL); | ||
} | ||
|
||
return false; | ||
} | ||
} |
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