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-20 01:33:20 +00:00
|
|
|
import net.shadowfacts.simplemultipart.client.util.RenderStateProvider;
|
2018-12-20 00:39:10 +00:00
|
|
|
import net.shadowfacts.simplemultipart.multipart.MultipartSlot;
|
|
|
|
import net.shadowfacts.simplemultipart.multipart.MultipartState;
|
2018-12-20 01:44:53 +00:00
|
|
|
import net.shadowfacts.simplemultipart.util.MultipartHelper;
|
|
|
|
import net.shadowfacts.simplemultipart.util.MultipartHitResult;
|
2018-12-20 00:39:10 +00:00
|
|
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @author shadowfacts
|
|
|
|
*/
|
|
|
|
public class MultipartContainerBlock extends Block implements BlockEntityProvider, RenderStateProvider {
|
|
|
|
|
|
|
|
public MultipartContainerBlock() {
|
|
|
|
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) {
|
|
|
|
// if (player.isSneaking()) {
|
|
|
|
// MultipartContainerBlockEntity container = (MultipartContainerBlockEntity)world.getBlockEntity(pos);
|
|
|
|
// System.out.println(container.getParts());
|
|
|
|
// return true;
|
|
|
|
// } else {
|
|
|
|
// return false;
|
|
|
|
// }
|
|
|
|
|
|
|
|
MultipartContainerBlockEntity container = (MultipartContainerBlockEntity)world.getBlockEntity(pos);
|
|
|
|
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
|
|
|
|
|
|
|
MultipartState partState = container.get(hit.partSlot);
|
|
|
|
return partState.activate(hit.partSlot, container, player, hand);
|
2018-12-20 00:39:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public BlockState getStateForRendering(BlockState state, BlockPos pos, ExtendedBlockView world) {
|
|
|
|
MultipartContainerBlockEntity container = (MultipartContainerBlockEntity)world.getBlockEntity(pos);
|
2018-12-20 01:44:53 +00:00
|
|
|
if (container == null) {
|
|
|
|
return state;
|
|
|
|
}
|
|
|
|
|
2018-12-20 00:39:10 +00:00
|
|
|
Map<MultipartSlot, MultipartState> parts = container.getParts();
|
|
|
|
return new MultipartContainerBlockState(state, parts);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public VoxelShape getBoundingShape(BlockState state, BlockView world, BlockPos pos) {
|
|
|
|
MultipartContainerBlockEntity container = (MultipartContainerBlockEntity)world.getBlockEntity(pos);
|
|
|
|
if (container == null) {
|
|
|
|
return VoxelShapes.empty();
|
|
|
|
}
|
|
|
|
|
|
|
|
VoxelShape shape = null;
|
|
|
|
for (Map.Entry<MultipartSlot, MultipartState> e : container.getParts().entrySet()) {
|
|
|
|
VoxelShape partShape = e.getValue().getBoundingShape(e.getKey(), container);
|
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 MultipartContainerBlockEntity createBlockEntity(BlockView world) {
|
|
|
|
return new MultipartContainerBlockEntity();
|
|
|
|
}
|
|
|
|
}
|