-
Notifications
You must be signed in to change notification settings - Fork 521
Environment attribute API for adding custom attribute layers #5201
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
FoxSamu
wants to merge
14
commits into
FabricMC:26.1
Choose a base branch
from
FoxSamu:26.1
base: 26.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.
+729
−0
Open
Changes from 2 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
14c0ce9
Environment attribute API for adding custom attribute layers
FoxSamu 02d8b03
Change v0 into v1
FoxSamu d05868b
Replace events with proper sortable registry.
FoxSamu 5a2e97a
Merge branch '26.1' into 26.1
FoxSamu 14e89bc
Be more consistent with terms used in docs and code
FoxSamu fcba42c
Merge remote-tracking branch 'origin/26.1' into 26.1
FoxSamu 8721f5a
Merge branch '26.1' into 26.1
FoxSamu 250d6d6
Fix javadoc errors
FoxSamu 66ebdd3
Add a small essential comment in the docs about the nature of layers
FoxSamu ece2392
Make implementation naming match API naming
FoxSamu 9af08de
Change module name to match convention
FoxSamu f0fc207
Fix environment attribute api dependencies
FoxSamu 6fa1af7
Fix test mod dependency
FoxSamu e4809a0
Merge branch '26.1' into 26.1
FoxSamu 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 |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| version = getSubprojectVersion(project) | ||
|
|
||
| moduleDependencies(project, [ | ||
| 'fabric-api-base', | ||
| 'fabric-lifecycle-events-v1' | ||
| ]) | ||
|
|
||
| testDependencies(project, [ | ||
| ':fabric-resource-loader-v1', | ||
| ':fabric-client-gametest-api-v1' | ||
| ]) |
48 changes: 48 additions & 0 deletions
48
...rc/main/java/net/fabricmc/fabric/api/environment/attribute/v1/AttributeLayerPosition.java
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 |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| /* | ||
| * Copyright (c) 2016, 2017, 2018, 2019 FabricMC | ||
| * | ||
| * 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 net.fabricmc.fabric.api.environment.attribute.v1; | ||
|
|
||
| /** | ||
| * Positions at which environment attribute layers can be inserted. Each position is associated with an event in | ||
| * {@link EnvironmentAttributeEvents}. The enum is ordered by in which order Minecraft adds its default layers. | ||
| */ | ||
| public enum AttributeLayerPosition { | ||
| /** | ||
| * The position before all vanilla layers. | ||
| */ | ||
| BEFORE_ALL, | ||
|
|
||
| /** | ||
| * The position between dimension and biome layers. | ||
| */ | ||
| BETWEEN_DIMENSION_AND_BIOMES, | ||
|
|
||
| /** | ||
| * The position between biome and timeline layers. | ||
| */ | ||
| BETWEEN_BIOMES_AND_TIMELINES, | ||
|
|
||
| /** | ||
| * The position between timeline and weather layers. | ||
| */ | ||
| BETWEEN_TIMELINES_AND_WEATHER, | ||
|
|
||
| /** | ||
| * The position after all vanilla layers. | ||
| */ | ||
| AFTER_ALL | ||
| } | ||
64 changes: 64 additions & 0 deletions
64
...ain/java/net/fabricmc/fabric/api/environment/attribute/v1/EnvironmentAttributeEvents.java
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 |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| /* | ||
| * Copyright (c) 2016, 2017, 2018, 2019 FabricMC | ||
| * | ||
| * 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 net.fabricmc.fabric.api.environment.attribute.v1; | ||
|
|
||
| import net.minecraft.world.attribute.EnvironmentAttributeSystem; | ||
| import net.minecraft.world.level.Level; | ||
|
|
||
| import net.fabricmc.fabric.api.event.Event; | ||
| import net.fabricmc.fabric.impl.environment.attribute.EnvironmentAttributeEventsImpl; | ||
|
|
||
| /** | ||
| * Events related to environment attributes. | ||
| */ | ||
| public class EnvironmentAttributeEvents { | ||
| /** | ||
| * Returns the {@link InsertLayers} event for the given {@link AttributeLayerPosition}. This event allows inserting | ||
| * extra attribute layers at that position during the environment attribute setup for a {@link Level}. By default, | ||
| * Minecraft adds layers for the dimension, then for biomes, then for timelines and lastly for weather. The event | ||
| * returned by this method is triggered before, in between and after each of these vanilla layers, depending on the | ||
| * selected position, in the following manner: | ||
| * <ul> | ||
| * <li>Event for {@link AttributeLayerPosition#BEFORE_ALL} is triggered.</li> | ||
| * <li>Minecraft adds the layer for dimension type attribute configurations.</li> | ||
| * <li>Event for {@link AttributeLayerPosition#BETWEEN_DIMENSION_AND_BIOMES} is triggered.</li> | ||
| * <li>Minecraft adds the layer for biome attribute configurations.</li> | ||
| * <li>Event for {@link AttributeLayerPosition#BETWEEN_BIOMES_AND_TIMELINES} is triggered.</li> | ||
| * <li>Minecraft adds the layer for timeline attribute animations.</li> | ||
| * <li>Event for {@link AttributeLayerPosition#BETWEEN_TIMELINES_AND_WEATHER} is triggered.</li> | ||
| * <li>Minecraft adds the layer for some hardcoded weather attribute overrides.</li> | ||
| * <li>Event for {@link AttributeLayerPosition#AFTER_ALL} is triggered.</li> | ||
| * </ul> | ||
| * @param position The position at which you want to insert attribute layers. | ||
| * @return The event to listen for layer setup. | ||
| */ | ||
| public static Event<InsertLayers> insertLayersEvent(AttributeLayerPosition position) { | ||
| return EnvironmentAttributeEventsImpl.getOrCreateInsertLayersEvent(position); | ||
| } | ||
|
|
||
| /** | ||
| * Callback for events returned from {@link #insertLayersEvent}. | ||
| */ | ||
| public interface InsertLayers { | ||
| /** | ||
| * Insert custom attribute layers into the {@link EnvironmentAttributeSystem.Builder}. | ||
| * @param systemBuilder The environment attribute system builder to modify. | ||
| * @param level The level for which the attribute system is built. | ||
| */ | ||
| void insertAttributeLayers(EnvironmentAttributeSystem.Builder systemBuilder, Level level); | ||
| } | ||
| } |
55 changes: 55 additions & 0 deletions
55
...n/java/net/fabricmc/fabric/impl/environment/attribute/EnvironmentAttributeEventsImpl.java
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 |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| /* | ||
| * Copyright (c) 2016, 2017, 2018, 2019 FabricMC | ||
| * | ||
| * 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 net.fabricmc.fabric.impl.environment.attribute; | ||
|
|
||
| import java.util.EnumMap; | ||
| import java.util.Map; | ||
|
|
||
| import org.jspecify.annotations.NonNull; | ||
|
|
||
| import net.minecraft.world.attribute.EnvironmentAttributeSystem; | ||
| import net.minecraft.world.level.Level; | ||
|
|
||
| import net.fabricmc.fabric.api.environment.attribute.v1.AttributeLayerPosition; | ||
| import net.fabricmc.fabric.api.environment.attribute.v1.EnvironmentAttributeEvents; | ||
| import net.fabricmc.fabric.api.event.Event; | ||
| import net.fabricmc.fabric.api.event.EventFactory; | ||
|
|
||
| public class EnvironmentAttributeEventsImpl { | ||
| private static final Map<AttributeLayerPosition, Event<EnvironmentAttributeEvents.InsertLayers>> POSITION_EVENT_MAP = new EnumMap<>(AttributeLayerPosition.class); | ||
|
|
||
| @NonNull | ||
| public static Event<EnvironmentAttributeEvents.InsertLayers> getOrCreateInsertLayersEvent(AttributeLayerPosition position) { | ||
| return POSITION_EVENT_MAP.computeIfAbsent(position, (p -> createInsertEvent())); | ||
| } | ||
|
|
||
| public static void insertLayers(AttributeLayerPosition position, EnvironmentAttributeSystem.Builder systemBuilder, Level level) { | ||
| Event<EnvironmentAttributeEvents.InsertLayers> event = POSITION_EVENT_MAP.get(position); | ||
|
|
||
| if (event != null) { | ||
| event.invoker().insertAttributeLayers(systemBuilder, level); | ||
| } | ||
| } | ||
|
|
||
| private static Event<EnvironmentAttributeEvents.InsertLayers> createInsertEvent() { | ||
| return EventFactory.createArrayBacked(EnvironmentAttributeEvents.InsertLayers.class, callbacks -> (systemBuilder, level) -> { | ||
| for (EnvironmentAttributeEvents.InsertLayers callback : callbacks) { | ||
| callback.insertAttributeLayers(systemBuilder, level); | ||
| } | ||
| }); | ||
| } | ||
| } |
71 changes: 71 additions & 0 deletions
71
...java/net/fabricmc/fabric/mixin/environment/attribute/EnvironmentAttributeSystemMixin.java
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 |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| /* | ||
| * Copyright (c) 2016, 2017, 2018, 2019 FabricMC | ||
| * | ||
| * 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 net.fabricmc.fabric.mixin.environment.attribute; | ||
|
|
||
| import org.spongepowered.asm.mixin.Mixin; | ||
| import org.spongepowered.asm.mixin.injection.At; | ||
| import org.spongepowered.asm.mixin.injection.Inject; | ||
| import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; | ||
|
|
||
| import net.minecraft.world.attribute.EnvironmentAttributeSystem; | ||
| import net.minecraft.world.level.Level; | ||
|
|
||
| import net.fabricmc.fabric.api.environment.attribute.v1.AttributeLayerPosition; | ||
| import net.fabricmc.fabric.impl.environment.attribute.EnvironmentAttributeEventsImpl; | ||
|
|
||
| @Mixin(EnvironmentAttributeSystem.class) | ||
| public class EnvironmentAttributeSystemMixin { | ||
| @Inject( | ||
| method = "addDefaultLayers", | ||
| at = @At(value = "HEAD") | ||
| ) | ||
| private static void addLayersBeforeAll(EnvironmentAttributeSystem.Builder builder, Level level, CallbackInfo ci) { | ||
| EnvironmentAttributeEventsImpl.insertLayers(AttributeLayerPosition.BEFORE_ALL, builder, level); | ||
| } | ||
|
|
||
| @Inject( | ||
| method = "addDefaultLayers", | ||
| at = @At(value = "INVOKE", target = "Lnet/minecraft/world/attribute/EnvironmentAttributeSystem;addBiomeLayer(Lnet/minecraft/world/attribute/EnvironmentAttributeSystem$Builder;Lnet/minecraft/core/HolderLookup;Lnet/minecraft/world/level/biome/BiomeManager;)V") | ||
| ) | ||
| private static void addLayersAfterDimension(EnvironmentAttributeSystem.Builder builder, Level level, CallbackInfo ci) { | ||
| EnvironmentAttributeEventsImpl.insertLayers(AttributeLayerPosition.BETWEEN_DIMENSION_AND_BIOMES, builder, level); | ||
| } | ||
|
|
||
| @Inject( | ||
| method = "addDefaultLayers", | ||
| at = @At(value = "INVOKE", target = "Lnet/minecraft/world/level/Level;dimensionType()Lnet/minecraft/world/level/dimension/DimensionType;") | ||
| ) | ||
| private static void addLayersAfterBiomes(EnvironmentAttributeSystem.Builder builder, Level level, CallbackInfo ci) { | ||
| EnvironmentAttributeEventsImpl.insertLayers(AttributeLayerPosition.BETWEEN_BIOMES_AND_TIMELINES, builder, level); | ||
| } | ||
|
|
||
| @Inject( | ||
| method = "addDefaultLayers", | ||
| at = @At(value = "INVOKE", target = "Lnet/minecraft/world/level/Level;canHaveWeather()Z") | ||
| ) | ||
| private static void addLayersAfterTimelines(EnvironmentAttributeSystem.Builder builder, Level level, CallbackInfo ci) { | ||
| EnvironmentAttributeEventsImpl.insertLayers(AttributeLayerPosition.BETWEEN_TIMELINES_AND_WEATHER, builder, level); | ||
| } | ||
|
|
||
| @Inject( | ||
| method = "addDefaultLayers", | ||
| at = @At(value = "TAIL") | ||
| ) | ||
| private static void addLayersAfterAll(EnvironmentAttributeSystem.Builder builder, Level level, CallbackInfo ci) { | ||
| EnvironmentAttributeEventsImpl.insertLayers(AttributeLayerPosition.AFTER_ALL, builder, level); | ||
| } | ||
| } |
Binary file added
BIN
+1.52 KB
...tributes-v1/src/main/resources/assets/fabric-environment-attributes-v1/icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
14 changes: 14 additions & 0 deletions
14
...environment-attributes-v1/src/main/resources/fabric-environment-attributes-v1.mixins.json
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 |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| { | ||
| "required": true, | ||
| "package": "net.fabricmc.fabric.mixin.environment.attribute", | ||
| "compatibilityLevel": "JAVA_25", | ||
| "mixins": [ | ||
| "EnvironmentAttributeSystemMixin" | ||
| ], | ||
| "injectors": { | ||
| "defaultRequire": 1 | ||
| }, | ||
| "overwrites": { | ||
| "requireAnnotations": true | ||
| } | ||
| } |
31 changes: 31 additions & 0 deletions
31
fabric-environment-attributes-v1/src/main/resources/fabric.mod.json
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 |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| { | ||
| "schemaVersion": 1, | ||
| "id": "fabric-environment-attributes-v1", | ||
| "name": "Fabric Environment Attributes API (v1)", | ||
| "version": "${version}", | ||
| "license": "Apache-2.0", | ||
| "icon": "assets/fabric-environment-attributes-v1/icon.png", | ||
| "contact" : { | ||
| "homepage": "https://fabricmc.net", | ||
| "irc": "irc://irc.esper.net:6667/fabric", | ||
| "issues": "https://github.com/FabricMC/fabric/issues", | ||
| "sources": "https://github.com/FabricMC/fabric" | ||
| }, | ||
| "authors": [ | ||
| "FabricMC" | ||
| ], | ||
| "entrypoints": { | ||
| }, | ||
| "depends": { | ||
| "fabricloader": ">=0.18.4", | ||
| "minecraft": ">=1.16-rc.3", | ||
| "fabric-api-base": "*" | ||
| }, | ||
| "description": "Fabric Environment Attributes API.", | ||
| "mixins": [ | ||
| "fabric-environment-attributes-v1.mixins.json" | ||
| ], | ||
| "custom": { | ||
| "fabric-api:module-lifecycle": "stable" | ||
| } | ||
| } |
37 changes: 37 additions & 0 deletions
37
.../java/net/fabricmc/fabric/test/environment/attribute/FabricEnvironmentAttributesTest.java
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 |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| /* | ||
| * Copyright (c) 2016, 2017, 2018, 2019 FabricMC | ||
| * | ||
| * 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 net.fabricmc.fabric.test.environment.attribute; | ||
|
|
||
| import net.minecraft.core.Registry; | ||
| import net.minecraft.core.registries.BuiltInRegistries; | ||
| import net.minecraft.resources.Identifier; | ||
| import net.minecraft.world.attribute.AttributeTypes; | ||
| import net.minecraft.world.attribute.EnvironmentAttribute; | ||
|
|
||
| import net.fabricmc.api.ModInitializer; | ||
|
|
||
| public class FabricEnvironmentAttributesTest implements ModInitializer { | ||
| public static final EnvironmentAttribute<Integer> TEST_COLOR = EnvironmentAttribute.builder(AttributeTypes.RGB_COLOR) | ||
| .defaultValue(0xFFFFFF) | ||
| .syncable() | ||
| .build(); | ||
|
|
||
| @Override | ||
| public void onInitialize() { | ||
| Registry.register(BuiltInRegistries.ENVIRONMENT_ATTRIBUTE, Identifier.fromNamespaceAndPath("fabric_environment_attributes", "test_color"), TEST_COLOR); | ||
| } | ||
| } |
19 changes: 19 additions & 0 deletions
19
fabric-environment-attributes-v1/src/testmod/resources/fabric.mod.json
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 |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| { | ||
| "schemaVersion": 1, | ||
| "id": "fabric-environment-attributes-v1-testmod", | ||
| "name": "Fabric Environment Attributes (v1) Test Mod", | ||
| "version": "1.0.0", | ||
| "environment": "*", | ||
| "license": "Apache-2.0", | ||
| "depends": { | ||
| "fabric-environment-attributes-v1": "*" | ||
| }, | ||
| "entrypoints": { | ||
| "main": [ | ||
| "net.fabricmc.fabric.test.environment.attribute.FabricEnvironmentAttributesTest" | ||
| ], | ||
| "fabric-client-gametest": [ | ||
| "net.fabricmc.fabric.test.environment.attribute.client.FabricEnvironmentAttributesClientTest" | ||
| ] | ||
| } | ||
| } |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.