mirror of
https://github.com/FabricMC/fabric.git
synced 2024-11-26 17:46:25 -05:00
Add common (client/server) FluidVariant attributes (#2095)
* Add common (client/server) FluidVariant properties * Remove density, move constants to FluidConstants, javadoc * Javadoc, downgrade exception to LOGGER.warn * attributes -> properties * Property -> attribute * Apply reviews * Use optional sounds, mix into BucketItem * Automatically implement Fluid#getBucketFillSound from the attribute * Update fabric-transfer-api-v1/src/main/java/net/fabricmc/fabric/api/transfer/v1/fluid/FluidConstants.java Co-authored-by: Juuxel <6596629+Juuxel@users.noreply.github.com> * Tests, a few fixes * Add gametest for viscosity, isLighterThanAir -> flowsUpwards * flowsUpwards -> isLighterThanAir Co-authored-by: Juuxel <6596629+Juuxel@users.noreply.github.com>
This commit is contained in:
parent
82a21997c3
commit
dbb7b03f94
12 changed files with 521 additions and 7 deletions
|
@ -32,6 +32,8 @@ import net.fabricmc.api.Environment;
|
|||
import net.fabricmc.fabric.api.client.render.fluid.v1.FluidRenderHandler;
|
||||
import net.fabricmc.fabric.api.client.render.fluid.v1.FluidRenderHandlerRegistry;
|
||||
import net.fabricmc.fabric.api.transfer.v1.fluid.FluidVariant;
|
||||
import net.fabricmc.fabric.api.transfer.v1.fluid.FluidVariantAttributes;
|
||||
import net.fabricmc.fabric.api.transfer.v1.fluid.FluidVariantAttributeHandler;
|
||||
|
||||
/**
|
||||
* Defines how {@linkplain FluidVariant fluid variants} of a given Fluid should be displayed to clients.
|
||||
|
@ -44,10 +46,12 @@ import net.fabricmc.fabric.api.transfer.v1.fluid.FluidVariant;
|
|||
@Environment(EnvType.CLIENT)
|
||||
public interface FluidVariantRenderHandler {
|
||||
/**
|
||||
* Return the name that should be used for the passed fluid variant.
|
||||
* @deprecated Implement {@link FluidVariantAttributeHandler#getName} instead.
|
||||
* This function will be removed in a future iteration of the API.
|
||||
*/
|
||||
@Deprecated(forRemoval = true)
|
||||
default Text getName(FluidVariant fluidVariant) {
|
||||
return fluidVariant.getFluid().getDefaultState().getBlockState().getBlock().getName();
|
||||
return FluidVariantAttributes.getName(fluidVariant);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -120,10 +124,12 @@ public interface FluidVariantRenderHandler {
|
|||
}
|
||||
|
||||
/**
|
||||
* Return {@code true} if this fluid should fill tanks from top.
|
||||
* @deprecated Implement {@link FluidVariantAttributeHandler#isLighterThanAir(FluidVariant)} instead.
|
||||
* This function will be removed in a future iteration of the API.
|
||||
*/
|
||||
@Deprecated(forRemoval = true)
|
||||
default boolean fillsFromTop(FluidVariant fluidVariant) {
|
||||
// By default, fluids should be filled from the bottom.
|
||||
return false;
|
||||
// By default, only fluids lighter than air should be filled from top.
|
||||
return FluidVariantAttributes.isLighterThanAir(fluidVariant);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -38,6 +38,7 @@ import net.fabricmc.api.EnvType;
|
|||
import net.fabricmc.api.Environment;
|
||||
import net.fabricmc.fabric.api.lookup.v1.custom.ApiProviderMap;
|
||||
import net.fabricmc.fabric.api.transfer.v1.fluid.FluidVariant;
|
||||
import net.fabricmc.fabric.api.transfer.v1.fluid.FluidVariantAttributes;
|
||||
|
||||
/**
|
||||
* Client-side display of fluid variants.
|
||||
|
@ -78,7 +79,9 @@ public class FluidVariantRendering {
|
|||
|
||||
/**
|
||||
* Return the name of the passed fluid variant.
|
||||
* @deprecated use {@link FluidVariantAttributes#getName} instead.
|
||||
*/
|
||||
@Deprecated(forRemoval = true)
|
||||
public static Text getName(FluidVariant fluidVariant) {
|
||||
return getHandlerOrDefault(fluidVariant.getFluid()).getName(fluidVariant);
|
||||
}
|
||||
|
@ -157,7 +160,9 @@ public class FluidVariantRendering {
|
|||
|
||||
/**
|
||||
* Return {@code true} if this fluid variant should be rendered as filling tanks from the top.
|
||||
* @deprecated use {@link FluidVariantAttributes#isLighterThanAir} instead.
|
||||
*/
|
||||
@Deprecated(forRemoval = true)
|
||||
public static boolean fillsFromTop(FluidVariant fluidVariant) {
|
||||
return getHandlerOrDefault(fluidVariant.getFluid()).fillsFromTop(fluidVariant);
|
||||
}
|
||||
|
|
|
@ -18,6 +18,8 @@ package net.fabricmc.fabric.api.transfer.v1.fluid;
|
|||
|
||||
import org.jetbrains.annotations.ApiStatus;
|
||||
|
||||
import net.minecraft.fluid.FlowableFluid;
|
||||
|
||||
/**
|
||||
* Constants for fluid transfer. In general, 1 bucket = 81000 droplets = 1 block.
|
||||
*
|
||||
|
@ -29,6 +31,9 @@ import org.jetbrains.annotations.ApiStatus;
|
|||
*/
|
||||
@ApiStatus.Experimental
|
||||
public final class FluidConstants {
|
||||
///////////////////////////
|
||||
// ==== FLUID UNITS ==== //
|
||||
///////////////////////////
|
||||
public static final long BUCKET = 81000;
|
||||
public static final long BOTTLE = 27000;
|
||||
public static final long BLOCK = 81000;
|
||||
|
@ -54,6 +59,26 @@ public final class FluidConstants {
|
|||
}
|
||||
}
|
||||
|
||||
// ==========================
|
||||
// ==== FLUID ATTRIBUTES ====
|
||||
// ==========================
|
||||
/**
|
||||
* Water temperature, in Kelvin.
|
||||
*/
|
||||
public static final int WATER_TEMPERATURE = 300;
|
||||
/**
|
||||
* Lava temperature, in Kelvin.
|
||||
*/
|
||||
public static final int LAVA_TEMPERATURE = 1300;
|
||||
|
||||
public static final int WATER_VISCOSITY = 1000;
|
||||
public static final int LAVA_VISCOSITY = 6000;
|
||||
public static final int LAVA_VISCOSITY_NETHER = 2000;
|
||||
/**
|
||||
* For flowable fluids, the viscosity should match {@code VISCOSITY_RATIO} * {@link FlowableFluid#getFlowSpeed}.
|
||||
*/
|
||||
public static final int VISCOSITY_RATIO = 200;
|
||||
|
||||
private FluidConstants() {
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,102 @@
|
|||
/*
|
||||
* 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.transfer.v1.fluid;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
import org.jetbrains.annotations.ApiStatus;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import net.minecraft.fluid.FlowableFluid;
|
||||
import net.minecraft.fluid.Fluid;
|
||||
import net.minecraft.item.BucketItem;
|
||||
import net.minecraft.sound.SoundEvent;
|
||||
import net.minecraft.text.Text;
|
||||
import net.minecraft.world.World;
|
||||
|
||||
/**
|
||||
* Defines the common attributes of {@linkplain FluidVariant fluid variants} of a given Fluid.
|
||||
* Register with {@link FluidVariantAttributes#register}.
|
||||
*
|
||||
* <p><b>Experimental feature</b>, we reserve the right to remove or change it without further notice.
|
||||
* The transfer API is a complex addition, and we want to be able to correct possible design mistakes.
|
||||
*/
|
||||
@ApiStatus.Experimental
|
||||
public interface FluidVariantAttributeHandler {
|
||||
/**
|
||||
* Return the name that should be used for the passed fluid variant.
|
||||
*/
|
||||
default Text getName(FluidVariant fluidVariant) {
|
||||
return fluidVariant.getFluid().getDefaultState().getBlockState().getBlock().getName();
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the sound corresponding to this fluid being filled, or none if no sound is available.
|
||||
*
|
||||
* <p>If a non-empty sound event is returned, {@link Fluid#getBucketFillSound} will return that sound.
|
||||
*/
|
||||
default Optional<SoundEvent> getFillSound(FluidVariant variant) {
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the sound corresponding to this fluid being emptied, or none if no sound is available.
|
||||
*
|
||||
* <p>If a non-empty sound event is returned, {@link BucketItem#playEmptyingSound} will play that sound.
|
||||
*/
|
||||
default Optional<SoundEvent> getEmptySound(FluidVariant variant) {
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
/**
|
||||
* Return an integer in [0, 15]: the light level emitted by this fluid, or 0 if it doesn't naturally emit light.
|
||||
*/
|
||||
default int getLuminance(FluidVariant variant) {
|
||||
return variant.getFluid().getDefaultState().getBlockState().getLuminance();
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a non-negative integer, representing the temperature of this fluid in Kelvin.
|
||||
* The reference values are {@value FluidConstants#WATER_TEMPERATURE} for water, and {@value FluidConstants#LAVA_TEMPERATURE} for lava.
|
||||
*/
|
||||
default int getTemperature(FluidVariant variant) {
|
||||
return FluidConstants.WATER_TEMPERATURE;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a positive integer, representing the viscosity of this fluid.
|
||||
* Fluids with lower viscosity generally flow faster than fluids with higher viscosity.
|
||||
*
|
||||
* <p>More precisely, viscosity should be {@value FluidConstants#VISCOSITY_RATIO} * {@link FlowableFluid#getFlowSpeed} for flowable fluids.
|
||||
* The reference values are {@value FluidConstants#WATER_VISCOSITY} for water,
|
||||
* {@value FluidConstants#LAVA_VISCOSITY_NETHER} for lava in ultrawarm dimensions (such as the nether),
|
||||
* and {@value FluidConstants#LAVA_VISCOSITY} for lava in other dimensions.
|
||||
*
|
||||
* @param world World if available, otherwise null.
|
||||
*/
|
||||
default int getViscosity(FluidVariant variant, @Nullable World world) {
|
||||
return FluidConstants.WATER_VISCOSITY;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return true if this fluid is lighter than air.
|
||||
* Fluids that are lighter than air generally flow upwards.
|
||||
*/
|
||||
default boolean isLighterThanAir(FluidVariant variant) {
|
||||
return false;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,188 @@
|
|||
/*
|
||||
* 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.transfer.v1.fluid;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
import org.jetbrains.annotations.ApiStatus;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import net.minecraft.fluid.FlowableFluid;
|
||||
import net.minecraft.fluid.Fluid;
|
||||
import net.minecraft.fluid.Fluids;
|
||||
import net.minecraft.sound.SoundEvent;
|
||||
import net.minecraft.sound.SoundEvents;
|
||||
import net.minecraft.text.Text;
|
||||
import net.minecraft.world.World;
|
||||
|
||||
import net.fabricmc.fabric.api.lookup.v1.custom.ApiProviderMap;
|
||||
import net.fabricmc.fabric.impl.transfer.TransferApiImpl;
|
||||
|
||||
/**
|
||||
* Common fluid variant attributes, accessible both client-side and server-side.
|
||||
*
|
||||
* <p><b>Experimental feature</b>, we reserve the right to remove or change it without further notice.
|
||||
* The transfer API is a complex addition, and we want to be able to correct possible design mistakes.
|
||||
*/
|
||||
@ApiStatus.Experimental
|
||||
public class FluidVariantAttributes {
|
||||
private static final ApiProviderMap<Fluid, FluidVariantAttributeHandler> HANDLERS = ApiProviderMap.create();
|
||||
private static final FluidVariantAttributeHandler DEFAULT_HANDLER = new FluidVariantAttributeHandler() { };
|
||||
|
||||
/**
|
||||
* Register an attribute handler for the passed fluid.
|
||||
*/
|
||||
public static void register(Fluid fluid, FluidVariantAttributeHandler handler) {
|
||||
if (HANDLERS.putIfAbsent(fluid, handler) != null) {
|
||||
throw new IllegalArgumentException("Duplicate handler registration for fluid " + fluid);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the attribute handler for the passed fluid, if available, and {@code null} otherwise.
|
||||
*/
|
||||
@Nullable
|
||||
public static FluidVariantAttributeHandler getHandler(Fluid fluid) {
|
||||
return HANDLERS.get(fluid);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the attribute handler for the passed fluid, if available, or the default instance otherwise.
|
||||
*/
|
||||
public static FluidVariantAttributeHandler getHandlerOrDefault(Fluid fluid) {
|
||||
FluidVariantAttributeHandler handler = HANDLERS.get(fluid);
|
||||
return handler == null ? DEFAULT_HANDLER : handler;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the name that should be used for the passed fluid variant.
|
||||
*/
|
||||
public static Text getName(FluidVariant variant) {
|
||||
return getHandlerOrDefault(variant.getFluid()).getName(variant);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the sound corresponding to a container of this fluid variant being filled if available,
|
||||
* or the default (water) filling sound otherwise.
|
||||
*/
|
||||
public static SoundEvent getFillSound(FluidVariant variant) {
|
||||
return getHandlerOrDefault(variant.getFluid()).getFillSound(variant)
|
||||
.or(() -> variant.getFluid().getBucketFillSound())
|
||||
.orElse(SoundEvents.ITEM_BUCKET_FILL);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the sound corresponding to a container of this fluid variant being emptied if available,
|
||||
* or the default (water) emptying sound otherwise.
|
||||
*/
|
||||
public static SoundEvent getEmptySound(FluidVariant variant) {
|
||||
return getHandlerOrDefault(variant.getFluid()).getEmptySound(variant).orElse(SoundEvents.ITEM_BUCKET_EMPTY);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return an integer in [0, 15]: the light level emitted by this fluid variant, or 0 if it doesn't naturally emit light.
|
||||
*/
|
||||
public static int getLuminance(FluidVariant variant) {
|
||||
int luminance = getHandlerOrDefault(variant.getFluid()).getLuminance(variant);
|
||||
|
||||
if (luminance < 0 || luminance > 15) {
|
||||
TransferApiImpl.LOGGER.warn("Broken FluidVariantAttributeHandler. Invalid luminance %d for fluid variant %s".formatted(luminance, variant));
|
||||
return DEFAULT_HANDLER.getLuminance(variant);
|
||||
}
|
||||
|
||||
return luminance;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a non-negative integer, representing the temperature of this fluid in Kelvin.
|
||||
* The reference values are {@value FluidConstants#WATER_TEMPERATURE} for water, and {@value FluidConstants#LAVA_TEMPERATURE} for lava.
|
||||
*/
|
||||
public static int getTemperature(FluidVariant variant) {
|
||||
int temperature = getHandlerOrDefault(variant.getFluid()).getTemperature(variant);
|
||||
|
||||
if (temperature < 0) {
|
||||
TransferApiImpl.LOGGER.warn("Broken FluidVariantAttributeHandler. Invalid temperature %d for fluid variant %s".formatted(temperature, variant));
|
||||
return DEFAULT_HANDLER.getTemperature(variant);
|
||||
}
|
||||
|
||||
return temperature;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a positive integer, representing the viscosity of this fluid variant.
|
||||
* Fluids with lower viscosity generally flow faster than fluids with higher viscosity.
|
||||
*
|
||||
* <p>More precisely, viscosity should be {@value FluidConstants#VISCOSITY_RATIO} * {@link FlowableFluid#getFlowSpeed} for flowable fluids.
|
||||
* The reference values are {@value FluidConstants#WATER_VISCOSITY} for water,
|
||||
* {@value FluidConstants#LAVA_VISCOSITY_NETHER} for lava in ultrawarm dimensions (such as the nether),
|
||||
* and {@value FluidConstants#LAVA_VISCOSITY} for lava in other dimensions.
|
||||
*
|
||||
* @param world World if available, otherwise null.
|
||||
*/
|
||||
public static int getViscosity(FluidVariant variant, @Nullable World world) {
|
||||
int viscosity = getHandlerOrDefault(variant.getFluid()).getViscosity(variant, world);
|
||||
|
||||
if (viscosity <= 0) {
|
||||
TransferApiImpl.LOGGER.warn("Broken FluidVariantAttributeHandler. Invalid viscosity %d for fluid variant %s".formatted(viscosity, variant));
|
||||
return DEFAULT_HANDLER.getViscosity(variant, world);
|
||||
}
|
||||
|
||||
return viscosity;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return true if this fluid is lighter than air.
|
||||
* Fluids that are lighter than air generally flow upwards.
|
||||
*/
|
||||
public static boolean isLighterThanAir(FluidVariant variant) {
|
||||
return getHandlerOrDefault(variant.getFluid()).isLighterThanAir(variant);
|
||||
}
|
||||
|
||||
static {
|
||||
register(Fluids.WATER, new FluidVariantAttributeHandler() {
|
||||
@Override
|
||||
public Optional<SoundEvent> getEmptySound(FluidVariant variant) {
|
||||
return Optional.of(SoundEvents.ITEM_BUCKET_EMPTY);
|
||||
}
|
||||
});
|
||||
register(Fluids.LAVA, new FluidVariantAttributeHandler() {
|
||||
@Override
|
||||
public Optional<SoundEvent> getFillSound(FluidVariant variant) {
|
||||
return Optional.of(SoundEvents.ITEM_BUCKET_FILL_LAVA);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<SoundEvent> getEmptySound(FluidVariant variant) {
|
||||
return Optional.of(SoundEvents.ITEM_BUCKET_EMPTY_LAVA);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getTemperature(FluidVariant variant) {
|
||||
return FluidConstants.LAVA_TEMPERATURE;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getViscosity(FluidVariant variant, @Nullable World world) {
|
||||
if (world != null && world.getDimension().isUltrawarm()) {
|
||||
return FluidConstants.LAVA_VISCOSITY_NETHER;
|
||||
} else {
|
||||
return FluidConstants.LAVA_VISCOSITY;
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
|
@ -0,0 +1,51 @@
|
|||
/*
|
||||
* 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.transfer;
|
||||
|
||||
import org.spongepowered.asm.mixin.Final;
|
||||
import org.spongepowered.asm.mixin.Mixin;
|
||||
import org.spongepowered.asm.mixin.Shadow;
|
||||
import org.spongepowered.asm.mixin.injection.At;
|
||||
import org.spongepowered.asm.mixin.injection.ModifyVariable;
|
||||
|
||||
import net.minecraft.fluid.Fluid;
|
||||
import net.minecraft.item.BucketItem;
|
||||
import net.minecraft.sound.SoundEvent;
|
||||
|
||||
import net.fabricmc.fabric.api.transfer.v1.fluid.FluidVariant;
|
||||
import net.fabricmc.fabric.api.transfer.v1.fluid.FluidVariantAttributeHandler;
|
||||
import net.fabricmc.fabric.api.transfer.v1.fluid.FluidVariantAttributes;
|
||||
|
||||
/**
|
||||
* Automatically uses the correct bucket emptying sound for
|
||||
* fluid attributes handlers overriding {@link FluidVariantAttributeHandler#getEmptySound}.
|
||||
*/
|
||||
@Mixin(BucketItem.class)
|
||||
public class BucketItemMixin {
|
||||
@Shadow
|
||||
@Final
|
||||
private Fluid fluid;
|
||||
|
||||
@ModifyVariable(
|
||||
method = "playEmptyingSound",
|
||||
at = @At("STORE"),
|
||||
index = 4
|
||||
)
|
||||
private SoundEvent hookEmptyingSound(SoundEvent previous) {
|
||||
return FluidVariantAttributes.getHandlerOrDefault(fluid).getEmptySound(FluidVariant.of(fluid)).orElse(previous);
|
||||
}
|
||||
}
|
|
@ -16,16 +16,28 @@
|
|||
|
||||
package net.fabricmc.fabric.mixin.transfer;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
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.CallbackInfoReturnable;
|
||||
|
||||
import net.minecraft.fluid.Fluid;
|
||||
import net.minecraft.sound.SoundEvent;
|
||||
|
||||
import net.fabricmc.fabric.api.transfer.v1.fluid.FluidVariant;
|
||||
import net.fabricmc.fabric.api.transfer.v1.fluid.FluidVariantAttributeHandler;
|
||||
import net.fabricmc.fabric.api.transfer.v1.fluid.FluidVariantAttributes;
|
||||
import net.fabricmc.fabric.impl.transfer.fluid.FluidVariantImpl;
|
||||
import net.fabricmc.fabric.impl.transfer.fluid.FluidVariantCache;
|
||||
|
||||
/**
|
||||
* Cache the FluidVariant with a null tag inside each Fluid directly.
|
||||
* <ul>
|
||||
* <li>Cache the FluidVariant with a null tag inside each Fluid directly.</li>
|
||||
* <li>Automatically uses the correct bucket filling sound for
|
||||
* fluid attributes handlers overriding {@link FluidVariantAttributeHandler#getEmptySound}.</li>
|
||||
* </ul>
|
||||
*/
|
||||
@Mixin(Fluid.class)
|
||||
@SuppressWarnings("unused")
|
||||
|
@ -37,4 +49,18 @@ public class FluidMixin implements FluidVariantCache {
|
|||
public FluidVariant fabric_getCachedFluidVariant() {
|
||||
return fabric_cachedFluidVariant;
|
||||
}
|
||||
|
||||
@Inject(
|
||||
method = "getBucketFillSound",
|
||||
at = @At("HEAD"),
|
||||
cancellable = true
|
||||
)
|
||||
public void hookGetBucketFillSound(CallbackInfoReturnable<Optional<SoundEvent>> cir) {
|
||||
Fluid fluid = (Fluid) (Object) this;
|
||||
Optional<SoundEvent> sound = FluidVariantAttributes.getHandlerOrDefault(fluid).getFillSound(FluidVariant.of(fluid));
|
||||
|
||||
if (sound.isPresent()) {
|
||||
cir.setReturnValue(sound);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -5,6 +5,7 @@
|
|||
"mixins": [
|
||||
"AbstractFurnaceBlockEntityMixin",
|
||||
"BucketItemAccessor",
|
||||
"BucketItemMixin",
|
||||
"DoubleInventoryAccessor",
|
||||
"DropperBlockMixin",
|
||||
"FluidMixin",
|
||||
|
|
|
@ -0,0 +1,50 @@
|
|||
/*
|
||||
* 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.transfer.gametests;
|
||||
|
||||
import static net.fabricmc.fabric.test.transfer.unittests.TestUtil.assertEquals;
|
||||
|
||||
import net.minecraft.fluid.Fluids;
|
||||
import net.minecraft.server.world.ServerWorld;
|
||||
import net.minecraft.test.GameTest;
|
||||
import net.minecraft.test.TestContext;
|
||||
|
||||
import net.fabricmc.fabric.api.gametest.v1.FabricGameTest;
|
||||
import net.fabricmc.fabric.api.transfer.v1.fluid.FluidConstants;
|
||||
import net.fabricmc.fabric.api.transfer.v1.fluid.FluidVariant;
|
||||
import net.fabricmc.fabric.api.transfer.v1.fluid.FluidVariantAttributes;
|
||||
|
||||
public class WorldDependentAttributesTest {
|
||||
@GameTest(structureName = FabricGameTest.EMPTY_STRUCTURE)
|
||||
public void testViscosity(TestContext context) {
|
||||
ServerWorld overworld = context.getWorld();
|
||||
ServerWorld nether = overworld.getServer().getWorld(ServerWorld.NETHER);
|
||||
FluidVariant lava = FluidVariant.of(Fluids.LAVA);
|
||||
|
||||
// Test that lava viscosity correctly depends on the dimension.
|
||||
assertEquals(FluidConstants.LAVA_VISCOSITY, FluidVariantAttributes.getViscosity(lava, overworld));
|
||||
assertEquals(FluidConstants.LAVA_VISCOSITY_NETHER, FluidVariantAttributes.getViscosity(lava, nether));
|
||||
|
||||
// Test that lava and water viscosities match VISCOSITY_RATIO * tick rate
|
||||
assertEquals(FluidConstants.WATER_VISCOSITY, FluidConstants.VISCOSITY_RATIO * Fluids.WATER.getTickRate(overworld));
|
||||
assertEquals(FluidConstants.WATER_VISCOSITY, FluidConstants.VISCOSITY_RATIO * Fluids.WATER.getTickRate(nether));
|
||||
assertEquals(FluidConstants.LAVA_VISCOSITY, FluidConstants.VISCOSITY_RATIO * Fluids.LAVA.getTickRate(overworld));
|
||||
assertEquals(FluidConstants.LAVA_VISCOSITY_NETHER, FluidConstants.VISCOSITY_RATIO * Fluids.LAVA.getTickRate(nether));
|
||||
|
||||
context.complete();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,58 @@
|
|||
/*
|
||||
* 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.transfer.unittests;
|
||||
|
||||
import static net.fabricmc.fabric.test.transfer.unittests.TestUtil.assertEquals;
|
||||
|
||||
import net.minecraft.fluid.Fluids;
|
||||
import net.minecraft.sound.SoundEvents;
|
||||
|
||||
import net.fabricmc.fabric.api.transfer.v1.fluid.FluidConstants;
|
||||
import net.fabricmc.fabric.api.transfer.v1.fluid.FluidVariant;
|
||||
import net.fabricmc.fabric.api.transfer.v1.fluid.FluidVariantAttributes;
|
||||
|
||||
/**
|
||||
* Test that fluid attributes for vanilla fluids have the correct values.
|
||||
*/
|
||||
public class AttributeTests {
|
||||
public static void run() {
|
||||
testWater();
|
||||
testLava();
|
||||
}
|
||||
|
||||
private static void testWater() {
|
||||
FluidVariant water = FluidVariant.of(Fluids.WATER);
|
||||
|
||||
assertEquals(SoundEvents.ITEM_BUCKET_FILL, FluidVariantAttributes.getFillSound(water));
|
||||
assertEquals(SoundEvents.ITEM_BUCKET_EMPTY, FluidVariantAttributes.getEmptySound(water));
|
||||
assertEquals(0, FluidVariantAttributes.getLuminance(water));
|
||||
assertEquals(FluidConstants.WATER_TEMPERATURE, FluidVariantAttributes.getTemperature(water));
|
||||
assertEquals(FluidConstants.WATER_VISCOSITY, FluidVariantAttributes.getViscosity(water, null));
|
||||
assertEquals(false, FluidVariantAttributes.isLighterThanAir(water));
|
||||
}
|
||||
|
||||
private static void testLava() {
|
||||
FluidVariant lava = FluidVariant.of(Fluids.LAVA);
|
||||
|
||||
assertEquals(SoundEvents.ITEM_BUCKET_FILL_LAVA, FluidVariantAttributes.getFillSound(lava));
|
||||
assertEquals(SoundEvents.ITEM_BUCKET_EMPTY_LAVA, FluidVariantAttributes.getEmptySound(lava));
|
||||
assertEquals(15, FluidVariantAttributes.getLuminance(lava));
|
||||
assertEquals(FluidConstants.LAVA_TEMPERATURE, FluidVariantAttributes.getTemperature(lava));
|
||||
assertEquals(FluidConstants.LAVA_VISCOSITY, FluidVariantAttributes.getViscosity(lava, null));
|
||||
assertEquals(false, FluidVariantAttributes.isLighterThanAir(lava));
|
||||
}
|
||||
}
|
|
@ -23,6 +23,7 @@ import net.fabricmc.api.ModInitializer;
|
|||
public class UnitTestsInitializer implements ModInitializer {
|
||||
@Override
|
||||
public void onInitialize() {
|
||||
AttributeTests.run();
|
||||
BaseStorageTests.run();
|
||||
FluidItemTests.run();
|
||||
FluidTests.run();
|
||||
|
|
|
@ -17,7 +17,8 @@
|
|||
"net.fabricmc.fabric.test.transfer.ingame.client.FluidVariantRenderTest"
|
||||
],
|
||||
"fabric-gametest": [
|
||||
"net.fabricmc.fabric.test.transfer.gametests.VanillaStorageTests"
|
||||
"net.fabricmc.fabric.test.transfer.gametests.VanillaStorageTests",
|
||||
"net.fabricmc.fabric.test.transfer.gametests.WorldDependentAttributesTest"
|
||||
]
|
||||
},
|
||||
"mixins": [
|
||||
|
|
Loading…
Reference in a new issue