下面列出了net.minecraft.util.shape.VoxelShape#net.minecraft.item.ItemPlacementContext 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public BlockState getPlacementState(ItemPlacementContext context) {
BlockState state = this.getDefaultState();
for (Direction direction : Direction.values()) {
Block block = context.getWorld().getBlockState(context.getBlockPos().offset(direction)).getBlock();
if (block instanceof WireConnectable) {
if (((WireConnectable) block).canWireConnect(context.getWorld(), direction.getOpposite(), context.getBlockPos(), context.getBlockPos().offset(direction)) != WireConnectionType.NONE) {
state = state.with(propFromDirection(direction), true);
}
} else if (block instanceof ComponentProvider && ((ComponentProvider) block).hasComponent(UniversalComponents.CAPACITOR_COMPONENT)) {
state = state.with(propFromDirection(direction), true);
}
}
return state;
}
@Inject(method = "canPlace", at = @At("HEAD"), cancellable = true)
private void onCanPlace(ItemPlacementContext context, BlockState state, CallbackInfoReturnable<Boolean> ci) {
if (ConnectionInfo.protocolVersion <= Protocols.V1_12_2) {
Block block = state.getBlock();
if (block == Blocks.CHEST || block == Blocks.TRAPPED_CHEST) {
World world = context.getWorld();
BlockPos pos = context.getBlockPos();
boolean foundAdjChest = false;
for (Direction dir : Direction.Type.HORIZONTAL) {
BlockState otherState = world.getBlockState(pos.offset(dir));
if (otherState.getBlock() == block) {
if (foundAdjChest) {
ci.setReturnValue(false);
return;
}
foundAdjChest = true;
if (otherState.get(ChestBlock.CHEST_TYPE) != ChestType.SINGLE) {
ci.setReturnValue(false);
return;
}
}
}
}
}
}
@Inject(method = "place(Lnet/minecraft/item/ItemPlacementContext;Lnet/minecraft/block/BlockState;)Z",
at = @At(value = "HEAD"),
cancellable = true
)
public void place(ItemPlacementContext context, BlockState state, CallbackInfoReturnable<Boolean> info) {
BlockEvent.Place event = EventDispatcher.publish(new BlockEvent.Place(
(World) context.getWorld(),
(Position) context.getBlockPos(),
(org.sandboxpowered.sandbox.api.state.BlockState) state
));
BlockState state2 = WrappingUtil.convert(event.getState());
if (event.isCancelled()) {
info.setReturnValue(false);
} else if (state2 != state) {
info.setReturnValue(context.getWorld().setBlockState(context.getBlockPos(), state2, 11));
}
}
@Redirect(method = "canPlace", at = @At(
value = "INVOKE",
target = "Lnet/minecraft/world/World;canPlace(Lnet/minecraft/block/BlockState;Lnet/minecraft/util/math/BlockPos;Lnet/minecraft/entity/EntityContext;)Z"
))
private boolean canSpectatingPlace(World world, BlockState state, BlockPos pos, EntityContext context,
ItemPlacementContext contextOuter, BlockState stateOuter)
{
PlayerEntity player = contextOuter.getPlayer();
if (CarpetSettings.creativeNoClip && player != null && player.isCreative() && player.abilities.flying)
{
// copy from canPlace
VoxelShape voxelShape = state.getCollisionShape(world, pos, context);
return voxelShape.isEmpty() || world.intersectsEntities(player, voxelShape.offset(pos.getX(), pos.getY(), pos.getZ()));
}
return world.canPlace(state, pos, context);
}
@Override
public BlockState getPlacementState(ItemPlacementContext context) {
FluidState fluidState = context.getWorld().getFluidState(context.getBlockPos());
BlockState blockState = this.getDefaultState().with(GRATING_STATE, GratingState.LOWER)
.with(FLUID, Registry.FLUID.getId(fluidState.getFluid()))
.with(FlowableFluid.LEVEL, Math.max(fluidState.getLevel(), 1));
BlockPos blockPos = context.getBlockPos();
Direction direction = context.getPlayerFacing();
return direction != Direction.DOWN && (direction == Direction.UP || context.getBlockPos().getY() - (double) blockPos.getY() <= 0.5D) ? blockState : blockState.with(GRATING_STATE, GratingState.UPPER);
}
@Override
public BlockState getPlacementState(ItemPlacementContext context) {
BlockState state = this.getDefaultState();
for (Direction direction : Direction.values()) {
Block block = context.getWorld().getBlockState(context.getBlockPos().offset(direction)).getBlock();
if (block instanceof FluidPipeBlock)
state = state.with(propFromDirection(direction), true);
}
return state;
}
@Override
public BlockState getPlacementState(ItemPlacementContext context) {
FluidState fluidState = context.getWorld().getFluidState(context.getBlockPos());
return this.getDefaultState()
.with(FLUID, Registry.FLUID.getId(fluidState.getFluid()))
.with(FlowableFluid.LEVEL, Math.max(fluidState.getLevel(), 1));
}
@Override
public BlockState getPlacementState(ItemPlacementContext context) {
return this.getDefaultState().with(FACING, context.getPlayerFacing().getOpposite())
.with(FRONT_SIDE_OPTION, SideOption.DEFAULT)
.with(BACK_SIDE_OPTION, SideOption.DEFAULT)
.with(RIGHT_SIDE_OPTION, SideOption.DEFAULT)
.with(LEFT_SIDE_OPTION, SideOption.DEFAULT)
.with(TOP_SIDE_OPTION, SideOption.DEFAULT)
.with(BOTTOM_SIDE_OPTION, SideOption.DEFAULT);
}
@Override
public BlockState getPlacementState(ItemPlacementContext context) {
return this.getDefaultState().with(FACING, context.getPlayerFacing().getOpposite())
.with(FRONT_SIDE_OPTION, SideOption.DEFAULT)
.with(BACK_SIDE_OPTION, SideOption.DEFAULT)
.with(RIGHT_SIDE_OPTION, SideOption.DEFAULT)
.with(LEFT_SIDE_OPTION, SideOption.DEFAULT)
.with(TOP_SIDE_OPTION, SideOption.DEFAULT)
.with(BOTTOM_SIDE_OPTION, SideOption.DEFAULT);
}
@Override
public BlockState getPlacementState(ItemPlacementContext context) {
return this.getDefaultState().with(FACING, context.getPlayerFacing().getOpposite())
.with(FRONT_SIDE_OPTION, SideOption.DEFAULT)
.with(BACK_SIDE_OPTION, SideOption.DEFAULT)
.with(RIGHT_SIDE_OPTION, SideOption.DEFAULT)
.with(LEFT_SIDE_OPTION, SideOption.DEFAULT)
.with(TOP_SIDE_OPTION, SideOption.DEFAULT)
.with(BOTTOM_SIDE_OPTION, SideOption.DEFAULT);
}
@Override
public BlockState getPlacementState(ItemPlacementContext itemPlacementContext_1) {
return super.getPlacementState(itemPlacementContext_1).with(FACING, itemPlacementContext_1.getPlayerFacing().getOpposite()).with(FRONT_SIDE_OPTION, SideOption.DEFAULT)
.with(BACK_SIDE_OPTION, SideOption.DEFAULT)
.with(RIGHT_SIDE_OPTION, SideOption.DEFAULT)
.with(LEFT_SIDE_OPTION, SideOption.DEFAULT)
.with(TOP_SIDE_OPTION, SideOption.DEFAULT)
.with(BOTTOM_SIDE_OPTION, SideOption.DEFAULT);
}
@Override
public BlockState getPlacementState(ItemPlacementContext context) {
return this.getDefaultState().with(FACING, context.getPlayerFacing().getOpposite())
.with(FRONT_SIDE_OPTION, SideOption.DEFAULT)
.with(BACK_SIDE_OPTION, SideOption.DEFAULT)
.with(RIGHT_SIDE_OPTION, SideOption.DEFAULT)
.with(LEFT_SIDE_OPTION, SideOption.DEFAULT)
.with(TOP_SIDE_OPTION, SideOption.DEFAULT)
.with(BOTTOM_SIDE_OPTION, SideOption.DEFAULT);
}
@Override
public BlockState getPlacementState(ItemPlacementContext context) {
return this.getDefaultState().with(FACING, context.getPlayerFacing().getOpposite())
.with(FRONT_SIDE_OPTION, SideOption.DEFAULT)
.with(BACK_SIDE_OPTION, SideOption.DEFAULT)
.with(RIGHT_SIDE_OPTION, SideOption.DEFAULT)
.with(LEFT_SIDE_OPTION, SideOption.DEFAULT)
.with(TOP_SIDE_OPTION, SideOption.DEFAULT)
.with(BOTTOM_SIDE_OPTION, SideOption.DEFAULT);
}
@Override
public BlockState getPlacementState(ItemPlacementContext context) {
return this.getDefaultState().with(FACING, context.getPlayerFacing().getOpposite())
.with(FRONT_SIDE_OPTION, SideOption.DEFAULT)
.with(BACK_SIDE_OPTION, SideOption.DEFAULT)
.with(RIGHT_SIDE_OPTION, SideOption.DEFAULT)
.with(LEFT_SIDE_OPTION, SideOption.DEFAULT)
.with(TOP_SIDE_OPTION, SideOption.DEFAULT)
.with(BOTTOM_SIDE_OPTION, SideOption.DEFAULT);
}
@Override
public BlockState getPlacementState(ItemPlacementContext placementContext) {
final BlockState blockState = this.getDefaultState().with(FACING, placementContext.getPlayerFacing().getOpposite());
if (blockState.contains(Properties.WATERLOGGED)) {
final FluidState fluidState = placementContext.getWorld().getFluidState(placementContext.getBlockPos());
return blockState.with(Properties.WATERLOGGED, fluidState.getFluid() == Fluids.WATER);
}
return blockState;
}
@Redirect(method = "getPlacementState", at = @At(
value = "INVOKE",
target = "Lnet/minecraft/block/Block;getPlacementState(Lnet/minecraft/item/ItemPlacementContext;)Lnet/minecraft/block/BlockState;"
))
private BlockState getAlternatePlacement(Block block, ItemPlacementContext itemPlacementContext_1)
{
if (CarpetExtraSettings.accurateBlockPlacement)
{
BlockState tryAlternative = BlockPlacer.alternativeBlockPlacement(block, itemPlacementContext_1);
if (tryAlternative != null)
return tryAlternative;
}
return block.getPlacementState(itemPlacementContext_1);
}
@Inject(method = "place(Lnet/minecraft/item/ItemPlacementContext;)Lnet/minecraft/util/ActionResult;", at = @At(
value = "INVOKE",
target = "Lnet/minecraft/block/Block;onPlaced(Lnet/minecraft/world/World;Lnet/minecraft/util/math/BlockPos;Lnet/minecraft/block/BlockState;Lnet/minecraft/entity/LivingEntity;Lnet/minecraft/item/ItemStack;)V",
shift = At.Shift.AFTER
))
private void afterPlacement(ItemPlacementContext context, CallbackInfoReturnable<ActionResult> cir)
{
if (context.getPlayer() instanceof ServerPlayerEntity && PLAYER_PLACES_BLOCK.isNeeded())
PLAYER_PLACES_BLOCK.onBlockPlaced((ServerPlayerEntity) context.getPlayer(), context.getBlockPos(), context.getHand(), context.getStack());
}
public BlockState getPlacementState(ItemPlacementContext context)
{
BlockState state = super.getPlacementState(context);
if (context.getPlayer() != null && !context.getWorld().isClient)
{
WoolTool.carpetPlacedAction(((CarpetBlock)(Object)this).getColor(), context.getPlayer(), context.getBlockPos(), (ServerWorld) context.getWorld());
}
return state;
}
@Redirect(method = "getPlacementState", at = @At(
value = "INVOKE",
target = "Lnet/minecraft/item/ItemPlacementContext;getSide()Lnet/minecraft/util/math/Direction;"
))
private Direction getOppositeOpposite(ItemPlacementContext context)
{
if (BlockRotator.flippinEligibility(context.getPlayer()))
{
return context.getSide().getOpposite();
}
return context.getSide();
}
@Override
public BlockState getPlacementState(ItemPlacementContext context) {
return this.getDefaultState().with(FACING, context.getPlayerFacing().getOpposite());
}
@Override
public BlockState getPlacementState(ItemPlacementContext context) {
FluidState fluidState = context.getWorld().getFluidState(context.getBlockPos());
return super.getPlacementState(context).with(WATERLOGGED, fluidState.getFluid() == Fluids.WATER).with(VINES, VineTypes.VINE_0);
}
@Override
public BlockState getPlacementState(ItemPlacementContext context) {
return this.getDefaultState().with(FACING, context.getPlayerFacing().getOpposite());
}
@Nullable
@Override
public BlockState getPlacementState(ItemPlacementContext context) {
return getDefaultState().with(FACING, context.getPlayerFacing().getOpposite());
}
@Override
public BlockState getPlacementState(ItemPlacementContext placementContext) {
return this.getDefaultState().with(FACING, placementContext.getPlayerFacing().getOpposite());
}
public static BlockState alternativeBlockPlacement(Block block, ItemPlacementContext context)//World worldIn, BlockPos pos, EnumFacing facing, float hitX, float hitY, float hitZ, int meta, EntityLivingBase placer)
{
//actual alternative block placement code
Direction facing;
Vec3d vec3d = context.getHitPos();
BlockPos pos = context.getBlockPos();
float hitX = (float) vec3d.x - pos.getX();
if (hitX<2) // vanilla
return null;
int code = (int)(hitX-2)/2;
//
// now it would be great if hitX was adjusted in context to original range from 0.0 to 1.0
// since its actually using it. Its private - maybe with Reflections?
//
PlayerEntity placer = context.getPlayer();
World world = context.getWorld();
if (block instanceof GlazedTerracottaBlock)
{
facing = Direction.byId(code);
if(facing == Direction.UP || facing == Direction.DOWN)
{
facing = placer.getHorizontalFacing().getOpposite();
}
return block.getDefaultState().with(HorizontalFacingBlock.FACING, facing);
}
else if (block instanceof ObserverBlock)
{
return block.getDefaultState()
.with(FacingBlock.FACING, Direction.byId(code))
.with(ObserverBlock.POWERED, true);
}
else if (block instanceof RepeaterBlock)
{
facing = Direction.byId(code % 16);
if(facing == Direction.UP || facing == Direction.DOWN)
{
facing = placer.getHorizontalFacing().getOpposite();
}
return block.getDefaultState()
.with(HorizontalFacingBlock.FACING, facing)
.with(RepeaterBlock.DELAY, MathHelper.clamp(code / 16, 1, 4))
.with(RepeaterBlock.LOCKED, Boolean.FALSE);
}
else if (block instanceof TrapdoorBlock)
{
return block.getDefaultState()
.with(TrapdoorBlock.FACING, Direction.byId(code % 16))
.with(TrapdoorBlock.OPEN, Boolean.FALSE)
.with(TrapdoorBlock.HALF, (code >= 16) ? BlockHalf.TOP : BlockHalf.BOTTOM)
.with(TrapdoorBlock.OPEN, world.isReceivingRedstonePower(pos));
}
else if (block instanceof ComparatorBlock)
{
facing = Direction.byId(code % 16);
if((facing == Direction.UP) || (facing == Direction.DOWN))
{
facing = placer.getHorizontalFacing().getOpposite();
}
ComparatorMode m = (hitX >= 16)?ComparatorMode.SUBTRACT: ComparatorMode.COMPARE;
return block.getDefaultState()
.with(HorizontalFacingBlock.FACING, facing)
.with(ComparatorBlock.POWERED, Boolean.FALSE)
.with(ComparatorBlock.MODE, m);
}
else if (block instanceof DispenserBlock)
{
return block.getDefaultState()
.with(DispenserBlock.FACING, Direction.byId(code))
.with(DispenserBlock.TRIGGERED, Boolean.FALSE);
}
else if (block instanceof PistonBlock)
{
return block.getDefaultState()
.with(FacingBlock.FACING, Direction.byId(code))
.with(PistonBlock.EXTENDED, Boolean.FALSE);
}
else if (block instanceof StairsBlock)
{
return block.getPlacementState(context)//worldIn, pos, facing, hitX, hitY, hitZ, meta, placer)
.with(StairsBlock.FACING, Direction.byId(code % 16))
.with(StairsBlock.HALF, ( hitX >= 16)?BlockHalf.TOP : BlockHalf.BOTTOM);
}
return null;
}
@Override
public boolean canReplace(BlockState blockState_1, ItemPlacementContext itemPlacementContext_1) {
return block.canReplace((org.sandboxpowered.sandbox.api.state.BlockState) blockState_1);
}
@Override
public boolean canReplace(BlockState blockState_1, ItemPlacementContext itemPlacementContext_1) {
return block.canReplace((org.sandboxpowered.sandbox.api.state.BlockState) blockState_1);
}
public static BlockState alternativeBlockPlacement(Block block, ItemPlacementContext context)//World worldIn, BlockPos pos, EnumFacing facing, float hitX, float hitY, float hitZ, int meta, EntityLivingBase placer)
{
//actual alternative block placement code
//
if (true) throw new UnsupportedOperationException("Alternative Block Placement / client controlled / is not implemnted");
Direction facing;
Vec3d vec3d = context.getHitPos();
float hitX = (float) vec3d.x;
if (hitX<2) // vanilla
return null;
int code = (int)(hitX-2)/2;
//
// now it would be great if hitX was adjusted in context to original range from 0.0 to 1.0
// since its actually using it. Its private - maybe with Reflections?
//
PlayerEntity placer = context.getPlayer();
BlockPos pos = context.getBlockPos();
World world = context.getWorld();
if (block instanceof GlazedTerracottaBlock)
{
facing = Direction.byId(code);
if(facing == Direction.UP || facing == Direction.DOWN)
{
facing = placer.getHorizontalFacing().getOpposite();
}
return block.getDefaultState().with(HorizontalFacingBlock.FACING, facing);
}
else if (block instanceof ObserverBlock)
{
return block.getDefaultState()
.with(FacingBlock.FACING, Direction.byId(code))
.with(ObserverBlock.POWERED, true);
}
else if (block instanceof RepeaterBlock)
{
facing = Direction.byId(code % 16);
if(facing == Direction.UP || facing == Direction.DOWN)
{
facing = placer.getHorizontalFacing().getOpposite();
}
return block.getDefaultState()
.with(HorizontalFacingBlock.FACING, facing)
.with(RepeaterBlock.DELAY, MathHelper.clamp(code / 16, 1, 4))
.with(RepeaterBlock.LOCKED, Boolean.FALSE);
}
else if (block instanceof TrapdoorBlock)
{
return block.getDefaultState()
.with(TrapdoorBlock.FACING, Direction.byId(code % 16))
.with(TrapdoorBlock.OPEN, Boolean.FALSE)
.with(TrapdoorBlock.HALF, (code >= 16) ? BlockHalf.TOP : BlockHalf.BOTTOM)
.with(TrapdoorBlock.OPEN, world.isReceivingRedstonePower(pos));
}
else if (block instanceof ComparatorBlock)
{
facing = Direction.byId(code % 16);
if((facing == Direction.UP) || (facing == Direction.DOWN))
{
facing = placer.getHorizontalFacing().getOpposite();
}
ComparatorMode m = (hitX >= 16)?ComparatorMode.SUBTRACT: ComparatorMode.COMPARE;
return block.getDefaultState()
.with(HorizontalFacingBlock.FACING, facing)
.with(ComparatorBlock.POWERED, Boolean.FALSE)
.with(ComparatorBlock.MODE, m);
}
else if (block instanceof DispenserBlock)
{
return block.getDefaultState()
.with(DispenserBlock.FACING, Direction.byId(code))
.with(DispenserBlock.TRIGGERED, Boolean.FALSE);
}
else if (block instanceof PistonBlock)
{
return block.getDefaultState()
.with(FacingBlock.FACING, Direction.byId(code))
.with(PistonBlock.EXTENDED, Boolean.FALSE);
}
else if (block instanceof StairsBlock)
{
return block.getPlacementState(context)//worldIn, pos, facing, hitX, hitY, hitZ, meta, placer)
.with(StairsBlock.FACING, Direction.byId(code % 16))
.with(StairsBlock.HALF, ( hitX >= 16)?BlockHalf.TOP : BlockHalf.BOTTOM);
}
return null;
}
BlockState getPlacementState(ItemPlacementContext context);