PrettyPipes/src/main/java/de/ellpeck/prettypipes/pipe/PipeBlock.java

285 lines
12 KiB
Java
Raw Normal View History

2020-04-16 04:42:42 +02:00
package de.ellpeck.prettypipes.pipe;
2020-04-13 21:48:35 +02:00
import com.google.common.collect.ImmutableMap;
2020-04-14 01:38:48 +02:00
import de.ellpeck.prettypipes.Utility;
import de.ellpeck.prettypipes.items.IModule;
2020-04-14 18:51:43 +02:00
import de.ellpeck.prettypipes.network.PipeItem;
2020-04-14 04:21:28 +02:00
import de.ellpeck.prettypipes.network.PipeNetwork;
2020-04-14 01:38:48 +02:00
import net.minecraft.block.*;
2020-04-13 21:48:35 +02:00
import net.minecraft.block.material.Material;
2020-04-14 14:10:58 +02:00
import net.minecraft.entity.LivingEntity;
2020-04-14 01:38:48 +02:00
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.player.ServerPlayerEntity;
2020-09-22 19:14:07 +02:00
import net.minecraft.fluid.FluidState;
import net.minecraft.fluid.Fluids;
2020-04-13 21:48:35 +02:00
import net.minecraft.item.BlockItemUseContext;
2020-04-14 01:38:48 +02:00
import net.minecraft.item.ItemStack;
2020-04-13 21:48:35 +02:00
import net.minecraft.state.EnumProperty;
import net.minecraft.state.StateContainer;
import net.minecraft.state.properties.BlockStateProperties;
import net.minecraft.tags.FluidTags;
2020-04-14 01:38:48 +02:00
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.ActionResultType;
2020-04-13 21:48:35 +02:00
import net.minecraft.util.Direction;
2020-04-14 01:38:48 +02:00
import net.minecraft.util.Hand;
2020-04-13 21:48:35 +02:00
import net.minecraft.util.math.BlockPos;
2020-04-14 01:38:48 +02:00
import net.minecraft.util.math.BlockRayTraceResult;
2020-10-17 00:58:31 +02:00
import net.minecraft.util.math.shapes.IBooleanFunction;
2020-04-13 21:48:35 +02:00
import net.minecraft.util.math.shapes.ISelectionContext;
import net.minecraft.util.math.shapes.VoxelShape;
import net.minecraft.util.math.shapes.VoxelShapes;
import net.minecraft.world.IBlockReader;
2020-04-16 20:03:18 +02:00
import net.minecraft.world.IWorld;
2020-04-13 21:48:35 +02:00
import net.minecraft.world.World;
2020-04-14 01:38:48 +02:00
import net.minecraftforge.fml.network.NetworkHooks;
import net.minecraftforge.items.CapabilityItemHandler;
import net.minecraftforge.items.IItemHandler;
import net.minecraftforge.items.ItemHandlerHelper;
2020-10-17 00:58:31 +02:00
import org.apache.commons.lang3.tuple.Pair;
2020-04-13 21:48:35 +02:00
import javax.annotation.Nullable;
import java.util.HashMap;
import java.util.Map;
2020-04-19 17:38:13 +02:00
public class PipeBlock extends ContainerBlock implements IPipeConnectable {
2020-04-13 21:48:35 +02:00
public static final Map<Direction, EnumProperty<ConnectionType>> DIRECTIONS = new HashMap<>();
2020-10-17 00:58:31 +02:00
private static final Map<Pair<BlockState, BlockState>, VoxelShape> SHAPE_CACHE = new HashMap<>();
2020-04-13 21:48:35 +02:00
private static final VoxelShape CENTER_SHAPE = makeCuboidShape(5, 5, 5, 11, 11, 11);
2020-04-13 22:54:18 +02:00
public static final Map<Direction, VoxelShape> DIR_SHAPES = ImmutableMap.<Direction, VoxelShape>builder()
2020-04-13 21:48:35 +02:00
.put(Direction.UP, makeCuboidShape(5, 10, 5, 11, 16, 11))
.put(Direction.DOWN, makeCuboidShape(5, 0, 5, 11, 6, 11))
.put(Direction.NORTH, makeCuboidShape(5, 5, 0, 11, 11, 6))
.put(Direction.SOUTH, makeCuboidShape(5, 5, 10, 11, 11, 16))
.put(Direction.EAST, makeCuboidShape(10, 5, 5, 16, 11, 11))
.put(Direction.WEST, makeCuboidShape(0, 5, 5, 6, 11, 11))
.build();
static {
for (Direction dir : Direction.values())
2020-09-22 19:14:07 +02:00
DIRECTIONS.put(dir, EnumProperty.create(dir.getName2(), ConnectionType.class));
2020-04-13 21:48:35 +02:00
}
public PipeBlock() {
super(Block.Properties.create(Material.ROCK).hardnessAndResistance(2).sound(SoundType.STONE).notSolid());
BlockState state = this.getDefaultState().with(BlockStateProperties.WATERLOGGED, false);
2020-04-13 21:48:35 +02:00
for (EnumProperty<ConnectionType> prop : DIRECTIONS.values())
state = state.with(prop, ConnectionType.DISCONNECTED);
this.setDefaultState(state);
}
2020-04-14 01:38:48 +02:00
@Override
2020-04-16 04:42:42 +02:00
public ActionResultType onBlockActivated(BlockState state, World worldIn, BlockPos pos, PlayerEntity player, Hand handIn, BlockRayTraceResult result) {
2020-04-14 01:38:48 +02:00
PipeTileEntity tile = Utility.getTileEntity(PipeTileEntity.class, worldIn, pos);
if (tile == null)
return ActionResultType.PASS;
if (!tile.canHaveModules())
2020-04-15 18:35:00 +02:00
return ActionResultType.PASS;
ItemStack stack = player.getHeldItem(handIn);
if (stack.getItem() instanceof IModule) {
ItemStack copy = stack.copy();
copy.setCount(1);
ItemStack remain = ItemHandlerHelper.insertItem(tile.modules, copy, false);
if (remain.isEmpty()) {
stack.shrink(1);
return ActionResultType.SUCCESS;
}
} else if (handIn == Hand.MAIN_HAND && stack.isEmpty()) {
if (!worldIn.isRemote)
NetworkHooks.openGui((ServerPlayerEntity) player, tile, pos);
return ActionResultType.SUCCESS;
}
return ActionResultType.PASS;
2020-04-14 01:38:48 +02:00
}
2020-04-13 21:48:35 +02:00
@Override
protected void fillStateContainer(StateContainer.Builder<Block, BlockState> builder) {
builder.add(DIRECTIONS.values().toArray(new EnumProperty[0]));
builder.add(BlockStateProperties.WATERLOGGED);
}
@Override
2020-09-22 19:14:07 +02:00
public FluidState getFluidState(BlockState state) {
return state.get(BlockStateProperties.WATERLOGGED) ? Fluids.WATER.getStillFluidState(false) : super.getFluidState(state);
2020-04-13 21:48:35 +02:00
}
@Override
public void neighborChanged(BlockState state, World worldIn, BlockPos pos, Block blockIn, BlockPos fromPos, boolean isMoving) {
2020-04-13 22:54:18 +02:00
BlockState newState = this.createState(worldIn, pos, state);
2020-04-14 01:38:48 +02:00
if (newState != state) {
2020-04-13 21:48:35 +02:00
worldIn.setBlockState(pos, newState);
2020-04-14 01:38:48 +02:00
onStateChanged(worldIn, pos, newState);
}
2020-04-13 21:48:35 +02:00
}
@Nullable
@Override
public BlockState getStateForPlacement(BlockItemUseContext context) {
2020-04-13 22:54:18 +02:00
return this.createState(context.getWorld(), context.getPos(), this.getDefaultState());
2020-04-13 21:48:35 +02:00
}
@Override
public BlockState updatePostPlacement(BlockState stateIn, Direction facing, BlockState facingState, IWorld worldIn, BlockPos currentPos, BlockPos facingPos) {
if (stateIn.get(BlockStateProperties.WATERLOGGED))
worldIn.getPendingFluidTicks().scheduleTick(currentPos, Fluids.WATER, Fluids.WATER.getTickRate(worldIn));
return super.updatePostPlacement(stateIn, facing, facingState, worldIn, currentPos, facingPos);
}
2020-04-14 14:10:58 +02:00
@Override
public void onBlockPlacedBy(World worldIn, BlockPos pos, BlockState state, @Nullable LivingEntity placer, ItemStack stack) {
onStateChanged(worldIn, pos, state);
}
2020-04-13 21:48:35 +02:00
@Override
public VoxelShape getShape(BlockState state, IBlockReader worldIn, BlockPos pos, ISelectionContext context) {
2020-10-17 00:58:31 +02:00
VoxelShape coverShape = null;
BlockState cover = null;
PipeTileEntity tile = Utility.getTileEntity(PipeTileEntity.class, worldIn, pos);
if (tile != null && tile.cover != null) {
cover = tile.cover;
// try catch since the block might expect to find itself at the position
try {
coverShape = cover.getShape(worldIn, pos, context);
} catch (Exception ignored) {
}
}
Pair<BlockState, BlockState> key = Pair.of(state, cover);
VoxelShape shape = SHAPE_CACHE.get(key);
if (shape == null) {
shape = CENTER_SHAPE;
for (Map.Entry<Direction, EnumProperty<ConnectionType>> entry : DIRECTIONS.entrySet()) {
if (state.get(entry.getValue()).isConnected())
shape = VoxelShapes.or(shape, DIR_SHAPES.get(entry.getKey()));
}
if (coverShape != null)
shape = VoxelShapes.or(shape, coverShape);
SHAPE_CACHE.put(key, shape);
2020-04-13 21:48:35 +02:00
}
return shape;
}
2020-04-16 19:47:09 +02:00
private BlockState createState(World world, BlockPos pos, BlockState curr) {
2020-04-13 22:54:18 +02:00
BlockState state = this.getDefaultState();
2020-09-22 19:14:07 +02:00
FluidState fluid = world.getFluidState(pos);
if (fluid.isTagged(FluidTags.WATER) && fluid.getLevel() == 8)
state = state.with(BlockStateProperties.WATERLOGGED, true);
2020-04-16 19:47:09 +02:00
for (Direction dir : Direction.values()) {
EnumProperty<ConnectionType> prop = DIRECTIONS.get(dir);
ConnectionType type = this.getConnectionType(world, pos, dir, state);
2020-04-16 19:47:09 +02:00
// don't reconnect on blocked faces
if (type.isConnected() && curr.get(prop) == ConnectionType.BLOCKED)
type = ConnectionType.BLOCKED;
state = state.with(prop, type);
2020-04-13 22:54:18 +02:00
}
return state;
}
protected ConnectionType getConnectionType(World world, BlockPos pos, Direction direction, BlockState state) {
2020-04-13 22:54:18 +02:00
BlockPos offset = pos.offset(direction);
if (!world.isBlockLoaded(offset))
return ConnectionType.DISCONNECTED;
2020-04-16 19:47:09 +02:00
BlockState offState = world.getBlockState(offset);
2020-04-19 17:38:13 +02:00
Block block = offState.getBlock();
if (block instanceof IPipeConnectable)
return ((IPipeConnectable) block).getConnectionType(world, pos, direction);
2020-04-15 18:35:00 +02:00
TileEntity tile = world.getTileEntity(offset);
if (tile != null) {
2020-04-14 01:38:48 +02:00
IItemHandler handler = tile.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, direction.getOpposite()).orElse(null);
2020-04-15 18:35:00 +02:00
if (handler != null)
return ConnectionType.CONNECTED;
2020-04-14 01:38:48 +02:00
}
2020-04-16 19:47:09 +02:00
if (hasLegsTo(world, offState, offset, direction)) {
if (DIRECTIONS.values().stream().noneMatch(d -> state.get(d) == ConnectionType.LEGS))
return ConnectionType.LEGS;
}
2020-04-15 18:35:00 +02:00
return ConnectionType.DISCONNECTED;
2020-04-13 22:54:18 +02:00
}
2020-08-28 00:29:44 +02:00
protected static boolean hasLegsTo(World world, BlockState state, BlockPos pos, Direction direction) {
2020-04-16 19:47:09 +02:00
if (state.getBlock() instanceof WallBlock || state.getBlock() instanceof FenceBlock)
return direction == Direction.DOWN;
if (state.getMaterial() == Material.ROCK || state.getMaterial() == Material.IRON)
2020-09-22 19:14:07 +02:00
return hasEnoughSolidSide(world, pos, direction.getOpposite());
2020-04-16 19:47:09 +02:00
return false;
}
2020-04-14 01:38:48 +02:00
public static void onStateChanged(World world, BlockPos pos, BlockState newState) {
// wait a few ticks before checking if we have to drop our modules, so that things like iron -> gold chest work
2020-04-15 18:35:00 +02:00
PipeTileEntity tile = Utility.getTileEntity(PipeTileEntity.class, world, pos);
if (tile != null)
tile.moduleDropCheck = 5;
2020-04-14 04:21:28 +02:00
PipeNetwork network = PipeNetwork.get(world);
int connections = 0;
2020-04-19 17:38:13 +02:00
boolean force = false;
2020-04-15 18:35:00 +02:00
for (Direction dir : Direction.values()) {
ConnectionType value = newState.get(DIRECTIONS.get(dir));
2020-04-14 04:21:28 +02:00
if (!value.isConnected())
continue;
connections++;
2020-04-19 17:38:13 +02:00
BlockState otherState = world.getBlockState(pos.offset(dir));
// force a node if we're connecting to a different block (inventory etc.)
if (otherState.getBlock() != newState.getBlock()) {
force = true;
2020-04-14 04:21:28 +02:00
break;
}
}
2020-04-19 17:38:13 +02:00
if (force || connections > 2) {
2020-04-14 04:21:28 +02:00
network.addNode(pos, newState);
} else {
network.removeNode(pos);
}
network.onPipeChanged(pos, newState);
2020-04-14 01:38:48 +02:00
}
2020-04-13 21:48:35 +02:00
2020-04-14 01:38:48 +02:00
@Override
public void onReplaced(BlockState state, World worldIn, BlockPos pos, BlockState newState, boolean isMoving) {
if (state.getBlock() != newState.getBlock()) {
PipeTileEntity tile = Utility.getTileEntity(PipeTileEntity.class, worldIn, pos);
2020-04-14 18:51:43 +02:00
if (tile != null) {
2020-04-16 00:39:53 +02:00
Utility.dropInventory(tile, tile.modules);
for (PipeItem item : tile.getItems())
2020-10-13 18:11:40 +02:00
item.drop(worldIn, item.stack);
2020-04-14 18:51:43 +02:00
}
2020-04-14 04:21:28 +02:00
PipeNetwork network = PipeNetwork.get(worldIn);
network.removeNode(pos);
network.onPipeChanged(pos, state);
2020-04-14 01:38:48 +02:00
super.onReplaced(state, worldIn, pos, newState, isMoving);
2020-04-13 21:48:35 +02:00
}
2020-04-14 01:38:48 +02:00
}
2020-04-13 21:48:35 +02:00
2020-05-03 15:33:56 +02:00
@Override
public boolean hasComparatorInputOverride(BlockState state) {
return true;
}
@Override
public int getComparatorInputOverride(BlockState blockState, World worldIn, BlockPos pos) {
PipeTileEntity pipe = Utility.getTileEntity(PipeTileEntity.class, worldIn, pos);
if (pipe == null)
return 0;
return Math.min(15, pipe.getItems().size());
}
2020-04-14 01:38:48 +02:00
@Nullable
@Override
public TileEntity createNewTileEntity(IBlockReader worldIn) {
return new PipeTileEntity();
}
@Override
public BlockRenderType getRenderType(BlockState state) {
return BlockRenderType.MODEL;
2020-04-13 21:48:35 +02:00
}
2020-04-19 17:38:13 +02:00
@Override
public ConnectionType getConnectionType(World world, BlockPos pipePos, Direction direction) {
BlockState state = world.getBlockState(pipePos.offset(direction));
if (state.get(DIRECTIONS.get(direction.getOpposite())) == ConnectionType.BLOCKED)
2020-04-19 17:38:13 +02:00
return ConnectionType.BLOCKED;
return ConnectionType.CONNECTED;
}
2020-04-13 21:48:35 +02:00
}