123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208 |
- package me.km.fluids;
- import java.util.Random;
- import javax.annotation.Nonnull;
- import me.km.KajetansMod;
- import me.km.utils.ReflectionUtils;
- import net.minecraft.block.Block;
- import net.minecraft.block.material.Material;
- import net.minecraft.block.state.IBlockState;
- import net.minecraft.creativetab.CreativeTabs;
- import net.minecraft.init.Blocks;
- import net.minecraft.init.SoundEvents;
- import net.minecraft.util.EnumFacing;
- import net.minecraft.util.EnumParticleTypes;
- import net.minecraft.util.SoundCategory;
- import net.minecraft.util.math.BlockPos;
- import net.minecraft.world.World;
- import net.minecraftforge.fluids.BlockFluidClassic;
- import net.minecraftforge.fluids.Fluid;
- public abstract class BlockFluidBase extends BlockFluidClassic
- {
- public BlockFluidBase(Fluid fluid, Material material)
- {
- super(fluid, material);
- this.setRegistryName(fluid.getName());
- super.setUnlocalizedName(fluid.getUnlocalizedName());
- super.setCreativeTab(CreativeTabs.BUILDING_BLOCKS);
- }
-
- public void registerBlockModel()
- {
- KajetansMod.proxy.registerFluidModel(this);
- }
- @Override
- public void updateTick(@Nonnull World w, @Nonnull BlockPos pos, @Nonnull IBlockState state, @Nonnull Random rand)
- {
- if(!isSourceBlock(w, pos))
- {
- int adjacentSourceBlocks =
- (isSourceBlock(w, pos.north()) ? 1 : 0) +
- (isSourceBlock(w, pos.south()) ? 1 : 0) +
- (isSourceBlock(w, pos.east()) ? 1 : 0) +
- (isSourceBlock(w, pos.west()) ? 1 : 0);
- if (adjacentSourceBlocks >= 2 && (w.getBlockState(pos.up(densityDir)).getMaterial().isSolid() || isSourceBlock(w, pos.up(densityDir))))
- {
- w.setBlockState(pos, state.withProperty(LEVEL, 0));
- }
- }
- int quantaRemaining = quantaPerBlock - state.getValue(LEVEL);
- int expQuanta;
- // check adjacent block levels if non-source
- if(quantaRemaining < quantaPerBlock)
- {
- if (w.getBlockState(pos.add( 0, -densityDir, 0)).getBlock() == this ||
- w.getBlockState(pos.add(-1, -densityDir, 0)).getBlock() == this ||
- w.getBlockState(pos.add( 1, -densityDir, 0)).getBlock() == this ||
- w.getBlockState(pos.add( 0, -densityDir, -1)).getBlock() == this ||
- w.getBlockState(pos.add( 0, -densityDir, 1)).getBlock() == this)
- {
- expQuanta = quantaPerBlock - 1;
- }
- else
- {
- int maxQuanta = -100;
- maxQuanta = getLargerQuanta(w, pos.add(-1, 0, 0), maxQuanta);
- maxQuanta = getLargerQuanta(w, pos.add( 1, 0, 0), maxQuanta);
- maxQuanta = getLargerQuanta(w, pos.add( 0, 0, -1), maxQuanta);
- maxQuanta = getLargerQuanta(w, pos.add( 0, 0, 1), maxQuanta);
- expQuanta = maxQuanta - 1;
- }
- // decay calculation
- if(expQuanta != quantaRemaining)
- {
- quantaRemaining = expQuanta;
- if(expQuanta <= 0)
- {
- w.setBlockToAir(pos);
- }
- else
- {
- w.setBlockState(pos, state.withProperty(LEVEL, quantaPerBlock - expQuanta), 2);
- w.scheduleUpdate(pos, this, tickRate);
- w.notifyNeighborsOfStateChange(pos, this, false);
- }
- }
- }
- // This is a "source" block, set meta to zero, and send a server only update
- else if(quantaRemaining >= quantaPerBlock)
- {
- w.setBlockState(pos, this.getDefaultState(), 2);
- }
- // Flow vertically if possible
- if(canDisplace(w, pos.up(densityDir)))
- {
- BlockPos bpos = pos.up(densityDir);
- Block b = w.getBlockState(bpos).getBlock();
- if(b == Blocks.LAVA || b == Blocks.WATER || b == Blocks.FLOWING_LAVA || b == Blocks.FLOWING_WATER ||
- (b != this && b instanceof net.minecraftforge.fluids.BlockFluidBase))
- {
- w.setBlockState(bpos, getFlownOnMixingBlock(b, ReflectionUtils.getBlockMaterial(b)));
- this.triggerMixEffects(w, bpos);
- return;
- }
- flowIntoBlock(w, pos.up(densityDir), 1);
- return;
- }
- // Flow outward if possible
- int flowMeta = quantaPerBlock - quantaRemaining + 1;
- if(flowMeta >= quantaPerBlock)
- {
- return;
- }
- if(isSourceBlock(w, pos) || !isFlowingVertically(w, pos))
- {
- if(w.getBlockState(pos.down(densityDir)).getBlock() == this)
- {
- flowMeta = 1;
- }
- boolean flowTo[] = getOptimalFlowDirections(w, pos);
- if(flowTo[0]) flowIntoBlock(w, pos.add(-1, 0, 0), flowMeta);
- if(flowTo[1]) flowIntoBlock(w, pos.add( 1, 0, 0), flowMeta);
- if(flowTo[2]) flowIntoBlock(w, pos.add( 0, 0, -1), flowMeta);
- if(flowTo[3]) flowIntoBlock(w, pos.add( 0, 0, 1), flowMeta);
- }
- }
-
- @Override
- public void onBlockAdded(World worldIn, BlockPos pos, IBlockState state)
- {
- checkForMixing(worldIn, pos, state);
- super.onBlockAdded(worldIn, pos, state);
- }
- @Override
- public void neighborChanged(IBlockState state, World worldIn, BlockPos pos, Block blockIn, BlockPos fromPos)
- {
- checkForMixing(worldIn, pos, state);
- super.neighborChanged(state, worldIn, pos, blockIn, fromPos);
- }
-
- private boolean checkForMixing(World worldIn, BlockPos pos, IBlockState state)
- {
- boolean flag = false;
- Block b = null;
- for (EnumFacing enumfacing : EnumFacing.values())
- {
- if (enumfacing != EnumFacing.DOWN)
- {
- b = worldIn.getBlockState(pos.offset(enumfacing)).getBlock();
- if(b == Blocks.LAVA || b == Blocks.WATER || b == Blocks.FLOWING_LAVA || b == Blocks.FLOWING_WATER ||
- (b != this && b instanceof net.minecraftforge.fluids.BlockFluidBase))
- {
- flag = true;
- break;
- }
- }
- }
- if(flag)
- {
- Integer integer = state.getValue(LEVEL);
- if (integer == 0)
- {
- worldIn.setBlockState(pos, getStrongMixingBlock(b, ReflectionUtils.getBlockMaterial(b)));
- this.triggerMixEffects(worldIn, pos);
- return true;
- }
- else
- {
- worldIn.setBlockState(pos, getWeakMixingBlock(b, ReflectionUtils.getBlockMaterial(b)));
- this.triggerMixEffects(worldIn, pos);
- return true;
- }
- }
- return false;
- }
-
- protected abstract IBlockState getStrongMixingBlock(Block b, Material m);
-
- protected abstract IBlockState getWeakMixingBlock(Block b, Material m);
-
- protected abstract IBlockState getFlownOnMixingBlock(Block b, Material m);
-
- protected void triggerMixEffects(World worldIn, BlockPos pos)
- {
- double d0 = pos.getX();
- double d1 = pos.getY();
- double d2 = pos.getZ();
-
- worldIn.playSound(null, pos, SoundEvents.BLOCK_LAVA_EXTINGUISH, SoundCategory.BLOCKS, 0.5F, 2.6F + (worldIn.rand.nextFloat() - worldIn.rand.nextFloat()) * 0.8F);
- for (int i = 0; i < 8; ++i)
- {
- worldIn.spawnParticle(EnumParticleTypes.SMOKE_LARGE, d0 + Math.random(), d1 + 1.2d, d2 + Math.random(), 0, 0, 0, new int[0]);
- }
- }
- }
|