CreateMod/src/main/java/com/simibubi/create/content/contraptions/fluids/pipes/EncasedPipeBlock.java
simibubi bf1c86ed87 Flow transfer
- Fluid pipes no longer reset their contained flows when changed to/from their windowed or encased variant
2021-06-16 22:32:58 +02:00

151 lines
5.3 KiB
Java

package com.simibubi.create.content.contraptions.fluids.pipes;
import static net.minecraft.state.properties.BlockStateProperties.DOWN;
import static net.minecraft.state.properties.BlockStateProperties.EAST;
import static net.minecraft.state.properties.BlockStateProperties.NORTH;
import static net.minecraft.state.properties.BlockStateProperties.SOUTH;
import static net.minecraft.state.properties.BlockStateProperties.UP;
import static net.minecraft.state.properties.BlockStateProperties.WEST;
import java.util.Map;
import java.util.Random;
import com.simibubi.create.AllBlocks;
import com.simibubi.create.AllTileEntities;
import com.simibubi.create.content.contraptions.fluids.FluidPropagator;
import com.simibubi.create.content.contraptions.fluids.FluidTransportBehaviour;
import com.simibubi.create.content.contraptions.wrench.IWrenchable;
import com.simibubi.create.content.schematics.ISpecialBlockItemRequirement;
import com.simibubi.create.content.schematics.ItemRequirement;
import com.simibubi.create.foundation.utility.Iterate;
import net.minecraft.block.Block;
import net.minecraft.block.BlockState;
import net.minecraft.block.SixWayBlock;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.item.ItemUseContext;
import net.minecraft.network.DebugPacketSender;
import net.minecraft.state.BooleanProperty;
import net.minecraft.state.StateContainer.Builder;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.ActionResultType;
import net.minecraft.util.Direction;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.RayTraceResult;
import net.minecraft.world.IBlockReader;
import net.minecraft.world.TickPriority;
import net.minecraft.world.World;
import net.minecraft.world.server.ServerWorld;
public class EncasedPipeBlock extends Block implements IWrenchable, ISpecialBlockItemRequirement {
public static final Map<Direction, BooleanProperty> FACING_TO_PROPERTY_MAP = SixWayBlock.FACING_TO_PROPERTY_MAP;
public EncasedPipeBlock(Properties p_i48339_1_) {
super(p_i48339_1_);
setDefaultState(getDefaultState().with(NORTH, false)
.with(SOUTH, false)
.with(DOWN, false)
.with(UP, false)
.with(WEST, false)
.with(EAST, false));
}
@Override
protected void fillStateContainer(Builder<Block, BlockState> builder) {
builder.add(NORTH, EAST, SOUTH, WEST, UP, DOWN);
super.fillStateContainer(builder);
}
@Override
public boolean hasTileEntity(BlockState state) {
return true;
}
@Override
public void onReplaced(BlockState state, World world, BlockPos pos, BlockState newState, boolean isMoving) {
boolean blockTypeChanged = state.getBlock() != newState.getBlock();
if (blockTypeChanged && !world.isRemote)
FluidPropagator.propagateChangedPipe(world, pos, state);
if (state.hasTileEntity() && (blockTypeChanged || !newState.hasTileEntity()))
world.removeTileEntity(pos);
}
@Override
public void onBlockAdded(BlockState state, World world, BlockPos pos, BlockState oldState, boolean isMoving) {
if (!world.isRemote && state != oldState)
world.getPendingBlockTicks()
.scheduleTick(pos, this, 1, TickPriority.HIGH);
}
@Override
public ItemStack getPickBlock(BlockState state, RayTraceResult target, IBlockReader world, BlockPos pos,
PlayerEntity player) {
return AllBlocks.FLUID_PIPE.asStack();
}
@Override
public void neighborChanged(BlockState state, World world, BlockPos pos, Block otherBlock, BlockPos neighborPos,
boolean isMoving) {
DebugPacketSender.func_218806_a(world, pos);
Direction d = FluidPropagator.validateNeighbourChange(state, world, pos, otherBlock, neighborPos, isMoving);
if (d == null)
return;
if (!state.get(FACING_TO_PROPERTY_MAP.get(d)))
return;
world.getPendingBlockTicks()
.scheduleTick(pos, this, 1, TickPriority.HIGH);
}
@Override
public void scheduledTick(BlockState state, ServerWorld world, BlockPos pos, Random r) {
FluidPropagator.propagateChangedPipe(world, pos, state);
}
@Override
public TileEntity createTileEntity(BlockState state, IBlockReader world) {
return AllTileEntities.ENCASED_FLUID_PIPE.create();
}
@Override
public ActionResultType onWrenched(BlockState state, ItemUseContext context) {
World world = context.getWorld();
BlockPos pos = context.getPos();
if (world.isRemote)
return ActionResultType.SUCCESS;
context.getWorld()
.playEvent(2001, context.getPos(), Block.getStateId(state));
BlockState equivalentPipe = transferSixWayProperties(state, AllBlocks.FLUID_PIPE.getDefaultState());
Direction firstFound = Direction.UP;
for (Direction d : Iterate.directions)
if (state.get(FACING_TO_PROPERTY_MAP.get(d))) {
firstFound = d;
break;
}
FluidTransportBehaviour.cacheFlows(world, pos);
world.setBlockState(pos, AllBlocks.FLUID_PIPE.get()
.updateBlockState(equivalentPipe, firstFound, null, world, pos));
FluidTransportBehaviour.loadFlows(world, pos);
return ActionResultType.SUCCESS;
}
public static BlockState transferSixWayProperties(BlockState from, BlockState to) {
for (Direction d : Iterate.directions) {
BooleanProperty property = FACING_TO_PROPERTY_MAP.get(d);
to = to.with(property, from.get(property));
}
return to;
}
@Override
public ItemRequirement getRequiredItems(BlockState state, TileEntity te) {
return ItemRequirement.of(AllBlocks.FLUID_PIPE.getDefaultState(), te);
}
}