Created
January 9, 2022 19:18
-
-
Save John-Paul-R/b63e5d74b7e37041107404190cdc7097 to your computer and use it in GitHub Desktop.
Get the "top" block at a given position using world.raycast.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class TopCommand implements Command<ServerCommandSource> { | |
@Override | |
public int run(CommandContext<ServerCommandSource> context) throws CommandSyntaxException { | |
ServerCommandSource source = context.getSource(); | |
ServerPlayerEntity player = source.getPlayer(); | |
World world = source.getWorld(); | |
Vec3d playerPos = player.getPos(); | |
int new_y; | |
double new_x = playerPos.x; | |
double new_z = playerPos.z; | |
BlockHitResult blockHitResult = world.raycast(new RaycastContext( | |
new Vec3d(new_x, world.getHeight(), new_z), | |
new Vec3d(new_x, 1, new_z), | |
RaycastContext.ShapeType.COLLIDER, | |
RaycastContext.FluidHandling.SOURCE_ONLY, | |
player | |
)); | |
new_y = blockHitResult.getBlockPos().getY() + 1; | |
return new_y; | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment