Add fluid block and flowing fluid (#251)

[ci skip]

Squash of the following commits:

* Add way to register fluids and fluid attributes, WIP UNTESTED
* Move to correct package
* Update forge/build.gradle
* Add bucket item wrapper and add test mod
* Make it easier to declare attributes by suppliers
* Fix fabric support
* Change SimpleArchitecturyFluidAttributes to accept Supplier<Optional<T>>
* Make ArchitecturyLiquidBlock and ArchitecturyBucketItem accept Supplier
* Update testmod
* Link javadocs in the builder to make it easier to check
* Add ArchitecturyMobBucketItem and fix caps on ArchitecturyBucketItem
* Make SimpleArchitecturyFluidAttributes accept wildcard fluids
* getContainingFluid -> getContainedFluid
* Add supplier variant of the methods

Co-authored-by: Juuxel <6596629+Juuxel@users.noreply.github.com>
Co-authored-by: Max <maxh2709@gmail.com>
This commit is contained in:
shedaniel
2022-05-06 19:51:25 +08:00
committed by GitHub
parent 3532d24577
commit bd9b3e73e4
23 changed files with 2368 additions and 44 deletions

View File

@@ -49,6 +49,17 @@ shadowJar {
configurations = [project.configurations.shadowCommon]
classifier "dev-shadow"
// Replace classes with forge's version
exclude "dev/architectury/core/block/ArchitecturyLiquidBlock.class"
exclude "dev/architectury/core/fluid/ArchitecturyFlowingFluid.class"
exclude 'dev/architectury/core/fluid/ArchitecturyFlowingFluid$Source.class'
exclude 'dev/architectury/core/fluid/ArchitecturyFlowingFluid$Flowing.class'
exclude 'dev/architectury/core/item/ArchitecturyBucketItem.class'
exclude 'dev/architectury/core/item/ArchitecturyMobBucketItem.class'
relocate "dev.architectury.core.block.forge.imitator", "dev.architectury.core.block"
relocate "dev.architectury.core.fluid.forge.imitator", "dev.architectury.core.fluid"
relocate "dev.architectury.core.item.forge.imitator", "dev.architectury.core.item"
}
remapJar {

View File

@@ -0,0 +1,31 @@
/*
* This file is part of architectury.
* Copyright (C) 2020, 2021, 2022 architectury
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package dev.architectury.core.block.forge.imitator;
import net.minecraft.world.level.block.LiquidBlock;
import net.minecraft.world.level.material.FlowingFluid;
import java.util.function.Supplier;
public class ArchitecturyLiquidBlock extends LiquidBlock {
public ArchitecturyLiquidBlock(Supplier<? extends FlowingFluid> fluid, Properties properties) {
super(fluid, properties);
}
}

View File

@@ -0,0 +1,179 @@
/*
* This file is part of architectury.
* Copyright (C) 2020, 2021, 2022 architectury
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package dev.architectury.core.fluid.forge.imitator;
import com.google.common.base.MoreObjects;
import dev.architectury.core.fluid.ArchitecturyFluidAttributes;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.sounds.SoundEvent;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.Items;
import net.minecraft.world.level.BlockGetter;
import net.minecraft.world.level.LevelAccessor;
import net.minecraft.world.level.LevelReader;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.level.block.LiquidBlock;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.block.state.StateDefinition;
import net.minecraft.world.level.material.Fluid;
import net.minecraft.world.level.material.FluidState;
import net.minecraftforge.fluids.FluidAttributes;
import net.minecraftforge.fluids.ForgeFlowingFluid;
import org.jetbrains.annotations.NotNull;
import java.util.Optional;
public abstract class ArchitecturyFlowingFluid extends ForgeFlowingFluid {
private final ArchitecturyFluidAttributes attributes;
ArchitecturyFlowingFluid(ArchitecturyFluidAttributes attributes) {
super(toForgeProperties(attributes));
this.attributes = attributes;
}
private static Properties toForgeProperties(ArchitecturyFluidAttributes attributes) {
FluidAttributes.Builder forgeAttributes = new FluidAttributes.Builder(attributes.getSourceTexture(), attributes.getFlowingTexture(), (builder, fluid) ->
new ArchitecturyFluidAttributesForge(builder, fluid, attributes)) {
};
Properties forge = new Properties(attributes::getSourceFluid, attributes::getFlowingFluid, forgeAttributes);
if (attributes.canConvertToSource()) forge.canMultiply();
forge.slopeFindDistance(attributes.getSlopeFindDistance());
forge.levelDecreasePerBlock(attributes.getDropOff());
forge.bucket(() -> MoreObjects.firstNonNull(attributes.getBucketItem(), Items.AIR));
forge.tickRate(attributes.getTickDelay());
forge.explosionResistance(attributes.getExplosionResistance());
forge.block(() -> MoreObjects.firstNonNull(attributes.getBlock(), (LiquidBlock) Blocks.WATER));
return forge;
}
@Override
public Fluid getFlowing() {
return attributes.getFlowingFluid();
}
@Override
public Fluid getSource() {
return attributes.getSourceFluid();
}
@Override
protected boolean canConvertToSource() {
return attributes.canConvertToSource();
}
@Override
protected void beforeDestroyingBlock(LevelAccessor level, BlockPos pos, BlockState state) {
// Same implementation as in WaterFluid.
BlockEntity blockEntity = state.hasBlockEntity() ? level.getBlockEntity(pos) : null;
Block.dropResources(state, level, pos, blockEntity);
}
@Override
protected int getSlopeFindDistance(LevelReader level) {
return attributes.getSlopeFindDistance(level);
}
@Override
protected int getDropOff(LevelReader level) {
return attributes.getDropOff(level);
}
@Override
public Item getBucket() {
Item item = attributes.getBucketItem();
return item == null ? Items.AIR : item;
}
@Override
protected boolean canBeReplacedWith(FluidState state, BlockGetter level, BlockPos pos, Fluid fluid, Direction direction) {
// Same implementation as in WaterFluid.
return direction == Direction.DOWN && !this.isSame(fluid);
}
@Override
public int getTickDelay(LevelReader level) {
return attributes.getTickDelay(level);
}
@Override
protected float getExplosionResistance() {
return attributes.getExplosionResistance();
}
@Override
protected BlockState createLegacyBlock(FluidState state) {
LiquidBlock block = attributes.getBlock();
if (block == null) return Blocks.AIR.defaultBlockState();
return block.defaultBlockState().setValue(LiquidBlock.LEVEL, getLegacyLevel(state));
}
@NotNull
@Override
public Optional<SoundEvent> getPickupSound() {
return Optional.ofNullable(attributes.getFillSound());
}
@Override
public boolean isSame(Fluid fluid) {
return fluid == getSource() || fluid == getFlowing();
}
public static class Source extends ArchitecturyFlowingFluid {
public Source(ArchitecturyFluidAttributes attributes) {
super(attributes);
}
@Override
public int getAmount(FluidState state) {
return 8;
}
@Override
public boolean isSource(FluidState state) {
return true;
}
}
public static class Flowing extends ArchitecturyFlowingFluid {
public Flowing(ArchitecturyFluidAttributes attributes) {
super(attributes);
this.registerDefaultState(this.getStateDefinition().any().setValue(LEVEL, 7));
}
@Override
protected void createFluidStateDefinition(StateDefinition.Builder<Fluid, FluidState> builder) {
super.createFluidStateDefinition(builder);
builder.add(LEVEL);
}
@Override
public int getAmount(FluidState state) {
return state.getValue(LEVEL);
}
@Override
public boolean isSource(FluidState state) {
return false;
}
}
}

View File

@@ -0,0 +1,209 @@
/*
* This file is part of architectury.
* Copyright (C) 2020, 2021, 2022 architectury
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package dev.architectury.core.fluid.forge.imitator;
import com.google.common.base.MoreObjects;
import dev.architectury.core.fluid.ArchitecturyFluidAttributes;
import dev.architectury.hooks.fluid.forge.FluidStackHooksForge;
import net.minecraft.Util;
import net.minecraft.core.BlockPos;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.sounds.SoundEvent;
import net.minecraft.world.item.Rarity;
import net.minecraft.world.level.BlockAndTintGetter;
import net.minecraft.world.level.material.Fluid;
import net.minecraftforge.fluids.FluidAttributes;
import net.minecraftforge.fluids.FluidStack;
class ArchitecturyFluidAttributesForge extends FluidAttributes {
private final ArchitecturyFluidAttributes attributes;
private final String defaultTranslationKey;
public ArchitecturyFluidAttributesForge(Builder builder, Fluid fluid, ArchitecturyFluidAttributes attributes) {
super(addArchIntoBuilder(builder, attributes), fluid);
this.attributes = attributes;
this.defaultTranslationKey = Util.makeDescriptionId("fluid", fluid.getRegistryName());
}
private static Builder addArchIntoBuilder(Builder builder, ArchitecturyFluidAttributes attributes) {
builder.luminosity(attributes.getLuminosity())
.density(attributes.getDensity())
.temperature(attributes.getTemperature())
.viscosity(attributes.getViscosity());
if (attributes.isLighterThanAir()) builder.gaseous();
return builder;
}
@Override
public ResourceLocation getStillTexture() {
return attributes.getSourceTexture();
}
@Override
public ResourceLocation getStillTexture(FluidStack stack) {
return attributes.getSourceTexture(stack == null ? null : FluidStackHooksForge.fromForge(stack));
}
@Override
public ResourceLocation getStillTexture(BlockAndTintGetter level, BlockPos pos) {
return attributes.getSourceTexture(null, level, pos);
}
@Override
public ResourceLocation getFlowingTexture() {
return attributes.getFlowingTexture();
}
@Override
public ResourceLocation getFlowingTexture(FluidStack stack) {
return attributes.getFlowingTexture(stack == null ? null : FluidStackHooksForge.fromForge(stack));
}
@Override
public ResourceLocation getFlowingTexture(BlockAndTintGetter level, BlockPos pos) {
return attributes.getFlowingTexture(null, level, pos);
}
@Override
public int getColor() {
return attributes.getColor();
}
@Override
public int getColor(FluidStack stack) {
return attributes.getColor(stack == null ? null : FluidStackHooksForge.fromForge(stack));
}
@Override
public int getColor(BlockAndTintGetter level, BlockPos pos) {
return attributes.getColor(null, level, pos);
}
@Override
public int getLuminosity(FluidStack stack) {
return attributes.getLuminosity(stack == null ? null : FluidStackHooksForge.fromForge(stack));
}
@Override
public int getLuminosity(BlockAndTintGetter level, BlockPos pos) {
return attributes.getLuminosity(null, level, pos);
}
@Override
public int getDensity(FluidStack stack) {
return attributes.getDensity(stack == null ? null : FluidStackHooksForge.fromForge(stack));
}
@Override
public int getDensity(BlockAndTintGetter level, BlockPos pos) {
return attributes.getDensity(null, level, pos);
}
@Override
public int getTemperature(FluidStack stack) {
return attributes.getTemperature(stack == null ? null : FluidStackHooksForge.fromForge(stack));
}
@Override
public int getTemperature(BlockAndTintGetter level, BlockPos pos) {
return attributes.getTemperature(null, level, pos);
}
@Override
public int getViscosity(FluidStack stack) {
return attributes.getViscosity(stack == null ? null : FluidStackHooksForge.fromForge(stack));
}
@Override
public int getViscosity(BlockAndTintGetter level, BlockPos pos) {
return attributes.getViscosity(null, level, pos);
}
@Override
public boolean isGaseous(FluidStack stack) {
return attributes.isLighterThanAir(stack == null ? null : FluidStackHooksForge.fromForge(stack));
}
@Override
public boolean isGaseous(BlockAndTintGetter level, BlockPos pos) {
return attributes.isLighterThanAir(null, level, pos);
}
@Override
public Rarity getRarity() {
return attributes.getRarity();
}
@Override
public Rarity getRarity(FluidStack stack) {
return attributes.getRarity(stack == null ? null : FluidStackHooksForge.fromForge(stack));
}
@Override
public Rarity getRarity(BlockAndTintGetter level, BlockPos pos) {
return attributes.getRarity(null, level, pos);
}
@Override
public Component getDisplayName(FluidStack stack) {
return attributes.getName(stack == null ? null : FluidStackHooksForge.fromForge(stack));
}
@Override
public String getTranslationKey() {
return MoreObjects.firstNonNull(attributes.getTranslationKey(), defaultTranslationKey);
}
@Override
public String getTranslationKey(FluidStack stack) {
return MoreObjects.firstNonNull(attributes.getTranslationKey(stack == null ? null : FluidStackHooksForge.fromForge(stack)), defaultTranslationKey);
}
@Override
public SoundEvent getFillSound() {
return attributes.getFillSound();
}
@Override
public SoundEvent getFillSound(FluidStack stack) {
return attributes.getFillSound(stack == null ? null : FluidStackHooksForge.fromForge(stack));
}
@Override
public SoundEvent getFillSound(BlockAndTintGetter level, BlockPos pos) {
return attributes.getFillSound(null, level, pos);
}
@Override
public SoundEvent getEmptySound() {
return attributes.getEmptySound();
}
@Override
public SoundEvent getEmptySound(FluidStack stack) {
return attributes.getEmptySound(stack == null ? null : FluidStackHooksForge.fromForge(stack));
}
@Override
public SoundEvent getEmptySound(BlockAndTintGetter level, BlockPos pos) {
return attributes.getEmptySound(null, level, pos);
}
}

View File

@@ -0,0 +1,45 @@
/*
* This file is part of architectury.
* Copyright (C) 2020, 2021, 2022 architectury
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package dev.architectury.core.item.forge.imitator;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.world.item.BucketItem;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.material.Fluid;
import net.minecraftforge.common.capabilities.ICapabilityProvider;
import net.minecraftforge.fluids.capability.wrappers.FluidBucketWrapper;
import javax.annotation.Nullable;
import java.util.function.Supplier;
public class ArchitecturyBucketItem extends BucketItem {
public ArchitecturyBucketItem(Supplier<? extends Fluid> fluid, Properties properties) {
super(fluid, properties);
}
public final Fluid getContainedFluid() {
return getFluid();
}
@Override
public ICapabilityProvider initCapabilities(ItemStack stack, @Nullable CompoundTag nbt) {
return this.getClass() == ArchitecturyBucketItem.class ? new FluidBucketWrapper(stack) : super.initCapabilities(stack, nbt);
}
}

View File

@@ -0,0 +1,33 @@
/*
* This file is part of architectury.
* Copyright (C) 2020, 2021, 2022 architectury
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package dev.architectury.core.item.forge.imitator;
import net.minecraft.sounds.SoundEvent;
import net.minecraft.world.entity.EntityType;
import net.minecraft.world.item.MobBucketItem;
import net.minecraft.world.level.material.Fluid;
import java.util.function.Supplier;
public class ArchitecturyMobBucketItem extends MobBucketItem {
public ArchitecturyMobBucketItem(Supplier<? extends EntityType<?>> entity, Supplier<? extends Fluid> fluid, Supplier<? extends SoundEvent> sound, Properties properties) {
super(entity, fluid, sound, properties);
}
}

View File

@@ -0,0 +1,95 @@
/*
* This file is part of architectury.
* Copyright (C) 2020, 2021, 2022 architectury
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package dev.architectury.fluid.forge;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.world.level.material.Fluid;
import net.minecraftforge.fluids.FluidStack;
import org.jetbrains.annotations.ApiStatus;
import java.util.function.Function;
import java.util.function.Supplier;
import static dev.architectury.utils.Amount.toInt;
@ApiStatus.Internal
public enum FluidStackImpl implements dev.architectury.fluid.FluidStack.FluidStackAdapter<FluidStack> {
INSTANCE;
public static Function<dev.architectury.fluid.FluidStack, Object> toValue;
public static Function<Object, dev.architectury.fluid.FluidStack> fromValue;
public static dev.architectury.fluid.FluidStack.FluidStackAdapter<Object> adapt(Function<dev.architectury.fluid.FluidStack, Object> toValue, Function<Object, dev.architectury.fluid.FluidStack> fromValue) {
FluidStackImpl.toValue = toValue;
FluidStackImpl.fromValue = fromValue;
return (dev.architectury.fluid.FluidStack.FluidStackAdapter<Object>) (dev.architectury.fluid.FluidStack.FluidStackAdapter<?>) INSTANCE;
}
@Override
public FluidStack create(Supplier<Fluid> fluid, long amount, CompoundTag tag) {
return new FluidStack(fluid.get(), toInt(amount), tag);
}
@Override
public Supplier<Fluid> getRawFluidSupplier(FluidStack object) {
return object.getRawFluid().delegate;
}
@Override
public Fluid getFluid(FluidStack object) {
return object.getFluid();
}
@Override
public long getAmount(FluidStack object) {
return object.getAmount();
}
@Override
public void setAmount(FluidStack object, long amount) {
object.setAmount(toInt(amount));
}
@Override
public CompoundTag getTag(FluidStack value) {
return value.getTag();
}
@Override
public void setTag(FluidStack value, CompoundTag tag) {
value.setTag(tag);
}
@Override
public FluidStack copy(FluidStack value) {
return value.copy();
}
@Override
public int hashCode(FluidStack value) {
var code = 1;
code = 31 * code + value.getFluid().hashCode();
code = 31 * code + value.getAmount();
var tag = value.getTag();
if (tag != null)
code = 31 * code + tag.hashCode();
return code;
}
}

View File

@@ -20,16 +20,17 @@
package dev.architectury.hooks.fluid.forge;
import dev.architectury.fluid.FluidStack;
import dev.architectury.fluid.forge.FluidStackImpl;
public final class FluidStackHooksForge {
private FluidStackHooksForge() {
}
public static FluidStack fromForge(net.minecraftforge.fluids.FluidStack stack) {
return FluidStack.create(stack.getFluid().delegate, stack.getAmount(), stack.getTag());
return FluidStackImpl.fromValue.apply(stack);
}
public static net.minecraftforge.fluids.FluidStack toForge(FluidStack stack) {
return new net.minecraftforge.fluids.FluidStack(stack.getRawFluid(), (int) stack.getAmount(), stack.getTag());
return (net.minecraftforge.fluids.FluidStack) FluidStackImpl.toValue.apply(stack);
}
}