Skip to content

Commit

Permalink
add: fluid registration
Browse files Browse the repository at this point in the history
  • Loading branch information
MegumiKasuga committed Nov 18, 2024
1 parent 91bd98e commit 9fa8f9e
Show file tree
Hide file tree
Showing 12 changed files with 368 additions and 45 deletions.
Original file line number Diff line number Diff line change
@@ -1,20 +1,52 @@
{
"variants": {
"level=0": {},
"level=1": {},
"level=2": {},
"level=3": {},
"level=4": {},
"level=5": {},
"level=6": {},
"level=7": {},
"level=8": {},
"level=9": {},
"level=10": {},
"level=11": {},
"level=12": {},
"level=13": {},
"level=14": {},
"level=15": {}
"level=0": {
"model": "minecraft:block/air"
},
"level=1": {
"model": "minecraft:block/air"
},
"level=2": {
"model": "minecraft:block/air"
},
"level=3": {
"model": "minecraft:block/air"
},
"level=4": {
"model": "minecraft:block/air"
},
"level=5": {
"model": "minecraft:block/air"
},
"level=6": {
"model": "minecraft:block/air"
},
"level=7": {
"model": "minecraft:block/air"
},
"level=8": {
"model": "minecraft:block/air"
},
"level=9": {
"model": "minecraft:block/air"
},
"level=10": {
"model": "minecraft:block/air"
},
"level=11": {
"model": "minecraft:block/air"
},
"level=12": {
"model": "minecraft:block/air"
},
"level=13": {
"model": "minecraft:block/air"
},
"level=14": {
"model": "minecraft:block/air"
},
"level=15": {
"model": "minecraft:block/air"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"values": [
"kasuga_lib:example_fluid_still",
"kasuga_lib:example_fluid_flow"
]
}
169 changes: 169 additions & 0 deletions src/main/java/kasuga/lib/core/base/BucketItem.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
package kasuga.lib.core.base;

import kasuga.lib.core.util.LazyRecomputable;
import net.minecraft.advancements.CriteriaTriggers;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.core.particles.ParticleTypes;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.sounds.SoundEvent;
import net.minecraft.sounds.SoundEvents;
import net.minecraft.sounds.SoundSource;
import net.minecraft.stats.Stats;
import net.minecraft.tags.FluidTags;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResultHolder;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.ItemUtils;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.LevelAccessor;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.BucketPickup;
import net.minecraft.world.level.block.LiquidBlockContainer;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.gameevent.GameEvent;
import net.minecraft.world.level.material.FlowingFluid;
import net.minecraft.world.level.material.Fluid;
import net.minecraft.world.level.material.Fluids;
import net.minecraft.world.level.material.Material;
import net.minecraft.world.phys.BlockHitResult;
import net.minecraft.world.phys.HitResult;
import net.minecraftforge.common.SoundActions;
import net.minecraftforge.event.ForgeEventFactory;
import net.minecraftforge.fluids.FluidStack;
import net.minecraftforge.fluids.FluidUtil;

import javax.annotation.Nullable;
import java.util.Optional;
import java.util.function.Supplier;

public class BucketItem extends net.minecraft.world.item.BucketItem {

private final LazyRecomputable<Fluid> fluidSupplier;

public BucketItem(Supplier<? extends Fluid> supplier, Properties builder) {
super(supplier, builder);
fluidSupplier = LazyRecomputable.of(supplier::get);
}

public InteractionResultHolder<ItemStack> use(Level pLevel, Player pPlayer, InteractionHand pHand) {
Fluid content = fluidSupplier.get();
ItemStack itemstack = pPlayer.getItemInHand(pHand);
BlockHitResult blockhitresult = getPlayerPOVHitResult(pLevel, pPlayer, content == Fluids.EMPTY ? net.minecraft.world.level.ClipContext.Fluid.SOURCE_ONLY : net.minecraft.world.level.ClipContext.Fluid.NONE);
InteractionResultHolder<ItemStack> ret = ForgeEventFactory.onBucketUse(pPlayer, pLevel, itemstack, blockhitresult);
if (ret != null) {
return ret;
} else if (blockhitresult.getType() == HitResult.Type.MISS) {
return InteractionResultHolder.pass(itemstack);
} else if (blockhitresult.getType() != HitResult.Type.BLOCK) {
return InteractionResultHolder.pass(itemstack);
} else {
BlockPos blockpos = blockhitresult.getBlockPos();
Direction direction = blockhitresult.getDirection();
BlockPos blockpos1 = blockpos.relative(direction);
if (pLevel.mayInteract(pPlayer, blockpos) && pPlayer.mayUseItemAt(blockpos1, direction, itemstack)) {
BlockState blockstate1;
if (content == Fluids.EMPTY) {
blockstate1 = pLevel.getBlockState(blockpos);
if (blockstate1.getBlock() instanceof BucketPickup) {
BucketPickup bucketpickup = (BucketPickup)blockstate1.getBlock();
ItemStack itemstack1 = bucketpickup.pickupBlock(pLevel, blockpos, blockstate1);
if (!itemstack1.isEmpty()) {
pPlayer.awardStat(Stats.ITEM_USED.get(this));
bucketpickup.getPickupSound(blockstate1).ifPresent((p_150709_) -> {
pPlayer.playSound(p_150709_, 1.0F, 1.0F);
});
pLevel.gameEvent(pPlayer, GameEvent.FLUID_PICKUP, blockpos);
ItemStack itemstack2 = ItemUtils.createFilledResult(itemstack, pPlayer, itemstack1);
if (!pLevel.isClientSide) {
CriteriaTriggers.FILLED_BUCKET.trigger((ServerPlayer)pPlayer, itemstack1);
}

return InteractionResultHolder.sidedSuccess(itemstack2, pLevel.isClientSide());
}
}

return InteractionResultHolder.fail(itemstack);
} else {
blockstate1 = pLevel.getBlockState(blockpos);
BlockPos blockpos2 = this.canBlockContainFluid(pLevel, blockpos, blockstate1) ? blockpos : blockpos1;
if (this.emptyContents(pPlayer, pLevel, blockpos2, blockhitresult, itemstack)) {
this.checkExtraContent(pPlayer, pLevel, itemstack, blockpos2);
if (pPlayer instanceof ServerPlayer) {
CriteriaTriggers.PLACED_BLOCK.trigger((ServerPlayer)pPlayer, blockpos2, itemstack);
}

pPlayer.awardStat(Stats.ITEM_USED.get(this));
return InteractionResultHolder.sidedSuccess(getEmptySuccessItem(itemstack, pPlayer), pLevel.isClientSide());
} else {
return InteractionResultHolder.fail(itemstack);
}
}
} else {
return InteractionResultHolder.fail(itemstack);
}
}
}

public boolean emptyContents(@Nullable Player p_150716_, Level p_150717_, BlockPos p_150718_, @Nullable BlockHitResult p_150719_, @Nullable ItemStack container) {
Fluid content = fluidSupplier.get();
if (!(content instanceof FlowingFluid)) {
return false;
} else {
BlockState blockstate = p_150717_.getBlockState(p_150718_);
Block block = blockstate.getBlock();
Material material = blockstate.getMaterial();
boolean flag = blockstate.canBeReplaced(content);
boolean flag1 = blockstate.isAir() || flag || block instanceof LiquidBlockContainer && ((LiquidBlockContainer)block).canPlaceLiquid(p_150717_, p_150718_, blockstate, content);
Optional<FluidStack> containedFluidStack = Optional.ofNullable(container).flatMap(FluidUtil::getFluidContained);
if (!flag1) {
return p_150719_ != null && this.emptyContents(p_150716_, p_150717_, p_150719_.getBlockPos().relative(p_150719_.getDirection()), (BlockHitResult)null, container);
} else if (containedFluidStack.isPresent() && content.getFluidType().isVaporizedOnPlacement(p_150717_, p_150718_, (FluidStack)containedFluidStack.get())) {
content.getFluidType().onVaporize(p_150716_, p_150717_, p_150718_, (FluidStack)containedFluidStack.get());
return true;
} else if (p_150717_.dimensionType().ultraWarm() && content.is(FluidTags.WATER)) {
int i = p_150718_.getX();
int j = p_150718_.getY();
int k = p_150718_.getZ();
p_150717_.playSound(p_150716_, p_150718_, SoundEvents.FIRE_EXTINGUISH, SoundSource.BLOCKS, 0.5F, 2.6F + (p_150717_.random.nextFloat() - p_150717_.random.nextFloat()) * 0.8F);

for(int l = 0; l < 8; ++l) {
p_150717_.addParticle(ParticleTypes.LARGE_SMOKE, (double)i + Math.random(), (double)j + Math.random(), (double)k + Math.random(), 0.0, 0.0, 0.0);
}

return true;
} else if (block instanceof LiquidBlockContainer && ((LiquidBlockContainer)block).canPlaceLiquid(p_150717_, p_150718_, blockstate, content)) {
((LiquidBlockContainer)block).placeLiquid(p_150717_, p_150718_, blockstate, ((FlowingFluid)content).getSource(false));
this.playEmptySound(p_150716_, p_150717_, p_150718_);
return true;
} else {
if (!p_150717_.isClientSide && flag && !material.isLiquid()) {
p_150717_.destroyBlock(p_150718_, true);
}

if (!p_150717_.setBlock(p_150718_, content.defaultFluidState().createLegacyBlock(), 11) && !blockstate.getFluidState().isSource()) {
return false;
} else {
this.playEmptySound(p_150716_, p_150717_, p_150718_);
return true;
}
}
}
}

protected void playEmptySound(@Nullable Player pPlayer, LevelAccessor pLevel, BlockPos pPos) {
Fluid content = fluidSupplier.get();
SoundEvent soundevent = content.getFluidType().getSound(pPlayer, pLevel, pPos, SoundActions.BUCKET_EMPTY);
if (soundevent == null) {
soundevent = content.is(FluidTags.LAVA) ? SoundEvents.BUCKET_EMPTY_LAVA : SoundEvents.BUCKET_EMPTY;
}

pLevel.playSound(pPlayer, pPos, soundevent, SoundSource.BLOCKS, 1.0F, 1.0F);
pLevel.gameEvent(pPlayer, GameEvent.FLUID_PLACE, pPos);
}

protected boolean canBlockContainFluid(Level worldIn, BlockPos posIn, BlockState blockstate) {
return blockstate.getBlock() instanceof LiquidBlockContainer && ((LiquidBlockContainer)blockstate.getBlock()).canPlaceLiquid(worldIn, posIn, blockstate, fluidSupplier.get());
}
}
12 changes: 7 additions & 5 deletions src/main/java/kasuga/lib/example_env/AllExampleElements.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package kasuga.lib.example_env;

import kasuga.lib.KasugaLib;
import kasuga.lib.core.base.BucketItem;
import kasuga.lib.core.menu.base.GuiBinding;
import kasuga.lib.core.menu.base.GuiMenu;
import kasuga.lib.core.menu.base.GuiMenuRegistry;
import kasuga.lib.core.menu.base.GuiMenuType;
import kasuga.lib.core.util.Envs;
import kasuga.lib.example_env.block.fluid.ExampleFluid;
import kasuga.lib.example_env.block.fluid.ExampleFluidBlock;
import kasuga.lib.example_env.block.green_apple.GreenAppleBlock;
import kasuga.lib.example_env.block.green_apple.GreenAppleItem;
import kasuga.lib.example_env.block.green_apple.GreenAppleTile;
Expand Down Expand Up @@ -117,16 +120,15 @@ public class AllExampleElements {
*/


/*

public static final FluidReg<ExampleFluid> exampleFluid = new FluidReg<ExampleFluid>("example_fluid")
.still(ExampleFluid::new, "block/fluid/water_still")
.flow(ExampleFluid::new, "block/fluid/water_flow")
.flow(ExampleFluid.Flowing::new, "block/fluid/water_flow")
.numericProperties(1, 8, 3, 10)
.overlayTexPath("block/fluid/water_overlay")
.bucketItem(BucketItem::new)
.blockType(ExampleFluidBlock::new)
.submit(testRegistry);
*/
.submit(REGISTRY);

public static final MenuReg<GreenAppleMenu, GreenAppleScreen> apple =
new MenuReg<GreenAppleMenu, GreenAppleScreen>("green_apple_screen")
Expand Down
48 changes: 48 additions & 0 deletions src/main/java/kasuga/lib/example_env/block/fluid/ExampleFluid.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package kasuga.lib.example_env.block.fluid;

import kasuga.lib.registrations.common.FluidReg;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.level.block.state.StateDefinition;
import net.minecraft.world.level.material.FlowingFluid;
import net.minecraft.world.level.material.Fluid;
import net.minecraft.world.level.material.FluidState;
import net.minecraftforge.common.extensions.IForgeFluid;
import net.minecraftforge.fluids.FluidType;
import net.minecraftforge.fluids.ForgeFlowingFluid;
import net.minecraftforge.registries.RegistryObject;
import org.jetbrains.annotations.NotNull;

public class ExampleFluid extends ForgeFlowingFluid {


public ExampleFluid(Properties properties) {
super(properties);
}

@Override
protected void createFluidStateDefinition(StateDefinition.Builder<Fluid, FluidState> pBuilder) {
super.createFluidStateDefinition(pBuilder.add(LEVEL));
}

@Override
public boolean isSource(FluidState fluidState) {
return true;
}

@Override
public int getAmount(@NotNull FluidState fluidState) {
return isSource(fluidState) ? 8 : (Integer) fluidState.getValue(LEVEL);
}

public static class Flowing extends ExampleFluid {

public Flowing(Properties properties) {
super(properties);
}

@Override
public boolean isSource(FluidState fluidState) {
return false;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package kasuga.lib.example_env.block.fluid;

import net.minecraft.world.item.context.BlockPlaceContext;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.LiquidBlock;
import net.minecraft.world.level.block.state.BlockBehaviour;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.block.state.StateDefinition;
import net.minecraft.world.level.material.FlowingFluid;
import net.minecraftforge.fluids.ForgeFlowingFluid;
import net.minecraftforge.fluids.IFluidBlock;
import org.jetbrains.annotations.Nullable;

import java.util.function.Supplier;

public class ExampleFluidBlock extends LiquidBlock {

public ExampleFluidBlock(Supplier<? extends ForgeFlowingFluid> supplier, Properties properties) {
super(supplier, properties);
}

@Override
public @Nullable BlockState getStateForPlacement(BlockPlaceContext pContext) {
return super.getStateForPlacement(pContext).setValue(LEVEL, 8);
}
}
11 changes: 7 additions & 4 deletions src/main/java/kasuga/lib/registrations/TagReg.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,18 @@ public abstract class TagReg<T> extends Reg {
public ResourceLocation location = null;

public final String otherNamespace;
public final String path;

public TagReg(String registrationKey) {
public TagReg(String registrationKey, String path) {
super(registrationKey);
otherNamespace = null;
this.path = path;
}

public TagReg(ResourceLocation location) {
super(location.getPath());
otherNamespace = location.getNamespace();
public TagReg(String namespace, String registrationKey, String path) {
super(registrationKey);
otherNamespace = namespace;
this.path = path;
}

public abstract TagKey<T> tag();
Expand Down
Loading

0 comments on commit 9fa8f9e

Please sign in to comment.