Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
11 changes: 11 additions & 0 deletions fabric-environment-attributes-v0/build.gradle
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'
])
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.v0;

/**
* 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
}
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.v0;

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);
}
}
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.v0.AttributeLayerPosition;
import net.fabricmc.fabric.api.environment.attribute.v0.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);
}
});
}
}
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.v0.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);
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"schemaVersion": 1,
"id": "fabric-environment-attributes-v0",
"name": "Fabric Environment Attributes API (v0)",
"version": "${version}",
"license": "Apache-2.0",
"icon": "assets/fabric-environment-attributes-v0/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-v0.mixins.json"
],
"custom": {
"fabric-api:module-lifecycle": "stable"
}
}
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);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"schemaVersion": 1,
"id": "fabric-environment-attributes-v0-testmod",
"name": "Fabric Environment Attributes (v0) Test Mod",
"version": "1.0.0",
"environment": "*",
"license": "Apache-2.0",
"depends": {
"fabric-environment-attributes-v0": "*"
},
"entrypoints": {
"main": [
"net.fabricmc.fabric.test.environment.attribute.FabricEnvironmentAttributesTest"
],
"fabric-client-gametest": [
"net.fabricmc.fabric.test.environment.attribute.client.FabricEnvironmentAttributesClientTest"
]
}
}
Loading
Loading