SimpleMultipart/src/main/java/net/shadowfacts/simplemultipart/container/ContainerBlock.java

78 lines
2.5 KiB
Java
Raw Normal View History

2018-12-20 00:39:10 +00:00
package net.shadowfacts.simplemultipart.container;
import net.fabricmc.fabric.block.FabricBlockSettings;
import net.minecraft.block.*;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.util.Hand;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Direction;
import net.minecraft.util.shape.VoxelShape;
import net.minecraft.util.shape.VoxelShapes;
import net.minecraft.world.BlockView;
import net.minecraft.world.ExtendedBlockView;
import net.minecraft.world.World;
2018-12-25 15:30:46 +00:00
import net.shadowfacts.simplemultipart.api.MultipartContainer;
2018-12-20 01:33:20 +00:00
import net.shadowfacts.simplemultipart.client.util.RenderStateProvider;
2018-12-20 01:44:53 +00:00
import net.shadowfacts.simplemultipart.util.MultipartHelper;
import net.shadowfacts.simplemultipart.util.MultipartHitResult;
2018-12-25 15:20:44 +00:00
import net.shadowfacts.simplemultipart.api.MultipartView;
2018-12-20 00:39:10 +00:00
2018-12-24 22:16:38 +00:00
import java.util.Set;
2018-12-20 00:39:10 +00:00
/**
* @author shadowfacts
*/
2018-12-25 15:30:46 +00:00
public class ContainerBlock extends Block implements BlockEntityProvider, RenderStateProvider {
2018-12-20 00:39:10 +00:00
2018-12-25 15:30:46 +00:00
public ContainerBlock() {
2018-12-20 00:39:10 +00:00
super(FabricBlockSettings.of(Material.STONE).build());
}
@Override
2018-12-20 01:44:53 +00:00
public boolean activate(BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand, Direction side, float hitX, float hitY, float hitZ) {
2018-12-25 15:30:46 +00:00
MultipartContainer container = (MultipartContainer)world.getBlockEntity(pos);
2018-12-20 01:44:53 +00:00
if (container == null) {
return false;
}
MultipartHitResult hit = MultipartHelper.rayTrace(container, world, pos, player);
if (hit == null) {
2018-12-20 00:39:10 +00:00
return false;
}
2018-12-20 01:44:53 +00:00
2018-12-25 15:20:44 +00:00
return hit.view.getState().activate(hit.view, player, hand);
2018-12-20 00:39:10 +00:00
}
@Override
public BlockState getStateForRendering(BlockState state, BlockPos pos, ExtendedBlockView world) {
2018-12-25 15:30:46 +00:00
MultipartContainer container = (MultipartContainer)world.getBlockEntity(pos);
2018-12-20 01:44:53 +00:00
if (container == null) {
return state;
}
2018-12-25 15:20:44 +00:00
Set<MultipartView> parts = container.getParts();
2018-12-25 15:30:46 +00:00
return new ContainerBlockState(state, parts);
2018-12-20 00:39:10 +00:00
}
@Override
2018-12-25 15:30:46 +00:00
@Deprecated
2018-12-20 00:39:10 +00:00
public VoxelShape getBoundingShape(BlockState state, BlockView world, BlockPos pos) {
2018-12-25 15:30:46 +00:00
MultipartContainer container = (MultipartContainer)world.getBlockEntity(pos);
2018-12-20 00:39:10 +00:00
if (container == null) {
return VoxelShapes.empty();
}
VoxelShape shape = null;
2018-12-25 15:20:44 +00:00
for (MultipartView view : container.getParts()) {
VoxelShape partShape = view.getState().getBoundingShape(view);
2018-12-20 01:23:04 +00:00
shape = shape == null ? partShape : VoxelShapes.union(shape, partShape);
2018-12-20 00:39:10 +00:00
}
return shape == null ? VoxelShapes.empty() : shape;
}
@Override
public AbstractContainerBlockEntity createBlockEntity(BlockView world) {
2018-12-25 15:30:46 +00:00
return new ContainerBlockEntity();
2018-12-20 00:39:10 +00:00
}
}