minecraft-source/src/net/minecraft/world/item/BlockPlaceContext.java

64 lines
2.5 KiB
Java

package net.minecraft.world.item;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.phys.Vec3;
import net.minecraft.core.Direction;
import net.minecraft.world.phys.BlockHitResult;
import net.minecraft.world.InteractionHand;
import javax.annotation.Nullable;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.level.Level;
import net.minecraft.core.BlockPos;
public class BlockPlaceContext extends UseOnContext {
private final BlockPos relativePos;
protected boolean replaceClicked;
public BlockPlaceContext(final UseOnContext bfw) {
this(bfw.getLevel(), bfw.getPlayer(), bfw.getHand(), bfw.getItemInHand(), bfw.hitResult);
}
protected BlockPlaceContext(final Level bjt, @Nullable final Player ayg, final InteractionHand ajh, final ItemStack bek, final BlockHitResult cvd) {
super(bjt, ayg, ajh, bek, cvd);
this.replaceClicked = true;
this.relativePos = cvd.getBlockPos().relative(cvd.getDirection());
this.replaceClicked = bjt.getBlockState(cvd.getBlockPos()).canBeReplaced(this);
}
public static BlockPlaceContext at(final BlockPlaceContext bcn, final BlockPos fk, final Direction fp) {
return new BlockPlaceContext(bcn.getLevel(), bcn.getPlayer(), bcn.getHand(), bcn.getItemInHand(), new BlockHitResult(new Vec3(fk.getX() + 0.5 + fp.getStepX() * 0.5, fk.getY() + 0.5 + fp.getStepY() * 0.5, fk.getZ() + 0.5 + fp.getStepZ() * 0.5), fp, fk, false));
}
@Override
public BlockPos getClickedPos() {
return this.replaceClicked ? super.getClickedPos() : this.relativePos;
}
public boolean canPlace() {
return this.replaceClicked || this.getLevel().getBlockState(this.getClickedPos()).canBeReplaced(this);
}
public boolean replacingClickedOnBlock() {
return this.replaceClicked;
}
public Direction getNearestLookingDirection() {
return Direction.orderedByNearest(this.player)[0];
}
public Direction[] getNearestLookingDirections() {
final Direction[] arr2 = Direction.orderedByNearest(this.player);
if (this.replaceClicked) {
return arr2;
}
Direction fp3;
int integer4;
for (fp3 = this.getClickedFace(), integer4 = 0; integer4 < arr2.length && arr2[integer4] != fp3.getOpposite(); ++integer4) {}
if (integer4 > 0) {
System.arraycopy(arr2, 0, arr2, 1, integer4);
arr2[0] = fp3.getOpposite();
}
return arr2;
}
}